Skip to content

Commit 5caca49

Browse files
authored
Merge pull request #4 from ember-learn/refactor-reuse
split out functions that can be reused in multiple commands
2 parents 838716c + 4441ee7 commit 5caca49

File tree

6 files changed

+116
-104
lines changed

6 files changed

+116
-104
lines changed

.prettierignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
pnpm-lock.yaml
1+
pnpm-lock.yaml
2+
node_modules
3+
CHANGELOG.md

projects/guides.js

+9-103
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,25 @@
11
import semver from 'semver';
2-
import { execa, execaCommand } from 'execa';
2+
import { execa } from 'execa';
33
import enq from 'enquirer';
44
import { parse, stringify } from 'yaml';
55

66
const { Select, prompt } = enq;
77

8-
import * as readline from 'node:readline/promises';
9-
import { stdin as input, stdout as output } from 'node:process';
10-
import { readFile, writeFile } from 'node:fs/promises';
8+
import { readFile } from 'node:fs/promises';
119

12-
async function manual(description) {
13-
const rl = readline.createInterface({ input, output });
14-
await rl.question(`🧑‍💻 ${description}
15-
16-
Press enter to continue...`);
17-
rl.close();
18-
}
19-
20-
/**
21-
*
22-
* @param {string} description
23-
*/
24-
function automated(description) {
25-
console.log(`🤖 ${description}`);
26-
}
27-
28-
/**
29-
*
30-
* @param {string} command
31-
* @param {boolean} dryRun
32-
*/
33-
function dryExeca(command, dryRun = true) {
34-
if (dryRun) {
35-
console.log(`🌵 Dry run: '${command}'`);
36-
} else {
37-
console.log(`🤖 Running command '${command}'`);
38-
return execaCommand(command, {
39-
preferLocal: true,
40-
stdout: 'inherit',
41-
stdin: 'inherit',
42-
});
43-
}
44-
}
45-
46-
/**
47-
*
48-
* @param {string} file
49-
* @param {string} contents
50-
* @param {boolean} dryRun
51-
*/
52-
function dryWrite(file, contents, dryRun = true) {
53-
if (dryRun) {
54-
console.log(`🌵 Dry run: Updating the contents of '${file}' to be the following:
55-
56-
${contents}`);
57-
} else {
58-
return writeFile(file, contents, 'utf-8');
59-
}
60-
}
61-
62-
/**
63-
*
64-
* @param {string} error
65-
*/
66-
function fatalError(error) {
67-
console.error(error);
68-
process.exit(1);
69-
}
10+
import ensureRepo from './lib/ensure-repo.js';
11+
import { automated, fatalError, manual } from './lib/log.js';
12+
import { dryExeca } from './lib/dry-execa.js';
13+
import dryWrite from './lib/dry-write.js';
7014

7115
async function minimumNodeVersion(minVersion) {
7216
const { stdout: nodeVerison } = await execa`node --version`;
7317

7418
if (!semver.gte(semver.clean(nodeVerison), semver.coerce(minVersion))) {
75-
console.error(
76-
`Guides can only be installed with node version greater than ${minVersion} and above right now. you have ${nodeVerison}`,
77-
);
78-
process.exit(1);
79-
}
80-
}
81-
82-
async function ensureRepo(repo, branch, dryRun) {
83-
let stdout;
84-
85-
try {
86-
let result = await execa`git remote get-url origin`;
87-
stdout = result.stdout;
88-
} catch (err) {
89-
fatalError(
90-
`Error checking current remote: [${err.message}]. Make sure you are in the cloned folder for ${repo}`,
91-
);
92-
}
93-
94-
if (repo !== stdout) {
95-
console.error(
96-
`It does not look like you are in the repo ${repo}. You can verify that you are by running 'git remote get-url origin'`,
97-
);
98-
process.exit(1);
99-
}
100-
101-
let { stdout: cleanDir } = await execa`git status --porcelain`;
102-
103-
if (cleanDir.length) {
104-
fatalError(`Make sure you are in a clean working directory. You can verify this by making sure 'git status --porcelain' returns nothign.
105-
106-
Current response:
107-
${cleanDir}`);
108-
}
109-
110-
let { stdout: currentBranch } = await execa`git rev-parse --abbrev-ref HEAD`;
111-
if (currentBranch !== branch) {
11219
fatalError(
113-
`Make sure you are on the '${branch}' branch. You are currently on '${currentBranch}'`,
20+
`Guides can only be installed with node version greater than ${minVersion} and above right now. you have ${nodeVerison}`,
11421
);
11522
}
116-
117-
automated('Pulling latest changes from origin');
118-
await dryExeca('git pull', dryRun);
11923
}
12024

12125
export default async function guides(args, options) {
@@ -176,6 +80,8 @@ export default async function guides(args, options) {
17680
automated(
17781
`Updating version number for links in /guides/${currentVersion}/**/*.md`,
17882
);
83+
84+
// TODO this should be pulled into this release scirpt rather than shelling out with execa
17985
await dryExeca(
18086
`node ./scripts/update-version-links guides/${currentVersion} ${currentVersion.replace(/^v/, '')} ${semver.coerce(emberDataCurrentVersion)} --silent`,
18187
dryRun,

projects/lib/dry-execa.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { execaCommand } from 'execa';
2+
3+
/**
4+
*
5+
* @param {string} command
6+
* @param {boolean} dryRun
7+
*/
8+
export function dryExeca(command, dryRun = true) {
9+
if (dryRun) {
10+
console.log(`🌵 Dry run: '${command}'`);
11+
} else {
12+
console.log(`🤖 Running command '${command}'`);
13+
return execaCommand(command, {
14+
preferLocal: true,
15+
stdout: 'inherit',
16+
stdin: 'inherit',
17+
});
18+
}
19+
}

projects/lib/dry-write.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { writeFile } from 'node:fs/promises';
2+
/**
3+
*
4+
* @param {string} file
5+
* @param {string} contents
6+
* @param {boolean} dryRun
7+
*/
8+
export default function dryWrite(file, contents, dryRun = true) {
9+
if (dryRun) {
10+
console.log(`🌵 Dry run: Updating the contents of '${file}' to be the following:
11+
12+
${contents}`);
13+
} else {
14+
return writeFile(file, contents, 'utf-8');
15+
}
16+
}

projects/lib/ensure-repo.js

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { execa } from 'execa';
2+
3+
import { fatalError, automated } from './log.js';
4+
import { dryExeca } from './dry-execa.js';
5+
6+
export default async function ensureRepo(repo, branch, dryRun) {
7+
let stdout;
8+
9+
try {
10+
let result = await execa`git remote get-url origin`;
11+
stdout = result.stdout;
12+
} catch (err) {
13+
fatalError(
14+
`Error checking current remote: [${err.message}]. Make sure you are in the cloned folder for ${repo}`,
15+
);
16+
}
17+
18+
if (repo !== stdout) {
19+
fatalError(
20+
`It does not look like you are in the repo ${repo}. You can verify that you are by running 'git remote get-url origin'`,
21+
);
22+
}
23+
24+
let { stdout: cleanDir } = await execa`git status --porcelain`;
25+
26+
if (cleanDir.length) {
27+
fatalError(`Make sure you are in a clean working directory. You can verify this by making sure 'git status --porcelain' returns nothign.
28+
29+
Current response:
30+
${cleanDir}`);
31+
}
32+
33+
let { stdout: currentBranch } = await execa`git rev-parse --abbrev-ref HEAD`;
34+
if (currentBranch !== branch) {
35+
fatalError(
36+
`Make sure you are on the '${branch}' branch. You are currently on '${currentBranch}'`,
37+
);
38+
}
39+
40+
automated('Pulling latest changes from origin');
41+
await dryExeca('git pull', dryRun);
42+
}

projects/lib/log.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as readline from 'node:readline/promises';
2+
import { stdin as input, stdout as output } from 'node:process';
3+
4+
/**
5+
*
6+
* @param {string} description
7+
*/
8+
export function automated(description) {
9+
console.log(`🤖 ${description}`);
10+
}
11+
12+
/**
13+
*
14+
* @param {string} error
15+
*/
16+
export function fatalError(error) {
17+
console.error(error);
18+
process.exit(1);
19+
}
20+
21+
export async function manual(description) {
22+
const rl = readline.createInterface({ input, output });
23+
await rl.question(`🧑‍💻 ${description}
24+
25+
Press enter to continue...`);
26+
rl.close();
27+
}

0 commit comments

Comments
 (0)