Skip to content

Commit c24d9e9

Browse files
committed
temp
1 parent 60db9d4 commit c24d9e9

File tree

9 files changed

+88
-1515
lines changed

9 files changed

+88
-1515
lines changed

.changeset/tasty-pigs-own.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@codeshift/cli': minor
3+
'@codeshift/fetcher': minor
4+
---
5+
6+
CLI now prompts users with codemods from their local codeshift.config.js files.

packages/cli/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"start:dev": "ts-node src/index.ts"
1616
},
1717
"dependencies": {
18-
"@codemod/cli": "^3.1.2",
1918
"@codeshift/fetcher": "^0.0.3",
2019
"@codeshift/initializer": "^0.3.0",
2120
"@codeshift/types": "*",

packages/cli/src/fetch-package.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ export async function fetchPackageConfig(
2929
);
3030
spinner.succeed(
3131
`${chalk.green(
32-
`Found CodeshiftCommunity package: `,
32+
'Found CodeshiftCommunity package:',
3333
)} ${getCodeshiftPackageName(packageName)}`,
3434
);
3535
} catch (error) {
3636
spinner.warn(
3737
`${chalk.yellow(
38-
`Unable to locate CodeshiftCommunity package: `,
38+
`Unable to locate CodeshiftCommunity package:`,
3939
)} ${getCodeshiftPackageName(packageName)}`,
4040
);
4141
}
4242

4343
try {
4444
remoteConfig = await fetchRemotePackage(packageName, packageManager);
4545
spinner.succeed(
46-
`${chalk.green(`Found codeshift package: `)} ${packageName}`,
46+
`${chalk.green('Found codeshift package:')} ${packageName}`,
4747
);
4848
} catch (error) {
4949
spinner.warn(
50-
`${chalk.yellow(`Unable to locate codeshift package: `)} ${packageName}`,
50+
`${chalk.yellow('Unable to locate codeshift package:')} ${packageName}`,
5151
);
5252
}
5353

@@ -62,7 +62,7 @@ Make sure the package name "${packageName}" is correct and try again.`,
6262

6363
if (!isValidConfig(config)) {
6464
throw new Error(
65-
`Unable to locate a valid codeshift.config in package ${packageName}`,
65+
`Unable to locate a valid codeshift.config in package: ${packageName}`,
6666
);
6767
}
6868

packages/cli/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ Examples:
7272
$ codeshift --sequence --packages @mylib/[email protected],@mylib/[email protected] /project/src
7373
7474
# Run the "my-custom-transform" transform
75-
$ codeshift -t path/to/my-custom-transform /project/src`,
75+
$ codeshift -t path/to/my-custom-transform /project/src
76+
77+
# Display a prompt with a list of codemods from my local \`codeshift.config.js\` file(s).
78+
$ codeshift /project/src`,
7679
)
7780
.action((path, options) => main(path, options));
7881

packages/cli/src/main.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
jest.mock('globby');
22
jest.mock('live-plugin-manager');
3+
jest.mock('find-up');
34
jest.mock('jscodeshift/src/Runner', () => ({
45
run: jest.fn().mockImplementation(() => Promise.resolve()),
56
}));
@@ -34,7 +35,7 @@ describe('main', () => {
3435
}
3536
});
3637

37-
it('should exit early if nether a package or transform is supplied', async () => {
38+
it('should throw if nether a package or transform is supplied and unable to find local config', async () => {
3839
expect.assertions(1);
3940

4041
try {

packages/cli/src/main.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export default async function main(paths: string[], flags: Flags) {
3232
'codeshift.config.js',
3333
'codeshift.config.ts',
3434
'codeshift.config.tsx',
35+
'src/codeshift.config.js',
36+
'src/codeshift.config.ts',
37+
'src/codeshift.config.tsx',
38+
'codemods/codeshift.config.js',
39+
'codemods/codeshift.config.ts',
40+
'codemods/codeshift.config.tsx',
3541
]);
3642

3743
if (!configFilePath) {

packages/fetcher/src/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ function resolveConfigExport(pkg: any): CodeshiftConfig {
99
return pkg.default ? pkg.default : pkg;
1010
}
1111

12-
function requireConfig(filePath: string, resolvedPath: string ) {
12+
function requireConfig(filePath: string, resolvedPath: string) {
1313
try {
14-
// eslint-disable-next-line @typescript-eslint/no-var-requires
15-
const pkg = require(resolvedPath);
16-
return resolveConfigExport(pkg);
17-
} catch (e) {
18-
throw new Error(
19-
`Found config file "${filePath}" but was unable to parse it. This can be caused when transform or preset paths are incorrect.`,
20-
);
21-
}
14+
// eslint-disable-next-line @typescript-eslint/no-var-requires
15+
const pkg = require(resolvedPath);
16+
return resolveConfigExport(pkg);
17+
} catch (e) {
18+
throw new Error(
19+
`Found config file "${filePath}" but was unable to parse it. This can be caused when transform or preset paths are incorrect.`,
20+
);
21+
}
2222
}
2323

2424
export async function fetchConfig(
@@ -49,10 +49,8 @@ export async function fetchConfigAtPath(
4949
const exists = fs.existsSync(resolvedFilePath);
5050

5151
if (!exists) {
52-
throw new Error(
53-
`Unable to find config at path: ${filePath}`,
54-
);
55-
};
52+
throw new Error(`Unable to find config at path: ${filePath}`);
53+
}
5654

5755
return requireConfig(filePath, resolvedFilePath);
5856
}

website/docs/api/codeshift-cli.mdx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,44 @@ and run with:
3131

3232
`$ codeshift` or `$ codeshift-cli`
3333

34-
## Options
34+
## default
35+
36+
The default CLI command (when no subcommand is specified,) will attempt to download and run transform(s) against the specified file path.
37+
38+
In the majority of cases you want to be sure to either provide the `--packages` flag for running remote codemods, or the `--transform, -t` flag to run a local transform file.
39+
40+
For running codemods as an end-user it's recommend to use the `--packages` flag, which accepts the following format: `--packages [package-name]@[semver-version]`. For example, running the codemod to migrate your codebase to `react` version `18.0.0` you would specify the following `--packages [email protected]`.
41+
42+
In special cases, codeshift package authors may choose to also expose codemod "presets", which can be considered as utility codemods for that package. Presets are also run via the `--packages` flag like so: `--packages react#remove-spread-props`.
43+
Notice that we have switched to a hash `#` here to denote that we want to run a preset.
44+
45+
Codeshift will then attempt to locate codemods from both the [community folder](https://github.com/CodeshiftCommunity/CodeshiftCommunity/tree/main/community) and the primary npm package ie [React – NPM](https://www.npmjs.com/package/react).
46+
(Note: Some packages wont have any codemods, you can use the [list](#list) subcommand to check if they exist.)
47+
48+
Lastly, when authoring a package, it's possible to test your transforms by omitting both the `--packages` and `--transform` flags. In this interactive mode, the `codeshift/cli` will attempt to locate
49+
a local `codeshift.config.js` from the current or parent directories and present an interactive prompt for you to choose from.
50+
51+
**example:**
52+
53+
Run a transform for "@mylib/button" version 3.0.0 only
54+
55+
- `$ codeshift --packages @mylib/[email protected] /project/src`
56+
57+
Run a transform for "@mylib/button" preset `foo-bar` only
58+
59+
- `$ codeshift --packages @mylib/button#foo-bar /project/src`
60+
61+
Run all transforms for "@mylib/button" greater than version 3.0.0 and @mylib/range greater than 4.0.0
62+
63+
- `$ codeshift --sequence --packages @mylib/[email protected],@mylib/[email protected] /project/src`
64+
65+
Run the "my-custom-transform" transform
66+
67+
- `$ codeshift -t path/to/my-custom-transform /project/src`
68+
69+
Display a prompt with a list of codemods from my local `codeshift.config.js` file(s).
70+
71+
- `$ codeshift /project/src`
3572

3673
### --transform, -t
3774

0 commit comments

Comments
 (0)