diff --git a/.changeset/wicked-flies-kiss.md b/.changeset/wicked-flies-kiss.md new file mode 100644 index 0000000..643c04b --- /dev/null +++ b/.changeset/wicked-flies-kiss.md @@ -0,0 +1,5 @@ +--- +"@manypkg/cli": minor +--- + +Added command line --help flag diff --git a/packages/cli/package.json b/packages/cli/package.json index 0a88f83..1c4408e 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -16,6 +16,7 @@ "@babel/runtime": "^7.5.5", "@manypkg/get-packages": "^1.1.3", "chalk": "^2.4.2", + "commander": "^8.3.0", "detect-indent": "^6.0.0", "find-up": "^4.1.0", "fs-extra": "^8.1.0", diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index a8d5f7f..7c8eb7b 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -10,6 +10,7 @@ import { upgradeDependency } from "./upgrade"; import { npmTagAll } from "./npm-tag"; import spawn from "spawndamnit"; import pLimit from "p-limit"; +import { Command } from 'commander'; type RootPackage = Package & { packageJson: { @@ -113,27 +114,49 @@ async function execCmd(args: string[]) { throw new ExitError(highestExitCode); } -(async () => { - let things = process.argv.slice(2); - if (things[0] === "exec") { - return execCmd(things.slice(1)); - } - if (things[0] === "run") { - return runCmd(things.slice(1), process.cwd()); - } - if (things[0] === "upgrade") { - return upgradeDependency(things.slice(1)); - } - if (things[0] === "npm-tag") { - return npmTagAll(things.slice(1)); - } - if (things[0] !== "check" && things[0] !== "fix") { - logger.error( - `command ${things[0]} not found, only check, exec, run, upgrade, npm-tag and fix exist` +const checkCmd = async () => { + + let shouldFix = false; + let { packages, root, tool } = (await getPackages( + process.cwd() + )) as PackagesWithConfig; + + let options: Options = { + ...defaultOptions, + ...root.packageJson.manypkg + }; + + let packagesByName = new Map( + packages.map(x => [x.packageJson.name, x]) + ); + packagesByName.set(root.packageJson.name, root); + let { hasErrored, requiresInstall } = runChecks( + packagesByName, + root, + shouldFix, + options + ); + if (shouldFix) { + await Promise.all( + [...packagesByName].map(async ([pkgName, workspace]) => { + writePackage(workspace); + }) ); + if (requiresInstall) { + await install(tool, root.dir); + } + + logger.success(`fixed workspaces!`); + } else if (hasErrored) { + logger.info(`the above errors may be fixable with yarn manypkg fix`); throw new ExitError(1); + } else { + logger.success(`workspaces valid!`); } - let shouldFix = things[0] === "fix"; +}; + +const fixCmd = async () => { + let shouldFix = true; let { packages, root, tool } = (await getPackages( process.cwd() )) as PackagesWithConfig; @@ -170,11 +193,51 @@ async function execCmd(args: string[]) { } else { logger.success(`workspaces valid!`); } -})().catch(err => { - if (err instanceof ExitError) { - process.exit(err.code); - } else { - logger.error(err); - process.exit(1); - } -}); +}; + +/** + * start parsing the command line to run the appropriate command + */ +const program = new Command(); + +program + .command('exec ') + .description('execute a command for every package in the monorepo') + .action(execCmd); + +program + .command('fix') + .description('runs checks and fixes everything it is able to') + .action(fixCmd); + +program + .command('run