Skip to content

Commit 5b2c728

Browse files
authored
Merge pull request #134 from ember-learn/commander
move from minimist to commander
2 parents 010f89a + 2005ba5 commit 5b2c728

File tree

5 files changed

+539
-310
lines changed

5 files changed

+539
-310
lines changed

Diff for: .eslintrc.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module.exports = {
55
},
66
extends: ['eslint:recommended', 'plugin:prettier/recommended', 'plugin:n/recommended'],
77
parserOptions: {
8-
ecmaVersion: 2020,
8+
ecmaVersion: 2022,
99
sourceType: 'module',
1010
},
1111
rules: {

Diff for: generate-local.js

100644100755
+73-78
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
1+
#!/usr/bin/env node
2+
13
import chalk from 'chalk';
24
import commandExists from 'command-exists';
35
import execa from 'execa';
46
import fsExtra from 'fs-extra';
5-
import minimist from 'minimist';
67
import path from 'path';
78

89
const { copyFileSync, ensureFileSync, existsSync, removeSync } = fsExtra;
910

1011
const docsPath = '../ember-api-docs-data';
1112

12-
const argv = minimist(process.argv.slice(2));
13-
14-
const { project, version, install } = argv;
13+
import { program, Option, InvalidArgumentError } from 'commander';
1514

16-
const exit = function exit() {
15+
function exit() {
1716
console.log(...arguments);
1817
process.exit(1);
19-
};
18+
}
19+
20+
function semverVersion(value) {
21+
if (!/^\d+\.\d+\.\d+$/.test(value)) {
22+
throw new InvalidArgumentError('Not a correctly defined semver version i.e. major.minor.patch');
23+
}
24+
return value;
25+
}
26+
27+
program
28+
.addOption(
29+
new Option('-p, --project <project>', 'the project that you want to run this for')
30+
.choices(['ember', 'ember-data'])
31+
.makeOptionMandatory(),
32+
)
33+
.requiredOption('-v, --version <version>', 'project version', semverVersion);
34+
35+
program.parse();
36+
37+
const options = program.opts();
38+
39+
const { project, version } = options;
2040

2141
async function runCmd(cmd, path, args = []) {
2242
console.log(chalk.underline(`Running '${chalk.green(cmd)}' in ${path}`));
@@ -31,80 +51,55 @@ async function runCmd(cmd, path, args = []) {
3151
console.log(executedCmd.stdout + '\n');
3252
}
3353

34-
(async () => {
35-
if (!project || !version) {
36-
exit(
37-
chalk.red('Both project and version args are required.\n'),
38-
chalk.yellow(' e.g., yarn gen --project ember --version 3.10.1'),
39-
);
40-
}
54+
try {
55+
await commandExists('yarn');
56+
} catch (e) {
57+
exit(chalk.red('We need yarn installed globally for this script to work'));
58+
}
59+
60+
let emberProjectPath = path.join('../', 'ember.js');
61+
let emberDataProjectPath = path.join('../', 'data');
4162

42-
if (!['ember', 'ember-data'].includes(project)) {
43-
exit(chalk.red(`Project has to be either 'ember' or 'ember-data'. (was given ${project})\n`));
63+
let checkIfProjectDirExists = dirPath => {
64+
if (!existsSync(dirPath)) {
65+
exit(chalk.yellow(`Please checkout the ${project} project at ${dirPath}`));
4466
}
67+
};
68+
69+
let buildDocs = async projDirPath => {
70+
checkIfProjectDirExists(projDirPath);
4571

46-
try {
47-
await commandExists('yarn');
48-
} catch (e) {
49-
exit(chalk.red('We need yarn installed globally for this script to work'));
72+
if (project === 'ember') {
73+
await runCmd('volta', projDirPath, ['run', 'yarn']);
74+
} else {
75+
await runCmd('corepack', projDirPath, ['pnpm', 'install']);
5076
}
5177

52-
let emberProjectPath = path.join('../', 'ember.js');
53-
let emberDataProjectPath = path.join('../', 'data');
54-
55-
let checkIfProjectDirExists = dirPath => {
56-
if (!existsSync(dirPath)) {
57-
exit(chalk.yellow(`Please checkout the ${project} project at ${dirPath}`));
58-
}
59-
};
60-
61-
let buildDocs = async projDirPath => {
62-
checkIfProjectDirExists(projDirPath);
63-
64-
if (project === 'ember') {
65-
await runCmd('volta', projDirPath, ['run', 'yarn']);
66-
} else {
67-
await runCmd('corepack', projDirPath, ['pnpm', 'install']);
68-
}
69-
70-
if (install) {
71-
await runCmd(project === 'ember' ? 'yarn' : 'pnpm install', projDirPath);
72-
console.log('\n\n');
73-
}
74-
75-
await runCmd(
76-
project === 'ember' ? 'volta run yarn docs' : 'corepack pnpm run build:docs',
77-
projDirPath,
78-
);
79-
80-
let destination = `${docsPath}/s3-docs/v${version}/${project}-docs.json`;
81-
ensureFileSync(destination);
82-
const projYuiDocFile = destination;
83-
removeSync(projYuiDocFile);
84-
removeSync(`${docsPath}/json-docs/${project}/${version}`);
85-
86-
const yuiDocFile = path.join(
87-
projDirPath,
88-
project === 'ember' ? 'docs/data.json' : 'packages/-ember-data/dist/docs/data.json',
89-
);
90-
copyFileSync(yuiDocFile, projYuiDocFile);
91-
};
92-
93-
let dirMap = {
94-
ember: emberProjectPath,
95-
'ember-data': emberDataProjectPath,
96-
};
97-
98-
await buildDocs(dirMap[project]);
99-
100-
await execa('volta', [
101-
'run',
102-
'yarn',
103-
'start',
104-
'--project',
105-
project,
106-
'--version',
107-
version,
108-
'--no-sync',
109-
]).stdout.pipe(process.stdout);
110-
})();
78+
await runCmd(
79+
project === 'ember' ? 'volta run yarn docs' : 'corepack pnpm run build:docs',
80+
projDirPath,
81+
);
82+
83+
let destination = `${docsPath}/s3-docs/v${version}/${project}-docs.json`;
84+
ensureFileSync(destination);
85+
const projYuiDocFile = destination;
86+
removeSync(projYuiDocFile);
87+
removeSync(`${docsPath}/json-docs/${project}/${version}`);
88+
89+
const yuiDocFile = path.join(
90+
projDirPath,
91+
project === 'ember' ? 'docs/data.json' : 'packages/-ember-data/dist/docs/data.json',
92+
);
93+
copyFileSync(yuiDocFile, projYuiDocFile);
94+
};
95+
96+
let dirMap = {
97+
ember: emberProjectPath,
98+
'ember-data': emberDataProjectPath,
99+
};
100+
101+
await buildDocs(dirMap[project]);
102+
103+
await execa('volta', ['run', 'yarn', 'start', '--projects', project, '--version', version], {
104+
stdio: 'inherit',
105+
});

Diff for: index.js

100644100755
+36-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1-
import minimist from 'minimist';
1+
#!/usr/bin/env node
2+
23
import { apiDocsProcessor } from './main.js';
4+
import { program, Option, InvalidArgumentError } from 'commander';
5+
import chalk from 'chalk';
6+
7+
function semverVersion(value) {
8+
if (!/^\d+\.\d+\.\d+$/.test(value)) {
9+
throw new InvalidArgumentError('Not a correctly defined semver version i.e. major.minor.patch');
10+
}
11+
return value;
12+
}
13+
14+
program
15+
.addOption(
16+
new Option('-p, --projects <project...>', 'the projects that you want to run this for')
17+
.choices(['ember', 'ember-data'])
18+
.makeOptionMandatory(),
19+
)
20+
.addOption(
21+
new Option('-v, --version <version>', 'project version', semverVersion).conflicts('all'),
22+
)
23+
.addOption(
24+
new Option('-a, --all', 'process all versions (this will take a long time)').conflicts(
25+
'version',
26+
),
27+
)
28+
.option('-c, --clean', 'clean (not sure what this does)');
29+
30+
program.parse();
331

4-
const argv = minimist(process.argv.slice(2));
32+
const options = program.opts();
533

6-
let possibleProjects = ['ember', 'ember-data'];
7-
let projects =
8-
argv.project && possibleProjects.includes(argv.project) ? [argv.project] : possibleProjects;
9-
let specificDocsVersion = argv.version ? argv.version : '';
34+
const { projects, version, clean, all } = options;
1035

11-
let runClean = !!argv.clean;
12-
let noSync = !argv.sync;
36+
if (!version && !all) {
37+
console.log(chalk.red('You need to specify a --version or pass --all to this program'));
38+
process.exit(1);
39+
}
1340

14-
apiDocsProcessor(projects, specificDocsVersion, runClean, noSync);
41+
apiDocsProcessor(projects, version, clean);

0 commit comments

Comments
 (0)