Files
Hornwitser 5dcaca4d0b Migrate to ECMAScript Modules
Migrate the usage of CommonJS specific code and imports to ESM.  Roughly
summarised this:
- Changes all require() calls to import statement or expressions.
- Changes usage of module.exports to export statements.
- Replaces require.resolve with import.meta.resolve.
- Replaces conditional imports with conditional subpath imports.
- Replaces module === require.main with import.meta.main.
- Adds the full filename with extention to all relative imports.
- Changes all usage of __dirname and __filename to their import.meta
  equivalents.
- Sets "type": "module" in package.json files.
- Removes usage of "use strict".
- Adds .ts an an alias of .js for the webpack build.

Due to differences in how require and import resolves files the plugin
entrypoints will have to include the filename and extension after this
migration in order to work.
2026-09-11 22:43:57 +02:00

70 lines
1.9 KiB
JavaScript

import child_process from "node:child_process";
import util from "node:util";
import assert from "node:assert/strict";
import path from "node:path";
import { escapeArg } from "../../packages/create/escape_arg.js";
const execFile = util.promisify(child_process.execFile);
async function exec(file, args) {
const command = [file, ...args.map(escapeArg)].join(" ");
const { stdout, stderr } = await execFile(
command,
{ shell: true, cwd: import.meta.dirname, env: { ...process.env, pct: "%"} }
);
return { stdout: JSON.parse(stdout), stderr };
}
const strings = [
'"a"',
"a sentence of text.",
" irregular \tspaces ",
'"C:\\path\\"',
"C:\\path\\",
'\\ "\\" \\\\ "\\\\" \\\\\\ "\\\\\\" \\\\\\\\ "\\\\\\\\" \\\\\\\\\\ "\\\\\\\\\\" \\\\\\\\\\\ "\\\\\\\\\\\\"',
'"C:\\path"',
"C:\\path",
"%OS%",
"!OS!",
"😮",
];
for (let i=1; i < 128; i++) {
const char = String.fromCodePoint(i);
// Skip newline and carriage return because those are difficult to escape
// Skip most alphanumeric characters as they don't behave differently to each other.
if (/[\n\r1-9B-Zb-z]/.test(char)) {
continue;
}
strings.push(char, `${char}${char}`, `${char}"`);
}
// Test 100 strings at a time.
const tests = [[]];
for (const string of strings) {
if (tests[tests.length - 1].length >= 100) {
tests.push([]);
}
tests[tests.length - 1].push(string);
}
describe("create/escape_arg", function() {
it("should correctly escape strings when args are forwarded", async function() {
const ext = process.platform === "win32" ? ".cmd" : ".sh";
for (const args of tests) {
assert.deepEqual(
await exec(`.${path.sep}forward_args${ext}`, ["echo_args.js", ...args]),
{ stdout: args, stderr: "" }
);
}
});
it("should correctly escape strings when invoking node directly", async function() {
for (const args of tests) {
assert.deepEqual(
await exec("node", ["echo_args.js", ...args]),
{ stdout: args, stderr: "" }
);
}
});
});