Skip to content

Commit 041d88e

Browse files
Merge pull request #94 from CodeshiftCommunity/cli-refactor
Cli refactor
2 parents 385b4c2 + 321d212 commit 041d88e

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

.changeset/modern-colts-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@codeshift/cli': patch
3+
---
4+
5+
Fixes --help command. Top-level options will now be shown correctly

packages/cli/src/index.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import chalk from 'chalk';
2+
import { Command, Option, CommanderError } from 'commander';
3+
24
import main from './main';
35
import list from './list';
46
import init from './init';
57
import validate from './validate';
68
import { InvalidUserInputError } from './errors';
79

810
import packageJson from '../package.json';
9-
import { Command, Option, CommanderError } from 'commander';
1011

1112
const program = new Command();
1213

1314
program
14-
.command(`codeshift/cli [path...]`, { isDefault: true })
15+
.enablePositionalOptions()
1516
.version(packageJson.version, '-v, --version')
17+
.name('codeshift')
18+
.argument('[path...]')
1619
.usage('[global options] <file-paths>...')
17-
.option(
18-
'-t, --transform <value>',
19-
'The transform(s) to run, will prompt for a transform if not provided and no module is passed\nTo provide multiple transforms, separate them with commas (e.g. "-t t1,t2,t3")',
20-
)
2120
.option(
2221
'--packages <value>',
2322
'Comma separated list of packages to run transforms for, @scope/package[@version]. If version is supplied, will only run transforms for that version and above',
@@ -26,6 +25,10 @@ program
2625
'-s, --sequence',
2726
'If the package flag is provided, runs all transforms from the provided version to the latest',
2827
)
28+
.option(
29+
'-t, --transform <value>',
30+
'The transform(s) to run, will prompt for a transform if not provided and no module is passed\nTo provide multiple transforms, separate them with commas (e.g. "-t t1,t2,t3")',
31+
)
2932
.addOption(
3033
new Option(
3134
'-p, --parser <parser>',
@@ -54,20 +57,30 @@ program
5457
`
5558
Examples:
5659
# Run a transform for "@mylib/button" version 3.0.0 only
57-
$ codeshift-cli --packages @mylib/button@3.0.0 /project/src
60+
$ codeshift --packages @mylib/button@3.0.0 /project/src
5861
5962
# Run all transforms for "@mylib/button" greater than version 3.0.0 and @mylib/range greater than 4.0.0
60-
$ codeshift-cli --sequence --packages @mylib/button@3.0.0,@mylib/range@4.0.0 /project/src
63+
$ codeshift --sequence --packages @mylib/button@3.0.0,@mylib/range@4.0.0 /project/src
6164
6265
# Run the "my-custom-transform" transform
63-
$ codeshift-cli -t path/to/my-custom-transform /project/src`,
66+
$ codeshift -t path/to/my-custom-transform /project/src`,
6467
)
6568
.action((path, options) => main(path, options));
6669

6770
program
6871
.command('list <package-names...>')
6972
.description('list available codemods for provided packages')
70-
.action(packageNames => list(packageNames));
73+
.action(packageNames => list(packageNames))
74+
.addHelpText(
75+
'after',
76+
`
77+
Examples:
78+
# Print a list of available codemods for a single package
79+
$ codeshift list mylib
80+
81+
# Print a list of available codemods for multiple packages
82+
$ codeshift list @atlaskit/avatar @emotion/monorepo`,
83+
);
7184

7285
program
7386
.command('init [path]')
@@ -83,14 +96,13 @@ program
8396
`
8497
Examples:
8598
# Initializes an empty codeshift package at the ~/Desktop directory
86-
$ codeshift-cli init --package-name foobar --transform 10.0.0 ~/Desktop
99+
$ codeshift init --package-name foobar --transform 10.0.0 ~/Desktop
87100
88101
# Initializes a new codeshift package with a transform for 10.0.0
89-
$ codeshift-cli init --package-name foobar --transform 10.0.0 ~/Desktop
102+
$ codeshift init --package-name foobar --transform 10.0.0 ~/Desktop
90103
91104
# Initializes a new codeshift package with a preset "update-imports"
92-
$ codeshift-cli init --package-name foobar --preset update-imports ~/Desktop
93-
`,
105+
$ codeshift init --package-name foobar --preset update-imports ~/Desktop`,
94106
);
95107

96108
program
@@ -101,9 +113,8 @@ program
101113
'after',
102114
`
103115
Examples:
104-
$ codeshift-cli validate
105-
$ codeshift-cli validate ./codemods/my-codemods
106-
`,
116+
$ codeshift validate
117+
$ codeshift validate ./codemods/my-codemods`,
107118
);
108119

109120
program.exitOverride();
@@ -113,6 +124,10 @@ program.exitOverride();
113124
await program.parseAsync(process.argv);
114125
} catch (error) {
115126
if (error instanceof CommanderError) {
127+
if (error.message === '(outputHelp)') {
128+
return;
129+
}
130+
116131
console.error(chalk.red(error.message));
117132
process.exit(error.exitCode);
118133
}

packages/cli/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ export default async function main(paths: string[], flags: Flags) {
112112
let communityConfig;
113113
let remoteConfig;
114114

115+
console.log(chalk.green('Attempting to download package:'), pkgName);
116+
115117
try {
116118
communityConfig = await fetchCommunityPackageConfig(
117119
pkgName,

scripts/docs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ Migrates ${packageLink} to version ${key}.
7575
### Usage
7676
7777
\`\`\`
78-
$ codeshift-cli --packages ${name}@${key} path/to/source
78+
$ codeshift --packages ${name}@${key} path/to/source
7979
\`\`\`
8080
`,
8181
)
@@ -95,7 +95,7 @@ ${Object.keys(config.presets)
9595
### Usage
9696
9797
\`\`\`
98-
$ codeshift-cli --packages ${name}#${key} path/to/source
98+
$ codeshift --packages ${name}#${key} path/to/source
9999
\`\`\`
100100
`,
101101
)

website/src/pages/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const App = () => (
180180
</div>
181181
<div className="col col--8">
182182
<CodeBlock className="language-bash">
183-
$ codeshift-cli init --packageName foobar
183+
$ codeshift init --packageName foobar
184184
</CodeBlock>
185185
</div>
186186
</div>
@@ -266,7 +266,7 @@ export default transformer;`}</CodeBlock>
266266
</div>
267267
<div className="col col--8">
268268
<CodeBlock className="language-bash">
269-
$ codeshift-cli -p foobar@12.0.0 path/to/src
269+
$ codeshift -p foobar@12.0.0 path/to/src
270270
</CodeBlock>
271271
</div>
272272
</div>

0 commit comments

Comments
 (0)