-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
70 lines (56 loc) · 1.68 KB
/
index.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
import fs from 'fs';
import { z } from 'zod';
import { version } from './package.json';
import child_process from 'child_process';
const CONFIG_FILE = 'action.toml';
const commandSchema = z.object({
cmd: z.string().min(1),
});
const cliCommands = ['list', 'version', 'help'];
let cmd;
try {
const commands = await import(`${process.cwd()}/${CONFIG_FILE}`);
const commandToRun = process.argv[2];
if (commandToRun && cliCommands.includes(commandToRun)) {
switch (commandToRun) {
case 'list':
console.log('Available commands:');
Object.keys(commands.default).forEach((command) => {
console.log(` ${command}`);
});
break;
case 'version':
console.log(`v${version}`);
break;
case 'help':
console.log('Usage: action <command>');
console.log('Available commands:');
cliCommands.forEach((command) => {
console.log(` ${command}`);
});
}
process.exit(0);
}
if (!fs.existsSync(CONFIG_FILE)) {
console.error(`failed to find config file: ${CONFIG_FILE}`);
process.exit(1);
}
if (!commandToRun) {
// Access the first command from the default export
// @ts-ignore
cmd = Object.values(commands.default)[0].cmd;
} else {
// Access the specified command from the default export
cmd = commands.default[commandToRun]?.cmd;
}
if (!cmd) {
throw new Error('Command not found');
}
commandSchema.parse({ cmd });
// Execute the command
child_process.execSync(cmd, { stdio: 'inherit' });
console.log('Program executed successfully');
} catch (error: any) {
console.error(`failed to execute program: ${error.message}`);
process.exit(1);
}