api2cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +2616 -0
- package/package.json +23 -0
- package/src/commands/bundle.ts +68 -0
- package/src/commands/create.ts +78 -0
- package/src/commands/doctor.ts +48 -0
- package/src/commands/install.ts +29 -0
- package/src/commands/link.ts +32 -0
- package/src/commands/list.ts +52 -0
- package/src/commands/publish.ts +22 -0
- package/src/commands/remove.ts +35 -0
- package/src/commands/tokens.ts +35 -0
- package/src/commands/unlink.ts +11 -0
- package/src/commands/update.ts +26 -0
- package/src/index.ts +41 -0
- package/src/lib/bashrc.ts +55 -0
- package/src/lib/config.ts +35 -0
- package/src/lib/shell.ts +57 -0
- package/src/lib/template.ts +63 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { existsSync, cpSync, readdirSync, statSync, readFileSync, writeFileSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { TEMPLATE_DIR } from "./config.js";
|
|
4
|
+
|
|
5
|
+
interface TemplateVars {
|
|
6
|
+
appName: string;
|
|
7
|
+
appCli: string;
|
|
8
|
+
baseUrl: string;
|
|
9
|
+
authType: string;
|
|
10
|
+
authHeader: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** Copy the template scaffold to a target directory */
|
|
14
|
+
export function copyTemplate(targetDir: string): void {
|
|
15
|
+
if (!existsSync(TEMPLATE_DIR)) {
|
|
16
|
+
throw new Error(`Template not found at ${TEMPLATE_DIR}. Run: api2cli doctor`);
|
|
17
|
+
}
|
|
18
|
+
cpSync(TEMPLATE_DIR, targetDir, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Replace all {{PLACEHOLDER}} tokens in every file in a directory tree */
|
|
22
|
+
export function replacePlaceholders(dir: string, vars: TemplateVars): void {
|
|
23
|
+
const replacements: [string, string][] = [
|
|
24
|
+
["{{APP_NAME}}", vars.appName],
|
|
25
|
+
["{{APP_CLI}}", vars.appCli],
|
|
26
|
+
["{{BASE_URL}}", vars.baseUrl],
|
|
27
|
+
["{{AUTH_TYPE}}", vars.authType],
|
|
28
|
+
["{{AUTH_HEADER}}", vars.authHeader],
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
walkFiles(dir, (filePath) => {
|
|
32
|
+
// Skip binary files and node_modules
|
|
33
|
+
if (filePath.includes("node_modules")) return;
|
|
34
|
+
const ext = filePath.split(".").pop() ?? "";
|
|
35
|
+
if (!["ts", "js", "json", "md", "txt", "template"].includes(ext)) return;
|
|
36
|
+
|
|
37
|
+
let content = readFileSync(filePath, "utf-8");
|
|
38
|
+
let changed = false;
|
|
39
|
+
|
|
40
|
+
for (const [placeholder, value] of replacements) {
|
|
41
|
+
if (content.includes(placeholder)) {
|
|
42
|
+
content = content.replaceAll(placeholder, value);
|
|
43
|
+
changed = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (changed) {
|
|
48
|
+
writeFileSync(filePath, content);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Recursively walk all files in a directory */
|
|
54
|
+
function walkFiles(dir: string, callback: (path: string) => void): void {
|
|
55
|
+
for (const entry of readdirSync(dir)) {
|
|
56
|
+
const full = join(dir, entry);
|
|
57
|
+
if (statSync(full).isDirectory()) {
|
|
58
|
+
walkFiles(full, callback);
|
|
59
|
+
} else {
|
|
60
|
+
callback(full);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|