Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow providing custom runner per transform file thru codeshiftConfig #97

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 103 additions & 17 deletions packages/cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chalk from 'chalk';
import findUp from 'find-up';
import inquirer from 'inquirer';

import { CodeshiftConfig } from '@codeshift/types';
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
Expand Down Expand Up @@ -231,22 +231,108 @@ export default async function main(paths: string[], flags: Flags) {
);

for (const transform of transforms) {
console.log(chalk.green('Running transform:'), transform);

await jscodeshift.run(transform, paths, {
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,
});
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();
Expand Down
24 changes: 24 additions & 0 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,27 @@ export interface CodeshiftConfig {
transforms?: Record<string, string>;
presets?: Record<string, string>;
}

export type DefaultRunner = (
jscodeshiftOptionOverrides?: object,
pathsToModify?: string[], //
transformerPath?: string,
) => Promise<void>;

export interface CustomRunnerCtx<Transform = unknown> {
pathsToModify: string[]; //
defaultRunner: DefaultRunner;
transformInsideFileThatSpecifiesCodeshiftConfig: Transform;
}

export type CustomRunner<
Transform = unknown, //
Return = unknown | Promise<unknown>,
> = (ctx: CustomRunnerCtx<Transform>) => Return;

export interface CodeshiftConfig<
Transform = unknown, //
RunnerReturn = unknown | Promise<unknown>,
> {
runner: CustomRunner<Transform, RunnerReturn>;
}