Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install commander, update cli to run using it #126

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-flies-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@manypkg/cli": minor
---

Added command line --help flag
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@babel/runtime": "^7.5.5",
"@manypkg/get-packages": "^1.1.3",
"chalk": "^2.4.2",
"commander": "^8.3.0",
Copy link
Collaborator

Choose a reason for hiding this comment

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

The preferred package to handle this sort of stuff would be this: https://www.npmjs.com/package/meow . It's the one used by both Changesets and Preconstruct (and those projects are maintained by roughly the same group of people~)

Copy link
Author

Choose a reason for hiding this comment

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

I see and understand why that makes sense. I want to argue in favor of commander over meow here, after looking over meow.

  1. commander provides a solid api, and docs for that api. https://github.com/commander-cli/commander
    • normally I would prefer meow's method of returning POJOs and arrays, but their documentation is solely lacking relative to commander, and is actually much less flexible than commander's api which is typically the benefit with returning POJOs and the like.
  2. commander's help text is automatically generated, it doesn't need maintaining, and won't go out of date. They're able to do this because you must declare each command and options within their api
  3. also due to how commands are called, with commander you can query help for commands individually, which comes for free, whereas you'd have to do that all yourself with meow.
  4. Commander allows for a lot of streamlining and simplification of the code you as a maintainer have to write, even after giving you all the above benefits. Commander takes care of parsing the command line more completely than meow does, and allows for the calling of disparate functions based on commands.

I created PR #128 that accomplishes similar with meow, which you can use for comparison. As the maintainer, it's, of course, completely up to you which you choose to take (if either!). I appreciate the work you've done and would love to contribute in the way most helpful to you.

"detect-indent": "^6.0.0",
"find-up": "^4.1.0",
"fs-extra": "^8.1.0",
Expand Down
115 changes: 89 additions & 26 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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<string, Package>(
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;
Expand Down Expand Up @@ -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 <cli-command...>')
.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 <pkg-name> <script>')
.description('runs a single script in a single package')
.action((pkg, scr) => runCmd([pkg, scr], process.cwd()));

program
.command('check')
.description('runs all the checks against your repo')
.action(checkCmd);

program
.command('upgrade <package-name> <tag-version>')
.description('probably upgrades a dependency')
.action(upgradeDependency);

program
.command('npm-tag <tag-name>')
.description('adds the npm tag to each public package in the repo')
.option('--otp', 'a otp code to use for something')
.action(npmTagAll);

program
.parseAsync(process.argv)
.catch(err => {
if (err instanceof ExitError) {
process.exit(err.code);
} else {
logger.error(err);
process.exit(1);
}
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4046,6 +4046,11 @@ commander@^2.19.0:
resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.0.tgz#d58bb2b5c1ee8f87b0d340027e9e94e222c5a422"
integrity sha512-7j2y+40w61zy6YC2iRNpUe/NwhNyoXrYpHMrSunaMG64nRnaf96zO/KMQR4OyN/UnE5KLyEBnKHd4aG3rskjpQ==

commander@^8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==

common-tags@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/common-tags/-/common-tags-1.8.0.tgz#8e3153e542d4a39e9b10554434afaaf98956a937"
Expand Down