-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.ts
More file actions
339 lines (298 loc) · 10.7 KB
/
main.ts
File metadata and controls
339 lines (298 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import path from 'path';
import fs from 'fs-extra';
import semver from 'semver';
import chalk from 'chalk';
import findUp from 'find-up';
import inquirer from 'inquirer';
import { CodeshiftConfig, DefaultRunner } from '@codeshift/types';
import { fetchConfigAtPath, fetchConfigs } from '@codeshift/fetcher';
import { PluginManager } from 'live-plugin-manager';
// @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
import * as jscodeshift from 'jscodeshift/src/Runner';
import { Flags } from './types';
import { InvalidUserInputError } from './errors';
import { fetchPackageConfig } from './fetch-package';
import { getConfigPrompt, getMultiConfigPrompt } from './prompt';
export default async function main(paths: string[], flags: Flags) {
if (paths.length === 0) {
throw new InvalidUserInputError(
'No path provided, please specify which files your codemod should modify',
);
}
const packageManager = new PluginManager({
pluginsPath: path.join(__dirname, 'node_modules'),
});
let transforms: string[] = [];
if (!flags.transform && !flags.packages) {
console.log(
chalk.green(
'No transforms specified, attempting to find local codeshift.config file(s)',
),
);
/**
* Attempt to locate a root package json with a workspaces config.
* If found, show a prompt with all available codemods
*/
let rootPackageJson: any;
const packageJsonPath = await findUp('package.json');
if (packageJsonPath) {
const packageJsonRaw = await fs.readFile(packageJsonPath, 'utf8');
rootPackageJson = JSON.parse(packageJsonRaw);
}
if (rootPackageJson && rootPackageJson.workspaces) {
const configs = await (rootPackageJson.workspaces as any[]).reduce<
Promise<{ filePath: string; config: CodeshiftConfig }[]>
>(async (accum, filePath) => {
const configs = await fetchConfigs(filePath);
if (!configs.length) return accum;
const results = await accum;
return [...results, ...configs];
}, Promise.resolve([]));
const answers = await inquirer.prompt([getMultiConfigPrompt(configs)]);
const selectedConfig = configs.find(
({ filePath }) => answers.codemod.filePath === filePath,
);
if (!selectedConfig) {
throw new Error(
`Unable to locate config at: ${answers.codemod.filePath}`,
);
}
if (
selectedConfig.config.transforms &&
selectedConfig.config.transforms[answers.codemod.selection]
) {
transforms.push(
selectedConfig.config.transforms[answers.codemod.selection],
);
} else if (
selectedConfig.config.presets &&
selectedConfig.config.presets[answers.codemod.selection]
) {
transforms.push(
selectedConfig.config.presets[answers.codemod.selection],
);
}
} else {
/**
* Otherwise, locate any config files in parent directories
*/
const configFilePath = await findUp([
'codeshift.config.js',
'codeshift.config.ts',
'codeshift.config.tsx',
'src/codeshift.config.js',
'src/codeshift.config.ts',
'src/codeshift.config.tsx',
'codemods/codeshift.config.js',
'codemods/codeshift.config.ts',
'codemods/codeshift.config.tsx',
]);
if (!configFilePath) {
throw new InvalidUserInputError(
'No transform provided, please specify a transform with either the --transform or --packages flags',
);
}
console.log(
chalk.green('Found local codeshift.config file at:'),
configFilePath,
);
const config = await fetchConfigAtPath(configFilePath);
const answers = await inquirer.prompt([getConfigPrompt(config)]);
if (config.transforms && config.transforms[answers.codemod]) {
transforms.push(config.transforms[answers.codemod]);
} else if (config.presets && config.presets[answers.codemod]) {
transforms.push(config.presets[answers.codemod]);
}
}
}
if (flags.transform) {
if (flags.transform.includes(',')) {
flags.transform.split(',').forEach(t => transforms.push(t.trim()));
} else {
transforms.push(flags.transform);
}
}
if (flags.packages) {
const pkgs = flags.packages.split(',').filter(pkg => !!pkg);
for (const pkg of pkgs) {
const shouldPrependAtSymbol = pkg.startsWith('@') ? '@' : '';
const pkgName =
shouldPrependAtSymbol + pkg.split(/[@#]/).filter(str => !!str)[0];
const config = await fetchPackageConfig(pkgName, packageManager);
const rawTransformIds = pkg.split(/(?=[@#])/).filter(str => !!str);
rawTransformIds.shift();
const transformIds = rawTransformIds
.filter(id => id.startsWith('@'))
.map(id => id.substring(1))
.sort((idA, idB) => {
if (semver.lt(idA, idB)) return -1;
if (semver.gt(idA, idB)) return 1;
return 0;
});
const presetIds = rawTransformIds
.filter(id => id.startsWith('#'))
.map(id => id.substring(1));
// Validate transforms/presets
transformIds.forEach(id => {
if (!semver.valid(semver.coerce(id.substring(1)))) {
throw new InvalidUserInputError(
`Invalid version provided to the --packages flag. Unable to resolve version "${id}" for package "${pkgName}". Please try: "[scope]/[package]@[version]" for example @mylib/mypackage@10.0.0`,
);
}
if (!config.transforms || !config.transforms[id]) {
throw new InvalidUserInputError(
`Invalid version provided to the --packages flag. Unable to resolve version "${id}" for package "${pkgName}"`,
);
}
});
presetIds.forEach(id => {
if (!config.presets || !config.presets[id]) {
throw new InvalidUserInputError(
`Invalid preset provided to the --packages flag. Unable to resolve preset "${id}" for package "${pkgName}"`,
);
}
});
if (presetIds.length === 0 && transformIds.length === 0) {
const res = await inquirer.prompt([getConfigPrompt(config)]);
if (semver.valid(semver.coerce(res.transform))) {
transformIds.push(res.transform);
} else {
presetIds.push(res.transform);
}
}
// Get transform file paths
if (config.transforms) {
if (flags.sequence) {
Object.entries(config.transforms as Record<string, string>)
.filter(([key]) => semver.satisfies(key, `>=${transformIds[0]}`))
.forEach(([, path]) => transforms.push(path));
} else {
Object.entries(config.transforms as Record<string, string>).forEach(
([id, path]) => {
if (transformIds.includes(id)) {
transforms.push(path);
}
},
);
}
}
// Get preset file paths
if (config.presets) {
Object.entries(config.presets as Record<string, string>).forEach(
([id, path]) => {
if (presetIds.includes(id)) {
transforms.push(path);
}
},
);
}
}
}
if (!transforms.length) {
throw new InvalidUserInputError(
'Unable to locate transforms from provided flags.',
);
}
// Dedupe transform array
transforms = transforms.filter(
(transform, i) => transforms.indexOf(transform) === i,
);
for (const transform of transforms) {
const resolvedTransformPath = path.resolve(transform);
console.log(chalk.green('Running transform:'), resolvedTransformPath);
const defaultRunner: DefaultRunner = (
jscodeshiftOptionOverrides = {},
pathsToModify = paths,
/**
* ideally you'd be able to pass in either the path,
* or the actual transform,
* but jscodeshift doesn't allow this (unless we fork?)
*/
transformerPath: string = resolvedTransformPath,
/**
* i think the jscodeshift.run is synchronous
* so the promise is not needed,
* but if we want to change it in the future,
* making it's return type a promise will help
* to avoid breaking changes for consumers who
* use the defaultRunner.
*/
): Promise<void> =>
jscodeshift.run(transformerPath, pathsToModify, {
verbose: flags.verbose,
dry: flags.dry,
print: true,
babel: true,
extensions: flags.extensions,
ignorePattern: flags.ignorePattern,
cpus: flags.cpus,
ignoreConfig: [],
runInBand: flags.runInBand,
silent: false,
parser: flags.parser,
stdin: false,
...jscodeshiftOptionOverrides,
});
let transformImported: any;
try {
/**
* TODO MAINTAINER -- i am not confident that this will work
* if the transform was provided thru an npm package.
*/
// eslint-disable-next-line @typescript-eslint/no-var-requires
transformImported = require(resolvedTransformPath);
} catch (_e) {}
const transformHasCustomRunner = (
ti: any,
): ti is {
/**
* ideally, `default` would be the type of the transformer,
* which would be identical to the type of the argument to
* `CustomTransformerConfig`,
*
* but unless we put the transformer itself into the config,
* we cannot ensure that the type is correct.
*
*/
default: unknown; //
codeshiftConfig: CodeshiftConfig<unknown>;
} => {
if (ti && 'codeshiftConfig' in ti) {
return 'runner' in transformImported['codeshiftConfig'];
}
return false;
};
if (transformHasCustomRunner(transformImported)) {
console.info(
'\nusing CUSTOM runner for transform',
resolvedTransformPath,
);
await transformImported.codeshiftConfig.runner({
pathsToModify: paths,
defaultRunner,
/**
* providing the `transform`, `resolvedTransformPath`, etc. here
* is quite useless, because it's file-based,
* so in whichever file the config is in,
* that default export will be the transform,
* and the file's path will be the resolved path.
*
* ...unless you have a custom runner defined in a separate file,
* and want it to be able to access the transform,
* esp. if that runner does not take in a path,
* but rather the transform function.
*/
transformInsideFileThatSpecifiesCodeshiftConfig:
transformImported.default,
// resolvedTransformPath
});
} else {
console.info(
'\nusing DEFAULT runner for transform',
resolvedTransformPath,
);
defaultRunner();
}
}
await packageManager.uninstallAll();
}