Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add child_process stdout to error object #1050

Merged
merged 8 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/e2e/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { runCLI } from "@polywrap/test-env-js";
import { GetPathToCliTestFiles } from "@polywrap/test-cases";
import path from "path";
import fs from "fs";
import os from "os";
import rimraf from "rimraf";
import { compareSync } from "dir-compare";

Expand Down Expand Up @@ -167,9 +168,8 @@ describe("e2e tests for app command", () => {

it("Should store generated files to specified codegen dir", async () => {
const testCaseDir = getTestCaseDir(0);
const codegenDir = path.resolve(
process.env.TMPDIR || "/tmp",
`codegen-${Date.now()}`
const codegenDir = fs.mkdtempSync(
path.join(os.tmpdir(), `polywrap-cli-tests`)
);
const { exitCode: code, stdout: output, stderr: error } = await runCLI({
args: ["app", "codegen", "--codegen-dir", codegenDir],
Expand Down
9 changes: 4 additions & 5 deletions packages/cli/src/__tests__/e2e/build.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { clearStyle, polywrapCli } from "./utils";
import { runCLI } from "@polywrap/test-env-js";
import { GetPathToCliTestFiles } from "@polywrap/test-cases";
import fs from "fs";
import os from "os";
import path from "path";

const HELP = `Usage: polywrap build|b [options]
Expand Down Expand Up @@ -164,20 +165,18 @@ describe("e2e tests for build command", () => {
});

it("Should store build files in specified output dir", async () => {
const outputDir = path.resolve(
process.env.TMPDIR || "/tmp",
`codegen-${Date.now()}`
const outputDir = fs.mkdtempSync(
path.join(os.tmpdir(), `polywrap-cli-tests`)
);
const testCaseDir = getTestCaseDir(0);
const { exitCode: code, stdout: output, stderr: error } = await runCLI({
const { exitCode: code, stdout: output } = await runCLI({
args: ["build", "-v", "--output-dir", outputDir],
cwd: testCaseDir,
cli: polywrapCli,
});

const buildDir = `./${path.relative(testCaseDir, outputDir)}`;

expect(error).toBe("");
expect(code).toEqual(0);
expect(output).toContain(`Artifacts written to ${buildDir}`);
expect(output).toContain(`WRAP manifest written in ${buildDir}/wrap.info`);
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/e2e/codegen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { runCLI } from "@polywrap/test-env-js";
import { GetPathToCliTestFiles } from "@polywrap/test-cases";
import path from "path";
import fs from "fs";
import os from "os";
import rimraf from "rimraf";
import { compareSync } from "dir-compare";

Expand Down Expand Up @@ -172,9 +173,8 @@ describe("e2e tests for codegen command", () => {
});

it("Should store build files in specified codegen dir", async () => {
const codegenDir = path.resolve(
process.env.TMPDIR || "/tmp",
`codegen-${Date.now()}`
const codegenDir = fs.mkdtempSync(
path.join(os.tmpdir(), `polywrap-cli-tests`)
);
const testCaseDir = getTestCaseDir(0);
const { exitCode: code, stdout: output, stderr: error } = await runCLI({
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/__tests__/e2e/docgen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { runCLI } from "@polywrap/test-env-js";
import { GetPathToCliTestFiles } from "@polywrap/test-cases";
import path from "path";
import fs from "fs";
import os from "os";
import rimraf from "rimraf";
import { compareSync } from "dir-compare";

Expand Down Expand Up @@ -147,9 +148,8 @@ describe("e2e tests for docgen command", () => {
});

it("Should store build files in specified docgen dir", async () => {
const docgenDir = path.resolve(
process.env.TMPDIR || "/tmp",
`docgen-${Date.now()}`
const docgenDir = fs.mkdtempSync(
path.join(os.tmpdir(), `polywrap-cli-tests`)
);
const testCaseDir = getTestCaseDir(0);
const { exitCode: code, stdout: output, stderr: error } = await runCLI({
Expand Down
8 changes: 5 additions & 3 deletions packages/cli/src/lib/helpers/workflow-validator.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { runCommand } from "../system";
import { intlMsg } from "../intl";

import path from "path";
import fs from "fs";
import os from "os";
import { InvokeResult } from "@polywrap/core-js";

const TMPDIR = process.env.TMPDIR || "/tmp";
const TMPDIR = fs.mkdtempSync(path.join(os.tmpdir(), `polywrap-cli`));

export async function cueExists(): Promise<boolean> {
try {
const { stdout } = await runCommand("cue version");
const { stdout } = await runCommand("cue version", true);
return stdout.startsWith("cue version ");
} catch (e) {
return false;
Expand Down Expand Up @@ -43,7 +45,7 @@ export async function validateOutput(
console.log("Validation: SUCCEED");
}
} catch (e) {
const msgLines = e.split(/\r?\n/);
const msgLines = e.stderr.split(/\r?\n/);
msgLines[1] = `${validateScriptPath}:${msgLines[1]
.split(":")
.slice(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import path from "path";
export class NodeDependencyFetcher extends InfraDependencyFetcher {
public async installPackages(packages: InfraPackageArg[]): Promise<void> {
this.composePackageJson(packages);
await runCommand(`cd ${this.config.installationDirectory} && npm i`);
await runCommand(`cd ${this.config.installationDirectory} && npm i`, true);
}

public getPackageDir(packageName: string): string {
Expand Down Expand Up @@ -44,6 +44,6 @@ export class NodeDependencyFetcher extends InfraDependencyFetcher {
export class YarnDependencyFetcher extends NodeDependencyFetcher {
public async installPackages(packages: InfraPackageArg[]): Promise<void> {
this.composePackageJson(packages);
await runCommand(`cd ${this.config.installationDirectory} && yarn`);
await runCommand(`cd ${this.config.installationDirectory} && yarn`, true);
}
}
6 changes: 3 additions & 3 deletions packages/cli/src/lib/system/child-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { exec, ExecException } from "child_process";

export async function runCommand(
command: string,
quiet = true,
quiet = false,
env: Record<string, string> | undefined = undefined
): Promise<{ stdout: string; stderr: string }> {
if (!quiet) {
Expand All @@ -16,7 +16,7 @@ export async function runCommand(
stderr: string
) => {
if (err) {
reject(stderr);
reject({ stdout, stderr });
} else {
resolve({ stdout, stderr });
}
Expand All @@ -40,7 +40,7 @@ export async function runCommand(
});

childObj.stderr?.on("data", (data) => {
console.log(data.toString());
console.error(data.toString());
});
}
});
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/lib/system/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function getDockerFileLock(): FileLock {
}

export async function isDockerBuildxInstalled(): Promise<boolean> {
const { stdout: version } = await runCommand("docker buildx version");
const { stdout: version } = await runCommand("docker buildx version", true);
return version.startsWith("github.com/docker/buildx");
}

Expand Down Expand Up @@ -134,7 +134,10 @@ export async function createBuildImage(
// Build the docker image
let buildxUseFailed: boolean;
try {
const { stderr } = await runCommand(`docker buildx use ${imageName}`);
const { stderr } = await runCommand(
`docker buildx use ${imageName}`,
quiet
);
buildxUseFailed = !!stderr;
} catch (e) {
buildxUseFailed = true;
Expand Down