@nuucognition/flint-cli 0.5.6-dev.10 → 0.5.6-dev.11

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.
@@ -0,0 +1,768 @@
1
+ import {
2
+ parse,
3
+ stringify
4
+ } from "./chunk-YQMGDALR.js";
5
+
6
+ // ../../packages/flint/dist/chunk-52SE24LG.js
7
+ import { readFile, writeFile, stat as stat2, mkdir } from "fs/promises";
8
+ import { join as join2 } from "path";
9
+ import { randomUUID } from "crypto";
10
+ import { stat } from "fs/promises";
11
+ import { join, dirname, basename } from "path";
12
+ var FLINT_DIR = ".flint";
13
+ function getFlintConfigDir(flintPath) {
14
+ return join(flintPath, FLINT_DIR);
15
+ }
16
+ async function isInsideMesh(dir) {
17
+ let current = dir;
18
+ while (current !== dirname(current)) {
19
+ try {
20
+ const tomlPath = join(current, "flint.toml");
21
+ await stat(tomlPath);
22
+ return current;
23
+ } catch {
24
+ current = dirname(current);
25
+ }
26
+ }
27
+ return null;
28
+ }
29
+ function getTypePrefix() {
30
+ return "(Flint)";
31
+ }
32
+ function getFlintName(flintPath) {
33
+ const folderName = basename(flintPath);
34
+ const match = folderName.match(/^\((Mesh|Flint)\)\s+(.+)$/);
35
+ if (match && match[2]) {
36
+ return match[2];
37
+ }
38
+ return folderName;
39
+ }
40
+ function resolveShardMode(decl) {
41
+ return decl.mode;
42
+ }
43
+ function isLocalShard(decl) {
44
+ const mode = resolveShardMode(decl);
45
+ return mode === "dev" || mode === "custom";
46
+ }
47
+ var FLINT_CONFIG_FILENAME = "flint.toml";
48
+ var FLINT_JSON_FILENAME = "flint.json";
49
+ var FLINT_VERSION = "0.4.0";
50
+ function toKebabCase(name) {
51
+ return name.replace(/\([^)]*\)\s*/g, "").toLowerCase().replace(/\s+/g, "-").replace(/[^a-z0-9-]/g, "").replace(/^-+|-+$/g, "");
52
+ }
53
+ function toSnakeCase(name) {
54
+ return name.replace(/\([^)]*\)\s*/g, "").toLowerCase().replace(/\s+/g, "_").replace(/[^a-z0-9_]/g, "").replace(/^_+|_+$/g, "");
55
+ }
56
+ function nameFormats(name) {
57
+ return {
58
+ proper: name,
59
+ slug: toKebabCase(name),
60
+ snake: toSnakeCase(name)
61
+ };
62
+ }
63
+ function getFlintConfigPath(flintPath) {
64
+ return join2(flintPath, FLINT_CONFIG_FILENAME);
65
+ }
66
+ function getFlintJsonPath(flintPath) {
67
+ return join2(flintPath, FLINT_JSON_FILENAME);
68
+ }
69
+ async function exists(path) {
70
+ try {
71
+ await stat2(path);
72
+ return true;
73
+ } catch {
74
+ return false;
75
+ }
76
+ }
77
+ function createFlintToml(name, shards = []) {
78
+ const shardDeclarations = {};
79
+ for (const source of shards) {
80
+ const repo = source.includes("/") ? source.split("/").pop() : source;
81
+ const key = repo.replace(/^shard-/, "");
82
+ shardDeclarations[key] = { source };
83
+ }
84
+ const config = {
85
+ flint: {
86
+ name
87
+ },
88
+ shards: shardDeclarations
89
+ };
90
+ return config;
91
+ }
92
+ function createFlintJson() {
93
+ return {
94
+ version: FLINT_VERSION,
95
+ id: randomUUID(),
96
+ type: "flint",
97
+ created: (/* @__PURE__ */ new Date()).toISOString()
98
+ };
99
+ }
100
+ async function readFlintJson(flintPath) {
101
+ const jsonPath = getFlintJsonPath(flintPath);
102
+ try {
103
+ const content = await readFile(jsonPath, "utf-8");
104
+ return JSON.parse(content);
105
+ } catch (error) {
106
+ if (error.code === "ENOENT") {
107
+ return null;
108
+ }
109
+ throw error;
110
+ }
111
+ }
112
+ async function writeFlintJson(flintPath, json) {
113
+ const jsonPath = getFlintJsonPath(flintPath);
114
+ await writeFile(jsonPath, JSON.stringify(json, null, 2) + "\n");
115
+ }
116
+ async function hasFlintJson(flintPath) {
117
+ return exists(getFlintJsonPath(flintPath));
118
+ }
119
+ async function recordMigration(flintPath, migrationId) {
120
+ let json = await readFlintJson(flintPath);
121
+ if (!json) {
122
+ json = createFlintJson();
123
+ }
124
+ if (!json.migrations) {
125
+ json.migrations = {};
126
+ }
127
+ json.migrations[migrationId] = (/* @__PURE__ */ new Date()).toISOString();
128
+ await writeFlintJson(flintPath, json);
129
+ }
130
+ async function hasMigration(flintPath, migrationId) {
131
+ const json = await readFlintJson(flintPath);
132
+ return !!json?.migrations?.[migrationId];
133
+ }
134
+ async function getFlintJsonVersion(flintPath) {
135
+ const json = await readFlintJson(flintPath);
136
+ return json?.version ?? null;
137
+ }
138
+ async function stampVersion(flintPath) {
139
+ let json = await readFlintJson(flintPath);
140
+ if (!json) {
141
+ json = createFlintJson();
142
+ }
143
+ json.version = FLINT_VERSION;
144
+ if (!json.id) json.id = randomUUID();
145
+ if (!json.type) json.type = "flint";
146
+ if (!json.created) json.created = (/* @__PURE__ */ new Date()).toISOString();
147
+ await writeFlintJson(flintPath, json);
148
+ }
149
+ async function stampSynced(flintPath) {
150
+ const syncPath = join2(getFlintConfigDir(flintPath), "sync.json");
151
+ let data = {};
152
+ try {
153
+ data = JSON.parse(await readFile(syncPath, "utf-8"));
154
+ } catch {
155
+ }
156
+ data.synced = (/* @__PURE__ */ new Date()).toISOString();
157
+ await writeFile(syncPath, JSON.stringify(data, null, 2) + "\n", "utf-8");
158
+ }
159
+ async function readFlintToml(flintPath) {
160
+ const configPath = getFlintConfigPath(flintPath);
161
+ try {
162
+ const content = await readFile(configPath, "utf-8");
163
+ return parse(content);
164
+ } catch (error) {
165
+ if (error.code === "ENOENT") {
166
+ return null;
167
+ }
168
+ throw error;
169
+ }
170
+ }
171
+ function tv(v) {
172
+ if (typeof v === "string") return `"${v}"`;
173
+ if (typeof v === "boolean") return v ? "true" : "false";
174
+ if (typeof v === "number") return String(v);
175
+ if (Array.isArray(v)) return `[${v.map(tv).join(", ")}]`;
176
+ return String(v);
177
+ }
178
+ function inlineTable(obj) {
179
+ const parts = Object.entries(obj).filter(([, v]) => v !== void 0 && v !== null).map(([k, v]) => `${k} = ${tv(v)}`);
180
+ return `{ ${parts.join(", ")} }`;
181
+ }
182
+ function inlineTableArray(items) {
183
+ return [
184
+ "[",
185
+ ...items.map((item) => ` ${inlineTable(item)},`),
186
+ "]"
187
+ ];
188
+ }
189
+ function stringArray(items) {
190
+ return [
191
+ "[",
192
+ ...items.map((s) => ` "${s}",`),
193
+ "]"
194
+ ];
195
+ }
196
+ function formatFlintToml(config) {
197
+ const lines = [];
198
+ lines.push("[flint]");
199
+ lines.push(`name = ${tv(config.flint.name)}`);
200
+ if (config.flint.type) lines.push(`type = ${tv(config.flint.type)}`);
201
+ if (config.flint.description) lines.push(`description = ${tv(config.flint.description)}`);
202
+ if (config.flint.tags?.length) lines.push(`tags = ${tv(config.flint.tags)}`);
203
+ if (config.flint.org) lines.push(`org = ${tv(config.flint.org)}`);
204
+ lines.push("");
205
+ if (config.shards) {
206
+ const declarations = getShardDeclarationsFromConfig(config);
207
+ if (Object.keys(declarations).length > 0) {
208
+ lines.push("[shards]");
209
+ for (const [shorthand, decl] of Object.entries(declarations)) {
210
+ const mode = resolveShardMode(decl);
211
+ const fields = { source: decl.source };
212
+ if (decl.version) fields.version = decl.version;
213
+ if (mode) fields.mode = mode;
214
+ lines.push(`${shorthand} = ${inlineTable(fields)}`);
215
+ }
216
+ lines.push("");
217
+ }
218
+ }
219
+ if (config.exports?.required?.length) {
220
+ lines.push("[exports]");
221
+ const items = config.exports.required.map((e) => {
222
+ const fields = { name: e.name, file: e.file, mode: e.mode };
223
+ if (e.depth !== void 0 && e.depth !== 1) fields.depth = e.depth;
224
+ return fields;
225
+ });
226
+ lines.push(`required = ${inlineTableArray(items).join("\n")}`);
227
+ lines.push("");
228
+ }
229
+ const hasSourceRepositories = Boolean(config.sources?.repositories?.length);
230
+ const hasMeshExportSources = Boolean(config.sources?.meshexports?.length);
231
+ if (hasSourceRepositories || hasMeshExportSources) {
232
+ lines.push("[sources]");
233
+ if (hasSourceRepositories) {
234
+ const items = config.sources.repositories.map((r) => ({ name: r.name, url: r.url }));
235
+ lines.push(`repositories = ${inlineTableArray(items).join("\n")}`);
236
+ }
237
+ if (hasMeshExportSources) {
238
+ lines.push(`meshexports = ${stringArray(config.sources.meshexports).join("\n")}`);
239
+ }
240
+ lines.push("");
241
+ }
242
+ if (config.workspace?.repositories?.length) {
243
+ lines.push("[workspace]");
244
+ const items = config.workspace.repositories.map((r) => ({ name: r.name, url: r.url }));
245
+ lines.push(`repositories = ${inlineTableArray(items).join("\n")}`);
246
+ lines.push("");
247
+ }
248
+ {
249
+ const plateDecls = getPlateDeclarationsFromConfig(config);
250
+ if (Object.keys(plateDecls).length > 0) {
251
+ lines.push("[plates]");
252
+ for (const [name, decl] of Object.entries(plateDecls)) {
253
+ const fields = {};
254
+ if (decl.title) fields.title = decl.title;
255
+ if (decl.source) fields.source = decl.source;
256
+ if (decl.version) fields.version = decl.version;
257
+ if (decl.path) fields.path = decl.path;
258
+ if (decl.repo) fields.repo = decl.repo;
259
+ lines.push(`${name} = ${inlineTable(fields)}`);
260
+ }
261
+ lines.push("");
262
+ }
263
+ }
264
+ const hasRefCodebases = config.references?.codebases?.length;
265
+ const hasRefFlints = config.references?.flints?.length;
266
+ if (hasRefCodebases || hasRefFlints) {
267
+ lines.push("[references]");
268
+ if (hasRefCodebases) {
269
+ const items = config.references.codebases.map((c) => ({ name: c.name }));
270
+ lines.push(`codebases = ${inlineTableArray(items).join("\n")}`);
271
+ }
272
+ if (hasRefFlints) {
273
+ const items = config.references.flints.map((f) => ({ name: f.name }));
274
+ lines.push(`flints = ${inlineTableArray(items).join("\n")}`);
275
+ }
276
+ lines.push("");
277
+ }
278
+ if (config.lattices?.references?.length) {
279
+ lines.push("[lattices]");
280
+ const items = config.lattices.references.map((l) => ({ name: l.name }));
281
+ lines.push(`references = ${inlineTableArray(items).join("\n")}`);
282
+ lines.push("");
283
+ }
284
+ const knownKeys = /* @__PURE__ */ new Set(["flint", "shards", "exports", "imports", "sources", "workspace", "plates", "connections", "references", "lattices"]);
285
+ const unknownKeys = Object.keys(config).filter((k) => !knownKeys.has(k));
286
+ if (unknownKeys.length > 0) {
287
+ const unknownConfig = {};
288
+ for (const key of unknownKeys) {
289
+ unknownConfig[key] = config[key];
290
+ }
291
+ lines.push(stringify(unknownConfig));
292
+ lines.push("");
293
+ }
294
+ return lines.join("\n");
295
+ }
296
+ async function writeFlintToml(flintPath, config) {
297
+ const configPath = getFlintConfigPath(flintPath);
298
+ const content = formatFlintToml(config);
299
+ await writeFile(configPath, content);
300
+ }
301
+ async function addShardToConfig(flintPath, shardName, source, options = {}) {
302
+ let config = await readFlintToml(flintPath);
303
+ if (!config) {
304
+ throw new Error("flint.toml not found");
305
+ }
306
+ const kebabName = toKebabCase(shardName);
307
+ if (!config.shards) {
308
+ config.shards = {};
309
+ }
310
+ if (config.shards.required && Array.isArray(config.shards.required)) {
311
+ await migrateShardConfig(flintPath);
312
+ config = await readFlintToml(flintPath);
313
+ }
314
+ const declarations = getShardDeclarationsFromConfig(config);
315
+ const existing = declarations[kebabName];
316
+ const effectiveSource = source || kebabName;
317
+ if (!existing || existing.source !== effectiveSource || options.version !== existing?.version) {
318
+ const decl = { source: effectiveSource };
319
+ if (options.version) decl.version = options.version;
320
+ if (options.mode) decl.mode = options.mode;
321
+ config.shards[kebabName] = decl;
322
+ await writeFlintToml(flintPath, config);
323
+ }
324
+ }
325
+ async function removeShardFromConfig(flintPath, shardName) {
326
+ let config = await readFlintToml(flintPath);
327
+ if (!config?.shards) return;
328
+ const kebabName = toKebabCase(shardName);
329
+ if (config.shards.required && Array.isArray(config.shards.required)) {
330
+ config.shards.required = config.shards.required.filter((n) => n !== kebabName);
331
+ }
332
+ if (kebabName in config.shards) {
333
+ delete config.shards[kebabName];
334
+ }
335
+ await writeFlintToml(flintPath, config);
336
+ }
337
+ async function getRequiredShardNames(flintPath) {
338
+ const config = await readFlintToml(flintPath);
339
+ if (!config?.shards) return [];
340
+ const declarations = getShardDeclarationsFromConfig(config);
341
+ return Object.keys(declarations);
342
+ }
343
+ function getShardDeclarationsFromConfig(config) {
344
+ if (!config.shards) return {};
345
+ const declarations = {};
346
+ for (const [key, value] of Object.entries(config.shards)) {
347
+ if (key === "required") continue;
348
+ if (value && typeof value === "object" && !Array.isArray(value) && "source" in value) {
349
+ declarations[key] = value;
350
+ }
351
+ }
352
+ if (config.shards.required && Array.isArray(config.shards.required)) {
353
+ for (const name of config.shards.required) {
354
+ if (!declarations[name]) {
355
+ declarations[name] = { source: name };
356
+ }
357
+ }
358
+ }
359
+ return declarations;
360
+ }
361
+ async function getShardDeclarations(flintPath) {
362
+ const config = await readFlintToml(flintPath);
363
+ if (!config) return {};
364
+ return getShardDeclarationsFromConfig(config);
365
+ }
366
+ async function migrateShardConfig(flintPath) {
367
+ const config = await readFlintToml(flintPath);
368
+ if (!config?.shards?.required || !Array.isArray(config.shards.required)) {
369
+ return false;
370
+ }
371
+ const hasNewFormat = Object.keys(config.shards).some(
372
+ (k) => k !== "required" && typeof config.shards[k] === "object" && !Array.isArray(config.shards[k])
373
+ );
374
+ if (hasNewFormat && config.shards.required.length === 0) {
375
+ return false;
376
+ }
377
+ const declarations = {};
378
+ for (const name of config.shards.required) {
379
+ declarations[name] = { source: name };
380
+ }
381
+ for (const [key, value] of Object.entries(config.shards)) {
382
+ if (key === "required") continue;
383
+ if (value && typeof value === "object" && !Array.isArray(value) && "source" in value) {
384
+ declarations[key] = value;
385
+ }
386
+ }
387
+ config.shards = declarations;
388
+ await writeFlintToml(flintPath, config);
389
+ return true;
390
+ }
391
+ async function hasFlintToml(flintPath) {
392
+ return exists(getFlintConfigPath(flintPath));
393
+ }
394
+ async function isFlint(flintPath) {
395
+ return hasFlintToml(flintPath);
396
+ }
397
+ async function addWorkspaceRepository(flintPath, name, url) {
398
+ let config = await readFlintToml(flintPath);
399
+ if (!config) {
400
+ throw new Error("flint.toml not found");
401
+ }
402
+ if (!config.workspace) {
403
+ config.workspace = {};
404
+ }
405
+ if (!config.workspace.repositories) {
406
+ config.workspace.repositories = [];
407
+ }
408
+ const existing = config.workspace.repositories.find((r) => r.name.toLowerCase() === name.toLowerCase());
409
+ if (existing) {
410
+ throw new Error(`Repository "${name}" already exists`);
411
+ }
412
+ config.workspace.repositories.push({ name, url });
413
+ await writeFlintToml(flintPath, config);
414
+ }
415
+ async function removeWorkspaceRepository(flintPath, name) {
416
+ let config = await readFlintToml(flintPath);
417
+ if (!config?.workspace?.repositories) return false;
418
+ const normalizedName = name.toLowerCase();
419
+ const index = config.workspace.repositories.findIndex((r) => r.name.toLowerCase() === normalizedName);
420
+ if (index === -1) return false;
421
+ config.workspace.repositories.splice(index, 1);
422
+ await writeFlintToml(flintPath, config);
423
+ return true;
424
+ }
425
+ async function getWorkspaceRepositories(flintPath) {
426
+ const config = await readFlintToml(flintPath);
427
+ return config?.workspace?.repositories || [];
428
+ }
429
+ async function getWorkspaceRepository(flintPath, name) {
430
+ const config = await readFlintToml(flintPath);
431
+ const normalizedName = name.toLowerCase();
432
+ return config?.workspace?.repositories?.find((r) => r.name.toLowerCase() === normalizedName) || null;
433
+ }
434
+ async function addPlateDeclaration(flintPath, name, platePath, options = {}) {
435
+ const config = await readFlintToml(flintPath);
436
+ if (!config) {
437
+ throw new Error("flint.toml not found");
438
+ }
439
+ if (!config.plates) {
440
+ config.plates = {};
441
+ }
442
+ const decl = { path: platePath };
443
+ if (options.title) decl.title = options.title;
444
+ config.plates[name] = decl;
445
+ await writeFlintToml(flintPath, config);
446
+ }
447
+ async function setPlateDeclaration(flintPath, name, declaration) {
448
+ const config = await readFlintToml(flintPath);
449
+ if (!config) {
450
+ throw new Error("flint.toml not found");
451
+ }
452
+ if (!config.plates) {
453
+ config.plates = {};
454
+ }
455
+ config.plates[name] = declaration;
456
+ await writeFlintToml(flintPath, config);
457
+ }
458
+ async function removePlateDeclaration(flintPath, name) {
459
+ const config = await readFlintToml(flintPath);
460
+ if (!config?.plates?.[name]) {
461
+ return false;
462
+ }
463
+ delete config.plates[name];
464
+ await writeFlintToml(flintPath, config);
465
+ return true;
466
+ }
467
+ async function getPlateDeclarations(flintPath) {
468
+ const config = await readFlintToml(flintPath);
469
+ return getPlateDeclarationsFromConfig(config);
470
+ }
471
+ function getPlateDeclarationsFromConfig(config) {
472
+ if (!config?.plates) return {};
473
+ const declarations = {};
474
+ for (const [key, value] of Object.entries(config.plates)) {
475
+ if (value && typeof value === "object" && ("path" in value || "source" in value)) {
476
+ declarations[key] = value;
477
+ }
478
+ }
479
+ return declarations;
480
+ }
481
+ async function setPlateRepo(flintPath, name, url) {
482
+ const config = await readFlintToml(flintPath);
483
+ if (!config) {
484
+ throw new Error("flint.toml not found");
485
+ }
486
+ if (!config.plates) {
487
+ config.plates = {};
488
+ }
489
+ const existing = config.plates[name];
490
+ if (!existing) {
491
+ throw new Error(`Plate "${name}" not found in flint.toml`);
492
+ }
493
+ config.plates[name] = { ...existing, repo: url };
494
+ await writeFlintToml(flintPath, config);
495
+ }
496
+ async function getPlateDeclaration(flintPath, name) {
497
+ const declarations = await getPlateDeclarations(flintPath);
498
+ return declarations[name] ?? null;
499
+ }
500
+ async function addSourceRepository(flintPath, name, url) {
501
+ const config = await readFlintToml(flintPath);
502
+ if (!config) {
503
+ throw new Error("flint.toml not found");
504
+ }
505
+ if (!config.sources) {
506
+ config.sources = {};
507
+ }
508
+ if (!config.sources.repositories) {
509
+ config.sources.repositories = [];
510
+ }
511
+ const existing = config.sources.repositories.find((r) => r.name.toLowerCase() === name.toLowerCase());
512
+ if (existing) {
513
+ throw new Error(`Source repository "${name}" already exists`);
514
+ }
515
+ config.sources.repositories.push({ name, url });
516
+ await writeFlintToml(flintPath, config);
517
+ }
518
+ async function removeSourceRepository(flintPath, name) {
519
+ const config = await readFlintToml(flintPath);
520
+ if (!config?.sources?.repositories) return false;
521
+ const normalizedName = name.toLowerCase();
522
+ const index = config.sources.repositories.findIndex((r) => r.name.toLowerCase() === normalizedName);
523
+ if (index === -1) return false;
524
+ config.sources.repositories.splice(index, 1);
525
+ await writeFlintToml(flintPath, config);
526
+ return true;
527
+ }
528
+ async function getSourceRepositories(flintPath) {
529
+ const config = await readFlintToml(flintPath);
530
+ return config?.sources?.repositories || [];
531
+ }
532
+ async function getSourceRepository(flintPath, name) {
533
+ const config = await readFlintToml(flintPath);
534
+ const normalizedName = name.toLowerCase();
535
+ return config?.sources?.repositories?.find((r) => r.name.toLowerCase() === normalizedName) || null;
536
+ }
537
+ function getLatticesStatePath(flintPath) {
538
+ return join2(flintPath, ".flint", "lattices.json");
539
+ }
540
+ async function readLatticesState(flintPath) {
541
+ const statePath = getLatticesStatePath(flintPath);
542
+ try {
543
+ const content = await readFile(statePath, "utf-8");
544
+ return JSON.parse(content);
545
+ } catch {
546
+ return { version: 1, lattices: [] };
547
+ }
548
+ }
549
+ async function writeLatticesState(flintPath, state) {
550
+ const statePath = getLatticesStatePath(flintPath);
551
+ const dir = join2(flintPath, ".flint");
552
+ await mkdir(dir, { recursive: true });
553
+ await writeFile(statePath, JSON.stringify(state, null, 2) + "\n");
554
+ }
555
+ async function addLatticeDeclaration(flintPath, name) {
556
+ let config = await readFlintToml(flintPath);
557
+ if (!config) {
558
+ throw new Error("flint.toml not found");
559
+ }
560
+ if (!config.lattices) {
561
+ config.lattices = { references: [] };
562
+ }
563
+ if (!config.lattices.references) {
564
+ config.lattices.references = [];
565
+ }
566
+ const existing = config.lattices.references.find(
567
+ (entry) => entry.name.toLowerCase() === name.toLowerCase()
568
+ );
569
+ if (existing) {
570
+ throw new Error(`Lattice "${name}" is already declared`);
571
+ }
572
+ config.lattices.references.push({ name });
573
+ await writeFlintToml(flintPath, config);
574
+ }
575
+ async function removeLatticeDeclaration(flintPath, name) {
576
+ let config = await readFlintToml(flintPath);
577
+ if (!config?.lattices?.references) return false;
578
+ const normalizedName = name.toLowerCase();
579
+ const index = config.lattices.references.findIndex(
580
+ (entry) => entry.name.toLowerCase() === normalizedName
581
+ );
582
+ if (index === -1) return false;
583
+ config.lattices.references.splice(index, 1);
584
+ await writeFlintToml(flintPath, config);
585
+ const state = await readLatticesState(flintPath);
586
+ state.lattices = state.lattices.filter(
587
+ (l) => l.name.toLowerCase() !== normalizedName
588
+ );
589
+ await writeLatticesState(flintPath, state);
590
+ return true;
591
+ }
592
+ async function fulfillLattice(flintPath, name, latticePath, lang) {
593
+ const fulfillment = {
594
+ name,
595
+ path: latticePath,
596
+ lang,
597
+ fulfilled: (/* @__PURE__ */ new Date()).toISOString()
598
+ };
599
+ const state = await readLatticesState(flintPath);
600
+ const existingIndex = state.lattices.findIndex(
601
+ (l) => l.name.toLowerCase() === name.toLowerCase()
602
+ );
603
+ if (existingIndex >= 0) {
604
+ state.lattices[existingIndex] = fulfillment;
605
+ } else {
606
+ state.lattices.push(fulfillment);
607
+ }
608
+ await writeLatticesState(flintPath, state);
609
+ return fulfillment;
610
+ }
611
+ async function getLatticeDeclarations(flintPath) {
612
+ const config = await readFlintToml(flintPath);
613
+ return config?.lattices?.references || [];
614
+ }
615
+ async function getLatticeDeclaration(flintPath, name) {
616
+ const config = await readFlintToml(flintPath);
617
+ const normalizedName = name.toLowerCase();
618
+ return config?.lattices?.references?.find(
619
+ (entry) => entry.name.toLowerCase() === normalizedName
620
+ ) || null;
621
+ }
622
+ async function getLatticeFulfillment(flintPath, name) {
623
+ const state = await readLatticesState(flintPath);
624
+ return state.lattices.find(
625
+ (l) => l.name.toLowerCase() === name.toLowerCase()
626
+ ) || null;
627
+ }
628
+ async function addExportToConfig(flintPath, declaration) {
629
+ let config = await readFlintToml(flintPath);
630
+ if (!config) {
631
+ throw new Error("flint.toml not found");
632
+ }
633
+ if (!config.exports) {
634
+ config.exports = { required: [] };
635
+ }
636
+ if (!config.exports.required) {
637
+ config.exports.required = [];
638
+ }
639
+ const existing = config.exports.required.find(
640
+ (e) => e.name.toLowerCase() === declaration.name.toLowerCase()
641
+ );
642
+ if (existing) {
643
+ throw new Error(`Export "${declaration.name}" already declared`);
644
+ }
645
+ config.exports.required.push({
646
+ name: declaration.name,
647
+ file: declaration.file,
648
+ mode: declaration.mode,
649
+ ...declaration.depth !== void 0 ? { depth: declaration.depth } : {}
650
+ });
651
+ await writeFlintToml(flintPath, config);
652
+ }
653
+ async function removeExportFromConfig(flintPath, exportName) {
654
+ let config = await readFlintToml(flintPath);
655
+ if (!config?.exports?.required) return false;
656
+ const index = config.exports.required.findIndex(
657
+ (e) => e.name.toLowerCase() === exportName.toLowerCase()
658
+ );
659
+ if (index === -1) return false;
660
+ config.exports.required.splice(index, 1);
661
+ await writeFlintToml(flintPath, config);
662
+ return true;
663
+ }
664
+ async function getExportDeclarations(flintPath) {
665
+ const config = await readFlintToml(flintPath);
666
+ return config?.exports?.required || [];
667
+ }
668
+ async function getMeshExportSources(flintPath) {
669
+ const config = await readFlintToml(flintPath);
670
+ return config?.sources?.meshexports || [];
671
+ }
672
+ async function addMeshExportToConfig(flintPath, ref) {
673
+ const config = await readFlintToml(flintPath);
674
+ if (!config) {
675
+ throw new Error("flint.toml not found");
676
+ }
677
+ if (!config.sources) {
678
+ config.sources = {};
679
+ }
680
+ if (!config.sources.meshexports) {
681
+ config.sources.meshexports = [];
682
+ }
683
+ if (!config.sources.meshexports.includes(ref)) {
684
+ config.sources.meshexports.push(ref);
685
+ await writeFlintToml(flintPath, config);
686
+ }
687
+ }
688
+ async function removeMeshExportFromConfig(flintPath, ref) {
689
+ const config = await readFlintToml(flintPath);
690
+ if (!config?.sources?.meshexports) return false;
691
+ const meshexports = config.sources.meshexports;
692
+ const index = meshexports.indexOf(ref);
693
+ if (index === -1) {
694
+ const lowerRef = ref.toLowerCase();
695
+ const foundIndex = meshexports.findIndex((r) => r.toLowerCase() === lowerRef);
696
+ if (foundIndex === -1) return false;
697
+ meshexports.splice(foundIndex, 1);
698
+ } else {
699
+ meshexports.splice(index, 1);
700
+ }
701
+ await writeFlintToml(flintPath, config);
702
+ return true;
703
+ }
704
+
705
+ export {
706
+ getFlintConfigDir,
707
+ isInsideMesh,
708
+ getTypePrefix,
709
+ getFlintName,
710
+ resolveShardMode,
711
+ isLocalShard,
712
+ FLINT_CONFIG_FILENAME,
713
+ FLINT_JSON_FILENAME,
714
+ FLINT_VERSION,
715
+ toKebabCase,
716
+ toSnakeCase,
717
+ nameFormats,
718
+ getFlintConfigPath,
719
+ getFlintJsonPath,
720
+ createFlintToml,
721
+ createFlintJson,
722
+ readFlintJson,
723
+ writeFlintJson,
724
+ hasFlintJson,
725
+ recordMigration,
726
+ hasMigration,
727
+ getFlintJsonVersion,
728
+ stampVersion,
729
+ stampSynced,
730
+ readFlintToml,
731
+ writeFlintToml,
732
+ addShardToConfig,
733
+ removeShardFromConfig,
734
+ getRequiredShardNames,
735
+ getShardDeclarationsFromConfig,
736
+ getShardDeclarations,
737
+ migrateShardConfig,
738
+ hasFlintToml,
739
+ isFlint,
740
+ addWorkspaceRepository,
741
+ removeWorkspaceRepository,
742
+ getWorkspaceRepositories,
743
+ getWorkspaceRepository,
744
+ addPlateDeclaration,
745
+ setPlateDeclaration,
746
+ removePlateDeclaration,
747
+ getPlateDeclarations,
748
+ getPlateDeclarationsFromConfig,
749
+ setPlateRepo,
750
+ getPlateDeclaration,
751
+ addSourceRepository,
752
+ removeSourceRepository,
753
+ getSourceRepositories,
754
+ getSourceRepository,
755
+ readLatticesState,
756
+ addLatticeDeclaration,
757
+ removeLatticeDeclaration,
758
+ fulfillLattice,
759
+ getLatticeDeclarations,
760
+ getLatticeDeclaration,
761
+ getLatticeFulfillment,
762
+ addExportToConfig,
763
+ removeExportFromConfig,
764
+ getExportDeclarations,
765
+ getMeshExportSources,
766
+ addMeshExportToConfig,
767
+ removeMeshExportFromConfig
768
+ };