1 | import { type AkteApp } from "./AkteApp";
|
2 |
|
3 | import { commandsAndFlags } from "./lib/commandsAndFlags";
|
4 | import { createDebugger } from "./lib/createDebugger";
|
5 | import { hasHelp, hasVersion } from "./lib/hasFlag";
|
6 | import { pkg } from "./lib/pkg";
|
7 |
|
8 | const debugCLI = createDebugger("akte:cli");
|
9 |
|
10 | const exit = (code: number): void => {
|
11 | debugCLI("done");
|
12 |
|
13 | process.exit(code);
|
14 | };
|
15 |
|
16 | const displayHelp = (): void => {
|
17 | debugCLI.log(`
|
18 | Akte CLI
|
19 |
|
20 | DOCUMENTATION
|
21 | https://akte.js.org
|
22 |
|
23 | VERSION
|
24 | ${pkg.name}@${pkg.version}
|
25 |
|
26 | USAGE
|
27 | $ node akte.app.js <command>
|
28 | $ npx tsx akte.app.ts <command>
|
29 |
|
30 | COMMANDS
|
31 | build Build Akte to file system
|
32 |
|
33 | OPTIONS
|
34 | --silent, -s Silence output
|
35 |
|
36 | --help, -h Display CLI help
|
37 | --version, -v Display CLI version
|
38 | `);
|
39 |
|
40 | exit(0);
|
41 | };
|
42 |
|
43 | const displayVersion = (): void => {
|
44 | debugCLI.log(`${pkg.name}@${pkg.version}`);
|
45 |
|
46 | exit(0);
|
47 | };
|
48 |
|
49 | const build = async (app: AkteApp): Promise<void> => {
|
50 | debugCLI.log("\nAkte → Beginning build...\n");
|
51 |
|
52 | await app.buildAll();
|
53 |
|
54 | const buildTime = `${Math.ceil(performance.now())}ms`;
|
55 | debugCLI.log("\nAkte → Done in %o", buildTime);
|
56 |
|
57 | return exit(0);
|
58 | };
|
59 |
|
60 | export const runCLI = async (app: AkteApp): Promise<void> => {
|
61 | debugCLI("started");
|
62 |
|
63 | process.title = "Akte CLI";
|
64 |
|
65 |
|
66 | if (hasHelp()) {
|
67 | debugCLI("displaying help");
|
68 |
|
69 | return displayHelp();
|
70 | } else if (hasVersion()) {
|
71 | debugCLI("displaying version");
|
72 |
|
73 | return displayVersion();
|
74 | }
|
75 |
|
76 |
|
77 | const [command] = commandsAndFlags();
|
78 | switch (command) {
|
79 | case "build":
|
80 | debugCLI("running %o command", command);
|
81 |
|
82 | return build(app);
|
83 |
|
84 | default:
|
85 | debugCLI.log(
|
86 | `Akte → Unknown command \`${command}\`, use \`--help\` flag for manual`,
|
87 | );
|
88 |
|
89 | exit(2);
|
90 | }
|
91 | };
|