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:
@@ -0,0 +1,6 @@
|
||||
---
|
||||
'@backstage/repo-tools': patch
|
||||
'@backstage/codemods': patch
|
||||
---
|
||||
|
||||
Updated to use new utilities from `@backstage/cli-common`.
|
||||
@@ -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();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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`);
|
||||
|
||||
+22
-17
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user