Skip to content

Commit

Permalink
Allow passing flags down from commands to CLI tools
Browse files Browse the repository at this point in the history
  • Loading branch information
eaviles committed Oct 21, 2021
1 parent b532f87 commit 7a98dcd
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 18 deletions.
8 changes: 6 additions & 2 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ const { description, homepage } = require('../package.json');
const learnMore = `Read the manual at ${homepage}`;
const { npm_lifecycle_event: lifecycleEvent } = process.env;

if (argv.length === 0 && lifecycleEvent) {
argv.push(lifecycleEvent);
if (lifecycleEvent) {
argv.splice(0, 0, lifecycleEvent);
}

if (argv.length > 1) {
argv.splice(1, 0, '--');
}

yargs(argv)
Expand Down
13 changes: 9 additions & 4 deletions lib/commands/build-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ const { readFile, writeFile } = require('fs').promises;

const { fileExists, loadConfig } = require('../utils');

const command = path.parse(__filename).name;

module.exports = {
command: path.parse(__filename).name,
command,
describe: 'Builds the README.md file from the JSDoc in the codebase.',
handler: async () => {
$.verbose = false;
if (process.argv[2] === command) {
process.argv.splice(2, 1);
}
$.verbose = true;
const cwd = process.cwd();
const readmePath = path.resolve(cwd, 'README.md');
const { docs } = await loadConfig();
Expand All @@ -28,7 +33,7 @@ module.exports = {
const output = await jsdocToMd.render(cfg);
await writeFile(readmePath, output, 'utf8');
await $`git add ./README.md`;
const cli = path.relative(process.cwd(), path.resolve(__dirname, '../../bin/cli.js'));
await $`${cli} prettier`;
// const cli = path.relative(process.cwd(), path.resolve(__dirname, '../../bin/cli.js'));
// await $`${cli} prettier`;
}
};
6 changes: 3 additions & 3 deletions lib/commands/build-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ module.exports = {
command,
describe: 'Build the types with TypeScript.',
handler: async () => {
if (process.argv[2] === command) {
process.argv.splice(2, 1);
}
$.verbose = false;
const cli = path.relative(process.cwd(), path.resolve(__dirname, '../../bin/cli.js'));
await $`${cli} clean-types`;
if (process.argv.slice(-1)[0] === command) {
process.argv.splice(2, 1);
}
process.argv.push('--emitDeclarationOnly');
require('typescript/bin/tsc');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/check-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
command,
describe: 'Check the types with TypeScript.',
handler: async () => {
if (process.argv.slice(-1)[0] === command) {
if (process.argv[2] === command) {
process.argv.splice(2, 1);
}
process.argv.push('--noEmit');
Expand Down
4 changes: 1 addition & 3 deletions lib/commands/clean-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
const path = require('path');
const { $ } = require('zx');

const command = path.parse(__filename).name;

module.exports = {
command,
command: path.parse(__filename).name,
describe: 'Delete all the types generated with TypeScript.',
handler: async () => {
$.verbose = false;
Expand Down
7 changes: 6 additions & 1 deletion lib/commands/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

const path = require('path');

const command = path.parse(__filename).name;

module.exports = {
command: path.parse(__filename).name,
command,
describe: 'Check the codebase with ESLint.',
handler: async () => {
if (process.argv[2] === command) {
process.argv.splice(2, 1);
}
process.argv.push(...[['.'], ['--ext', '.js,.json,.md'], ['--ignore-pattern', '!.*.*']].flat());
require('eslint/bin/eslint');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/prettier.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ module.exports = {
command,
describe: 'Format the codebase with Prettier.',
handler: async () => {
const exts = ['hbs', 'js', 'json', 'md', 'ts'];
if (process.argv.slice(-1)[0] === command) {
if (process.argv[2] === command) {
process.argv.splice(2, 1);
}
const exts = ['hbs', 'js', 'json', 'md', 'ts'];
process.argv.push('--write', `**/*.{${exts.join(',')}}`, '.*rc', '!coverage/**/*');
require('prettier/bin-prettier');
}
Expand Down
7 changes: 6 additions & 1 deletion lib/commands/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,15 @@ module.exports = {
version: cmd
};

// Remove the legacy core-commons tag.
pkg['@lifion/core-commons'] = undefined;

// Store the changes in the target package.json file.
await storeJsonFile('./package.json', pkg);

// Upgrade dependencies.
// Upgrade and reconcile dependencies.
await $`npx lifion-verify-deps -u`;
await $`rm -rf ./node_modules ./package-lock.json`;
await $`npm install`;
}
};
7 changes: 6 additions & 1 deletion lib/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ const { $ } = require('zx');

const { loadConfig } = require('../utils');

const command = path.parse(__filename).name;

module.exports = {
command: path.parse(__filename).name,
command,
describe: 'Run tests with Jest.',
handler: async () => {
if (process.argv[2] === command) {
process.argv.splice(2, 1);
}
$.verbose = false;
await $`mkdir -p ./reports`;
const { test } = await loadConfig();
Expand Down

0 comments on commit 7a98dcd

Please sign in to comment.