codemods: add codemod runner with shell transform

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-06-01 22:00:27 +02:00
parent d131851373
commit 20a5c789a4
7 changed files with 451 additions and 39 deletions
+72
View File
@@ -0,0 +1,72 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { relative as relativePath } from 'path';
import { spawn } from 'child_process';
import { Command } from 'commander';
import { findPaths } from '@backstage/cli-common';
import { ExitCodeError } from './errors';
// eslint-disable-next-line no-restricted-syntax
const paths = findPaths(__dirname);
export function createCodemodAction(name: string) {
return async (_: unknown, cmd: Command) => {
const transformPath = relativePath(
process.cwd(),
paths.resolveOwn('transforms', `${name}.js`),
);
const args = [
'--parser=tsx',
'--extensions=tsx,js,ts,tsx',
'--transform',
transformPath,
'--ignore-pattern=**/node_modules/**',
...cmd.args,
];
console.log(`Running jscodeshift with these arguments: ${args.join(' ')}`);
const jscodeshiftScript = require.resolve('.bin/jscodeshift');
const child = spawn(process.argv0, [jscodeshiftScript, ...args], {
stdio: 'inherit',
shell: true,
env: {
...process.env,
FORCE_COLOR: 'true',
},
});
if (typeof child.exitCode === 'number') {
if (child.exitCode) {
throw new ExitCodeError(child.exitCode, name);
}
return;
}
await new Promise<void>((resolve, reject) => {
child.once('error', error => reject(error));
child.once('exit', code => {
if (code) {
reject(new ExitCodeError(code, name));
} else {
resolve();
}
});
});
};
}
+28
View File
@@ -0,0 +1,28 @@
/*
* Copyright 2021 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export interface Codemod {
name: string;
description: string;
}
export const codemods: Codemod[] = [
{
name: 'core-imports',
description:
'Updates @backstage/core imports to use @backstage/core-* imports instead.',
},
];
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import chalk from 'chalk';
export class CustomError extends Error {
get name(): string {
return this.constructor.name;
}
}
export class ExitCodeError extends CustomError {
readonly code: number;
constructor(code: number, command?: string) {
if (command) {
super(`Command '${command}' exited with code ${code}`);
} else {
super(`Child exited with code ${code}`);
}
this.code = code;
}
}
export function exitWithError(error: Error): never {
if (error instanceof ExitCodeError) {
process.stderr.write(`\n${chalk.red(error.message)}\n\n`);
process.exit(error.code);
} else {
process.stderr.write(`\n${chalk.red(`${error}`)}\n\n`);
process.exit(1);
}
}
+5 -26
View File
@@ -14,30 +14,19 @@
* limitations under the License.
*/
import program, { Command } from 'commander';
import program from 'commander';
import chalk from 'chalk';
import { codemods } from './codemods';
import { exitWithError } from './errors';
import { createCodemodAction } from './action';
import { version } from '../package.json';
const codemods = [
{
name: 'core-imports',
description:
'Updates @backstage/core imports to use @backstage/core-* imports instead.',
},
];
function createCodemodAction(name: string) {
return (cmd: Command) => {
console.log(`Fake codemod ${cmd} ${name}`);
};
}
async function main(argv: string[]) {
program.name('backstage-codemods').version(version);
for (const codemod of codemods) {
program
.command(codemod.name)
.command(`${codemod.name} <target-dir>`)
.description(codemod.description)
.option('-d, --dry', 'Dry run, no changes written to files')
.action(createCodemodAction(codemod.name));
@@ -54,16 +43,6 @@ async function main(argv: string[]) {
program.parse(argv);
}
function exitWithError(err: Error & { code?: unknown }) {
process.stdout.write(`${err.name}: ${err.stack || err.message}\n`);
if (typeof err.code === 'number') {
process.exit(err.code);
} else {
process.exit(1);
}
}
process.on('unhandledRejection', (rejection: unknown) => {
if (rejection instanceof Error) {
exitWithError(rejection);