Skip to content

Commit

Permalink
Merge pull request #134 from ember-learn/commander
Browse files Browse the repository at this point in the history
move from minimist to commander
  • Loading branch information
mansona authored Nov 10, 2023
2 parents 010f89a + 2005ba5 commit 5b2c728
Show file tree
Hide file tree
Showing 5 changed files with 539 additions and 310 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
},
extends: ['eslint:recommended', 'plugin:prettier/recommended', 'plugin:n/recommended'],
parserOptions: {
ecmaVersion: 2020,
ecmaVersion: 2022,
sourceType: 'module',
},
rules: {
Expand Down
151 changes: 73 additions & 78 deletions generate-local.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
#!/usr/bin/env node

import chalk from 'chalk';
import commandExists from 'command-exists';
import execa from 'execa';
import fsExtra from 'fs-extra';
import minimist from 'minimist';
import path from 'path';

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

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

const argv = minimist(process.argv.slice(2));

const { project, version, install } = argv;
import { program, Option, InvalidArgumentError } from 'commander';

const exit = function exit() {
function exit() {
console.log(...arguments);
process.exit(1);
};
}

function semverVersion(value) {
if (!/^\d+\.\d+\.\d+$/.test(value)) {
throw new InvalidArgumentError('Not a correctly defined semver version i.e. major.minor.patch');
}
return value;
}

program
.addOption(
new Option('-p, --project <project>', 'the project that you want to run this for')
.choices(['ember', 'ember-data'])
.makeOptionMandatory(),
)
.requiredOption('-v, --version <version>', 'project version', semverVersion);

program.parse();

const options = program.opts();

const { project, version } = options;

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

(async () => {
if (!project || !version) {
exit(
chalk.red('Both project and version args are required.\n'),
chalk.yellow(' e.g., yarn gen --project ember --version 3.10.1'),
);
}
try {
await commandExists('yarn');
} catch (e) {
exit(chalk.red('We need yarn installed globally for this script to work'));
}

let emberProjectPath = path.join('../', 'ember.js');
let emberDataProjectPath = path.join('../', 'data');

if (!['ember', 'ember-data'].includes(project)) {
exit(chalk.red(`Project has to be either 'ember' or 'ember-data'. (was given ${project})\n`));
let checkIfProjectDirExists = dirPath => {
if (!existsSync(dirPath)) {
exit(chalk.yellow(`Please checkout the ${project} project at ${dirPath}`));
}
};

let buildDocs = async projDirPath => {
checkIfProjectDirExists(projDirPath);

try {
await commandExists('yarn');
} catch (e) {
exit(chalk.red('We need yarn installed globally for this script to work'));
if (project === 'ember') {
await runCmd('volta', projDirPath, ['run', 'yarn']);
} else {
await runCmd('corepack', projDirPath, ['pnpm', 'install']);
}

let emberProjectPath = path.join('../', 'ember.js');
let emberDataProjectPath = path.join('../', 'data');

let checkIfProjectDirExists = dirPath => {
if (!existsSync(dirPath)) {
exit(chalk.yellow(`Please checkout the ${project} project at ${dirPath}`));
}
};

let buildDocs = async projDirPath => {
checkIfProjectDirExists(projDirPath);

if (project === 'ember') {
await runCmd('volta', projDirPath, ['run', 'yarn']);
} else {
await runCmd('corepack', projDirPath, ['pnpm', 'install']);
}

if (install) {
await runCmd(project === 'ember' ? 'yarn' : 'pnpm install', projDirPath);
console.log('\n\n');
}

await runCmd(
project === 'ember' ? 'volta run yarn docs' : 'corepack pnpm run build:docs',
projDirPath,
);

let destination = `${docsPath}/s3-docs/v${version}/${project}-docs.json`;
ensureFileSync(destination);
const projYuiDocFile = destination;
removeSync(projYuiDocFile);
removeSync(`${docsPath}/json-docs/${project}/${version}`);

const yuiDocFile = path.join(
projDirPath,
project === 'ember' ? 'docs/data.json' : 'packages/-ember-data/dist/docs/data.json',
);
copyFileSync(yuiDocFile, projYuiDocFile);
};

let dirMap = {
ember: emberProjectPath,
'ember-data': emberDataProjectPath,
};

await buildDocs(dirMap[project]);

await execa('volta', [
'run',
'yarn',
'start',
'--project',
project,
'--version',
version,
'--no-sync',
]).stdout.pipe(process.stdout);
})();
await runCmd(
project === 'ember' ? 'volta run yarn docs' : 'corepack pnpm run build:docs',
projDirPath,
);

let destination = `${docsPath}/s3-docs/v${version}/${project}-docs.json`;
ensureFileSync(destination);
const projYuiDocFile = destination;
removeSync(projYuiDocFile);
removeSync(`${docsPath}/json-docs/${project}/${version}`);

const yuiDocFile = path.join(
projDirPath,
project === 'ember' ? 'docs/data.json' : 'packages/-ember-data/dist/docs/data.json',
);
copyFileSync(yuiDocFile, projYuiDocFile);
};

let dirMap = {
ember: emberProjectPath,
'ember-data': emberDataProjectPath,
};

await buildDocs(dirMap[project]);

await execa('volta', ['run', 'yarn', 'start', '--projects', project, '--version', version], {
stdio: 'inherit',
});
45 changes: 36 additions & 9 deletions index.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,41 @@
import minimist from 'minimist';
#!/usr/bin/env node

import { apiDocsProcessor } from './main.js';
import { program, Option, InvalidArgumentError } from 'commander';
import chalk from 'chalk';

function semverVersion(value) {
if (!/^\d+\.\d+\.\d+$/.test(value)) {
throw new InvalidArgumentError('Not a correctly defined semver version i.e. major.minor.patch');
}
return value;
}

program
.addOption(
new Option('-p, --projects <project...>', 'the projects that you want to run this for')
.choices(['ember', 'ember-data'])
.makeOptionMandatory(),
)
.addOption(
new Option('-v, --version <version>', 'project version', semverVersion).conflicts('all'),
)
.addOption(
new Option('-a, --all', 'process all versions (this will take a long time)').conflicts(
'version',
),
)
.option('-c, --clean', 'clean (not sure what this does)');

program.parse();

const argv = minimist(process.argv.slice(2));
const options = program.opts();

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

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

apiDocsProcessor(projects, specificDocsVersion, runClean, noSync);
apiDocsProcessor(projects, version, clean);
Loading

0 comments on commit 5b2c728

Please sign in to comment.