Skip to content

Commit 12cc69a

Browse files
committed
✨ Improve overal CLI flow.
1 parent 88632e4 commit 12cc69a

File tree

4 files changed

+67
-28
lines changed

4 files changed

+67
-28
lines changed

.node-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14.16.0

packages/cli/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
},
1515
"dependencies": {
1616
"chalk": "^4.1.0",
17+
"fs-extra": "^9.1.0",
1718
"jscodeshift": "^0.12.0",
1819
"live-plugin-manager": "^0.15.1",
1920
"meow": "^9.0.0",
21+
"semver": "^7.3.5",
2022
"ts-node": "^9.1.1"
2123
}
2224
}

packages/cli/src/cli.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Usage
2222
2323
Options
2424
--transform, -t the transform to run, will prompt for a transform if not provided and no module is passed
25-
--packages, runs transforms for the specified comma separated list of packages, optionally include a version for each package to run all transforms since that version
26-
--parser, -p babel|babylon|flow|ts|tsx parser to use for parsing the source files (default: babel)
25+
--packages -pkgs, Comma separated list of packages to run transforms for, @scope/package[@version]. If version is supplied, will only run transforms above that version
2726
--version, -v version number
27+
--parser, -p babel|babylon|flow|ts|tsx parser to use for parsing the source files (default: babel)
2828
--help, 😱
2929
3030
Examples
@@ -43,6 +43,11 @@ Examples
4343
},
4444
packages: {
4545
type: 'string',
46+
alias: 'pkgs',
47+
},
48+
range: {
49+
type: 'string',
50+
alias: 'r',
4651
},
4752
parser: {
4853
type: 'string',

packages/cli/src/main.ts

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// import { ParsedPath } from 'path';
2-
// import semver from 'semver';
3-
1+
import semver from 'semver';
2+
import fs from 'fs-extra';
43
import { PluginManager } from 'live-plugin-manager';
5-
// @ts-ignore
4+
// @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
65
import * as jscodeshift from 'jscodeshift/src/Runner';
76

87
import { Flags } from './cli';
@@ -12,51 +11,77 @@ export default async function main(
1211
paths: string[],
1312
flags: Flags = { parser: 'babel' },
1413
) {
15-
console.log('CodeshiftCommunity CLI');
16-
console.log(paths, flags);
14+
console.log('CodeshiftCommunity CLI', paths, flags);
1715

18-
const transforms = [];
16+
let transforms: string[] = [];
1917

2018
if (!flags.transform && !flags.packages) {
2119
throw new InvalidUserInputError(
2220
'No transform provided, please specify a transform with either the --transform or --packages flags',
2321
);
2422
}
2523

24+
if (paths.length === 0) {
25+
throw new InvalidUserInputError(
26+
'No path provided, please specify which files your codemod should modify',
27+
);
28+
}
29+
2630
if (flags.transform) {
2731
transforms.push(flags.transform);
2832
}
2933

34+
const packageManager = new PluginManager();
35+
3036
if (flags.packages) {
31-
const rawPackageName = flags.packages.replace('@', '').replace('/', '__');
32-
const packageName = `@codeshift/mod-${rawPackageName}`;
33-
console.log(rawPackageName, packageName);
37+
const pkgs = flags.packages.split(',');
3438

35-
const packageManager = new PluginManager();
36-
await packageManager.install(packageName);
39+
for (const pkg of pkgs) {
40+
const pkgSplit = pkg.split('@').filter(str => !!str);
41+
const name = pkgSplit[0].replace('/', '__');
42+
const baseVersion = semver.coerce(pkgSplit[pkgSplit.length - 1]);
3743

38-
const codemod = packageManager.require(packageName);
44+
if (!baseVersion && !semver.valid(baseVersion)) {
45+
throw new InvalidUserInputError(
46+
`Invalid version provided to the --packages flag. Package ${pkg} is missing version. Please try: "@[scope]/[package]@[version]" for example @atlaskit/avatar@10.0.0`,
47+
);
48+
}
3949

40-
console.log(
41-
// codemod,
42-
codemod.transform18_0_0,
43-
codemod.transform19_0_0,
44-
// packageManager.list(),
45-
// packageManager.getInfo(packageName),
46-
);
50+
const codemodName = `@codeshift/mod-${name}`;
4751

48-
// TODO: We'll have to uninstall the mod at some point
49-
// await packageManager.uninstall('codemod');
52+
await packageManager.install(codemodName);
53+
const info = await packageManager.getInfo(codemodName);
54+
55+
/**
56+
* TODO: currently jscodeshift only accepts a path to a transform rather than a function or module.
57+
* The below logic will need to be refactored once this is fixed
58+
*/
59+
const modulePath = `${info?.location}/src`;
60+
const directories = await fs.readdir(modulePath);
61+
62+
directories
63+
.filter(
64+
dir => semver.valid(dir) && semver.satisfies(dir, `>=${baseVersion}`),
65+
)
66+
.forEach(dir => transforms.push(`${modulePath}/${dir}/transform.ts`));
67+
}
5068
}
5169

5270
if (!transforms.length) {
53-
throw new Error('Unable to locate transforms from provided flags');
71+
throw new InvalidUserInputError(
72+
'Unable to locate transforms from provided flags.',
73+
);
5474
}
5575

76+
// Dedupe transform array
77+
transforms = transforms.filter(
78+
(transform, i) => transforms.indexOf(transform) === i,
79+
);
80+
5681
for (const transform of transforms) {
57-
// run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
58-
jscodeshift.run('', paths, {
59-
transform: transform,
82+
console.log('Running transform', transform);
83+
84+
await jscodeshift.run(transform, paths, {
6085
verbose: 0,
6186
dry: false,
6287
print: true,
@@ -70,4 +95,10 @@ export default async function main(
7095
stdin: false,
7196
});
7297
}
98+
99+
/**
100+
* TODO: uncomment the below when jscodeshift can be used as an async function
101+
*/
102+
// await packageManager.uninstallAll();
103+
// console.log('Done!');
73104
}

0 commit comments

Comments
 (0)