Skip to content

Commit bfbf31a

Browse files
committed
feat: allow providing custom runner per transform file thru CodeshiftConfig
depends on: - #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: - #67 Signed-off-by: Kipras Melnikovas <[email protected]>
1 parent a7a01b2 commit bfbf31a

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
@@ -8,7 +8,10 @@ import * as jscodeshift from 'jscodeshift/src/Runner';
88

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

1316
import { Flags } from './types';
1417
import { InvalidUserInputError } from './errors';
@@ -175,20 +178,105 @@ Make sure the package name "${pkgName}" is correct and try again.`,
175178
const resolvedTransformPath = path.resolve(transform);
176179
console.log(chalk.green('Running transform:'), resolvedTransformPath);
177180

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

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