codemods,repo-tools: update to use new run utils from cli-common

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2025-11-29 18:11:45 +01:00
parent ab13a89744
commit 688f070676
4 changed files with 39 additions and 59 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/repo-tools': patch
'@backstage/codemods': patch
---
Updated to use new utilities from `@backstage/cli-common`.
+10 -29
View File
@@ -15,11 +15,9 @@
*/
import { relative as relativePath } from 'path';
import { spawn } from 'child_process';
import { OptionValues } from 'commander';
import { findPaths } from '@backstage/cli-common';
import { findPaths, run } from '@backstage/cli-common';
import { platform } from 'os';
import { ExitCodeError } from './errors';
// eslint-disable-next-line no-restricted-syntax
const paths = findPaths(__dirname);
@@ -51,42 +49,25 @@ export function createCodemodAction(name: string) {
console.log(`Running jscodeshift with these arguments: ${args.join(' ')}`);
let command;
let commandArgs: string[];
if (platform() === 'win32') {
command = 'jscodeshift';
commandArgs = ['jscodeshift', ...args];
} else {
// jscodeshift ships a slightly broken bin script with windows
// line endings so we need to execute it using node rather than
// letting the `#!/usr/bin/env node` take care of it
command = process.argv0;
args.unshift(require.resolve('.bin/jscodeshift'));
commandArgs = [
process.argv0,
require.resolve('.bin/jscodeshift'),
...args,
];
}
const child = spawn(command, args, {
stdio: 'inherit',
shell: true,
await run(commandArgs, {
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();
}
});
});
}).waitForExit();
};
}
+1 -13
View File
@@ -15,6 +15,7 @@
*/
import chalk from 'chalk';
import { ExitCodeError } from '@backstage/cli-common';
export class CustomError extends Error {
get name(): string {
@@ -22,19 +23,6 @@ export class CustomError extends Error {
}
}
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`);
@@ -15,7 +15,7 @@
*/
import fs from 'fs-extra';
import { spawnSync } from 'child_process';
import { run, ExitCodeError } from '@backstage/cli-common';
import { paths as cliPaths } from '../../../lib/paths';
/**
@@ -30,21 +30,26 @@ import { paths as cliPaths } from '../../../lib/paths';
*/
export async function generateTypeDeclarations(tsconfigFilePath: string) {
await fs.remove(cliPaths.resolveTargetRoot('dist-types'));
const { status } = spawnSync(
'yarn',
[
'tsc',
['--project', tsconfigFilePath],
['--skipLibCheck', 'false'],
['--incremental', 'false'],
].flat(),
{
stdio: 'inherit',
shell: true,
cwd: cliPaths.targetRoot,
},
);
if (status !== 0) {
process.exit(status || undefined);
try {
await run(
[
'yarn',
'tsc',
'--project',
tsconfigFilePath,
'--skipLibCheck',
'false',
'--incremental',
'false',
],
{
cwd: cliPaths.targetRoot,
},
).waitForExit();
} catch (error) {
if (error instanceof ExitCodeError) {
process.exit(error.code);
}
throw error;
}
}