Skip to content

Commit 9f88109

Browse files
committed
Adds transfrom prompt
1 parent b732f5c commit 9f88109

File tree

6 files changed

+1661
-28
lines changed

6 files changed

+1661
-28
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@changesets/cli": "^2.6.2",
4343
"@manypkg/cli": "^0.11.1",
4444
"@preconstruct/cli": "^2.0.0",
45+
"@types/inquirer": "^8.2.1",
4546
"@types/jest": "^26.0.15",
4647
"@types/jscodeshift": "^0.11.0",
4748
"@types/tar": "^4.0.4",

packages/cli/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,16 @@
1515
"start:dev": "ts-node src/index.ts"
1616
},
1717
"dependencies": {
18+
"@codemod/cli": "^3.1.2",
1819
"@codeshift/fetcher": "^0.0.3",
1920
"@codeshift/initializer": "^0.3.0",
2021
"@codeshift/types": "*",
2122
"@codeshift/validator": "^0.4.0",
2223
"chalk": "^4.1.0",
2324
"commander": "^8.2.0",
25+
"find-up": "^5.0.0",
2426
"fs-extra": "^9.1.0",
27+
"inquirer": "^8.2.4",
2528
"jscodeshift": "^0.13.1",
2629
"live-plugin-manager": "^0.15.1",
2730
"lodash": "^4.17.21",

packages/cli/src/main.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
import path from 'path';
22
import semver from 'semver';
33
import chalk from 'chalk';
4-
import { PluginManager } from 'live-plugin-manager';
4+
import findUp from 'find-up';
5+
import inquirer from 'inquirer';
56

7+
import { fetchConfigAtPath } from '@codeshift/fetcher';
8+
import { PluginManager } from 'live-plugin-manager';
69
// @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
710
import * as jscodeshift from 'jscodeshift/src/Runner';
811

912
import { Flags } from './types';
1013
import { InvalidUserInputError } from './errors';
1114
import { fetchPackageConfig } from './fetch-package';
15+
import { getTransformPrompt } from './prompt';
1216

1317
export default async function main(paths: string[], flags: Flags) {
1418
const packageManager = new PluginManager({
@@ -18,9 +22,32 @@ export default async function main(paths: string[], flags: Flags) {
1822
let transforms: string[] = [];
1923

2024
if (!flags.transform && !flags.packages) {
21-
throw new InvalidUserInputError(
22-
'No transform provided, please specify a transform with either the --transform or --packages flags',
25+
console.log(
26+
chalk.green(
27+
'No transforms specified, attempting to find local codeshift.config file',
28+
),
2329
);
30+
31+
const configFilePath = await findUp([
32+
'codeshift.config.js',
33+
'codeshift.config.ts',
34+
'codeshift.config.tsx',
35+
]);
36+
37+
if (!configFilePath) {
38+
throw new InvalidUserInputError(
39+
'No transform provided, please specify a transform with either the --transform or --packages flags',
40+
);
41+
}
42+
43+
console.log(
44+
chalk.green('Found local codeshift.config file at:'),
45+
configFilePath,
46+
);
47+
48+
const config = await fetchConfigAtPath(configFilePath);
49+
const answers = await inquirer.prompt([getTransformPrompt(config)]);
50+
transforms.push(answers.transform);
2451
}
2552

2653
if (paths.length === 0) {

packages/cli/src/prompt.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import inquirer from 'inquirer';
2+
3+
import { CodeshiftConfig } from '@codeshift/types';
4+
5+
export const getTransformPrompt = (config: CodeshiftConfig) => {
6+
const transforms = Object.keys(config.transforms || {});
7+
const presets = Object.keys(config.presets || {});
8+
9+
const choices = [
10+
transforms.length ? new inquirer.Separator('Transforms') : undefined,
11+
...transforms,
12+
presets.length ? new inquirer.Separator('Presets') : undefined,
13+
...presets,
14+
].filter(item => item !== undefined);
15+
16+
return {
17+
type: 'list',
18+
name: 'transform',
19+
message: 'Which transform would you like to run?',
20+
choices,
21+
};
22+
};

packages/fetcher/src/index.ts

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

12+
function requireConfig(filePath: string, resolvedPath: string ) {
13+
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+
}
22+
}
23+
1224
export async function fetchConfig(
1325
filePath: string,
1426
): Promise<CodeshiftConfig | undefined> {
@@ -24,20 +36,27 @@ export async function fetchConfig(
2436

2537
if (!exists) continue;
2638

27-
try {
28-
// eslint-disable-next-line @typescript-eslint/no-var-requires
29-
const pkg = require(resolvedMatchedPath);
30-
return resolveConfigExport(pkg);
31-
} catch (e) {
32-
throw new Error(
33-
`Found config file "${matchedPath}" but was unable to parse it. This can be caused when transform or preset paths are incorrect.`,
34-
);
35-
}
39+
return requireConfig(matchedPath, resolvedMatchedPath);
3640
}
3741

3842
return undefined;
3943
}
4044

45+
export async function fetchConfigAtPath(
46+
filePath: string,
47+
): Promise<CodeshiftConfig> {
48+
const resolvedFilePath = path.resolve(filePath);
49+
const exists = fs.existsSync(resolvedFilePath);
50+
51+
if (!exists) {
52+
throw new Error(
53+
`Unable to find config at path: ${filePath}`,
54+
);
55+
};
56+
57+
return requireConfig(filePath, resolvedFilePath);
58+
}
59+
4160
export async function fetchPackage(
4261
packageName: string,
4362
packageManager: PluginManager,

0 commit comments

Comments
 (0)