-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.ts
76 lines (67 loc) · 1.96 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
71
72
73
74
75
76
import fs from "fs/promises";
import prettier from "./prettier.js";
import openai from "./openai/openai.js";
import humanify from "./humanify.js";
import yargs from "yargs/yargs";
import { ensureFileExists } from "./fs-utils.js";
import { env } from "./env.js";
import { nop } from "./plugin-utils.js";
import fixShadowing from "./fix-shadowing.js";
import { localReanme } from "./local-rename.js";
import { webcrack } from "./webcrack.js";
const argv = yargs(process.argv.slice(2))
.example(
"npm start -o example-formatted.js example.js",
"Format example.js and save to example-formatted.js"
)
.scriptName("npm start --")
.command("<file>", "File to format")
.options({
output: {
type: "string",
alias: "o",
description: "Output file",
require: true,
},
key: {
type: "string",
alias: "openai-key",
description: "OpenAI key (defaults to OPENAI_TOKEN environment variable)",
},
local: {
type: "boolean",
alias: "no-openai",
default: false,
description: "Don't use OpenAI API, only local plugins",
},
"4k": {
type: "boolean",
alias: "use-cheaper-model",
default: false,
description:
"Use the cheaper GPT-3.5 model with 4k context window (default is 16k)",
},
})
.demandCommand(1)
.help()
.parseSync();
const filename = argv._[0] as string;
await ensureFileExists(filename);
const bundledCode = await fs.readFile(filename, "utf-8");
const PLUGINS = [
humanify,
argv.local
? localReanme()
: openai({ apiKey: argv.key ?? env("OPENAI_TOKEN"), use4k: argv["4k"] }),
prettier,
];
const extractedFiles = await webcrack(bundledCode, argv.output);
for (const file of extractedFiles) {
const code = await fs.readFile(file.path, "utf-8");
const formattedCode = await PLUGINS.reduce(
(p, next) => p.then(next),
Promise.resolve(code)
);
await fs.writeFile(file.path, formattedCode);
}
process.exit(0); // Kills the zeromq socket