Skip to content

Commit

Permalink
Try switching my-ts-node to js
Browse files Browse the repository at this point in the history
  • Loading branch information
pokey committed Nov 10, 2023
1 parent 2191878 commit 8af373f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 36 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ repos:
name: format-recorded-tests
files: ^packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/.*/[^/]*\.yml$
language: system
entry: pnpm exec ./packages/common/scripts/my-ts-node.sh packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts
entry: pnpm exec ./packages/common/scripts/my-ts-node.js packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts
- repo: local
hooks:
- id: check-recorded-test-marks
name: check-recorded-test-marks
files: ^packages/cursorless-vscode-e2e/src/suite/fixtures/recorded/.*/[^/]*\.yml$
language: system
entry: pnpm exec ./packages/common/scripts/my-ts-node.sh packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts --check-marks
entry: pnpm exec ./packages/common/scripts/my-ts-node.js packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts --check-marks
- repo: https://github.com/ikamensh/flynt/
rev: "0.78"
hooks:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"preinstall": "npx only-allow pnpm",
"test-compile": "tsc --build",
"test": "pnpm compile && pnpm lint && pnpm -F '!test-harness' test && pnpm -F test-harness test",
"transform-recorded-tests": "env CURSORLESS_REPO_ROOT=. ./packages/common/scripts/my-ts-node.sh packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts",
"transform-recorded-tests": "env CURSORLESS_REPO_ROOT=. ./packages/common/scripts/my-ts-node.js packages/cursorless-engine/src/scripts/transformRecordedTests/index.ts",
"watch": "pnpm run -w --parallel '/^watch:.*/'",
"watch:esbuild": "pnpm run -r --parallel --if-present watch:esbuild",
"watch:tsc": "tsc --build --watch"
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
},
"bin": {
"my-ts-node": "./scripts/my-ts-node.sh"
"my-ts-node": "./scripts/my-ts-node.js"
},
"type": "module"
}
69 changes: 69 additions & 0 deletions packages/common/scripts/my-ts-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env node
/* eslint-disable no-undef */
import { spawn } from "child_process";
import { existsSync, mkdirSync, rmdirSync } from "fs";
import { join } from "path";

// Function to run a command with arguments and return a child process
function runCommand(command, args, options) {
return spawn(command, args, { stdio: "inherit", ...options });
}

// Function to create a temporary directory and return its path
function createTempDirectory(baseDir) {
const tempDir = join(baseDir, "out/my-ts-node-tmp");
if (!existsSync(tempDir)) {
mkdirSync(tempDir, { recursive: true });
}
return tempDir;
}

// Function to clean up the temporary directory
function cleanupTempDirectory(tempDir) {
rmdirSync(tempDir, { recursive: true });
}

// Main function to execute the script
function main() {
const args = process.argv.slice(2);

// Check if the input file is specified
if (args.length === 0) {
console.error("Error: No input file specified.");
console.error("Usage: node myTsNode.js <file.ts> [script args...]");
process.exit(1);
}

const fileToRun = args.shift();
const tempDir = createTempDirectory(process.cwd());
const outFile = join(tempDir, "out.cjs");

// Set up cleanup for when the script exits
process.on("exit", () => cleanupTempDirectory(tempDir));
process.on("SIGINT", () => cleanupTempDirectory(tempDir));
process.on("SIGTERM", () => cleanupTempDirectory(tempDir));

// Run esbuild to bundle the TypeScript file
const esbuildProcess = runCommand("esbuild", [
"--sourcemap",
"--log-level=warning",
"--conditions=cursorless:bundler",
"--bundle",
"--format=cjs",
"--platform=node",
fileToRun,
"--outfile=" + outFile,
]);

esbuildProcess.on("close", (code) => {
if (code === 0) {
// Execute the bundled file with Node, passing any additional arguments
runCommand(process.execPath, [outFile, ...args]);
} else {
console.error(`esbuild failed with code ${code}`);
process.exit(code);
}
});
}

main();
32 changes: 0 additions & 32 deletions packages/common/scripts/my-ts-node.sh

This file was deleted.

0 comments on commit 8af373f

Please sign in to comment.