diff --git a/src/commands/src/helpers.ts b/src/commands/src/helpers.ts index 81aa28658..74dd2b06f 100644 --- a/src/commands/src/helpers.ts +++ b/src/commands/src/helpers.ts @@ -15,22 +15,32 @@ export class ExecuteCommandError extends Error { } } +interface CliCommandParams { + command: string; + args?: readonly string[]; + options?: SpawnOptions; + writeStream?: fs.WriteStream; +} + interface CliCommandResponse { stdout: string; stderr: string; } -export async function executeCliCommand( - command: string, - args: readonly string[] = [], - options: SpawnOptions = {} -): Promise { +export async function executeCliCommand({ + command, + args = [], + options = {}, + writeStream, +}: CliCommandParams): Promise { return new Promise((resolve, reject) => { const stdout: string[] = []; const stderr: string[] = []; const executedCommand = spawn(command, args, options); + if (writeStream) executedCommand.stdout?.pipe(writeStream); + executedCommand.stdout?.on('data', (data: Buffer) => { stdout.push(data.toString()); }); @@ -58,9 +68,17 @@ export async function executeCliCommand( }); } +export async function executeAndWriteToFile() { + return null; +} + export async function readFileAndExec(command: string, filePath: string, args?: string[]): Promise { const fileId = await openAsync(filePath, 'r'); - const response = await executeCliCommand(command, args, { stdio: [fileId, process.stdout, process.stderr] }); + const response = await executeCliCommand({ + command, + args, + options: { stdio: [fileId, process.stdout, process.stderr] }, + }); await closeAsync(fileId); @@ -76,8 +94,8 @@ export async function getPatchId(repoDir: string): Promise { } export async function getCommitHash(): Promise { - // git rev-parse --short HEAD - const response = await executeCliCommand('git', ['rev-parse', '--short', 'HEAD']); + // equivalent to git rev-parse --short HEAD + const response = await executeCliCommand({ command: 'git', args: ['rev-parse', '--short', 'HEAD'] }); return response.stdout; } diff --git a/src/commands/src/shared/base-job.ts b/src/commands/src/shared/base-job.ts deleted file mode 100644 index 49c787cb5..000000000 --- a/src/commands/src/shared/base-job.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { getRepoDir, checkIfPatched, getCommitHash, executeCliCommand, getPatchId, RSTSPEC_FLAG } from '../helpers'; - -export abstract class BaseJob { - repoName: string; - - constructor(repoName: string) { - this.repoName = repoName; - } - - async nextGenParse(repoName: string): Promise { - const repoDir = getRepoDir(repoName); - - const commandArgs = ['build', repoDir, '--output', `${repoDir}/bundle.zip`, RSTSPEC_FLAG]; - - const hasPatch = await checkIfPatched(repoDir); - - if (hasPatch) { - const [patchId, commitHash] = await Promise.all([getPatchId(repoDir), getCommitHash()]); - - commandArgs.push('--commit'); - commandArgs.push(commitHash); - - commandArgs.push('--patch'); - commandArgs.push(patchId); - } - - await executeCliCommand('snooty', commandArgs); - } - async nextGenHtml(repoName: string) { - getRepoDir(repoName); - } - - async nextGenDeploy() { - throw new Error('Not implemented'); - } -} diff --git a/src/commands/src/shared/standard-job-runner.ts b/src/commands/src/shared/standard-job-runner.ts new file mode 100644 index 000000000..e1c010c37 --- /dev/null +++ b/src/commands/src/shared/standard-job-runner.ts @@ -0,0 +1,43 @@ +import { getRepoDir, checkIfPatched, getCommitHash, executeCliCommand, getPatchId, RSTSPEC_FLAG } from '../helpers'; + +export class StandardJobRunner { + repoName: string; + repoDir: string; + project: string; + + constructor(repoName: string, project: string) { + this.repoName = repoName; + this.project = project; + + this.repoDir = getRepoDir(repoName); + } + + async nextGenParse(): Promise { + const commandArgs = ['build', this.repoDir, '--output', `${this.repoDir}/bundle.zip`, RSTSPEC_FLAG]; + + const hasPatch = await checkIfPatched(this.repoDir); + + if (hasPatch) { + const [patchId, commitHash] = await Promise.all([getPatchId(this.repoDir), getCommitHash()]); + + commandArgs.push('--commit'); + commandArgs.push(commitHash); + + commandArgs.push('--patch'); + commandArgs.push(patchId); + } + + await executeCliCommand({ command: 'snooty', args: commandArgs }); + } + + async nextGenHtml() { + // copy .env.production to the snooty directory + await executeCliCommand({ command: 'cp', args: [`${this.repoDir}/.env.production`, 'snooty'] }); + + await executeCliCommand({ command: 'echo', args: [`"GATSBY_SITE=${this.project}"`] }); + } + + async nextGenDeploy() { + throw new Error('Not implemented'); + } +}