Skip to content

Commit da6a96e

Browse files
committed
Use shared helper method.
1 parent 8d07492 commit da6a96e

1 file changed

Lines changed: 43 additions & 17 deletions

File tree

.github/workflows/src/api-md-consistency/api-md-consistency.js

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,48 @@
11
const fs = require("fs");
22
const path = require("path");
3-
const { spawnSync } = require("child_process");
3+
const { pathToFileURL } = require("url");
44

5-
function runNode(scriptRelativePath, workspace, core) {
6-
const result = spawnSync("node", [scriptRelativePath], {
7-
cwd: workspace,
8-
env: process.env,
9-
encoding: "utf-8",
10-
});
5+
const SHARED_SRC_ROOT = path.resolve(__dirname, "..", "..", "..", "shared", "src");
6+
const sharedModuleCache = new Map();
117

12-
if (result.stdout) {
13-
core.info(result.stdout.trimEnd());
8+
async function loadSharedModule(fileName) {
9+
if (sharedModuleCache.has(fileName)) {
10+
return sharedModuleCache.get(fileName);
1411
}
15-
if (result.stderr) {
16-
core.info(result.stderr.trimEnd());
17-
}
18-
if (result.status !== 0) {
19-
throw new Error(`Command failed (${result.status}): node ${scriptRelativePath}`);
12+
13+
const filePath = path.join(SHARED_SRC_ROOT, fileName);
14+
const modulePromise = import(pathToFileURL(filePath).href);
15+
sharedModuleCache.set(fileName, modulePromise);
16+
return modulePromise;
17+
}
18+
19+
async function runNode(scriptRelativePath, workspace, core) {
20+
const { execFile, isExecError } = await loadSharedModule("exec.js");
21+
22+
try {
23+
const result = await execFile("node", [scriptRelativePath], {
24+
cwd: workspace,
25+
logger: core,
26+
});
27+
28+
if (result.stdout) {
29+
core.info(result.stdout.trimEnd());
30+
}
31+
if (result.stderr) {
32+
core.info(result.stderr.trimEnd());
33+
}
34+
} catch (error) {
35+
if (isExecError(error)) {
36+
if (error.stdout) {
37+
core.info(error.stdout.trimEnd());
38+
}
39+
if (error.stderr) {
40+
core.info(error.stderr.trimEnd());
41+
}
42+
}
43+
44+
const status = isExecError(error) && Number.isInteger(error.code) ? error.code : 1;
45+
throw new Error(`Command failed (${status}): node ${scriptRelativePath}`);
2046
}
2147
}
2248

@@ -53,7 +79,7 @@ function formatIssueSection(title, apiFiles) {
5379
module.exports = async function apiMdConsistency({ core }) {
5480
const workspace = process.env.GITHUB_WORKSPACE || process.cwd();
5581

56-
runNode("scripts/api_md_workflow/find_affected.js", workspace, core);
82+
await runNode("scripts/api_md_workflow/find_affected.js", workspace, core);
5783

5884
const affected = readLines(process.env.API_MD_PACKAGES_FILE, workspace);
5985
const changedCount = affected.length;
@@ -71,8 +97,8 @@ module.exports = async function apiMdConsistency({ core }) {
7197
};
7298
}
7399

74-
runNode("scripts/api_md_workflow/regenerate.js", workspace, core);
75-
runNode("scripts/api_md_workflow/find_mismatches.js", workspace, core);
100+
await runNode("scripts/api_md_workflow/regenerate.js", workspace, core);
101+
await runNode("scripts/api_md_workflow/find_mismatches.js", workspace, core);
76102

77103
const mismatches = readLines(process.env.API_MD_MISMATCHES_FILE, workspace);
78104
const missing = readLines(process.env.API_MD_MISSING_FILE, workspace);

0 commit comments

Comments
 (0)