Skip to content

Commit

Permalink
[DOP-4033]: Add the ability to append to file using executeCliCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
branberry committed Sep 26, 2023
1 parent 43efb54 commit 6a39606
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 44 deletions.
34 changes: 26 additions & 8 deletions src/commands/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CliCommandResponse> {
export async function executeCliCommand({
command,
args = [],
options = {},
writeStream,
}: CliCommandParams): Promise<CliCommandResponse> {
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());
});
Expand Down Expand Up @@ -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<CliCommandResponse> {
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);

Expand All @@ -76,8 +94,8 @@ export async function getPatchId(repoDir: string): Promise<string> {
}

export async function getCommitHash(): Promise<string> {
// 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;
}
Expand Down
36 changes: 0 additions & 36 deletions src/commands/src/shared/base-job.ts

This file was deleted.

43 changes: 43 additions & 0 deletions src/commands/src/shared/standard-job-runner.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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');
}
}

0 comments on commit 6a39606

Please sign in to comment.