-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.ts
More file actions
128 lines (104 loc) · 3.38 KB
/
cli.ts
File metadata and controls
128 lines (104 loc) · 3.38 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { existsSync } from "node:fs";
import path from "node:path";
import { createJiti } from "jiti";
import type { StaticBuildConfig } from "./build/index.js";
function isStaticBuildConfig(value: unknown): value is StaticBuildConfig {
if (typeof value !== "object" || value === null) return false;
const obj = value as Record<string, unknown>;
return typeof obj.workspaceId === "string" && typeof obj.searchDir === "string";
}
function printUsage() {
console.log(`
Usage: slack-blockbook <script-path> [options]
slack-blockbook build <script-path> [options]
Commands:
(default) Start the development server
build Build static HTML output for hosting
Options:
--help Show this help message
--outDir <path> Output directory for static build (default: ./blockbook-static)
Examples:
slack-blockbook blockbook.ts
slack-blockbook build blockbook.ts
slack-blockbook build blockbook.ts --outDir ./dist
`);
}
function parseOutDir(args: string[]): string | undefined {
const idx = args.indexOf("--outDir");
if (idx !== -1 && idx + 1 < args.length) {
return args[idx + 1];
}
return undefined;
}
function resolveScriptPath(scriptPath: string): string {
const absolutePath = path.resolve(process.cwd(), scriptPath);
if (!existsSync(absolutePath)) {
console.error(`❌ Error: Script file not found: ${absolutePath}`);
process.exit(1);
}
return absolutePath;
}
function createJitiInstance() {
return createJiti(import.meta.url, {
interopDefault: true,
moduleCache: false,
jsx: { runtime: "automatic" },
});
}
async function runBuild(scriptPath: string, args: string[]) {
const absoluteScriptPath = resolveScriptPath(scriptPath);
const outDir = parseOutDir(args);
console.log(`📦 SlackBlockbook Static Build`);
console.log(`📝 Script: ${scriptPath}`);
console.log("");
const jiti = createJitiInstance();
const mod = (await jiti.import(absoluteScriptPath)) as Record<string, unknown>;
const config = mod.config ?? mod.default;
if (!isStaticBuildConfig(config)) {
console.error(
"❌ Error: Script must export a `config` object with `workspaceId` and `searchDir` for build mode.",
);
console.error(
" Example: export const config = { workspaceId: '...', searchDir: '...' };",
);
process.exit(1);
}
const { buildStaticBlockBook } = await import("./build/index.js");
await buildStaticBlockBook({
...config,
...(outDir ? { outputDir: outDir } : {}),
});
}
async function runDev(scriptPath: string) {
const absoluteScriptPath = resolveScriptPath(scriptPath);
console.log(`🚀 Starting SlackBlockbook Server...`);
console.log(`📝 Script: ${scriptPath}`);
console.log("");
const jiti = createJitiInstance();
await jiti.import(absoluteScriptPath);
}
async function main() {
const args = process.argv.slice(2);
if (args.length === 0 || args.includes("--help")) {
printUsage();
process.exit(args.includes("--help") ? 0 : 1);
}
const isBuild = args[0] === "build";
const scriptPath = isBuild ? args[1] : args[0];
if (!scriptPath) {
console.error("❌ Error: Script path is required");
printUsage();
process.exit(1);
}
try {
if (isBuild) {
await runBuild(scriptPath, args);
} else {
await runDev(scriptPath);
}
} catch (err) {
console.error("❌ Failed to execute script:", err);
process.exit(1);
}
}
main();