Skip to content

Commit e2a36b1

Browse files
committed
feat: allow providing custom runner per transform file thru CodeshiftConfig
depends on: - hypermod-io#63 example on how it could be used: - https://github.com/pipedrive/CodeshiftCommunity/pull/22 - though note we might refactor into separate PRs, idk, preferably would use directly from upstream (you). fixes a lot of cases for us: 1. we have a postcss codemod that we want to run, while still utilizing the @codeshift/cli. though, i don't know if these changes will work if we're using a remote package, will they? 2. we'll want to do some global pre-processing on files before running our codemod. though, there's still no way to provide the codemod as a __function__ instead of an __import path__ to jscodeshift, which will force us to do dependency injection instead of just passing the pre-processed results as an argument to a function. this is where the considerations to fork jscodeshift come into play again: - hypermod-io#67 Signed-off-by: Kipras Melnikovas <kipras@kipras.org>
1 parent e28a6e6 commit e2a36b1

File tree

2 files changed

+108
-15
lines changed

2 files changed

+108
-15
lines changed

packages/cli/src/main.ts

Lines changed: 88 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import merge from 'lodash/merge';
66
// @ts-ignore Run transform(s) on path https://github.com/facebook/jscodeshift/issues/398
77
import * as jscodeshift from 'jscodeshift/src/Runner';
88
import { isValidConfig } from '@codeshift/validator';
9-
import { CodeshiftConfig } from '@codeshift/types';
9+
import {
10+
CodeshiftConfig, //
11+
DefaultRunner,
12+
} from '@codeshift/types';
1013

1114
import { Flags } from './types';
1215
import { InvalidUserInputError } from './errors';
@@ -218,20 +221,90 @@ Make sure the package name ${pkgName} has been spelled correctly and exists befo
218221
const resolvedTransformPath = path.resolve(transform);
219222
console.log(chalk.green('Running transform:'), resolvedTransformPath);
220223

221-
await jscodeshift.run(resolvedTransformPath, paths, {
222-
verbose: 0,
223-
dry: flags.dry,
224-
print: true,
225-
babel: true,
226-
extensions: flags.extensions,
227-
ignorePattern: flags.ignorePattern,
228-
cpus: flags.cpus,
229-
ignoreConfig: [],
230-
runInBand: flags.runInBand,
231-
silent: false,
232-
parser: flags.parser,
233-
stdin: false,
234-
});
224+
const defaultRunner: DefaultRunner = (
225+
jscodeshiftOptionOverrides = {},
226+
pathsToModify = paths,
227+
/**
228+
* ideally you'd be able to pass in either the path,
229+
* or the actual transform,
230+
* but jscodeshift doesn't allow this (unless we fork?)
231+
*/
232+
transformerPath: string = resolvedTransformPath,
233+
/**
234+
* i think the jscodeshift.run is synchronous
235+
* so the promise is not needed,
236+
* but if we want to change it in the future,
237+
* making it's return type a promise will help
238+
* to avoid breaking changes for consumers who
239+
* use the defaultRunner.
240+
*/
241+
): Promise<void> =>
242+
jscodeshift.run(transformerPath, pathsToModify, {
243+
verbose: 0,
244+
dry: flags.dry,
245+
print: true,
246+
babel: true,
247+
extensions: flags.extensions,
248+
ignorePattern: flags.ignorePattern,
249+
cpus: flags.cpus,
250+
ignoreConfig: [],
251+
runInBand: flags.runInBand,
252+
silent: false,
253+
parser: flags.parser,
254+
stdin: false,
255+
...jscodeshiftOptionOverrides,
256+
});
257+
258+
let transformImported: any;
259+
try {
260+
// eslint-disable-next-line @typescript-eslint/no-var-requires
261+
transformImported = require(resolvedTransformPath);
262+
} catch (_e) {}
263+
console.log({ transformImported });
264+
265+
const transformHasCustomRunner = (
266+
ti: any,
267+
): ti is {
268+
/**
269+
* ideally, `default` would be the type of the transformer,
270+
* which would be identical to the type of the argument to
271+
* `CustomTransformerConfig`,
272+
*
273+
* but unless we put the transformer itself into the config,
274+
* we cannot ensure that the type is correct.
275+
*
276+
*/
277+
default: unknown; //
278+
codeshiftConfig: CodeshiftConfig<unknown>;
279+
} => {
280+
if (ti && 'codeshiftConfig' in ti) {
281+
return 'runner' in transformImported['codeshiftConfig'];
282+
}
283+
return false;
284+
};
285+
286+
if (transformHasCustomRunner(transformImported)) {
287+
await transformImported.codeshiftConfig.runner(paths, {
288+
defaultRunner,
289+
/**
290+
* providing the `transform`, `resolvedTransformPath`, etc. here
291+
* is quite useless, because it's file-based,
292+
* so in whichever file the config is in,
293+
* that default export will be the transform,
294+
* and the file's path will be the resolved path.
295+
*
296+
* ...unless you have a custom runner defined in a separate file,
297+
* and want it to be able to access the transform,
298+
* esp. if that runner does not take in a path,
299+
* but rather the transform function.
300+
*/
301+
transformInsideFileThatSpecifiesCodeshiftConfig:
302+
transformImported.default,
303+
// resolvedTransformPath
304+
});
305+
} else {
306+
defaultRunner();
307+
}
235308
}
236309

237310
await packageManager.uninstallAll();

packages/types/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,23 @@ export interface CodeshiftConfig {
55
transforms?: Record<string, string>;
66
presets?: Record<string, string>;
77
}
8+
9+
export type DefaultRunner = (
10+
jscodeshiftOptionOverrides?: object,
11+
pathsToModify?: string[], //
12+
transformerPath?: string,
13+
) => Promise<void>;
14+
15+
export interface CustomRunnerCtx<Transform = unknown> {
16+
transformInsideFileThatSpecifiesCodeshiftConfig: Transform;
17+
defaultRunner: DefaultRunner;
18+
}
19+
20+
export type CustomRunner<Transform = unknown> = (
21+
pathsToModify: string[], //
22+
options: CustomRunnerCtx<Transform>,
23+
) => void | Promise<void>;
24+
25+
export interface CodeshiftConfig<Transform = unknown> {
26+
runner: CustomRunner<Transform>;
27+
}

0 commit comments

Comments
 (0)