-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
executable file
·57 lines (46 loc) · 1.48 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env node
import path from 'node:path';
import url from 'node:url';
process.env.CODEWHISPER_CLI = 'true';
const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let cliPath;
if (__dirname.includes(`${path.sep}dist`)) {
cliPath = path.resolve(__dirname, 'cli', 'index.js');
} else {
cliPath = path.resolve(__dirname, 'src', 'cli', 'index.ts');
}
function getTemplatesDir() {
const isCLI = process.env.CODEWHISPER_CLI === 'true';
if (isCLI) {
// We're running from CLI (global install or npx)
return path.resolve(__dirname);
}
if (__dirname.includes(`${path.sep}node_modules`)) {
// We're running in production mode (e.g., programmatic usage of installed package)
return path.resolve(__dirname, '..', 'dist');
}
if (__dirname.includes(`${path.sep}dist`)) {
// We're running in production mode (e.g., local build)
return __dirname;
}
// We're running in development mode
return path.resolve(__dirname, 'src', 'templates');
}
// Set the TEMPLATES_DIR environment variable
process.env.TEMPLATES_DIR = getTemplatesDir();
import(url.pathToFileURL(cliPath).href)
.then((cliModule) => {
if (cliModule.default) {
cliModule.default(process.argv.slice(2));
} else if (cliModule.cli) {
cliModule.cli(process.argv.slice(2));
} else {
console.error('CLI function not found in module');
process.exit(1);
}
})
.catch((err) => {
console.error(err);
process.exit(1);
});