Skip to content

add glitch support #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const pkg = JSON.parse(

import guides from './projects/guides.js';
import guidesSearch from './projects/guides-search.js';
import glitch from './projects/glitch.js';

program
.name(pkg.name)
Expand All @@ -27,7 +28,7 @@ program
.action((args, commandOptions) =>
guides(args, {
...program.opts(),
...commandOptions,
...commandOptions.opts(),
}),
);

Expand All @@ -37,8 +38,19 @@ program
.action((args, commandOptions) =>
guidesSearch(args, {
...program.opts(),
...commandOptions,
...commandOptions.opts(),
}),
);

program
.command('glitch')
.description('Update the Glitch starter pack')
.requiredOption('-v, --version <version>', `Set this to be the version you're trying to release`)
.action((args, commandOptions) =>
glitch(args, {
...program.opts(),
...commandOptions.opts(),
})
);

program.parse();
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
"commander": "^12.1.0",
"enquirer": "^2.4.1",
"execa": "^9.5.2",
"extract-zip": "^2.0.1",
"semver": "^7.6.3",
"tmp-promise": "^3.0.3",
"yaml": "^2.6.1"
},
"devDependencies": {
Expand Down
81 changes: 81 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

117 changes: 117 additions & 0 deletions projects/glitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { execa } from 'execa';

import { readFile, cp } from 'node:fs/promises';
import fs from 'fs';

import ensureOnePassword from './lib/ensure-one-password.js';
import { automated } from './lib/log.js';
import { dryExeca } from './lib/dry-execa.js';
import dryWrite from './lib/dry-write.js';

import tmp from 'tmp-promise';

import { basename, join } from 'node:path';
import extract from 'extract-zip';
import { finished } from 'stream/promises';
import { Readable } from 'node:stream';

tmp.setGracefulCleanup();

async function dryDownload(url, dest, dryRun) {
if(dryRun) {
console.log(`🌵 Downloading ${url} to ${dest}`);
return
}

automated(`Downloading ${url}`);

var file = fs.createWriteStream(dest);
const res = await fetch(url);
await finished(Readable.fromWeb(res.body).pipe(file));

}

function dryExtract(path, dir, dryRun) {
if(dryRun) {
console.log(`🌵 Extracting ${path} into ${dir}`)
return
}

automated(`Extracting ${basename(path)}`)
return extract(path, { dir })
}

function dryCopy(from, to, dryRun) {
if(dryRun) {
console.log(`🌵 Copying ${from} to ${to}`);
return
}
automated(`Copying ${from} to ${to}`);
return cp(from, to, {recursive: true});
}


export default async function guides(args, options) {
const dryRun = options.dryRun ?? false;

await ensureOnePassword();

const { stdout: glitchRepo } = await execa`op read ${"op://Ember Learning Team/Glitch/password"}`;
const tmpDirectory = await tmp.dir({
unsafeCleanup: true
});

// we need to make sure to clean up the temp directory since the remote url
// has the password embedded in it and it's a security risk to keep it around
try {
await dryExeca(`git clone ${glitchRepo}`, dryRun, {cwd: tmpDirectory.path });

await dryDownload(`https://github.com/ember-cli/ember-new-output/archive/v${options.version}.zip`, join(tmpDirectory.path, 'version.zip'), dryRun);

await dryExtract(join(tmpDirectory.path, 'version.zip'), tmpDirectory.path, dryRun);

await dryCopy(join(tmpDirectory.path, `ember-new-output-${options.version}`), join(tmpDirectory.path, 'emberjs'), dryRun);

let pkgJsonPath = join(tmpDirectory.path, 'emberjs', 'package.json');
let pkgJson;

if(dryRun) {
pkgJson = `{ "name": "fake-content" }`;
} else {
pkgJson = await readFile(pkgJsonPath, 'utf-8');
pkgJson = pkgJson.replace('ember serve', 'npx ember serve -p 4200')
}

await dryWrite(pkgJsonPath, pkgJson, dryRun);

await dryExeca(`npm install`, dryRun, {
cwd: join(tmpDirectory.path, 'emberjs')
});


let indexHtmlPath = join(tmpDirectory.path, 'emberjs', 'app', 'index.html');
let indexHtml;

if(dryRun) {
indexHtml = `<body> fake content </body>`;
} else {
indexHtml = await readFile(indexHtmlPath, "utf-8");
indexHtml = indexHtml.replace(` </body>`, `
<!-- include the Glitch button to show what the webpage is about and
to make it easier for folks to view source and remix -->
<div class="glitchButton" style="position:fixed;top:20px;right:20px;"></div>
<script src="https://button.glitch.me/button.js"></script>
</body>`)
}

await dryWrite(indexHtmlPath, indexHtml, dryRun);

await dryExeca(`git add .`, dryRun, {cwd: join(tmpDirectory.path, 'emberjs')})
await dryExeca(`git commit -m ${options.version}`, dryRun, {cwd: join(tmpDirectory.path, 'emberjs')})
await dryExeca(`git push`, dryRun, {cwd: join(tmpDirectory.path, 'emberjs')})
} finally {
await tmpDirectory.cleanup();
}

console.log('success');
}
3 changes: 2 additions & 1 deletion projects/lib/dry-execa.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { execaCommand } from 'execa';
* @param {string} command
* @param {boolean} dryRun
*/
export function dryExeca(command, dryRun = true) {
export function dryExeca(command, dryRun = true, options) {
if (dryRun) {
console.log(`🌵 Dry run: '${command}'`);
} else {
Expand All @@ -14,6 +14,7 @@ export function dryExeca(command, dryRun = true) {
preferLocal: true,
stdout: 'inherit',
stdin: 'inherit',
...options,
});
}
}
Loading
Loading