Skip to content

Commit 8af373f

Browse files
committed
Try switching my-ts-node to js
1 parent 2191878 commit 8af373f

File tree

5 files changed

+73
-36
lines changed

5 files changed

+73
-36
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@ repos:
6363
name: format-recorded-tests
6464
files: ^packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/.*/[^/]*\.yml$
6565
language: system
66-
entry: pnpm exec ./packages/common/scripts/my-ts-node.sh packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts
66+
entry: pnpm exec ./packages/common/scripts/my-ts-node.js packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts
6767
- repo: local
6868
hooks:
6969
- id: check-recorded-test-marks
7070
name: check-recorded-test-marks
7171
files: ^packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/.*/[^/]*\.yml$
7272
language: system
73-
entry: pnpm exec ./packages/common/scripts/my-ts-node.sh packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts --check-marks
73+
entry: pnpm exec ./packages/common/scripts/my-ts-node.js packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts --check-marks
7474
- repo: https://github.com/ikamensh/flynt/
7575
rev: "0.78"
7676
hooks:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"preinstall": "npx only-allow pnpm",
1919
"test-compile": "tsc --build",
2020
"test": "pnpm compile && pnpm lint && pnpm -F '!test-harness' test && pnpm -F test-harness test",
21-
"transform-recorded-tests": "env CURSORLESS_REPO_ROOT=. ./packages/common/scripts/my-ts-node.sh packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts",
21+
"transform-recorded-tests": "env CURSORLESS_REPO_ROOT=. ./packages/common/scripts/my-ts-node.js packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts",
2222
"watch": "pnpm run -w --parallel '/^watch:.*/'",
2323
"watch:esbuild": "pnpm run -r --parallel --if-present watch:esbuild",
2424
"watch:tsc": "tsc --build --watch"

packages/common/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
}
3737
},
3838
"bin": {
39-
"my-ts-node": "./scripts/my-ts-node.sh"
39+
"my-ts-node": "./scripts/my-ts-node.js"
4040
},
4141
"type": "module"
4242
}

packages/common/scripts/my-ts-node.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-undef */
3+
import { spawn } from "child_process";
4+
import { existsSync, mkdirSync, rmdirSync } from "fs";
5+
import { join } from "path";
6+
7+
// Function to run a command with arguments and return a child process
8+
function runCommand(command, args, options) {
9+
return spawn(command, args, { stdio: "inherit", ...options });
10+
}
11+
12+
// Function to create a temporary directory and return its path
13+
function createTempDirectory(baseDir) {
14+
const tempDir = join(baseDir, "out/my-ts-node-tmp");
15+
if (!existsSync(tempDir)) {
16+
mkdirSync(tempDir, { recursive: true });
17+
}
18+
return tempDir;
19+
}
20+
21+
// Function to clean up the temporary directory
22+
function cleanupTempDirectory(tempDir) {
23+
rmdirSync(tempDir, { recursive: true });
24+
}
25+
26+
// Main function to execute the script
27+
function main() {
28+
const args = process.argv.slice(2);
29+
30+
// Check if the input file is specified
31+
if (args.length === 0) {
32+
console.error("Error: No input file specified.");
33+
console.error("Usage: node myTsNode.js <file.ts> [script args...]");
34+
process.exit(1);
35+
}
36+
37+
const fileToRun = args.shift();
38+
const tempDir = createTempDirectory(process.cwd());
39+
const outFile = join(tempDir, "out.cjs");
40+
41+
// Set up cleanup for when the script exits
42+
process.on("exit", () => cleanupTempDirectory(tempDir));
43+
process.on("SIGINT", () => cleanupTempDirectory(tempDir));
44+
process.on("SIGTERM", () => cleanupTempDirectory(tempDir));
45+
46+
// Run esbuild to bundle the TypeScript file
47+
const esbuildProcess = runCommand("esbuild", [
48+
"--sourcemap",
49+
"--log-level=warning",
50+
"--conditions=cursorless:bundler",
51+
"--bundle",
52+
"--format=cjs",
53+
"--platform=node",
54+
fileToRun,
55+
"--outfile=" + outFile,
56+
]);
57+
58+
esbuildProcess.on("close", (code) => {
59+
if (code === 0) {
60+
// Execute the bundled file with Node, passing any additional arguments
61+
runCommand(process.execPath, [outFile, ...args]);
62+
} else {
63+
console.error(`esbuild failed with code ${code}`);
64+
process.exit(code);
65+
}
66+
});
67+
}
68+
69+
main();

packages/common/scripts/my-ts-node.sh

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)