Skip to content

Commit

Permalink
feat: enhance build commands with --test-* (#8)
Browse files Browse the repository at this point in the history
- Add the installDir to PATH automatically
- Automatically cleanup the installDir
  • Loading branch information
riywo authored Sep 12, 2024
1 parent 04f4014 commit e2a3874
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 39 deletions.
4 changes: 2 additions & 2 deletions src/cli/build/build-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const buildCommand = (program: Command) => {
const { sh, ps1 } = build(manifest);
if (sh) writeScript(outDir, sh);
if (ps1) writeScript(outDir, ps1);
if (testSh) testRun(outDir, { sh });
if (testPs1) testRun(outDir, { ps1 });
if (testSh) await testRun(outDir, { sh });
if (testPs1) await testRun(outDir, { ps1 });
});
};
83 changes: 46 additions & 37 deletions src/cli/build/test-run.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { join } from "node:path";
import { temporaryDirectory } from "tempy";
import { temporaryDirectoryTask } from "tempy";

export const testRun = (
export const testRun = async (
outDir: string,
{
sh,
Expand All @@ -12,38 +12,47 @@ export const testRun = (
sh?: { file: string; env: string };
ps1?: { file: string; env: string };
},
) => {
const tempDir = temporaryDirectory();
if (sh) {
const script = readFileSync(join(outDir, sh.file));
const installDir = join(tempDir, "sh");
execFileSync("sh", {
input: script,
env: {
...process.env,
[sh.env]: installDir,
},
stdio: ["pipe", "inherit", "inherit"],
});
execFileSync(process.env.SHELL ?? "sh", {
cwd: installDir,
stdio: ["inherit", "inherit", "inherit"],
});
}
if (ps1) {
const script = readFileSync(join(outDir, ps1.file));
const installDir = join(tempDir, "ps1");
execFileSync("pwsh", ["-c", "$Input | Out-String | iex"], {
input: script,
env: {
...process.env,
[ps1.env]: installDir,
},
stdio: ["pipe", "inherit", "inherit"],
});
execFileSync("pwsh", {
cwd: installDir,
stdio: ["inherit", "inherit", "inherit"],
});
}
};
) =>
temporaryDirectoryTask((tempDir) => {
if (sh) {
const script = readFileSync(join(outDir, sh.file));
const installDir = join(tempDir, "sh");
execFileSync("sh", {
input: script,
env: {
...process.env,
[sh.env]: installDir,
},
stdio: ["pipe", "inherit", "inherit"],
});
execFileSync(process.env.SHELL ?? "sh", {
cwd: installDir,
env: {
...process.env,
PATH: `${installDir}:${process.env.PATH}`,
},
stdio: ["inherit", "inherit", "inherit"],
});
}
if (ps1) {
const script = readFileSync(join(outDir, ps1.file));
const installDir = join(tempDir, "ps1");
execFileSync("pwsh", ["-c", "$Input | Out-String | iex"], {
input: script,
env: {
...process.env,
[ps1.env]: installDir,
},
stdio: ["pipe", "inherit", "inherit"],
});
execFileSync("pwsh", {
cwd: installDir,
env: {
...process.env,
PATH: `${installDir}:${process.env.PATH}`,
Path: `${installDir};${process.env.Path}`,
},
stdio: ["inherit", "inherit", "inherit"],
});
}
});

0 comments on commit e2a3874

Please sign in to comment.