Skip to content

Commit

Permalink
chore: use cac instead of mri in all ui-kit CLIs (#2525)
Browse files Browse the repository at this point in the history
* chore: use cac instead of mri in all ui-kit CLIs

* chore: add changeset

* chore: use specific version of cac
  • Loading branch information
YahiaElTai authored May 19, 2023
1 parent 928dd81 commit 6e66d69
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 258 deletions.
6 changes: 6 additions & 0 deletions .changeset/sharp-ties-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@commercetools-local/generator-package-json': minor
'@commercetools-local/generator-readme': minor
---

use cac instead of mri for flags parsing
69 changes: 33 additions & 36 deletions generators/package-json/bin/generate-package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,38 @@ process.env.BABEL_ENV = 'development';
process.env.NODE_ENV = 'development';

import type { CommandFlags } from '../src/types';

import mri from 'mri';
import { cac } from 'cac';
import { generate } from '../src/index';

const flags = mri(process.argv.slice(2), { alias: { help: ['h'] } });
const [pathToPackage] = flags._;

const dryRun: boolean = flags['dry-run'] ? true : false;
const allWorkspacePackages: boolean = flags['all-workspace-packages']
? true
: false;
const hasRequiredArguments = allWorkspacePackages || pathToPackage;

if (flags.help || !hasRequiredArguments) {
console.log(`
Usage: generate-package-json [path-to-package] [options]
Displays help information.
[path-to-package]
The path to the package for which the package.json file should be generated.
Options:
--all-workspace-packages (optional) Run the script to all workspace packages. The "path-to-package" option is ignored.
--dry-run (optional) Simulate a run, the generated content will be printed to stdout.
`);
process.exit(0);
}

const options: CommandFlags = {
dryRun,
allWorkspacePackages,
};

generate(pathToPackage, options).catch((error) => {
console.error(error);
process.exit(1);
});
const cli = cac('generate-package-json');

cli
.command(
'[path-to-package]',
'The path to the package for which the package.json file should be generated.'
)
.option(
'--all-workspace-packages',
'(optional) Run the script to all workspace packages. The "path-to-package" option is ignored.'
)
.option(
'--dry-run',
'(optional) Simulate a run, the generated content will be printed to stdout.'
)
.action((pathToPackage: string, options: CommandFlags) => {
const hasRequiredArguments = options.allWorkspacePackages || pathToPackage;

if (!hasRequiredArguments) {
cli.outputHelp();
process.exit(0);
}

generate(pathToPackage, options).catch((error) => {
console.error(error);
process.exit(1);
});
});

cli.help();

cli.parse();
3 changes: 1 addition & 2 deletions generators/package-json/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
"dependencies": {
"@manypkg/find-root": "1.1.0",
"@manypkg/get-packages": "1.1.3",
"@types/mri": "1.1.1",
"@types/node": "16.18.31",
"mri": "1.2.0",
"cac": "6.7.14",
"omit-empty-es": "1.1.3",
"rcfile": "1.0.3"
},
Expand Down
73 changes: 35 additions & 38 deletions generators/readme/bin/generate-readme.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
#!/usr/bin/env node

// Do this as the first thing so that any code reading it knows the right env.
process.env.BABEL_ENV = 'development';
process.env.NODE_ENV = 'development';

import type { CommandFlags } from '../src/types';

import mri from 'mri';
import report from 'vfile-reporter';
import { cac } from 'cac';
import { generate } from '../src/index';

const flags = mri(process.argv.slice(2), { alias: { help: ['h'] } });
const [pathToPackage] = flags._;

const dryRun: boolean = flags['dry-run'] ? true : false;
const allWorkspacePackages: boolean = flags['all-workspace-packages']
? true
: false;
const hasRequiredArguments = allWorkspacePackages || pathToPackage;

if (flags.help || !hasRequiredArguments) {
console.log(`
Usage: generate-readme [path-to-package] [options]
Displays help information.
[path-to-package]
The path to the package for which the README.md file should be generated.
This assumes that the path is the package folder containing a "package.json".
Options:
--all-workspace-packages (optional) Run the script to all workspace packages. The "path-to-package" option is ignored.
--dry-run (optional) Simulate a run, the generated content will be printed to stdout.
`);
process.exit(0);
}

const options: CommandFlags = {
dryRun,
allWorkspacePackages,
};

generate(pathToPackage, options).catch((error) => {
console.error(report(error));
process.exit(1);
});
const cli = cac('generate-readme');

cli
.command(
'[path-to-package]',
'The path to the package for which the README.md file should be generated. \nThis assumes that the path is the package folder containing a "package.json".'
)
.option(
'--all-workspace-packages',
'(optional) Run the script to all workspace packages. The "path-to-package" option is ignored.'
)
.option(
'--dry-run',
'(optional) Simulate a run, the generated content will be printed to stdout.'
)
.action((pathToPackage: string, options: CommandFlags) => {
const hasRequiredArguments = options.allWorkspacePackages || pathToPackage;

if (!hasRequiredArguments) {
cli.outputHelp();
process.exit(0);
}

generate(pathToPackage, options).catch((error) => {
console.error(error);
process.exit(1);
});
});

cli.help();

cli.parse();
3 changes: 1 addition & 2 deletions generators/readme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
"@types/listr": "0.14.4",
"@types/lodash": "4.14.194",
"@types/mdast": "3.0.11",
"@types/mri": "1.1.1",
"@types/node": "16.18.31",
"@types/shelljs": "0.8.12",
"@types/unist": "2.0.6",
"@types/vfile": "4.0.0",
"cac": "6.7.14",
"listr": "0.14.3",
"listr-verbose-renderer": "0.6.0",
"lodash": "4.17.21",
"mri": "1.2.0",
"rcfile": "1.0.3",
"react-docgen": "5.4.3",
"remark-gfm": "1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"babel-plugin-typescript-to-proptypes": "1.4.2",
"browserslist": "4.21.5",
"bundlesize": "0.18.1",
"cac": "6.7.14",
"colors": "1.4.0",
"conventional-changelog-cli": "2.2.2",
"cross-env": "7.0.3",
Expand All @@ -120,7 +121,6 @@
"lint-staged": "13.2.2",
"moment": "2.29.4",
"moment-timezone": "0.5.43",
"mri": "1.2.0",
"omit-empty-es": "1.1.3",
"patch-package": "6.5.1",
"postcss": "8.4.23",
Expand Down
Loading

1 comment on commit 6e66d69

@vercel
Copy link

@vercel vercel bot commented on 6e66d69 May 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.