Skip to content

Commit 18d2391

Browse files
committed
feat: improve script execution with better error handling and absolute paths
1 parent b3f9e39 commit 18d2391

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

bin/exec.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
const { access, constants } = require("fs");
2-
const { resolve } = require("path");
2+
const path = require("path");
33

4-
const exec = (path) => {
5-
access(resolve(__dirname, `${path}.js`), constants.F_OK, (error) => {
4+
const exec = (scriptPath) => {
5+
// Get the absolute path to the project root
6+
const projectRoot = path.resolve(__dirname, '..');
7+
8+
// Resolve the script path relative to the project root
9+
const absoluteScriptPath = path.resolve(projectRoot, scriptPath);
10+
const scriptJsPath = `${absoluteScriptPath}.js`;
11+
12+
// Check if the compiled JS file exists
13+
access(scriptJsPath, constants.F_OK, (error) => {
614
if (error) {
7-
console.log(`Unable to execute script ${path}`);
15+
console.log(`Unable to execute script ${scriptPath}`);
16+
console.log(`File not found: ${scriptJsPath}`);
817
console.log("Remember to compile project with `npm run build`");
918
process.exit(1);
1019
}
20+
21+
// Execute the script
1122
(async function () {
12-
await require(path).run();
23+
try {
24+
await require(absoluteScriptPath).run();
25+
} catch (err) {
26+
console.error(`Error executing ${scriptPath}:`, err);
27+
process.exit(1);
28+
}
1329
})();
1430
});
1531
};

0 commit comments

Comments
 (0)