Skip to content

Commit d8d7c05

Browse files
committed
feat(cli): rewrite in JS for better compatibility (#244)
1 parent 2d646c3 commit d8d7c05

File tree

1 file changed

+49
-21
lines changed

1 file changed

+49
-21
lines changed

bin/cli

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,49 @@
1-
#!/usr/bin/env bash
2-
set -eou pipefail
3-
4-
if [ $# -eq 0 ]; then
5-
echo "Usage: $0 <subcommand>"
6-
echo
7-
echo "Subcommands:"
8-
echo " migrate Run migrations to update from openai v3 to v4"
9-
echo
10-
exit 1
11-
fi
12-
13-
if [ "$1" = "migrate" ]; then
14-
echo "This automatic code migration is provided by grit.io"
15-
echo "Visit https://app.grit.io/studio?preset=openai_v4 for more details."
16-
shift
17-
npx -y @getgrit/launcher apply openai_v4 "$@"
18-
else
19-
echo "Unknown subcommand $1; Expected 'migrate'" >&2
20-
exit 1
21-
fi
1+
#!/usr/bin/env node
2+
3+
const { spawnSync } = require('child_process');
4+
5+
const commands = {
6+
migrate: {
7+
description: 'Run migrations to update from openai v3 to v4',
8+
fn: () => {
9+
console.log('This automatic code migration is provided by grit.io');
10+
console.log('Visit https://app.grit.io/studio?preset=openai_v4 for more details.')
11+
12+
const result = spawnSync(
13+
'npx',
14+
['-y', '@getgrit/launcher', 'apply', 'openai_v4', ...process.argv.slice(3)],
15+
{ stdio: 'inherit' },
16+
);
17+
if (result.status !== 0) {
18+
process.exit(result.status);
19+
}
20+
}
21+
}
22+
}
23+
24+
function exitWithHelp() {
25+
console.log("Usage: $0 <subcommand>");
26+
console.log();
27+
console.log('Subcommands:');
28+
29+
for (const [name, info] of Object.entries(commands)) {
30+
console.log(` ${name} ${info.description}`);
31+
}
32+
33+
console.log();
34+
process.exit(1);
35+
}
36+
37+
if (process.argv.length < 3) {
38+
exitWithHelp();
39+
}
40+
41+
const commandName = process.argv[2];
42+
43+
const command = commands[commandName];
44+
if (!command) {
45+
console.log(`Unknown subcommand ${commandName}.`);
46+
exitWithHelp();
47+
}
48+
49+
command.fn();

0 commit comments

Comments
 (0)