Skip to content

Commit eb4059c

Browse files
committed
Clean up
1 parent 306d33b commit eb4059c

File tree

9 files changed

+62
-138
lines changed

9 files changed

+62
-138
lines changed

.github/workflows/ci.yml

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,6 @@ jobs:
9191
cache: yarn
9292
node-version-file: .nvmrc
9393

94-
- run: corepack enable
95-
96-
- name: Show resolved npm via execPath
97-
run: |
98-
node -e 'import { dirname, join } from "node:path";
99-
import { execPath } from "node:process";
100-
import { spawnSync } from "node:child_process";
101-
const npmBin = join(dirname(execPath), "npm");
102-
console.log("Resolved npm:", npmBin);
103-
const res = spawnSync(npmBin, ["--version"], { stdio: "inherit" });
104-
process.exit(res.status);'
105-
106-
- name: Find and export npm path
107-
run: |
108-
which npm || echo "npm not found"
109-
npm --version || echo "npm version check failed"
110-
111-
- name: Show node path
112-
run: "which node"
113-
114-
- name: cat npm-cli
115-
run: cat /opt/hostedtoolcache/node/22.19.0/x64/lib/node_modules/npm/bin/npm-cli.js
116-
11794
- name: Install npm packages
11895
run: yarn install
11996

@@ -365,13 +342,6 @@ jobs:
365342
- name: Run tests
366343
run: node scripts/test.js -all
367344

368-
369-
- name: test outside of node tests
370-
run: node tests/rewatch_tests/src/foo.mjs
371-
372-
- name: Run new rewatch tests
373-
run: 'node --test "tests/rewatch_tests/tests/**/*.test.mjs"'
374-
375345
- name: Check for diffs in tests folder
376346
run: git diff --ignore-cr-at-eol --exit-code tests
377347

lib_dev/process.js

Lines changed: 8 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,8 @@
11
import * as child_process from "node:child_process";
22
import * as fs from "node:fs/promises";
3-
import os from "node:os";
43
import * as path from "node:path";
5-
import { execPath } from "node:process";
64
import { bsc_exe, rescript_legacy_exe } from "#cli/bins";
75

8-
function resolveBin(bin) {
9-
try {
10-
if (os.platform() === "win32") {
11-
// `where` returns all matches, take the first line
12-
return child_process
13-
.execSync(`where ${bin}`, { encoding: "utf8" })
14-
.split(/\r?\n/)[0]
15-
.trim();
16-
}
17-
// `command -v` is POSIX standard
18-
return child_process
19-
.execSync(`command -v ${bin}`, { encoding: "utf8" })
20-
.trim();
21-
} catch (err) {
22-
throw new Error(`Could not resolve binary: ${bin} ${err}`);
23-
}
24-
}
25-
26-
const gitBin = resolveBin("git");
27-
28-
// Helpers to resolve node and npm-cli when needed
29-
function getNodeBin() {
30-
try {
31-
return resolveBin("node");
32-
} catch (_err) {
33-
return execPath;
34-
}
35-
}
36-
37-
function getNpmCli(nodeBin) {
38-
return path.join(
39-
path.dirname(nodeBin),
40-
"../lib/node_modules/npm/bin/npm-cli.js",
41-
);
42-
}
43-
446
/**
457
* @typedef {{
468
* throwOnFail?: boolean,
@@ -88,26 +50,13 @@ export function setup(cwd = process.cwd()) {
8850
async function exec(command, args = [], options = {}) {
8951
const { throwOnFail = options.stdio === "inherit" } = options;
9052

91-
// Build env
92-
const env = options.env
93-
? { ...process.env, ...options.env }
94-
: { ...process.env };
95-
9653
const stdoutChunks = [];
9754
const stderrChunks = [];
98-
console.log("About to spawn", command, args, {
99-
cwd,
100-
// shell: process.platform === "win32",
101-
stdio: ["ignore", "pipe", "pipe"],
102-
env,
103-
...options,
104-
});
10555

10656
const subprocess = child_process.spawn(command, args, {
10757
cwd,
108-
// shell: process.platform === "win32",
58+
shell: process.platform === "win32",
10959
stdio: ["ignore", "pipe", "pipe"],
110-
env,
11160
...options,
11261
});
11362

@@ -121,9 +70,6 @@ export function setup(cwd = process.cwd()) {
12170

12271
return await new Promise((resolve, reject) => {
12372
subprocess.once("error", err => {
124-
console.error(`Command failed: ${command} ${args.join(" ")}`);
125-
console.error(`Working directory: ${options.cwd || cwd}`);
126-
console.error("Error:", err);
12773
reject(err);
12874
});
12975

@@ -138,12 +84,6 @@ export function setup(cwd = process.cwd()) {
13884
}
13985

14086
if (throwOnFail && code !== 0) {
141-
console.error(`Command failed: ${command} ${args.join(" ")}`);
142-
console.error(`Working directory: ${options.cwd || cwd}`);
143-
console.error(`Exit code: ${code}`);
144-
console.error(`Signal: ${signal}`);
145-
console.error("Stdout:", stdout);
146-
console.error("Stderr:", stderr);
14787
reject(
14888
new Error(
14989
`Command ${command} exited with non-zero status: ${code}`,
@@ -232,7 +172,10 @@ export function setup(cwd = process.cwd()) {
232172
* `rescript` CLI
233173
*/
234174
rewatch(command, args = [], options = {}) {
235-
const cliPath = path.join(import.meta.dirname, "../cli/rescript.js");
175+
const cliPath = path.join(
176+
import.meta.dirname,
177+
"../cli/rescript.js",
178+
);
236179
return exec("node", [cliPath, command, ...args].filter(Boolean), options);
237180
},
238181

@@ -291,7 +234,7 @@ export function setup(cwd = process.cwd()) {
291234
* @return {Promise<ExecResult>}
292235
*/
293236
git(args = [], options = {}) {
294-
return exec(gitBin, args, options);
237+
return exec("git", args, options);
295238
},
296239

297240
/**
@@ -301,47 +244,8 @@ export function setup(cwd = process.cwd()) {
301244
* @param {ExecOptions} [options]
302245
* @return {Promise<ExecResult>}
303246
*/
304-
async npm(args = [], options = {}) {
305-
console.log("the options", options);
306-
// Validate working directory to avoid confusing ENOENT from invalid cwd
307-
if (options.cwd) {
308-
try {
309-
await fs.stat(options.cwd);
310-
} catch (e) {
311-
console.error("Invalid cwd for npm", options.cwd, e);
312-
throw e;
313-
}
314-
}
315-
316-
// 1) Prefer corepack to resolve the correct npm
317-
try {
318-
return await exec("corepack", ["npm", ...args], options);
319-
} catch (_corepackErr) {
320-
// ignore and try next
321-
}
322-
323-
// 2) Try PATH npm
324-
try {
325-
return await exec("npm", args, options);
326-
} catch (_npmErr) {
327-
// ignore and try fallback
328-
}
329-
330-
// 3) Fallback: node + npm-cli resolved at call time
331-
const nodeBin = getNodeBin();
332-
const npmCli = getNpmCli(nodeBin);
333-
try {
334-
await fs.access(nodeBin);
335-
} catch (e) {
336-
console.error("nodeBin not accessible", nodeBin, e);
337-
}
338-
try {
339-
await fs.access(npmCli);
340-
} catch (e) {
341-
console.error("npmCli not accessible", npmCli, e);
342-
}
343-
console.log("Falling back to node+cli", { nodeBin, npmCli });
344-
return exec(nodeBin, [npmCli, ...args], options);
247+
npm(args = [], options = {}) {
248+
return exec("npm", args, options);
345249
},
346250
};
347251
}

scripts/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
docstringTestDir,
1010
ounitTestBin,
1111
projectDir,
12+
rewatchTestDir,
1213
} from "#dev/paths";
1314

1415
import {
@@ -19,13 +20,15 @@ import {
1920
node,
2021
rescript,
2122
shell,
23+
yarn,
2224
} from "#dev/process";
2325

2426
let ounitTest = false;
2527
let mochaTest = false;
2628
let bsbTest = false;
2729
let formatTest = false;
2830
let runtimeDocstrings = false;
31+
let rewatch = false;
2932

3033
if (process.argv.includes("-ounit")) {
3134
ounitTest = true;
@@ -47,12 +50,17 @@ if (process.argv.includes("-docstrings")) {
4750
runtimeDocstrings = true;
4851
}
4952

53+
if (process.argv.includes("-rewatch")) {
54+
rewatch = true;
55+
}
56+
5057
if (process.argv.includes("-all")) {
5158
ounitTest = true;
5259
mochaTest = true;
5360
bsbTest = true;
5461
formatTest = true;
5562
runtimeDocstrings = true;
63+
rewatch = true;
5664
}
5765

5866
if (formatTest) {
@@ -195,3 +203,20 @@ if (runtimeDocstrings) {
195203
});
196204
}
197205
}
206+
207+
if (rewatch) {
208+
await execClean([], {
209+
cwd: rewatchTestDir,
210+
stdio: "inherit",
211+
});
212+
213+
await execBuild([], {
214+
cwd: rewatchTestDir,
215+
stdio: "inherit",
216+
});
217+
218+
await yarn("test", [], {
219+
cwd: rewatchTestDir,
220+
stdio: "inherit",
221+
});
222+
}

tests/rewatch_tests/src/Node.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ let Path = {};
55

66
let Url = {};
77

8+
let FsPromises = {};
9+
810
let Test = {};
911

1012
export {
1113
Path,
1214
Url,
15+
FsPromises,
1316
Test,
1417
}
1518
/* No side effect */

tests/rewatch_tests/src/Node.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ module Url = {
1111
external fileURLToPath: string => string = "fileURLToPath"
1212
}
1313

14+
module FsPromises = {
15+
@module("node:fs/promises")
16+
external access: string => promise<unit> = "access"
17+
}
18+
1419
module Test = {
1520
@module("node:test")
1621
external describe: (string, unit => unit) => unit = "describe"

tests/rewatch_tests/src/Setup.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
import * as Nodeurl from "node:url";
44
import * as Nodepath from "node:path";
5+
import * as Stdlib_Promise from "@rescript/runtime/lib/es6/Stdlib_Promise.js";
6+
import * as Promises from "node:fs/promises";
57

68
let processUtils = await import(Nodepath.resolve(Nodepath.dirname(Nodeurl.fileURLToPath(import.meta.url)), "../../../lib_dev/process.js"));
79

8-
function commands(workingDirectory) {
10+
async function commands(workingDirectory) {
11+
let pwdExists = await Stdlib_Promise.$$catch(Promises.access(workingDirectory).then(() => true), param => Promise.resolve(false));
12+
if (!pwdExists) {
13+
throw {
14+
RE_EXN_ID: "Failure",
15+
_1: "Working directory does not exist: " + workingDirectory,
16+
Error: new Error()
17+
};
18+
}
919
let build = async () => {
1020
await processUtils.rewatch("build", [], {
1121
cwd: workingDirectory

tests/rewatch_tests/src/Setup.res

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ type commands = {
3232
}
3333

3434
/// Returns a set of helpers to invoke cli/rescript.js in a working directory
35-
let commands = (workingDirectory: string): commands => {
35+
let commands = async (workingDirectory: string): commands => {
36+
// Ensure working directory exists before invoking any process
37+
let pwdExists = await FsPromises.access(workingDirectory)->Promise.thenResolve(_ => true)->Promise.catch(_ => Promise.resolve(false))
38+
39+
if (!pwdExists) {
40+
throw(Failure("Working directory does not exist: " ++ workingDirectory))
41+
}
42+
3643
let rescript = {
3744
let build = async () => {
3845
let _ = await processUtils.rewatch("build", [], {cwd: workingDirectory})

tests/rewatch_tests/tests/SingleRepo.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as Nodetest from "node:test";
77

88
let repo = Nodepath.resolve(Nodepath.dirname(Nodeurl.fileURLToPath(import.meta.url)), "../repos/npm/single-project");
99

10-
let commands = Setup.commands(repo);
10+
let commands = await Setup.commands(repo);
1111

1212
Nodetest.describe("A single ReScript project using npm as package manager", () => {
1313
Nodetest.before(async () => await commands.npm.install());

tests/rewatch_tests/tests/SingleRepo.test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let repo = Path.resolve(
55
Path.dirname(Url.fileURLToPath(Node.importMetaUrl)),
66
"../repos/npm/single-project",
77
)
8-
let commands = Setup.commands(repo)
8+
let commands = await Setup.commands(repo)
99

1010
describe("A single ReScript project using npm as package manager", () => {
1111
before(async () => {

0 commit comments

Comments
 (0)