Skip to content

Commit d1ca62e

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 <[email protected]>
1 parent e28a6e6 commit d1ca62e

File tree

2 files changed

+100
-15
lines changed

2 files changed

+100
-15
lines changed

packages/cli/src/main.ts

Lines changed: 80 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,82 @@ 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+
): Promise<void> =>
234+
jscodeshift.run(transformerPath, pathsToModify, {
235+
verbose: 0,
236+
dry: flags.dry,
237+
print: true,
238+
babel: true,
239+
extensions: flags.extensions,
240+
ignorePattern: flags.ignorePattern,
241+
cpus: flags.cpus,
242+
ignoreConfig: [],
243+
runInBand: flags.runInBand,
244+
silent: false,
245+
parser: flags.parser,
246+
stdin: false,
247+
...jscodeshiftOptionOverrides,
248+
});
249+
250+
let transformImported: any;
251+
try {
252+
// eslint-disable-next-line @typescript-eslint/no-var-requires
253+
transformImported = require(resolvedTransformPath);
254+
} catch (_e) {}
255+
console.log({ transformImported });
256+
257+
const transformHasCustomRunner = (
258+
ti: any,
259+
): ti is {
260+
/**
261+
* ideally, `default` would be the type of the transformer,
262+
* which would be identical to the type of the argument to
263+
* `CustomTransformerConfig`,
264+
*
265+
* but unless we put the transformer itself into the config,
266+
* we cannot ensure that the type is correct.
267+
*
268+
*/
269+
default: unknown; //
270+
codeshiftConfig: CodeshiftConfig<unknown>;
271+
} => {
272+
if (ti && 'codeshiftConfig' in ti) {
273+
return 'runner' in transformImported['codeshiftConfig'];
274+
}
275+
return false;
276+
};
277+
278+
if (transformHasCustomRunner(transformImported)) {
279+
await transformImported.codeshiftConfig.runner(paths, {
280+
defaultRunner,
281+
/**
282+
* providing the `transform`, `resolvedTransformPath`, etc. here
283+
* is quite useless, because it's file-based,
284+
* so in whichever file the config is in,
285+
* that default export will be the transform,
286+
* and the file's path will be the resolved path.
287+
*
288+
* ...unless you have a custom runner defined in a separate file,
289+
* and want it to be able to access the transform,
290+
* esp. if that runner does not take in a path,
291+
* but rather the transform function.
292+
*/
293+
transformInsideFileThatSpecifiesCodeshiftConfig:
294+
transformImported.default,
295+
// resolvedTransformPath
296+
});
297+
} else {
298+
defaultRunner();
299+
}
235300
}
236301

237302
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)