forked from TabularisDB/tabularis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscaffold.ts
More file actions
142 lines (124 loc) · 4.49 KB
/
Copy pathscaffold.ts
File metadata and controls
142 lines (124 loc) · 4.49 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { execFileSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
import { dirname, join, relative, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { substitute } from "./substitute";
import type { DbType, IdentifierQuote } from "./validate";
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Resolve the template root. In dev (tsup watching) this lives next to `src/`;
* in a published package it lives next to `dist/`. Both resolve correctly via
* `../templates` from the compiled output.
*/
function templateRoot(): string {
return resolve(__dirname, "../templates");
}
export interface ScaffoldOptions {
slug: string;
displayName: string;
dbType: DbType;
quote: IdentifierQuote;
withUi: boolean;
targetDir: string;
gitInit: boolean;
pluginApiVersion: string;
minTabularisVersion: string;
}
/** Derive all template variables from the high-level scaffold options. */
function buildVars(opts: ScaffoldOptions): Record<string, string> {
const fileBased = opts.dbType === "file";
const folderBased = opts.dbType === "folder";
const apiBased = opts.dbType === "api";
const defaultPort = opts.dbType === "network" ? "5432" : "null";
// The .rs template uses this constant as a literal Rust expression
// (`Some(5432)` or `None`) — translate here.
const defaultPortRust = opts.dbType === "network" ? "Some(5432)" : "None";
// The manifest expects an actual JSON quote character, not the escaped string.
// For " → "\"" (already escaped inside JSON string). For ` → "`".
const quoteJson = opts.quote === "\"" ? "\\\"" : "`";
return {
NAME: opts.slug,
DISPLAY_NAME: opts.displayName,
ID: opts.slug,
BIN_NAME: `${opts.slug}-plugin`,
DB_TYPE: opts.dbType,
QUOTE: opts.quote,
QUOTE_JSON: quoteJson,
FILE_BASED: String(fileBased),
FOLDER_BASED: String(folderBased),
API_BASED: String(apiBased),
NO_CONNECTION_REQUIRED: String(apiBased),
DEFAULT_PORT: defaultPort,
DEFAULT_PORT_RUST: defaultPortRust,
YEAR: String(new Date().getUTCFullYear()),
PLUGIN_API_VERSION: opts.pluginApiVersion,
MIN_TABULARIS_VERSION: opts.minTabularisVersion,
UI_EXTENSIONS_ENTRY: opts.withUi
? ` {\n "slot": "data-grid.toolbar.actions",\n "module": "ui/dist/index.js"\n }\n `
: "",
};
}
function walk(dir: string): string[] {
const out: string[] = [];
const entries = readdirSync(dir);
for (const entry of entries) {
const full = join(dir, entry);
if (statSync(full).isDirectory()) {
out.push(...walk(full));
} else {
out.push(full);
}
}
return out;
}
/** Copy a template subdirectory with substitution and .tmpl-stripping. */
function copyTemplate(
templateSubdir: string,
targetDir: string,
vars: Record<string, string>,
): void {
const sourceRoot = join(templateRoot(), templateSubdir);
if (!existsSync(sourceRoot)) {
throw new Error(`Template not found at ${sourceRoot}`);
}
for (const sourcePath of walk(sourceRoot)) {
const rel = relative(sourceRoot, sourcePath);
const isTemplate = rel.endsWith(".tmpl");
const outRel = isTemplate ? rel.slice(0, -".tmpl".length) : rel;
const outPath = join(targetDir, outRel);
mkdirSync(dirname(outPath), { recursive: true });
const raw = readFileSync(sourcePath, "utf8");
const out = isTemplate ? substitute(raw, vars) : raw;
writeFileSync(outPath, out, "utf8");
}
}
export function scaffold(opts: ScaffoldOptions): void {
if (existsSync(opts.targetDir)) {
const contents = readdirSync(opts.targetDir);
if (contents.length > 0) {
throw new Error(
`Target directory ${opts.targetDir} already exists and is not empty. ` +
`Refusing to overwrite. Pick a new --dir or remove the existing one.`,
);
}
}
mkdirSync(opts.targetDir, { recursive: true });
const vars = buildVars(opts);
copyTemplate("rust-driver", opts.targetDir, vars);
if (opts.withUi) {
copyTemplate("ui-extension", join(opts.targetDir, "ui"), vars);
// i18n strings live at the plugin root (`<root>/locales/<lang>.json`), not
// under `ui/` — that's where the host loads them from.
copyTemplate("ui-extension-locales", opts.targetDir, vars);
}
if (opts.gitInit) {
try {
execFileSync("git", ["init", "--quiet"], {
cwd: opts.targetDir,
stdio: "ignore",
});
} catch {
// Non-fatal — user can still init manually.
}
}
}