diff --git a/packages/cli/src/commands/build-cache/index.ts b/packages/cli/src/commands/build-cache/index.ts index 49a02f6939..b01b5309ab 100644 --- a/packages/cli/src/commands/build-cache/index.ts +++ b/packages/cli/src/commands/build-cache/index.ts @@ -16,13 +16,9 @@ import fs from 'fs-extra'; import { resolve as resolvePath, relative as relativePath } from 'path'; -import { promisify } from 'util'; -import { exec as execCb } from 'child_process'; import { Command } from 'commander'; -import { ExitCodeError } from '../../helpers/errors'; -import { run } from '../../helpers/run'; +import { run, runPlain } from '../../helpers/run'; import { readFileFromArchive, extractArchive, createArchive } from './archive'; -const exec = promisify(execCb); const INFO_FILE = '.backstage-build-cache'; const CACHE_ARCHIVE = 'cache.tgz'; @@ -35,7 +31,7 @@ type Options = { }; async function parseOptions(cmd: Command): Promise { - const repoRoot = await cmdOutput('git rev-parse --show-toplevel'); + const repoRoot = await runPlain('git rev-parse --show-toplevel'); const argTransformer = (arg: string) => resolvePath(arg.replace(//g, repoRoot).replace(/'/g, '')); @@ -73,7 +69,7 @@ export default async (cmd: Command, args: string[]) => { } else { print('cache miss, need to build'); - await build(args); + await run(args[0], args.slice(1)); if (cache.writable) { print('caching build output'); @@ -84,22 +80,6 @@ export default async (cmd: Command, args: string[]) => { } }; -async function build([prog, ...args]: string[]): Promise { - await run(prog, args); -} - -async function cmdOutput(cmd: string) { - try { - const { stdout } = await exec(cmd); - return stdout.trim(); - } catch (error) { - if (error.stderr) { - process.stderr.write(error.stderr); - } - throw new ExitCodeError(error.code, cmd); - } -} - type Cache = { // External location of the cache outside the output folder archivePath: string; @@ -160,7 +140,7 @@ async function readCache(options: Options): Promise { async function getInputHashes(options: Options): Promise { const trees = []; for (const input of options.inputs) { - const output = await cmdOutput(`git ls-tree HEAD '${input}'`); + const output = await runPlain(`git ls-tree HEAD '${input}'`); const [, , sha] = output.split(/\s+/, 3); trees.push(sha); } diff --git a/packages/cli/src/helpers/run.ts b/packages/cli/src/helpers/run.ts index 3eec19a148..6a1376baf8 100644 --- a/packages/cli/src/helpers/run.ts +++ b/packages/cli/src/helpers/run.ts @@ -14,8 +14,15 @@ * limitations under the License. */ -import { SpawnOptions, spawn, ChildProcess } from 'child_process'; +import { + SpawnOptions, + spawn, + ChildProcess, + exec as execCb, +} from 'child_process'; import { ExitCodeError } from './errors'; +import { promisify } from 'util'; +const exec = promisify(execCb); type SpawnOptionsPartialEnv = Omit & { env?: Partial; @@ -43,6 +50,18 @@ export async function run( await waitForExit(child, name); } +export async function runPlain(cmd: string) { + try { + const { stdout } = await exec(cmd); + return stdout.trim(); + } catch (error) { + if (error.stderr) { + process.stderr.write(error.stderr); + } + throw new ExitCodeError(error.code, cmd); + } +} + export async function waitForExit( child: ChildProcess & { exitCode?: number }, name?: string,