@mono-lit/utility 0.0.0-stage → 0.0.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.
Files changed (54) hide show
  1. package/LICENSE.md +54 -0
  2. package/README.md +157 -3
  3. package/dist/app-roots-CvJnmXcs.js +128 -0
  4. package/dist/app-roots-DK41xMWj.d.ts +90 -0
  5. package/dist/config-node-UzJrXg0Z.js +76 -0
  6. package/dist/config-node.d.ts +923 -0
  7. package/dist/config-node.js +7 -0
  8. package/dist/config.d.ts +2 -0
  9. package/dist/config.js +3 -0
  10. package/dist/create-config-B8IhX4ht.d.ts +735 -0
  11. package/dist/create-config-DdL3Fh6T.js +357 -0
  12. package/dist/fetching.d.ts +244 -0
  13. package/dist/fetching.js +1015 -0
  14. package/dist/generate-CJ7MtcDd.js +291 -0
  15. package/dist/index-BAy6tbOG.d.ts +371 -0
  16. package/dist/index-DYlzhHq7.d.ts +514 -0
  17. package/dist/index.d.ts +25 -0
  18. package/dist/index.js +941 -0
  19. package/dist/merge-file-CLc3wxY4.d.ts +99 -0
  20. package/dist/merge-file-CtyKTu5L.js +133 -0
  21. package/dist/mock-db-4tTWrF-P.js +1282 -0
  22. package/dist/mock-db.d.ts +2 -0
  23. package/dist/mock-db.js +4 -0
  24. package/dist/mono-alias-DNDm-jB_.js +508 -0
  25. package/dist/mono-clone.mjs +958 -0
  26. package/dist/mono-db.d.ts +1 -0
  27. package/dist/mono-db.js +158 -0
  28. package/dist/mono-env.mjs +231 -0
  29. package/dist/mono-git.mjs +798 -0
  30. package/dist/mono-link-YlOQ2PVS.js +488 -0
  31. package/dist/mono-prepare.d.ts +1 -0
  32. package/dist/mono-prepare.js +28 -0
  33. package/dist/mono-skills.d.ts +1 -0
  34. package/dist/mono-skills.js +837 -0
  35. package/dist/mono-tsconfig-D2qRL9_R.js +204 -0
  36. package/dist/mono.mjs +34 -0
  37. package/dist/nuxt-layers-DP1m7f1-.js +176 -0
  38. package/dist/nuxt-layers.d.ts +36 -0
  39. package/dist/nuxt-layers.js +3 -0
  40. package/dist/nuxt-runtime.d.ts +17 -0
  41. package/dist/nuxt-runtime.js +102 -0
  42. package/dist/nuxt.d.ts +390 -0
  43. package/dist/nuxt.js +737 -0
  44. package/dist/runtime-vs7U9Ed4.js +6054 -0
  45. package/dist/runtime.d.ts +2518 -0
  46. package/dist/runtime.js +4 -0
  47. package/dist/session-Bcig-rPi.js +1721 -0
  48. package/dist/types-B6FdB7LJ.d.ts +57 -0
  49. package/dist/universal-BR0O7Qwk.d.ts +59 -0
  50. package/dist/universal-DIVEzCvN.js +600 -0
  51. package/dist/virtual-mono-apps.d.ts +16 -0
  52. package/dist/vite.d.ts +943 -0
  53. package/dist/vite.js +1132 -0
  54. package/package.json +141 -3
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,158 @@
1
+ import { c as resolveMonoConfig } from "./create-config-DdL3Fh6T.js";
2
+ import { t as getMonoConfig } from "./config-node-UzJrXg0Z.js";
3
+ import { s as monoAlias } from "./mono-alias-DNDm-jB_.js";
4
+ import { n as generateSeed, s as parseMockSchema } from "./generate-CJ7MtcDd.js";
5
+ import { writeFileSync } from "node:fs";
6
+ import path from "node:path";
7
+ import { defineCommand, runMain } from "citty";
8
+
9
+ //#region pkg/cli/db/index.ts
10
+ /**
11
+ * The merged config, including `extends`.
12
+ *
13
+ * Two things are load-bearing here:
14
+ *
15
+ * 1. jiti (inside c12) knows nothing about the `@mono-host` / `@<app>` aliases, and
16
+ * every real mono.config.ts imports through them (`@mono-host/odata/...`, and
17
+ * `@mono-host-root/mono.config` for `extends`). So teach it the same alias map
18
+ * Vite and the Nuxt module use — otherwise the config fails to load outright.
19
+ * 2. `getMonoConfig` runs c12 with `extend: false` (mono's `extends` holds thunks,
20
+ * which c12 can't resolve), so it returns the config with `extends` still raw.
21
+ * `resolveMonoConfig` is what merges the layers — which is how several apps'
22
+ * schemas end up in one place.
23
+ */
24
+ async function loadMergedConfig(cwd) {
25
+ const dirname = path.resolve(cwd ?? process.cwd());
26
+ const alias = monoAlias({ dirname });
27
+ const raw = await getMonoConfig({
28
+ cwd: dirname,
29
+ jitiOptions: { alias }
30
+ });
31
+ return resolveMonoConfig(raw);
32
+ }
33
+ function requireMock(config) {
34
+ const mock = config?.mockIndexedDB;
35
+ if (!mock?.schema || !Object.keys(mock.schema).length) {
36
+ console.error("[mono db] no `mockIndexedDB.schema` in the merged mono config.\n Declare one in mono.config.ts, or in a synced app that this config extends.");
37
+ process.exit(1);
38
+ }
39
+ return mock;
40
+ }
41
+ const validateCommand = defineCommand({
42
+ meta: {
43
+ name: "validate",
44
+ description: "Parse every mock schema and report bad fields/relations."
45
+ },
46
+ args: { cwd: {
47
+ type: "string",
48
+ description: "App root (defaults to the current directory)."
49
+ } },
50
+ async run({ args }) {
51
+ const mock = requireMock(await loadMergedConfig(args.cwd));
52
+ const { schemas, errors } = parseMockSchema(mock);
53
+ for (const schema of schemas) {
54
+ const entities = Object.values(schema.entities);
55
+ console.log(`\n ${schema.baseUrl} (${entities.length} entit${entities.length === 1 ? "y" : "ies"})`);
56
+ for (const entity of entities) {
57
+ const relations = entity.relations.map((f) => f.name);
58
+ console.log(` - ${entity.name} pk=${entity.primaryKey} columns=${entity.columns.length}` + (relations.length ? ` expand=[${relations.join(", ")}]` : "") + (entity.seed ? ` seed=${entity.seed.length} row(s)` : ""));
59
+ }
60
+ }
61
+ if (errors.length) {
62
+ console.error(`\n ${errors.length} error(s):`);
63
+ for (const error of errors) console.error(` x ${error.baseUrl} -> ${error.entity}${error.field ? `.${error.field}` : ""}: ${error.message}`);
64
+ process.exit(1);
65
+ }
66
+ console.log("\n schema OK\n");
67
+ }
68
+ });
69
+ const previewCommand = defineCommand({
70
+ meta: {
71
+ name: "preview",
72
+ description: "Print the rows the generator would produce."
73
+ },
74
+ args: {
75
+ cwd: {
76
+ type: "string",
77
+ description: "App root (defaults to the current directory)."
78
+ },
79
+ entity: {
80
+ type: "string",
81
+ description: "Only this entity."
82
+ },
83
+ count: {
84
+ type: "string",
85
+ description: "Rows per entity (overrides seedCount)."
86
+ }
87
+ },
88
+ async run({ args }) {
89
+ const mock = requireMock(await loadMergedConfig(args.cwd));
90
+ const { schemas, errors } = parseMockSchema(mock);
91
+ if (errors.length) {
92
+ console.error("[mono db] schema has errors — run `mono db validate` first.");
93
+ process.exit(1);
94
+ }
95
+ for (const schema of schemas) {
96
+ const seed = generateSeed(schema, {
97
+ count: args.count ? Number(args.count) : mock.seedCount,
98
+ random: mock.seedRandom
99
+ });
100
+ for (const [entityName, rows] of Object.entries(seed)) {
101
+ if (args.entity && args.entity !== entityName) continue;
102
+ console.log(`\n ${schema.baseUrl} / ${entityName} (${rows.length} rows)`);
103
+ console.log(JSON.stringify(rows, null, 2));
104
+ }
105
+ }
106
+ }
107
+ });
108
+ const exportCommand = defineCommand({
109
+ meta: {
110
+ name: "export",
111
+ description: "Write generated rows to a JSON file (to promote them into an explicit `seed`)."
112
+ },
113
+ args: {
114
+ cwd: {
115
+ type: "string",
116
+ description: "App root (defaults to the current directory)."
117
+ },
118
+ out: {
119
+ type: "string",
120
+ description: "Output file.",
121
+ default: "mock-seed.json"
122
+ },
123
+ count: {
124
+ type: "string",
125
+ description: "Rows per entity (overrides seedCount)."
126
+ }
127
+ },
128
+ async run({ args }) {
129
+ const mock = requireMock(await loadMergedConfig(args.cwd));
130
+ const { schemas, errors } = parseMockSchema(mock);
131
+ if (errors.length) {
132
+ console.error("[mono db] schema has errors — run `mono db validate` first.");
133
+ process.exit(1);
134
+ }
135
+ const out = {};
136
+ for (const schema of schemas) out[schema.baseUrl] = generateSeed(schema, {
137
+ count: args.count ? Number(args.count) : mock.seedCount,
138
+ random: mock.seedRandom
139
+ });
140
+ const file = path.resolve(process.cwd(), args.out);
141
+ writeFileSync(file, JSON.stringify(out, null, 2));
142
+ console.log(`[mono db] wrote ${file}`);
143
+ }
144
+ });
145
+ const main = defineCommand({
146
+ meta: {
147
+ name: "db",
148
+ description: "Mock IndexedDB backend: validate schemas and preview generated seed data. The store itself lives in the browser — use monoMockDb().reset()/.export() at runtime."
149
+ },
150
+ subCommands: {
151
+ validate: validateCommand,
152
+ preview: previewCommand,
153
+ export: exportCommand
154
+ }
155
+ });
156
+ runMain(main);
157
+
158
+ //#endregion
@@ -0,0 +1,231 @@
1
+ #!/usr/bin/env node
2
+ // bin/mono-env.mjs
3
+ import { existsSync, readdirSync, statSync } from 'node:fs'
4
+ import { spawnSync } from 'node:child_process'
5
+ import { resolve, isAbsolute, basename, relative, dirname } from 'node:path'
6
+ import { fileURLToPath, pathToFileURL } from 'node:url'
7
+ import dotenv from 'dotenv'
8
+
9
+ const args = process.argv.slice(2)
10
+
11
+ const envFiles = []
12
+ const commandArgs = []
13
+
14
+ let appName = null
15
+ let appRoot = '.mono/apps'
16
+
17
+ function resolvePath(file) {
18
+ return isAbsolute(file) ? file : resolve(process.cwd(), file)
19
+ }
20
+
21
+ for (let i = 0; i < args.length; i++) {
22
+ const arg = args[i]
23
+
24
+ if (arg === '--') {
25
+ commandArgs.push(...args.slice(i + 1))
26
+ break
27
+ }
28
+
29
+ if (arg === '-e' || arg === '--env') {
30
+ const file = args[i + 1]
31
+
32
+ if (!file) {
33
+ console.error('[mono-env] Missing value after -e / --env')
34
+ process.exit(1)
35
+ }
36
+
37
+ envFiles.push(file)
38
+ i++
39
+ continue
40
+ }
41
+
42
+ if (arg === '-a' || arg === '--app') {
43
+ const name = args[i + 1]
44
+
45
+ if (!name) {
46
+ console.error('[mono-env] Missing value after -a / --app')
47
+ process.exit(1)
48
+ }
49
+
50
+ appName = name
51
+ i++
52
+ continue
53
+ }
54
+
55
+ if (arg === '-r' || arg === '--app-root') {
56
+ const root = args[i + 1]
57
+
58
+ if (!root) {
59
+ console.error('[mono-env] Missing value after -r / --app-root')
60
+ process.exit(1)
61
+ }
62
+
63
+ appRoot = root
64
+ i++
65
+ continue
66
+ }
67
+
68
+ /**
69
+ * Supports:
70
+ * node mono-env.mjs -e .env.dev vite
71
+ */
72
+ commandArgs.push(...args.slice(i))
73
+ break
74
+ }
75
+
76
+ if (!envFiles.length) {
77
+ console.warn('[mono-env] Warning: no env file provided. Example: -e .env.dev')
78
+ }
79
+
80
+ if (!commandArgs.length) {
81
+ console.error('[mono-env] Missing command.')
82
+ console.error('[mono-env] Example: node mono-env.mjs -e .env.dev -- vite')
83
+ process.exit(1)
84
+ }
85
+
86
+ const childEnv = {
87
+ ...process.env,
88
+ }
89
+
90
+ function loadEnvByPath(fullPath, displayPath, label) {
91
+ if (!existsSync(fullPath)) {
92
+ console.warn(`[mono-env] Warning: ${label} env file not found: ${displayPath}`)
93
+ return
94
+ }
95
+
96
+ const result = dotenv.config({
97
+ path: fullPath,
98
+ override: true,
99
+ processEnv: childEnv,
100
+ })
101
+
102
+ if (result.error) {
103
+ console.warn(`[mono-env] Warning: failed to load ${label} env: ${displayPath}`)
104
+ console.warn(result.error.message)
105
+ return
106
+ }
107
+
108
+ console.log(`[mono-env] Loaded ${label} env: ${displayPath}`)
109
+ }
110
+
111
+ /**
112
+ * Every federated app as `{ name, dir }`, the same answer every other command
113
+ * gives: the `.mono/apps` clones PLUS every app read in place through
114
+ * `apps[].path` (a mono-lith sibling), which a directory listing cannot see.
115
+ *
116
+ * The resolver lives in the built `config-node.js` next to this file in
117
+ * `dist/`. Run from the repo's `bin/` (not built) or without a mono.config, it
118
+ * falls back to listing the apps folder — exactly what this script always did.
119
+ */
120
+ async function getAppRoots() {
121
+ try {
122
+ const here = dirname(fileURLToPath(import.meta.url))
123
+ // A file URL, not a bare path: on Windows `import('C:\…')` is rejected as an unknown scheme.
124
+ const { resolveFederatedRoots } = await import(pathToFileURL(resolve(here, 'config-node.js')).href)
125
+ return resolveFederatedRoots({ dirname: process.cwd(), appsDir: appRoot })
126
+ .map((r) => ({ name: r.name, dir: r.root }))
127
+ .sort((a, b) => a.name.localeCompare(b.name))
128
+ } catch (e) {
129
+ // A declared `path` with no directory and no `url` is a config error every
130
+ // command reports; do not paper over it with the directory listing.
131
+ if (e instanceof Error && e.message.includes('does not exist and no url is declared')) {
132
+ console.error(`[mono-env] ${e.message}`)
133
+ process.exit(1)
134
+ }
135
+ return listAppsFolder()
136
+ }
137
+ }
138
+
139
+ function listAppsFolder() {
140
+ const fullAppRoot = resolve(process.cwd(), appRoot)
141
+
142
+ if (!existsSync(fullAppRoot)) {
143
+ console.warn(`[mono-env] Warning: app root folder not found: ${appRoot}`)
144
+ return []
145
+ }
146
+
147
+ return readdirSync(fullAppRoot)
148
+ .filter((name) => statSync(resolve(fullAppRoot, name)).isDirectory())
149
+ .sort()
150
+ .map((name) => ({ name, dir: resolve(fullAppRoot, name) }))
151
+ }
152
+
153
+ function loadRootEnv(file) {
154
+ const fullPath = resolvePath(file)
155
+ loadEnvByPath(fullPath, file, 'root')
156
+ }
157
+
158
+ function loadAppEnv(app) {
159
+ if (!existsSync(app.dir)) {
160
+ console.warn(`[mono-env] Warning: app folder not found: ${displayDir(app.dir)}`)
161
+ return
162
+ }
163
+
164
+ for (const file of envFiles) {
165
+ const envFileName = basename(file)
166
+ const appEnvPath = resolve(app.dir, envFileName)
167
+ const displayPath = `${displayDir(app.dir)}/${envFileName}`
168
+
169
+ loadEnvByPath(appEnvPath, displayPath, `app:${app.name}`)
170
+ }
171
+ }
172
+
173
+ function displayDir(dir) {
174
+ const rel = relative(process.cwd(), dir)
175
+ return (rel || dir).replace(/\\/g, '/')
176
+ }
177
+
178
+ /**
179
+ * 1. Load root env first.
180
+ *
181
+ * Example:
182
+ * .env.dev
183
+ */
184
+ for (const file of envFiles) {
185
+ loadRootEnv(file)
186
+ }
187
+
188
+ /**
189
+ * 2. Load app env second.
190
+ *
191
+ * Default behavior:
192
+ * - if -a sales is provided, only load that app (by name — a clone under
193
+ * .mono/apps or a sibling read through apps[].path)
194
+ * - if no -a is provided, load every federated app
195
+ */
196
+ const allApps = await getAppRoots()
197
+ const apps = appName
198
+ ? [allApps.find((a) => a.name === appName) ?? { name: appName, dir: resolve(process.cwd(), appRoot, appName) }]
199
+ : allApps
200
+ const appNames = apps.map((a) => a.name)
201
+
202
+ for (const app of apps) {
203
+ loadAppEnv(app)
204
+ }
205
+
206
+ if (apps.length === 1) {
207
+ childEnv.MONO_APP_NAME = apps[0].name
208
+ childEnv.MONO_APP_DIR = apps[0].dir
209
+ }
210
+
211
+ if (appNames.length > 1) {
212
+ childEnv.MONO_APP_NAMES = appNames.join(',')
213
+
214
+ console.warn(
215
+ '[mono-env] Warning: multiple app env files loaded. Duplicate keys will override each other. Use unique env names per app.',
216
+ )
217
+ }
218
+
219
+ const command = commandArgs.join(' ')
220
+
221
+ console.log(`[mono-env] Apps: ${appNames.length ? appNames.join(', ') : '-'}`)
222
+ console.log(`[mono-env] Command: ${command}`)
223
+
224
+ const result = spawnSync(command, {
225
+ stdio: 'inherit',
226
+ shell: true,
227
+ cwd: process.cwd(),
228
+ env: childEnv,
229
+ })
230
+
231
+ process.exit(result.status ?? 1)