Skip to content

Commit c8a7ddf

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 1c72768 commit c8a7ddf

File tree

2 files changed

+127
-15
lines changed

2 files changed

+127
-15
lines changed

packages/cli/src/main.ts

+103-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ import * as jscodeshift from 'jscodeshift/src/Runner';
99

1010
import { fetchPackage, fetchRemotePackage } from '@codeshift/fetcher';
1111
import { isValidConfig } from '@codeshift/validator';
12-
import { CodeshiftConfig } from '@codeshift/types';
12+
import {
13+
CodeshiftConfig, //
14+
DefaultRunner,
15+
} from '@codeshift/types';
1316

1417
import { Flags } from './types';
1518
import { InvalidUserInputError } from './errors';
@@ -185,20 +188,105 @@ Make sure the package name "${pkgName}" is correct and try again.`,
185188
const resolvedTransformPath = path.resolve(transform);
186189
console.log(chalk.green('Running transform:'), resolvedTransformPath);
187190

188-
await jscodeshift.run(resolvedTransformPath, paths, {
189-
verbose: flags.verbose,
190-
dry: flags.dry,
191-
print: true,
192-
babel: true,
193-
extensions: flags.extensions,
194-
ignorePattern: flags.ignorePattern,
195-
cpus: flags.cpus,
196-
ignoreConfig: [],
197-
runInBand: flags.runInBand,
198-
silent: false,
199-
parser: flags.parser,
200-
stdin: false,
201-
});
191+
const defaultRunner: DefaultRunner = (
192+
jscodeshiftOptionOverrides = {},
193+
pathsToModify = paths,
194+
/**
195+
* ideally you'd be able to pass in either the path,
196+
* or the actual transform,
197+
* but jscodeshift doesn't allow this (unless we fork?)
198+
*/
199+
transformerPath: string = resolvedTransformPath,
200+
/**
201+
* i think the jscodeshift.run is synchronous
202+
* so the promise is not needed,
203+
* but if we want to change it in the future,
204+
* making it's return type a promise will help
205+
* to avoid breaking changes for consumers who
206+
* use the defaultRunner.
207+
*/
208+
): Promise<void> =>
209+
jscodeshift.run(transformerPath, pathsToModify, {
210+
verbose: flags.verbose,
211+
dry: flags.dry,
212+
print: true,
213+
babel: true,
214+
extensions: flags.extensions,
215+
ignorePattern: flags.ignorePattern,
216+
cpus: flags.cpus,
217+
ignoreConfig: [],
218+
runInBand: flags.runInBand,
219+
silent: false,
220+
parser: flags.parser,
221+
stdin: false,
222+
...jscodeshiftOptionOverrides,
223+
});
224+
225+
let transformImported: any;
226+
try {
227+
/**
228+
* TODO MAINTAINER -- i am not confident that this will work
229+
* if the transform was provided thru an npm package.
230+
*/
231+
232+
// eslint-disable-next-line @typescript-eslint/no-var-requires
233+
transformImported = require(resolvedTransformPath);
234+
} catch (_e) {}
235+
236+
const transformHasCustomRunner = (
237+
ti: any,
238+
): ti is {
239+
/**
240+
* ideally, `default` would be the type of the transformer,
241+
* which would be identical to the type of the argument to
242+
* `CustomTransformerConfig`,
243+
*
244+
* but unless we put the transformer itself into the config,
245+
* we cannot ensure that the type is correct.
246+
*
247+
*/
248+
default: unknown; //
249+
codeshiftConfig: CodeshiftConfig<unknown>;
250+
} => {
251+
if (ti && 'codeshiftConfig' in ti) {
252+
return 'runner' in transformImported['codeshiftConfig'];
253+
}
254+
return false;
255+
};
256+
257+
if (transformHasCustomRunner(transformImported)) {
258+
console.info(
259+
'\nusing CUSTOM runner for transform',
260+
resolvedTransformPath,
261+
);
262+
263+
await transformImported.codeshiftConfig.runner({
264+
pathsToModify: paths,
265+
defaultRunner,
266+
/**
267+
* providing the `transform`, `resolvedTransformPath`, etc. here
268+
* is quite useless, because it's file-based,
269+
* so in whichever file the config is in,
270+
* that default export will be the transform,
271+
* and the file's path will be the resolved path.
272+
*
273+
* ...unless you have a custom runner defined in a separate file,
274+
* and want it to be able to access the transform,
275+
* esp. if that runner does not take in a path,
276+
* but rather the transform function.
277+
*/
278+
transformInsideFileThatSpecifiesCodeshiftConfig:
279+
transformImported.default,
280+
// resolvedTransformPath
281+
});
282+
} else {
283+
console.info(
284+
'\nusing DEFAULT runner for transform',
285+
resolvedTransformPath,
286+
);
287+
288+
defaultRunner();
289+
}
202290
}
203291

204292
await packageManager.uninstallAll();

packages/types/src/index.ts

+24
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,27 @@ 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+
pathsToModify: string[]; //
17+
defaultRunner: DefaultRunner;
18+
transformInsideFileThatSpecifiesCodeshiftConfig: Transform;
19+
}
20+
21+
export type CustomRunner<
22+
Transform = unknown, //
23+
Return = unknown | Promise<unknown>
24+
> = (ctx: CustomRunnerCtx<Transform>) => Return;
25+
26+
export interface CodeshiftConfig<
27+
Transform = unknown, //
28+
RunnerReturn = unknown | Promise<unknown>
29+
> {
30+
runner: CustomRunner<Transform, RunnerReturn>;
31+
}

0 commit comments

Comments
 (0)