Skip to content

Commit a86cb8e

Browse files
committed
feat: remove runCommand func
1 parent 50ee0c6 commit a86cb8e

File tree

4 files changed

+43
-91
lines changed

4 files changed

+43
-91
lines changed

bin/cli.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ if (!command || command === "bench") {
116116
await $`cat /etc/os-release`;
117117
await $`uname -r`;
118118
await $`uptime`;
119-
119+
120120
await $`echo "===== Resource Usage ====="`;
121121
await $`top -b -n1 | head -n10`;
122122
await $`vmstat`;
123123
await $`df -h`;
124124
await $`free -h`;
125-
125+
126126
await $`echo "===== Hardware Information ====="`;
127127
await $`lscpu`;
128128
await $`lsblk`;

bin/upload.js

+13-31
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { readFile, writeFile, readdir, mkdir, copyFile } from "fs/promises";
22
import { existsSync } from "fs";
33
import { resolve, join } from "path";
44
import { fileURLToPath } from "url";
5-
import { runCommand, dirExist, formatDate } from "../lib/utils.js";
5+
import { $, cd } from "zx";
6+
import { dirExist, formatDate } from "../lib/utils.js";
67

78
const [, , token] = process.argv;
89
const GITHUB_ACTOR = process.env.GITHUB_ACTOR;
@@ -16,12 +17,7 @@ const rspackDir = process.env.RSPACK_DIR || resolve(rootDir, ".rspack");
1617
const dateDir = resolve(dataDir, date);
1718

1819
async function getCommitSHA() {
19-
let commitSHA;
20-
await runCommand("git", ["rev-parse", "HEAD"], {
21-
onData(stdout) {
22-
commitSHA = stdout.toString().trim();
23-
}
24-
});
20+
const commitSHA = (await $`git rev-parse HEAD`).toString().trim();
2521
console.log("Current Commit SHA", commitSHA);
2622
return commitSHA;
2723
}
@@ -40,22 +36,13 @@ async function appendRspackBuildInfo() {
4036

4137
(async () => {
4238
if (!(await dirExist(dataDir))) {
43-
await runCommand("git", [
44-
"clone",
45-
"--branch",
46-
"data",
47-
"--single-branch",
48-
"--depth",
49-
"1",
50-
repoUrl,
51-
".data"
52-
]);
39+
await $`git clone --branch data --single-branch --depth 1 ${repoUrl} .data`;
5340
}
5441

55-
process.chdir(dataDir);
56-
await runCommand("git", ["remote", "set-url", "origin", repoUrl]);
57-
await runCommand("git", ["reset", "--hard", "origin/data"]);
58-
await runCommand("git", ["pull", "--rebase"]);
42+
cd(dataDir);
43+
await $`git remote set-url origin ${repoUrl}`;
44+
await $`git reset --hard origin/data`;
45+
await $`git pull --rebase`;
5946

6047
console.log("== copy output files ==");
6148
const indexFile = resolve(dataDir, "index.txt");
@@ -77,23 +64,18 @@ async function appendRspackBuildInfo() {
7764
await writeFile(indexFile, Array.from(files, f => `${f}\n`).join("") + "\n");
7865

7966
console.log("== update build-info.json ==");
80-
process.chdir(rspackDir);
67+
cd(rspackDir);
8168
await appendRspackBuildInfo();
82-
process.chdir(dataDir);
69+
cd(dataDir);
8370

8471
console.log("== commit ==");
85-
await runCommand("git", [
86-
"add",
87-
`${date}/*.json`,
88-
"index.txt",
89-
"build-info.json"
90-
]);
72+
await $`git add ${date}/*.json index.txt build-info.json`;
9173
try {
92-
await runCommand("git", ["commit", "-m", `"add ${date} results"`]);
74+
await $`git commit -m "add ${date} results"`;
9375
} catch {}
9476

9577
console.log("== push ==");
96-
await runCommand("git", ["push"]);
78+
await $`git push`;
9779
})().catch(err => {
9880
process.exitCode = 1;
9981
console.error(err.stack);

lib/scenarios/index.js

+28-31
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import path from "path";
22
import { readFile, unlink, writeFile } from "fs/promises";
33
import { fileURLToPath } from "url";
44
import actionsCore from "@actions/core";
5-
import { isGitHubActions, runCommand } from "../utils.js";
5+
import { $, cd } from "zx";
6+
import { isGitHubActions } from "../utils.js";
67
import {
78
getDirSizes,
89
calcStatistics,
@@ -41,27 +42,29 @@ async function runRspack(ctx) {
4142
data[name] = (data[name] || 0) + +valueStr;
4243
}
4344
};
44-
let remainingLine = "";
45-
await runCommand(
45+
const p = $.spawn(
4646
path.join(rootDir, "node_modules/@rspack/cli/bin/rspack"),
4747
ctx.rspackArgs,
4848
{
49-
verbose: false,
50-
onData: function (chunk) {
51-
const lines = (remainingLine + chunk).split("\n");
52-
remainingLine = lines.pop();
53-
lines.forEach(processLine);
54-
}
49+
shell: true,
50+
stdio: ["ignore", "pipe", "ignore"]
5551
}
5652
);
57-
53+
let remainingLine = "";
54+
p.stdout.on("data", chunk => {
55+
const lines = (remainingLine + chunk).split("\n");
56+
remainingLine = lines.pop();
57+
lines.forEach(processLine);
58+
});
59+
const exitCode = await new Promise(resolve => p.once("exit", resolve));
5860
data.exec = Date.now() - start;
5961
await promise;
6062
if (dataSetCounter > 1) {
6163
for (const key of Object.keys(data)) {
6264
data[key] /= dataSetCounter;
6365
}
6466
}
67+
if (exitCode !== 0) throw new Error(`Build failed with ${exitCode}`);
6568
data["dist size"] = await getDirSizes("dist");
6669
return data;
6770
}
@@ -70,7 +73,7 @@ export function getScenario(caseName) {
7073
return {
7174
async setup() {
7275
const caseDir = path.resolve(rootDir, "cases", caseName);
73-
process.chdir(caseDir);
76+
cd(caseDir);
7477
const configFilePath = path.resolve(caseDir, "rspack.config.js");
7578
const config = await readFile(configFilePath);
7679
const hmrConfig = await getHmrConfig(path.resolve(caseDir, "hmr.js"));
@@ -105,25 +108,19 @@ module.exports.plugins.push(new (require("../../lib/scenarios/build-plugin.cjs")
105108
const rspackDir =
106109
process.env.RSPACK_DIR || path.resolve(rootDir, ".rspack");
107110
console.log("Create Rspack package link");
108-
await runCommand("mkdir", [
109-
"-p",
110-
path.resolve(rootDir, "node_modules/@rspack")
111-
]);
112-
await runCommand("ln", [
113-
"-nsf",
114-
path.resolve(rspackDir, "packages/rspack"),
115-
path.resolve(rootDir, "node_modules/@rspack/core")
116-
]);
117-
await runCommand("ln", [
118-
"-nsf",
119-
path.resolve(rspackDir, "packages/rspack-cli"),
120-
path.resolve(rootDir, "node_modules/@rspack/cli")
121-
]);
122-
await runCommand("ln", [
123-
"-nsf",
124-
path.resolve(rspackDir, "packages/rspack-plugin-react-refresh"),
125-
path.resolve(rootDir, "node_modules/@rspack/plugin-react-refresh")
126-
]);
111+
await $`mkdir -p ${path.resolve(rootDir, "node_modules/@rspack")}`;
112+
await $`ln -nsf ${path.resolve(
113+
rspackDir,
114+
"packages/rspack"
115+
)} ${path.resolve(rootDir, "node_modules/@rspack/core")}`;
116+
await $`ln -nsf ${path.resolve(
117+
rspackDir,
118+
"packages/rspack-cli"
119+
)} ${path.resolve(rootDir, "node_modules/@rspack/cli")}`;
120+
await $`ln -nsf ${path.resolve(
121+
rspackDir,
122+
"packages/rspack-plugin-react-refresh"
123+
)} ${path.resolve(rootDir, "node_modules/@rspack/plugin-react-refresh")}`;
127124
},
128125
async warmup(ctx) {
129126
console.log("Run Rspack with args:", ctx.rspackArgs);
@@ -155,7 +152,7 @@ module.exports.plugins.push(new (require("../../lib/scenarios/build-plugin.cjs")
155152
return writeFile(path, content);
156153
})
157154
);
158-
process.chdir(rootDir);
155+
cd(rootDir);
159156
}
160157
};
161158
}

lib/utils.js

-27
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { spawn } from "child_process";
21
import { stat } from "fs/promises";
32

43
export async function useAddons(addons, stage, ...args) {
@@ -7,32 +6,6 @@ export async function useAddons(addons, stage, ...args) {
76
}
87
}
98

10-
export async function runCommand(
11-
command,
12-
args,
13-
{ verbose = true, env, onData } = {}
14-
) {
15-
const hasOnData = typeof onData === "function";
16-
const stdio = verbose ? "inherit" : "ignore";
17-
const p = spawn(command, args, {
18-
shell: true,
19-
stdio: [stdio, hasOnData ? "pipe" : stdio, "inherit"],
20-
env: env
21-
? {
22-
...process.env,
23-
...env
24-
}
25-
: undefined
26-
});
27-
if (hasOnData) {
28-
p.stdout.on("data", onData);
29-
}
30-
31-
const exitCode = await new Promise(resolve => p.once("exit", resolve));
32-
if (exitCode !== 0)
33-
throw new Error(`${command} ${args.join(" ")} failed with ${exitCode}`);
34-
}
35-
369
export function getType(metric) {
3710
if (metric.endsWith(" memory")) return "memory";
3811
if (metric.endsWith(" size")) return "size";

0 commit comments

Comments
 (0)