Skip to content

Commit

Permalink
Merge pull request #4 from ember-learn/refactor-reuse
Browse files Browse the repository at this point in the history
split out functions that can be reused in multiple commands
  • Loading branch information
mansona authored Dec 10, 2024
2 parents 838716c + 4441ee7 commit 5caca49
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 104 deletions.
4 changes: 3 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
pnpm-lock.yaml
pnpm-lock.yaml
node_modules
CHANGELOG.md
112 changes: 9 additions & 103 deletions projects/guides.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions projects/lib/dry-execa.js
Original file line number Diff line number Diff line change
@@ -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',
});
}
}
16 changes: 16 additions & 0 deletions projects/lib/dry-write.js
Original file line number Diff line number Diff line change
@@ -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');
}
}
42 changes: 42 additions & 0 deletions projects/lib/ensure-repo.js
Original file line number Diff line number Diff line change
@@ -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);
}
27 changes: 27 additions & 0 deletions projects/lib/log.js
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 5caca49

Please sign in to comment.