@allbluecn/kernel 0.2.0 → 0.2.2

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/bin.js CHANGED
@@ -1,41 +1,116 @@
1
1
  #!/usr/bin/env node
2
- import { n as generateAssemblyCode, r as resolvePluginOrder, t as dumpModules } from "./src-Dvcggyr-.js";
2
+ import { c as generateAssemblyCode, f as resolvePluginOrder, l as generateAssemblyFiles, s as dumpModules, u as parseProfileImports } from "./src-B6_ZEkl9.js";
3
3
  import { parseArgs } from "node:util";
4
4
  import { pathToFileURL } from "node:url";
5
5
  import { dirname, relative, resolve } from "node:path";
6
6
  //#region src/bin.ts
7
- const { values } = parseArgs({
7
+ const { values, positionals } = parseArgs({
8
8
  allowPositionals: true,
9
9
  options: {
10
10
  profile: {
11
11
  type: "string",
12
12
  short: "p"
13
13
  },
14
- "gen-out": { type: "string" }
14
+ "gen-out": { type: "string" },
15
+ "app-dir": { type: "string" },
16
+ "dry-run": {
17
+ type: "boolean",
18
+ default: false
19
+ },
20
+ assemble: {
21
+ type: "boolean",
22
+ default: false
23
+ },
24
+ modules: {
25
+ type: "boolean",
26
+ default: false
27
+ }
15
28
  }
16
29
  });
30
+ const isAssemble = values.assemble === true;
31
+ const isModules = values.modules === true || !isAssemble;
32
+ const appDir = typeof values["app-dir"] === "string" ? values["app-dir"] : "apps/web";
33
+ const dryRun = values["dry-run"] === true;
17
34
  async function main() {
18
35
  const profileArg = values.profile;
19
36
  if (!profileArg) {
20
- console.error("用法: allblue modules --profile <oss|saas|desktop> [--gen-out <file>]");
37
+ console.error("用法: allblue [--modules] --profile <oss|saas|desktop> [--gen-out <file>]");
38
+ console.error(" allblue --assemble --profile <oss|saas|desktop> [--app-dir <dir>] [--dry-run]");
21
39
  process.exit(1);
22
40
  }
23
41
  const profilePath = resolve(profileArg.endsWith(".ts") ? profileArg : `profiles/${profileArg}.ts`);
24
42
  const profile = (await import(pathToFileURL(profilePath).href)).default;
25
43
  const resolved = resolvePluginOrder(profile.plugins);
26
- if (values["gen-out"]) {
27
- const outPath = resolve(values["gen-out"]);
28
- const relProfile = relImportPath(outPath, profilePath);
29
- const code = generateAssemblyCode({
44
+ if (isModules) {
45
+ if (values["gen-out"]) {
46
+ const outPath = resolve(values["gen-out"]);
47
+ const relProfile = relImportPath(outPath, profilePath);
48
+ const code = generateAssemblyCode({
49
+ profileId: profile.id,
50
+ profileModulePath: relProfile,
51
+ resolved
52
+ });
53
+ const { writeFile } = await import("node:fs/promises");
54
+ await writeFile(outPath, code + "\n", "utf8");
55
+ console.log(`已生成 ${outPath} (${resolved.order.length} plugins)`);
56
+ }
57
+ console.log(dumpModules(profile.id, resolved));
58
+ return;
59
+ }
60
+ if (isAssemble) {
61
+ const { readFileSync } = await import("node:fs");
62
+ const profileSource = readFileSync(profilePath, "utf8");
63
+ const imports = parseProfileImports(profileSource);
64
+ const pluginLocalNames = extractPluginLocalNames(profileSource);
65
+ const relProfilePath = relImportPath(resolve(appDir, "src/plugins/plugins.gen.ts"), profilePath);
66
+ const assembly = generateAssemblyFiles({
30
67
  profileId: profile.id,
31
- profileModulePath: relProfile,
32
- resolved
68
+ profileModulePath: relProfilePath,
69
+ resolved,
70
+ imports,
71
+ pluginLocalNames
33
72
  });
34
- const { writeFile } = await import("node:fs/promises");
35
- await writeFile(outPath, code + "\n", "utf8");
36
- console.log(`已生成 ${outPath} (${resolved.order.length} plugins)`);
73
+ if (dryRun) {
74
+ console.log("=== [dry-run] plugins.gen.ts ===");
75
+ console.log(assembly.pluginsGen);
76
+ console.log("=== [dry-run] modules.gen.ts ===");
77
+ console.log(assembly.modulesGen);
78
+ console.log("=== [dry-run] ui.gen.ts ===");
79
+ console.log(assembly.uiGen);
80
+ for (const [file, content] of assembly.routeFiles.entries()) {
81
+ console.log(`=== [dry-run] ${file} ===`);
82
+ console.log(content);
83
+ }
84
+ } else await writeAssemblyFiles(appDir, assembly);
85
+ console.log(dumpModules(profile.id, resolved));
86
+ return;
87
+ }
88
+ }
89
+ function extractPluginLocalNames(source) {
90
+ const match = source.match(/plugins:\s*\[([^\]]*)\]/s);
91
+ if (!match) return [];
92
+ return match[1].split(",").map((s) => s.trim()).filter(Boolean);
93
+ }
94
+ async function writeAssemblyFiles(appDir, assembly) {
95
+ const { writeFile, readFile, unlink, mkdir } = await import("node:fs/promises");
96
+ const { join } = await import("node:path");
97
+ const pluginsDir = join(appDir, "src/plugins");
98
+ await mkdir(pluginsDir, { recursive: true });
99
+ const pluginsGenContent = assembly.pluginsGen;
100
+ await writeFile(join(pluginsDir, "plugins.gen.ts"), pluginsGenContent + "\n");
101
+ await writeFile(join(pluginsDir, "modules.gen.ts"), assembly.modulesGen + "\n");
102
+ await writeFile(join(pluginsDir, "ui.gen.ts"), assembly.uiGen + "\n");
103
+ const manifestPath = join(pluginsDir, ".gen-manifest.json");
104
+ const oldManifest = await readFile(manifestPath, "utf8").catch(() => null);
105
+ const oldFiles = oldManifest ? JSON.parse(oldManifest).routeFiles : [];
106
+ const newFiles = [...assembly.routeFiles.keys()];
107
+ for (const f of oldFiles) if (!newFiles.includes(f)) await unlink(join(appDir, f)).catch(() => {});
108
+ for (const [file, content] of assembly.routeFiles.entries()) {
109
+ const filePath = join(appDir, file);
110
+ await mkdir(dirname(filePath), { recursive: true });
111
+ await writeFile(filePath, content + "\n");
37
112
  }
38
- console.log(dumpModules(profile.id, resolved));
113
+ await writeFile(manifestPath, JSON.stringify({ routeFiles: newFiles }, null, 2));
39
114
  }
40
115
  function relImportPath(fromFile, toFile) {
41
116
  let rel = relative(dirname(fromFile), toFile).replace(/\.ts$/, "");