@xrystal/core 3.52.4 → 3.52.6
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/README.md +19 -13
- package/package.json +1 -1
- package/x/bin/commands/create.mjs +12 -3
- package/x/bin/commands/project-info.mjs +94 -0
- package/x/bin/commands/projects.mjs +1 -1
- package/x/bin/commands/sync.mjs +18 -2
- package/x/bin/commands/telepresence.mjs +134 -0
- package/x/bin/commands/tmp.mjs +10 -1
- package/x/bin/helpers/.generated/seed-from-tmp.mjs +1 -0
- package/x/bin/helpers/.generated/tmp-schema.mjs +1 -0
- package/x/bin/helpers/.generated/workspace.mjs +6 -0
- package/x/bin/helpers/.generated/xrystal-home-registry.mjs +192 -44
- package/x/bin/helpers/cli-help.mjs +23 -12
- package/x/bin/helpers/project-context.mjs +144 -0
- package/x/bin/helpers/scaffold-service.mjs +60 -0
- package/x/bin/helpers/sync-core-dependency.mjs +10 -4
- package/x/bin/helpers/telepresence-layout.mjs +61 -0
- package/x/bin/helpers/tmp-layout.mjs +7 -1
- package/x/bin/helpers/tmp-schema-version.mjs +46 -0
- package/x/bin/helpers/workspace-scaffold.mjs +33 -3
- package/x/bin/main-cli.js +12 -1
- package/x/dist/utils/constants/defaults.json +1 -0
- package/x/dist/utils/helpers/workspace/index.js +5 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// source/utils/helpers/xrystal-home/registry.ts
|
|
2
|
-
import
|
|
2
|
+
import fs3 from "node:fs";
|
|
3
3
|
import os from "node:os";
|
|
4
|
-
import
|
|
4
|
+
import path3 from "node:path";
|
|
5
5
|
|
|
6
6
|
// source/utils/helpers/workspace/index.ts
|
|
7
|
-
import
|
|
8
|
-
import
|
|
7
|
+
import fs2 from "node:fs";
|
|
8
|
+
import path2 from "node:path";
|
|
9
9
|
|
|
10
10
|
// source/utils/models/enums/index.ts
|
|
11
11
|
var GlobalTypeEnum;
|
|
@@ -14,6 +14,167 @@ var GlobalTypeEnum;
|
|
|
14
14
|
GlobalTypeEnum2["API_SERVICE"] = "api-service";
|
|
15
15
|
GlobalTypeEnum2["FRONTEND"] = "frontend";
|
|
16
16
|
})(GlobalTypeEnum ||= {});
|
|
17
|
+
// source/utils/constants/defaults.json
|
|
18
|
+
var defaults_default = {
|
|
19
|
+
packageName: "@xrystal/core",
|
|
20
|
+
packageRootFolder: "x",
|
|
21
|
+
cliName: "xry",
|
|
22
|
+
cliBinNames: [
|
|
23
|
+
"xry"
|
|
24
|
+
],
|
|
25
|
+
cliBinFile: "main-cli.js",
|
|
26
|
+
tmpBaseName: "tmp",
|
|
27
|
+
tmpExtension: ".yml",
|
|
28
|
+
tmpSchemaVersion: 1,
|
|
29
|
+
apiServiceTemplateRel: "x/templates/api-service/tmp.yml",
|
|
30
|
+
templateRepoUri: "https://github.com/xdloper/api-gpt-svc",
|
|
31
|
+
publicFolderName: "public",
|
|
32
|
+
kafkaLogsTopic: "log",
|
|
33
|
+
pathAliasKey: "@/*",
|
|
34
|
+
eslintRestrictedImportDepth: 2,
|
|
35
|
+
brand: {
|
|
36
|
+
primary: "xrystal",
|
|
37
|
+
secondary: "core",
|
|
38
|
+
tagline: "microservice runtime library",
|
|
39
|
+
capabilities: "loaders · tmp.yml · dto · i18n · multi-protocol · scaffold"
|
|
40
|
+
},
|
|
41
|
+
runtimes: {
|
|
42
|
+
bun: {
|
|
43
|
+
label: "Bun",
|
|
44
|
+
packageManager: "bun",
|
|
45
|
+
installCommand: "bun install",
|
|
46
|
+
scriptRunner: "bun run",
|
|
47
|
+
binRunner: "bun",
|
|
48
|
+
testRunner: "bun test",
|
|
49
|
+
tscBin: "bunx tsc",
|
|
50
|
+
esbuildBin: "bunx esbuild",
|
|
51
|
+
devInvoker: null
|
|
52
|
+
},
|
|
53
|
+
node: {
|
|
54
|
+
label: "Node.js",
|
|
55
|
+
packageManager: "npm",
|
|
56
|
+
installCommand: "npm install",
|
|
57
|
+
scriptRunner: "npm run",
|
|
58
|
+
binRunner: "node",
|
|
59
|
+
testRunner: "node --test",
|
|
60
|
+
tscBin: "npx tsc",
|
|
61
|
+
esbuildBin: "npx esbuild",
|
|
62
|
+
devInvoker: "npx"
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
bundlers: [
|
|
66
|
+
"auto",
|
|
67
|
+
"bun",
|
|
68
|
+
"tsc",
|
|
69
|
+
"esbuild"
|
|
70
|
+
],
|
|
71
|
+
tmpWatchDefaults: {
|
|
72
|
+
intervalMs: 500,
|
|
73
|
+
minIntervalMs: 100,
|
|
74
|
+
maxIntervalMs: 1e4
|
|
75
|
+
},
|
|
76
|
+
buildDefaults: {
|
|
77
|
+
bundler: "auto",
|
|
78
|
+
tsconfig: "tsconfig.json",
|
|
79
|
+
typecheck: false,
|
|
80
|
+
tscLenient: true,
|
|
81
|
+
declarations: false,
|
|
82
|
+
sourcemap: false,
|
|
83
|
+
minify: false
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// source/utils/helpers/tmp/seed-from-tmp.ts
|
|
88
|
+
import fs from "node:fs";
|
|
89
|
+
import path from "node:path";
|
|
90
|
+
import { fileURLToPath } from "node:url";
|
|
91
|
+
import yaml from "yaml";
|
|
92
|
+
|
|
93
|
+
// source/utils/helpers/tmp/path-normalize.ts
|
|
94
|
+
function normalizeRootFolder(value, fallback) {
|
|
95
|
+
if (typeof value !== "string" || !value.trim())
|
|
96
|
+
return fallback;
|
|
97
|
+
return value.trim().replace(/^\.\/+/, "");
|
|
98
|
+
}
|
|
99
|
+
function normalizeSourceFolder(value, fallback) {
|
|
100
|
+
if (typeof value !== "string" || !value.trim())
|
|
101
|
+
return fallback;
|
|
102
|
+
const trimmed = value.trim();
|
|
103
|
+
if (trimmed.startsWith("./") || trimmed.startsWith("../"))
|
|
104
|
+
return trimmed;
|
|
105
|
+
return `./${trimmed}`;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// source/utils/helpers/tmp/seed-from-tmp.ts
|
|
109
|
+
var here = path.dirname(fileURLToPath(import.meta.url));
|
|
110
|
+
var templateRel = defaults_default.apiServiceTemplateRel ?? "x/templates/api-service/tmp.yml";
|
|
111
|
+
function requireString(value, label) {
|
|
112
|
+
if (typeof value !== "string" || !value.trim()) {
|
|
113
|
+
throw new Error(`${defaults_default.packageName}: bundled template missing ${label}`);
|
|
114
|
+
}
|
|
115
|
+
return value.trim();
|
|
116
|
+
}
|
|
117
|
+
function readLayoutFromSpecial(special) {
|
|
118
|
+
const block = special && typeof special === "object" && !Array.isArray(special) ? special : {};
|
|
119
|
+
return {
|
|
120
|
+
tests: requireString(block.tests, "configs.global.paths.special.tests"),
|
|
121
|
+
environments: requireString(block.environments, "configs.global.paths.special.environments"),
|
|
122
|
+
dist: requireString(block.dist, "configs.global.paths.special.dist")
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
function findApiTemplate() {
|
|
126
|
+
const starts = [here, process.cwd()].map((value) => path.resolve(value));
|
|
127
|
+
for (const start of starts) {
|
|
128
|
+
let dir = start;
|
|
129
|
+
for (;; ) {
|
|
130
|
+
const candidate = path.join(dir, templateRel);
|
|
131
|
+
if (fs.existsSync(candidate))
|
|
132
|
+
return candidate;
|
|
133
|
+
const parent = path.dirname(dir);
|
|
134
|
+
if (parent === dir)
|
|
135
|
+
break;
|
|
136
|
+
dir = parent;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
throw new Error(`${defaults_default.packageName}: api-service template not found (${templateRel})`);
|
|
140
|
+
}
|
|
141
|
+
function loadSeedFromBundledTmp(tmpFilePath) {
|
|
142
|
+
const file = tmpFilePath ?? findApiTemplate();
|
|
143
|
+
const doc = yaml.parse(fs.readFileSync(file, "utf8"));
|
|
144
|
+
const configs = doc?.configs && typeof doc.configs === "object" ? doc.configs : {};
|
|
145
|
+
const global = configs.global && typeof configs.global === "object" ? configs.global : {};
|
|
146
|
+
const paths = global.paths && typeof global.paths === "object" ? global.paths : {};
|
|
147
|
+
const special = paths.special;
|
|
148
|
+
const root = requireString(paths.root, "configs.global.paths.root");
|
|
149
|
+
const rootNorm = normalizeRootFolder(root, defaults_default.packageRootFolder).replace(/^\.\//, "");
|
|
150
|
+
const tmpName = typeof paths.tmp === "string" && paths.tmp.trim() ? paths.tmp.trim().replace(/^\.\//, "") : "tmp.yml";
|
|
151
|
+
const sourceRaw = requireString(paths.source, "configs.global.paths.source");
|
|
152
|
+
return {
|
|
153
|
+
rootFolder: rootNorm,
|
|
154
|
+
sourceFolder: normalizeSourceFolder(sourceRaw, sourceRaw),
|
|
155
|
+
framework: requireString(global.framework, "configs.global.framework"),
|
|
156
|
+
layout: readLayoutFromSpecial(special),
|
|
157
|
+
tmpRelPath: `${rootNorm}/${tmpName}`.replace(/\/+/g, "/")
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
var seedFromTmp = loadSeedFromBundledTmp();
|
|
161
|
+
|
|
162
|
+
// source/utils/helpers/tmp/tmp-interpolate.ts
|
|
163
|
+
import Handlebars from "handlebars";
|
|
164
|
+
|
|
165
|
+
// source/utils/helpers/tmp/tmp-schema.ts
|
|
166
|
+
function normalizeGlobalType(value) {
|
|
167
|
+
if (value === undefined || value === null || value === "")
|
|
168
|
+
return;
|
|
169
|
+
const raw = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
170
|
+
if (raw === "workspace" /* WORKSPACE */)
|
|
171
|
+
return "workspace" /* WORKSPACE */;
|
|
172
|
+
if (raw === "api-service" /* API_SERVICE */)
|
|
173
|
+
return "api-service" /* API_SERVICE */;
|
|
174
|
+
if (raw === "frontend" /* FRONTEND */)
|
|
175
|
+
return "frontend" /* FRONTEND */;
|
|
176
|
+
throw new Error(`${defaults_default.packageName}: configs.global.type must be one of ${Object.values(GlobalTypeEnum).join("|")} (got "${String(value)}")`);
|
|
177
|
+
}
|
|
17
178
|
|
|
18
179
|
// source/utils/helpers/workspace/index.ts
|
|
19
180
|
var TMP_FILE = "tmp.yml";
|
|
@@ -31,7 +192,7 @@ function parseScalar(raw) {
|
|
|
31
192
|
return trimmed;
|
|
32
193
|
}
|
|
33
194
|
function readTmpFile(filePath) {
|
|
34
|
-
const text =
|
|
195
|
+
const text = fs2.readFileSync(filePath, "utf8");
|
|
35
196
|
const out = { path: filePath, folderStructure: { ...DEFAULT_FOLDER_STRUCTURE } };
|
|
36
197
|
const projectMatch = text.match(/^project:\s*\n(?:[ \t]+[^\n]+\n)*?[ \t]+name:\s*(.+?)\s*(?:#.*)?$/m) ?? text.match(/^project:\s*\n[ \t]+name:\s*(.+?)\s*(?:#.*)?$/m);
|
|
37
198
|
if (projectMatch)
|
|
@@ -48,10 +209,7 @@ function readTmpFile(filePath) {
|
|
|
48
209
|
const tmpBlock = text.match(/paths:\s*\n(?:[ \t]+[^\n]+\n)*?[ \t]+tmp:\s*(.+?)\s*(?:#.*)?$/m);
|
|
49
210
|
if (tmpBlock)
|
|
50
211
|
out.pathsTmp = parseScalar(tmpBlock[1]);
|
|
51
|
-
|
|
52
|
-
if (clusterBlock)
|
|
53
|
-
out.clusterDeployment = parseScalar(clusterBlock[1]);
|
|
54
|
-
else if (out.projectName)
|
|
212
|
+
if (out.projectName)
|
|
55
213
|
out.clusterDeployment = out.projectName;
|
|
56
214
|
out.folderStructure = parseFolderStructure(text);
|
|
57
215
|
return out;
|
|
@@ -70,16 +228,6 @@ function parseFolderStructure(text) {
|
|
|
70
228
|
web: readSlot("web")
|
|
71
229
|
};
|
|
72
230
|
}
|
|
73
|
-
function normalizeGlobalType(value) {
|
|
74
|
-
const raw = typeof value === "string" ? value.trim().toLowerCase() : "";
|
|
75
|
-
if (raw === "workspace" /* WORKSPACE */)
|
|
76
|
-
return "workspace" /* WORKSPACE */;
|
|
77
|
-
if (raw === "api-service" /* API_SERVICE */)
|
|
78
|
-
return "api-service" /* API_SERVICE */;
|
|
79
|
-
if (raw === "frontend" /* FRONTEND */)
|
|
80
|
-
return "frontend" /* FRONTEND */;
|
|
81
|
-
throw new Error(`@xrystal/core: configs.global.type must be one of ${Object.values(GlobalTypeEnum).join("|")} (got "${String(value)}")`);
|
|
82
|
-
}
|
|
83
231
|
function entryName(meta) {
|
|
84
232
|
const project = (meta.projectName ?? "").trim();
|
|
85
233
|
return project || null;
|
|
@@ -88,7 +236,7 @@ function entryType(meta, slot) {
|
|
|
88
236
|
const explicit = (meta.globalType ?? "").trim();
|
|
89
237
|
if (explicit) {
|
|
90
238
|
try {
|
|
91
|
-
return normalizeGlobalType(explicit);
|
|
239
|
+
return normalizeGlobalType(explicit) ?? null;
|
|
92
240
|
} catch {
|
|
93
241
|
return null;
|
|
94
242
|
}
|
|
@@ -98,15 +246,15 @@ function entryType(meta, slot) {
|
|
|
98
246
|
return null;
|
|
99
247
|
}
|
|
100
248
|
function findWorkspaceRoot(startDir = process.cwd()) {
|
|
101
|
-
let cur =
|
|
249
|
+
let cur = path2.resolve(startDir);
|
|
102
250
|
for (;; ) {
|
|
103
|
-
const candidate =
|
|
104
|
-
if (
|
|
251
|
+
const candidate = path2.join(cur, TMP_FILE);
|
|
252
|
+
if (fs2.existsSync(candidate)) {
|
|
105
253
|
const meta = readTmpFile(candidate);
|
|
106
254
|
if (meta.globalType === "workspace" /* WORKSPACE */)
|
|
107
255
|
return cur;
|
|
108
256
|
}
|
|
109
|
-
const parent =
|
|
257
|
+
const parent = path2.dirname(cur);
|
|
110
258
|
if (parent === cur)
|
|
111
259
|
break;
|
|
112
260
|
cur = parent;
|
|
@@ -118,7 +266,7 @@ function isTraversableDir(entry, fullPath) {
|
|
|
118
266
|
return true;
|
|
119
267
|
if (entry.isSymbolicLink()) {
|
|
120
268
|
try {
|
|
121
|
-
return
|
|
269
|
+
return fs2.statSync(fullPath).isDirectory();
|
|
122
270
|
} catch {
|
|
123
271
|
return false;
|
|
124
272
|
}
|
|
@@ -127,19 +275,19 @@ function isTraversableDir(entry, fullPath) {
|
|
|
127
275
|
}
|
|
128
276
|
function walkTmpUnder(rootDir) {
|
|
129
277
|
const found = [];
|
|
130
|
-
if (!
|
|
278
|
+
if (!fs2.existsSync(rootDir))
|
|
131
279
|
return found;
|
|
132
280
|
const stack = [rootDir];
|
|
133
281
|
while (stack.length) {
|
|
134
282
|
const dir = stack.pop();
|
|
135
283
|
let entries;
|
|
136
284
|
try {
|
|
137
|
-
entries =
|
|
285
|
+
entries = fs2.readdirSync(dir, { withFileTypes: true });
|
|
138
286
|
} catch {
|
|
139
287
|
continue;
|
|
140
288
|
}
|
|
141
289
|
for (const entry of entries) {
|
|
142
|
-
const full =
|
|
290
|
+
const full = path2.join(dir, entry.name);
|
|
143
291
|
if (isTraversableDir(entry, full)) {
|
|
144
292
|
if (SKIP_DIRS.has(entry.name) || entry.name.startsWith("."))
|
|
145
293
|
continue;
|
|
@@ -166,15 +314,15 @@ function repoRootFromTmp(tmpPath, meta) {
|
|
|
166
314
|
throw new Error(`tmp path mismatch: expected */${rel}, got ${abs}`);
|
|
167
315
|
}
|
|
168
316
|
function findEntries(workspaceRoot) {
|
|
169
|
-
const wsMeta = readTmpFile(
|
|
317
|
+
const wsMeta = readTmpFile(path2.join(workspaceRoot, TMP_FILE));
|
|
170
318
|
const fsMap = wsMeta.folderStructure;
|
|
171
319
|
const entries = [];
|
|
172
320
|
const seen = new Set;
|
|
173
321
|
const slots = ["api", "admin", "web"];
|
|
174
322
|
for (const slot of slots) {
|
|
175
|
-
const folder =
|
|
323
|
+
const folder = path2.join(workspaceRoot, fsMap[slot]);
|
|
176
324
|
for (const tmpPath of walkTmpUnder(folder)) {
|
|
177
|
-
if (
|
|
325
|
+
if (path2.resolve(tmpPath) === path2.join(workspaceRoot, TMP_FILE))
|
|
178
326
|
continue;
|
|
179
327
|
const meta = readTmpFile(tmpPath);
|
|
180
328
|
const kind = entryType(meta, slot);
|
|
@@ -207,14 +355,14 @@ function findEntries(workspaceRoot) {
|
|
|
207
355
|
// source/utils/helpers/xrystal-home/registry.ts
|
|
208
356
|
var XRYSTAL_HOME_DIR = ".xrystal";
|
|
209
357
|
function resolveXrystalHome() {
|
|
210
|
-
return
|
|
358
|
+
return path3.join(os.homedir(), XRYSTAL_HOME_DIR);
|
|
211
359
|
}
|
|
212
360
|
function registryFilePath() {
|
|
213
|
-
return
|
|
361
|
+
return path3.join(resolveXrystalHome(), "registry.json");
|
|
214
362
|
}
|
|
215
363
|
function ensureXrystalHome() {
|
|
216
364
|
const home = resolveXrystalHome();
|
|
217
|
-
|
|
365
|
+
fs3.mkdirSync(path3.join(home, "workspaces"), { recursive: true });
|
|
218
366
|
return home;
|
|
219
367
|
}
|
|
220
368
|
function entryToRegistryProject(entry) {
|
|
@@ -227,9 +375,9 @@ function entryToRegistryProject(entry) {
|
|
|
227
375
|
};
|
|
228
376
|
}
|
|
229
377
|
function normalizeRegistryPath(input) {
|
|
230
|
-
const resolved =
|
|
378
|
+
const resolved = path3.resolve(input);
|
|
231
379
|
try {
|
|
232
|
-
return
|
|
380
|
+
return fs3.realpathSync.native(resolved);
|
|
233
381
|
} catch {
|
|
234
382
|
return resolved;
|
|
235
383
|
}
|
|
@@ -237,7 +385,7 @@ function normalizeRegistryPath(input) {
|
|
|
237
385
|
function scanWorkspace(workspacePath, workspaceName) {
|
|
238
386
|
const root = normalizeRegistryPath(workspacePath);
|
|
239
387
|
const entries = findEntries(root);
|
|
240
|
-
const name = workspaceName ??
|
|
388
|
+
const name = workspaceName ?? path3.basename(root);
|
|
241
389
|
return {
|
|
242
390
|
name,
|
|
243
391
|
path: root,
|
|
@@ -248,23 +396,23 @@ function scanWorkspace(workspacePath, workspaceName) {
|
|
|
248
396
|
function readRegistry() {
|
|
249
397
|
ensureXrystalHome();
|
|
250
398
|
const file = registryFilePath();
|
|
251
|
-
if (!
|
|
399
|
+
if (!fs3.existsSync(file)) {
|
|
252
400
|
return { version: 1, updatedAt: new Date().toISOString(), workspaces: [] };
|
|
253
401
|
}
|
|
254
|
-
return JSON.parse(
|
|
402
|
+
return JSON.parse(fs3.readFileSync(file, "utf8"));
|
|
255
403
|
}
|
|
256
404
|
function writeRegistry(registry) {
|
|
257
405
|
ensureXrystalHome();
|
|
258
406
|
registry.updatedAt = new Date().toISOString();
|
|
259
|
-
|
|
407
|
+
fs3.writeFileSync(registryFilePath(), `${JSON.stringify(registry, null, 2)}
|
|
260
408
|
`);
|
|
261
409
|
for (const ws of registry.workspaces) {
|
|
262
|
-
const wsDir =
|
|
263
|
-
|
|
264
|
-
|
|
410
|
+
const wsDir = path3.join(resolveXrystalHome(), "workspaces", ws.name);
|
|
411
|
+
fs3.mkdirSync(wsDir, { recursive: true });
|
|
412
|
+
fs3.writeFileSync(path3.join(wsDir, "workspace.json"), `${JSON.stringify(ws, null, 2)}
|
|
265
413
|
`);
|
|
266
414
|
for (const project of ws.projects) {
|
|
267
|
-
|
|
415
|
+
fs3.writeFileSync(path3.join(wsDir, `${project.name}.json`), `${JSON.stringify(project, null, 2)}
|
|
268
416
|
`);
|
|
269
417
|
}
|
|
270
418
|
}
|
|
@@ -7,11 +7,13 @@ import { brandTitle } from './banner.mjs'
|
|
|
7
7
|
import { packageDefaults } from './package-defaults.mjs'
|
|
8
8
|
|
|
9
9
|
const QUICK_COMMANDS = [
|
|
10
|
-
{ cmd: '
|
|
11
|
-
{ cmd: '
|
|
12
|
-
{ cmd: '
|
|
13
|
-
{ cmd: '
|
|
14
|
-
{ cmd: '
|
|
10
|
+
{ cmd: 'project', desc: 'Where am I? workspace / service / core library context' },
|
|
11
|
+
{ cmd: 'create <name> --workspace', desc: 'Outer tmp + api/general + admin/web' },
|
|
12
|
+
{ cmd: 'tmp', desc: 'Ensure tmp.yml (inner x/ or outer ./ workspace)' },
|
|
13
|
+
{ cmd: 'sync', desc: 'Inner tsconfig + pin @xrystal/core · registry if workspace' },
|
|
14
|
+
{ cmd: 'workspaces', desc: 'Global registry ~/.xrystal (not repo tmp)' },
|
|
15
|
+
{ cmd: 'tp dev', hint: '<deploy>', desc: 'Telepresence local intercept' },
|
|
16
|
+
{ cmd: 'dev / build / doctor', desc: 'Develop & diagnostics' },
|
|
15
17
|
]
|
|
16
18
|
|
|
17
19
|
const COMMAND_GROUPS = [
|
|
@@ -24,20 +26,27 @@ const COMMAND_GROUPS = [
|
|
|
24
26
|
{ cmd: 'test', desc: 'Run project tests (paths.tests)' },
|
|
25
27
|
],
|
|
26
28
|
},
|
|
29
|
+
{
|
|
30
|
+
title: 'Context',
|
|
31
|
+
items: [
|
|
32
|
+
{ cmd: 'project', desc: 'Current folder: workspace root, linked service, standalone, or core library' },
|
|
33
|
+
{ cmd: 'workspaces [list|sync]', desc: 'Global registry ~/.xrystal (external to repo tmp files)' },
|
|
34
|
+
],
|
|
35
|
+
},
|
|
27
36
|
{
|
|
28
37
|
title: 'Workspace',
|
|
29
38
|
items: [
|
|
30
|
-
{ cmd: 'create <name> --workspace', desc: '
|
|
31
|
-
{ cmd: 'sync', desc: '
|
|
32
|
-
{ cmd: '
|
|
33
|
-
{ cmd: '
|
|
39
|
+
{ cmd: 'create <name> --workspace', desc: 'Outer ./tmp.yml + api/general inner monolith + admin/web' },
|
|
40
|
+
{ cmd: 'sync', desc: 'All inner projects: tsconfig + @xrystal/core pin + registry rescan' },
|
|
41
|
+
{ cmd: 'telepresence dev <deploy>', aliases: 'tp dev', desc: 'Intercept cluster pod → local dev' },
|
|
42
|
+
{ cmd: 'telepresence connect', aliases: 'tp connect', desc: 'Connect Telepresence to dev/test namespace' },
|
|
34
43
|
],
|
|
35
44
|
},
|
|
36
45
|
{
|
|
37
46
|
title: 'Project',
|
|
38
47
|
items: [
|
|
39
|
-
{ cmd: 'create', hint: '<name> [path]', desc: '
|
|
40
|
-
{ cmd: 'tmp', desc: 'Ensure tmp.yml
|
|
48
|
+
{ cmd: 'create', hint: '<name> [path]', desc: 'Standalone service (gpt template + inner x/tmp.yml)' },
|
|
49
|
+
{ cmd: 'tmp', desc: 'Ensure inner/outer tmp.yml · end.version = schema (currently 1)' },
|
|
41
50
|
{ cmd: 'hooks install', desc: 'Install .git/hooks pre-commit version check' },
|
|
42
51
|
{ cmd: 'hooks status', desc: 'Show whether pre-commit hook is installed' },
|
|
43
52
|
],
|
|
@@ -58,6 +67,7 @@ const COMMAND_GROUPS = [
|
|
|
58
67
|
]
|
|
59
68
|
|
|
60
69
|
const SECTION_COLORS = {
|
|
70
|
+
Context: chalk.bold.white,
|
|
61
71
|
Develop: chalk.bold.cyan,
|
|
62
72
|
Workspace: chalk.bold.blue,
|
|
63
73
|
Project: chalk.bold.green,
|
|
@@ -90,6 +100,7 @@ export function printQuickStartGuide({ cliName = CLI_PRIMARY_NAME } = {}) {
|
|
|
90
100
|
console.log('')
|
|
91
101
|
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} -h`)} ${chalk.gray('· all commands & options')}`)
|
|
92
102
|
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} doctor`)} ${chalk.gray('· verify install & versions')}`)
|
|
103
|
+
console.log(` ${chalk.dim('tip')} ${chalk.bold.white(`${cliName} tp dev general`)} ${chalk.gray('· telepresence local intercept')}`)
|
|
93
104
|
console.log('')
|
|
94
105
|
}
|
|
95
106
|
|
|
@@ -130,7 +141,7 @@ export function formatRootHelp({ cliName = CLI_PRIMARY_NAME } = {}) {
|
|
|
130
141
|
|
|
131
142
|
export function formatCreateHelp({ cliName = CLI_PRIMARY_NAME } = {}) {
|
|
132
143
|
const examples = [
|
|
133
|
-
{ cmd: `${cliName} create demo --workspace`, desc: '→ workspace
|
|
144
|
+
{ cmd: `${cliName} create demo --workspace`, desc: '→ workspace + api/general + admin/web' },
|
|
134
145
|
{ cmd: `${cliName} create my-api`, desc: '→ ./my-api (current folder)' },
|
|
135
146
|
{ cmd: `${cliName} create my-api ..`, desc: '→ ../my-api' },
|
|
136
147
|
{ cmd: `${cliName} create my-api ~/services`, desc: '→ ~/services/my-api' },
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import fs from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import yaml from 'yaml'
|
|
4
|
+
|
|
5
|
+
import { packageDefaults } from './package-defaults.mjs'
|
|
6
|
+
import { isCorePackageRoot } from './tmp-layout.mjs'
|
|
7
|
+
import { findEntries, findWorkspaceRoot, workspaceProjectName } from './workspace-resolve.mjs'
|
|
8
|
+
import { getInstalledCoreVersion, readCliPackageJson, readProjectPackageJson, resolveProjectRoot } from './project-root.mjs'
|
|
9
|
+
import { readTmpSchemaVersion, TMP_SCHEMA_VERSION } from './tmp-schema-version.mjs'
|
|
10
|
+
|
|
11
|
+
const WORKSPACE_TYPE = 'workspace'
|
|
12
|
+
|
|
13
|
+
function readTmpMeta(tmpFilePath) {
|
|
14
|
+
if (!tmpFilePath || !fs.existsSync(tmpFilePath)) return null
|
|
15
|
+
try {
|
|
16
|
+
const doc = yaml.parse(fs.readFileSync(tmpFilePath, 'utf8'))
|
|
17
|
+
const globalType = doc?.configs?.global?.type ?? null
|
|
18
|
+
const projectName = doc?.project?.name ?? null
|
|
19
|
+
return {
|
|
20
|
+
path: tmpFilePath,
|
|
21
|
+
projectName,
|
|
22
|
+
globalType,
|
|
23
|
+
schemaVersion: readTmpSchemaVersion(tmpFilePath),
|
|
24
|
+
}
|
|
25
|
+
} catch {
|
|
26
|
+
return { path: tmpFilePath, projectName: null, globalType: null, schemaVersion: null }
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function findOuterWorkspaceTmp(startDir) {
|
|
31
|
+
const wsRoot = findWorkspaceRoot(startDir)
|
|
32
|
+
if (!wsRoot) return null
|
|
33
|
+
const outer = path.join(wsRoot, 'tmp.yml')
|
|
34
|
+
return fs.existsSync(outer) ? outer : null
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function resolveProjectContext(startDir = process.cwd()) {
|
|
38
|
+
const cwd = path.resolve(startDir)
|
|
39
|
+
const cliVersion = readCliPackageJson().version
|
|
40
|
+
const workspaceRoot = findWorkspaceRoot(cwd)
|
|
41
|
+
const projectRoot = resolveProjectRoot(cwd)
|
|
42
|
+
const atWorkspaceRoot = workspaceRoot != null && path.resolve(workspaceRoot) === cwd
|
|
43
|
+
|
|
44
|
+
if (isCorePackageRoot(projectRoot)) {
|
|
45
|
+
const coreRoot = projectRoot
|
|
46
|
+
const innerTmpPath = path.join(coreRoot, packageDefaults.packageRootFolder, `${packageDefaults.tmpBaseName}${packageDefaults.tmpExtension}`)
|
|
47
|
+
const innerMeta = readTmpMeta(fs.existsSync(innerTmpPath) ? innerTmpPath : null)
|
|
48
|
+
return {
|
|
49
|
+
kind: 'core-library',
|
|
50
|
+
label: '@xrystal/core (library source)',
|
|
51
|
+
cwd,
|
|
52
|
+
projectRoot: coreRoot,
|
|
53
|
+
workspaceRoot: null,
|
|
54
|
+
outerTmp: null,
|
|
55
|
+
innerTmp: innerMeta,
|
|
56
|
+
tmp: innerMeta,
|
|
57
|
+
package: readProjectPackageJson(coreRoot),
|
|
58
|
+
core: { cli: cliVersion, declared: cliVersion, installed: cliVersion, isGlobalLibrary: true },
|
|
59
|
+
workspaceEntries: [],
|
|
60
|
+
hints: [
|
|
61
|
+
'Global runtime library — publish via xry tag',
|
|
62
|
+
'Consumers: bun add @xrystal/core && xry tmp in their repo',
|
|
63
|
+
],
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const outerTmpPath = findOuterWorkspaceTmp(cwd)
|
|
68
|
+
const outerMeta = outerTmpPath ? readTmpMeta(outerTmpPath) : null
|
|
69
|
+
|
|
70
|
+
const innerTmpPath = (() => {
|
|
71
|
+
if (atWorkspaceRoot) return null
|
|
72
|
+
const fromProject = path.join(projectRoot, packageDefaults.packageRootFolder, `${packageDefaults.tmpBaseName}${packageDefaults.tmpExtension}`)
|
|
73
|
+
if (fs.existsSync(fromProject)) return fromProject
|
|
74
|
+
const atRoot = path.join(projectRoot, `${packageDefaults.tmpBaseName}${packageDefaults.tmpExtension}`)
|
|
75
|
+
if (fs.existsSync(atRoot)) return atRoot
|
|
76
|
+
return null
|
|
77
|
+
})()
|
|
78
|
+
|
|
79
|
+
let pkg = null
|
|
80
|
+
const pkgPath = path.join(projectRoot, 'package.json')
|
|
81
|
+
if (fs.existsSync(pkgPath)) {
|
|
82
|
+
try {
|
|
83
|
+
pkg = readProjectPackageJson(projectRoot)
|
|
84
|
+
} catch {
|
|
85
|
+
pkg = null
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
let core = { declared: null, installed: null, path: null, isWorkspace: false }
|
|
90
|
+
if (pkg) {
|
|
91
|
+
core = getInstalledCoreVersion(projectRoot)
|
|
92
|
+
}
|
|
93
|
+
const innerMeta = innerTmpPath ? readTmpMeta(innerTmpPath) : null
|
|
94
|
+
|
|
95
|
+
let kind = 'standalone-service'
|
|
96
|
+
let label = 'Standalone service'
|
|
97
|
+
if (atWorkspaceRoot && outerMeta?.globalType === WORKSPACE_TYPE) {
|
|
98
|
+
kind = 'workspace-root'
|
|
99
|
+
label = 'Workspace root (outer tmp.yml)'
|
|
100
|
+
} else if (workspaceRoot && outerMeta?.globalType === WORKSPACE_TYPE) {
|
|
101
|
+
kind = 'workspace-service'
|
|
102
|
+
label = 'Workspace linked service (inner x/tmp.yml)'
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const workspaceEntries = workspaceRoot ? findEntries(workspaceRoot) : []
|
|
106
|
+
const selfEntry = workspaceEntries.find((e) => path.resolve(e.path) === path.resolve(projectRoot))
|
|
107
|
+
|
|
108
|
+
const hints = []
|
|
109
|
+
if (kind === 'workspace-root') {
|
|
110
|
+
hints.push('Outer tmp: ./tmp.yml · inner tmp: each api|admin|web project')
|
|
111
|
+
hints.push('xry sync — all linked inner projects + ~/.xrystal registry')
|
|
112
|
+
hints.push('xry workspaces — global registry (~/.xrystal), not this folder alone')
|
|
113
|
+
} else if (kind === 'workspace-service') {
|
|
114
|
+
hints.push(`Outer workspace: ${workspaceRoot}`)
|
|
115
|
+
hints.push('xry tmp — inner x/tmp.yml · xry project — this view')
|
|
116
|
+
hints.push('Run xry sync from workspace root to pin @xrystal/core on all services')
|
|
117
|
+
} else {
|
|
118
|
+
hints.push('xry tmp — seed inner x/tmp.yml')
|
|
119
|
+
hints.push('@xrystal/core is the global library (npm) powering this service')
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
kind,
|
|
124
|
+
label,
|
|
125
|
+
cwd,
|
|
126
|
+
projectRoot,
|
|
127
|
+
workspaceRoot,
|
|
128
|
+
atWorkspaceRoot,
|
|
129
|
+
outerTmp: outerMeta,
|
|
130
|
+
innerTmp: innerMeta,
|
|
131
|
+
tmp: innerMeta ?? outerMeta,
|
|
132
|
+
selfEntry: selfEntry ?? null,
|
|
133
|
+
package: pkg,
|
|
134
|
+
core: {
|
|
135
|
+
cli: cliVersion,
|
|
136
|
+
declared: core.declared,
|
|
137
|
+
installed: core.installed,
|
|
138
|
+
isGlobalLibrary: false,
|
|
139
|
+
},
|
|
140
|
+
workspaceEntries,
|
|
141
|
+
schemaVersionExpected: TMP_SCHEMA_VERSION,
|
|
142
|
+
hints,
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { execSync } from 'node:child_process'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
import yaml from 'yaml'
|
|
5
|
+
|
|
6
|
+
import { Constants } from '../constants/index.mjs'
|
|
7
|
+
import { ensureTmpFile } from './tmp-layout.mjs'
|
|
8
|
+
import { ensureTmpSchemaVersion } from './tmp-schema-version.mjs'
|
|
9
|
+
import { setupProjectGitHooks } from '../scripts/install-git-hooks.mjs'
|
|
10
|
+
|
|
11
|
+
function patchTmpProjectName(tmpFilePath, projectName) {
|
|
12
|
+
const doc = yaml.parse(fs.readFileSync(tmpFilePath, 'utf8'))
|
|
13
|
+
doc.project = { ...(doc.project ?? {}), name: projectName }
|
|
14
|
+
fs.writeFileSync(tmpFilePath, yaml.stringify(doc))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function scaffoldServiceFromTemplate(targetPath, {
|
|
18
|
+
projectName,
|
|
19
|
+
templateRepoUri = Constants.templateRepoUri,
|
|
20
|
+
installCommand,
|
|
21
|
+
cliName,
|
|
22
|
+
setupHooks = true,
|
|
23
|
+
} = {}) {
|
|
24
|
+
const resolved = path.resolve(targetPath)
|
|
25
|
+
if (fs.existsSync(resolved)) {
|
|
26
|
+
const entries = fs.readdirSync(resolved)
|
|
27
|
+
if (entries.length > 0) {
|
|
28
|
+
throw new Error(`Target directory is not empty: ${resolved}`)
|
|
29
|
+
}
|
|
30
|
+
fs.rmdirSync(resolved)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fs.mkdirSync(path.dirname(resolved), { recursive: true })
|
|
34
|
+
execSync(`git clone --depth 1 ${templateRepoUri} "${resolved}"`, { stdio: 'ignore' })
|
|
35
|
+
|
|
36
|
+
const gitFolder = path.join(resolved, '.git')
|
|
37
|
+
if (fs.existsSync(gitFolder)) fs.rmSync(gitFolder, { recursive: true, force: true })
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
execSync('git init', { cwd: resolved, stdio: 'ignore' })
|
|
41
|
+
} catch {
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const tmpResult = ensureTmpFile(resolved)
|
|
45
|
+
patchTmpProjectName(tmpResult.tmpFilePath, projectName)
|
|
46
|
+
ensureTmpSchemaVersion(tmpResult.tmpFilePath)
|
|
47
|
+
|
|
48
|
+
execSync(installCommand, { cwd: resolved, stdio: 'inherit' })
|
|
49
|
+
|
|
50
|
+
let hooks = null
|
|
51
|
+
if (setupHooks) {
|
|
52
|
+
try {
|
|
53
|
+
hooks = setupProjectGitHooks(resolved, { cliName })
|
|
54
|
+
} catch {
|
|
55
|
+
hooks = { skipped: true }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return { root: resolved, tmpFilePath: tmpResult.tmpFilePath, hooks }
|
|
60
|
+
}
|
|
@@ -52,10 +52,16 @@ export function syncCoreDependency(root, targetVersion, { dryRun = false } = {})
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
const packageManager = resolveProjectPackageManager(root)
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
stdio: 'inherit'
|
|
58
|
-
})
|
|
55
|
+
const addCmd = `${packageManager} add ${packageDefaults.packageName}@${targetVersion}`
|
|
56
|
+
try {
|
|
57
|
+
execSync(addCmd, { cwd: root, stdio: 'inherit' })
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (packageManager !== 'npm') {
|
|
60
|
+
execSync(`npm add ${packageDefaults.packageName}@${targetVersion}`, { cwd: root, stdio: 'inherit' })
|
|
61
|
+
} else {
|
|
62
|
+
throw error
|
|
63
|
+
}
|
|
64
|
+
}
|
|
59
65
|
|
|
60
66
|
return { synced: true, declared, current, targetVersion }
|
|
61
67
|
}
|