diff --git a/.prettierignore b/.prettierignore index eeeb7be..8149a19 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1 +1,3 @@ -pnpm-lock.yaml \ No newline at end of file +pnpm-lock.yaml +node_modules +CHANGELOG.md \ No newline at end of file diff --git a/projects/guides.js b/projects/guides.js index 8d033dd..8d4ec80 100644 --- a/projects/guides.js +++ b/projects/guides.js @@ -1,121 +1,25 @@ import semver from 'semver'; -import { execa, execaCommand } from 'execa'; +import { execa } from 'execa'; import enq from 'enquirer'; import { parse, stringify } from 'yaml'; const { Select, prompt } = enq; -import * as readline from 'node:readline/promises'; -import { stdin as input, stdout as output } from 'node:process'; -import { readFile, writeFile } from 'node:fs/promises'; +import { readFile } from 'node:fs/promises'; -async function manual(description) { - const rl = readline.createInterface({ input, output }); - await rl.question(`🧑‍💻 ${description} - -Press enter to continue...`); - rl.close(); -} - -/** - * - * @param {string} description - */ -function automated(description) { - console.log(`🤖 ${description}`); -} - -/** - * - * @param {string} command - * @param {boolean} dryRun - */ -function dryExeca(command, dryRun = true) { - if (dryRun) { - console.log(`🌵 Dry run: '${command}'`); - } else { - console.log(`🤖 Running command '${command}'`); - return execaCommand(command, { - preferLocal: true, - stdout: 'inherit', - stdin: 'inherit', - }); - } -} - -/** - * - * @param {string} file - * @param {string} contents - * @param {boolean} dryRun - */ -function dryWrite(file, contents, dryRun = true) { - if (dryRun) { - console.log(`🌵 Dry run: Updating the contents of '${file}' to be the following: - -${contents}`); - } else { - return writeFile(file, contents, 'utf-8'); - } -} - -/** - * - * @param {string} error - */ -function fatalError(error) { - console.error(error); - process.exit(1); -} +import ensureRepo from './lib/ensure-repo.js'; +import { automated, fatalError, manual } from './lib/log.js'; +import { dryExeca } from './lib/dry-execa.js'; +import dryWrite from './lib/dry-write.js'; async function minimumNodeVersion(minVersion) { const { stdout: nodeVerison } = await execa`node --version`; if (!semver.gte(semver.clean(nodeVerison), semver.coerce(minVersion))) { - console.error( - `Guides can only be installed with node version greater than ${minVersion} and above right now. you have ${nodeVerison}`, - ); - process.exit(1); - } -} - -async function ensureRepo(repo, branch, dryRun) { - let stdout; - - try { - let result = await execa`git remote get-url origin`; - stdout = result.stdout; - } catch (err) { - fatalError( - `Error checking current remote: [${err.message}]. Make sure you are in the cloned folder for ${repo}`, - ); - } - - if (repo !== stdout) { - console.error( - `It does not look like you are in the repo ${repo}. You can verify that you are by running 'git remote get-url origin'`, - ); - process.exit(1); - } - - let { stdout: cleanDir } = await execa`git status --porcelain`; - - if (cleanDir.length) { - fatalError(`Make sure you are in a clean working directory. You can verify this by making sure 'git status --porcelain' returns nothign. - -Current response: -${cleanDir}`); - } - - let { stdout: currentBranch } = await execa`git rev-parse --abbrev-ref HEAD`; - if (currentBranch !== branch) { fatalError( - `Make sure you are on the '${branch}' branch. You are currently on '${currentBranch}'`, + `Guides can only be installed with node version greater than ${minVersion} and above right now. you have ${nodeVerison}`, ); } - - automated('Pulling latest changes from origin'); - await dryExeca('git pull', dryRun); } export default async function guides(args, options) { @@ -176,6 +80,8 @@ export default async function guides(args, options) { automated( `Updating version number for links in /guides/${currentVersion}/**/*.md`, ); + + // TODO this should be pulled into this release scirpt rather than shelling out with execa await dryExeca( `node ./scripts/update-version-links guides/${currentVersion} ${currentVersion.replace(/^v/, '')} ${semver.coerce(emberDataCurrentVersion)} --silent`, dryRun, diff --git a/projects/lib/dry-execa.js b/projects/lib/dry-execa.js new file mode 100644 index 0000000..7ce66f4 --- /dev/null +++ b/projects/lib/dry-execa.js @@ -0,0 +1,19 @@ +import { execaCommand } from 'execa'; + +/** + * + * @param {string} command + * @param {boolean} dryRun + */ +export function dryExeca(command, dryRun = true) { + if (dryRun) { + console.log(`🌵 Dry run: '${command}'`); + } else { + console.log(`🤖 Running command '${command}'`); + return execaCommand(command, { + preferLocal: true, + stdout: 'inherit', + stdin: 'inherit', + }); + } +} diff --git a/projects/lib/dry-write.js b/projects/lib/dry-write.js new file mode 100644 index 0000000..40278c1 --- /dev/null +++ b/projects/lib/dry-write.js @@ -0,0 +1,16 @@ +import { writeFile } from 'node:fs/promises'; +/** + * + * @param {string} file + * @param {string} contents + * @param {boolean} dryRun + */ +export default function dryWrite(file, contents, dryRun = true) { + if (dryRun) { + console.log(`🌵 Dry run: Updating the contents of '${file}' to be the following: + +${contents}`); + } else { + return writeFile(file, contents, 'utf-8'); + } +} diff --git a/projects/lib/ensure-repo.js b/projects/lib/ensure-repo.js new file mode 100644 index 0000000..34fcebe --- /dev/null +++ b/projects/lib/ensure-repo.js @@ -0,0 +1,42 @@ +import { execa } from 'execa'; + +import { fatalError, automated } from './log.js'; +import { dryExeca } from './dry-execa.js'; + +export default async function ensureRepo(repo, branch, dryRun) { + let stdout; + + try { + let result = await execa`git remote get-url origin`; + stdout = result.stdout; + } catch (err) { + fatalError( + `Error checking current remote: [${err.message}]. Make sure you are in the cloned folder for ${repo}`, + ); + } + + if (repo !== stdout) { + fatalError( + `It does not look like you are in the repo ${repo}. You can verify that you are by running 'git remote get-url origin'`, + ); + } + + let { stdout: cleanDir } = await execa`git status --porcelain`; + + if (cleanDir.length) { + fatalError(`Make sure you are in a clean working directory. You can verify this by making sure 'git status --porcelain' returns nothign. + +Current response: +${cleanDir}`); + } + + let { stdout: currentBranch } = await execa`git rev-parse --abbrev-ref HEAD`; + if (currentBranch !== branch) { + fatalError( + `Make sure you are on the '${branch}' branch. You are currently on '${currentBranch}'`, + ); + } + + automated('Pulling latest changes from origin'); + await dryExeca('git pull', dryRun); +} diff --git a/projects/lib/log.js b/projects/lib/log.js new file mode 100644 index 0000000..1567c47 --- /dev/null +++ b/projects/lib/log.js @@ -0,0 +1,27 @@ +import * as readline from 'node:readline/promises'; +import { stdin as input, stdout as output } from 'node:process'; + +/** + * + * @param {string} description + */ +export function automated(description) { + console.log(`🤖 ${description}`); +} + +/** + * + * @param {string} error + */ +export function fatalError(error) { + console.error(error); + process.exit(1); +} + +export async function manual(description) { + const rl = readline.createInterface({ input, output }); + await rl.question(`🧑‍💻 ${description} + +Press enter to continue...`); + rl.close(); +}