zova-cli-set-front 1.2.112 → 1.2.114

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 CHANGED
@@ -6,18 +6,18 @@ import { rimraf } from 'rimraf';
6
6
  import { rolldown } from 'rolldown';
7
7
  import { dts } from 'rolldown-plugin-dts';
8
8
  import { build } from 'vite';
9
- import { createRequire } from 'node:module';
9
+ import { createRequire, isBuiltin } from 'node:module';
10
10
  import { camelToKebab, toUpperCaseFirstChar, toLowerCaseFirstChar, skipPrefix, stringToCapitalize, replaceTemplate } from '@cabloy/word-utils';
11
11
  import fse from 'fs-extra';
12
12
  import { build as build$1 } from 'tsdown';
13
- import yaml from 'yaml';
14
13
  import { createConfigUtils, saveJSONFile as saveJSONFile$1 } from 'zova-vite';
14
+ import ts from 'typescript';
15
+ import yaml from 'yaml';
15
16
  import fs from 'node:fs';
16
17
  import 'node:url';
17
18
  import { getOnionScenesMeta, getOnionMetasMeta } from '@cabloy/module-info';
18
19
  import { extend } from '@cabloy/extend';
19
20
  import { catchError, matchSelector, getPropertyObject, evaluateSimple, StringPrefixRegexp } from '@cabloy/utils';
20
- import ts from 'typescript';
21
21
  import { globbySync, globby } from 'globby';
22
22
  import gogocode from 'gogocode';
23
23
  import { optimize } from 'svgo';
@@ -151,6 +151,196 @@ class CliBinBuildModule extends BeanCliBase {
151
151
  }
152
152
  }
153
153
 
154
+ function extractRestDependencies(content) {
155
+ const source = ts.createSourceFile('index.d.mts', content, ts.ScriptTarget.Latest);
156
+ // createSourceFile exposes syntax diagnostics at runtime, but not in its public return type.
157
+ const {
158
+ parseDiagnostics
159
+ } = source;
160
+ if (parseDiagnostics.length) {
161
+ const diagnostic = parseDiagnostics[0];
162
+ const {
163
+ line,
164
+ character
165
+ } = source.getLineAndCharacterOfPosition(diagnostic.start ?? 0);
166
+ throw new Error(`Invalid REST declaration at ${source.fileName}:${line + 1}:${character + 1}: ${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`);
167
+ }
168
+ for (const kind of ['path', 'types']) {
169
+ const references = kind === 'path' ? source.referencedFiles : source.typeReferenceDirectives;
170
+ if (references.length) {
171
+ throw new Error(`Unsupported REST declaration reference directive: ${kind} ${JSON.stringify(references[0].fileName)}. ` + 'The current REST bundling workflow does not support this directive.');
172
+ }
173
+ }
174
+ // lib references are compiler-owned, not package dependencies.
175
+ const dependencies = new Set();
176
+ function addDependency(specifier) {
177
+ if (!specifier) return;
178
+ if (!ts.isStringLiteral(specifier)) {
179
+ throw new Error('Unsupported REST declaration module specifier');
180
+ }
181
+ const id = specifier.text;
182
+ if (id.startsWith('.') || id.startsWith('#') || id.startsWith('\0') || path.isAbsolute(id) || path.win32.isAbsolute(id) || /^[a-z][a-z\d+.-]*:/i.test(id) || isBuiltin(id)) {
183
+ return;
184
+ }
185
+ if (!id || id.includes(':')) {
186
+ throw new Error(`Unsupported REST declaration package reference: ${id}`);
187
+ }
188
+ const parts = id.split('/');
189
+ if (id.startsWith('@') && (!parts[0].slice(1) || !parts[1])) {
190
+ throw new Error(`Invalid REST declaration package reference: ${id}`);
191
+ }
192
+ dependencies.add(parts.slice(0, id.startsWith('@') ? 2 : 1).join('/'));
193
+ }
194
+ function visit(node) {
195
+ if (ts.isImportDeclaration(node) || ts.isExportDeclaration(node)) {
196
+ addDependency(node.moduleSpecifier);
197
+ } else if (ts.isImportTypeNode(node)) {
198
+ if (!ts.isLiteralTypeNode(node.argument)) {
199
+ throw new Error('Unsupported REST declaration import type');
200
+ }
201
+ addDependency(node.argument.literal);
202
+ } else if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
203
+ addDependency(node.moduleReference.expression);
204
+ } else if (ts.isCallExpression(node) && node.expression.kind === ts.SyntaxKind.ImportKeyword) {
205
+ throw new Error('Unsupported REST declaration dynamic import');
206
+ }
207
+ ts.forEachChild(node, visit);
208
+ }
209
+ visit(source);
210
+ return Array.from(dependencies).sort();
211
+ }
212
+
213
+ const dependencySections = ['dependencies', 'devDependencies', 'optionalDependencies'];
214
+ async function extractRestDependencyVersions(projectPath, deps, moduleRoots = []) {
215
+ const lockfilePath = path.join(projectPath, 'pnpm-lock.yaml');
216
+ const lockfileContent = await fse.readFile(lockfilePath, 'utf8');
217
+ const parsedLockfile = yaml.parse(lockfileContent);
218
+ const depsVersion = {};
219
+ for (const dep of deps) {
220
+ const rootRange = getPackageDependencyRangeFromLock(parsedLockfile, dep);
221
+ if (rootRange) {
222
+ depsVersion[dep] = rootRange;
223
+ continue;
224
+ }
225
+ const moduleCandidates = await getModuleCandidates(projectPath, parsedLockfile, dep, moduleRoots);
226
+ const distinctCandidates = getDistinctCandidates(moduleCandidates);
227
+ if (distinctCandidates.length > 1) {
228
+ throw createConflictingCandidatesError(dep, distinctCandidates);
229
+ }
230
+ if (distinctCandidates.length === 1) {
231
+ depsVersion[dep] = distinctCandidates[0].range;
232
+ continue;
233
+ }
234
+ const rootVersion = await getPackageVersionFromNodeModules(projectPath, dep);
235
+ if (rootVersion) {
236
+ depsVersion[dep] = `^${rootVersion}`;
237
+ continue;
238
+ }
239
+ throw createUnresolvedDependencyError(projectPath, dep, moduleRoots);
240
+ }
241
+ return depsVersion;
242
+ }
243
+ function getPackageDependencyRangeFromLock(parsedLockfile, packageName) {
244
+ const importer = parsedLockfile.importers?.['.'];
245
+ return getImporterDependencyRange(importer, packageName);
246
+ }
247
+ function getImporterDependencyRange(importer, packageName) {
248
+ if (!importer) return null;
249
+ for (const section of dependencySections) {
250
+ const dependencyRange = normalizeImporterDependency(importer[section]?.[packageName]);
251
+ if (dependencyRange) return dependencyRange;
252
+ }
253
+ return null;
254
+ }
255
+ function normalizeImporterDependency(importerDependency) {
256
+ if (!importerDependency) return null;
257
+ const specifier = importerDependency.specifier;
258
+ if (specifier?.startsWith('npm:')) return specifier;
259
+ const version = normalizeLockfileVersion(importerDependency.version);
260
+ if (!version) return null;
261
+ return `^${version}`;
262
+ }
263
+ function normalizeLockfileVersion(version) {
264
+ if (!version) return null;
265
+ if (version.startsWith('link:') || version.startsWith('file:')) return null;
266
+ const versionWithoutPeers = version.split('(')[0];
267
+ if (!versionWithoutPeers) return null;
268
+ if (/^\d/.test(versionWithoutPeers)) return versionWithoutPeers;
269
+ const aliasSeparator = versionWithoutPeers.lastIndexOf('@');
270
+ if (aliasSeparator === -1) return null;
271
+ const aliasedVersion = versionWithoutPeers.slice(aliasSeparator + 1);
272
+ if (!aliasedVersion || !/^\d/.test(aliasedVersion)) return null;
273
+ return aliasedVersion;
274
+ }
275
+ async function getModuleCandidates(projectPath, parsedLockfile, packageName, moduleRoots) {
276
+ const candidates = [];
277
+ for (const moduleRoot of moduleRoots) {
278
+ const absoluteModuleRoot = path.resolve(projectPath, moduleRoot);
279
+ if (absoluteModuleRoot === path.resolve(projectPath)) continue;
280
+ const importerKey = getImporterKey(projectPath, absoluteModuleRoot);
281
+ const importerRange = getImporterDependencyRange(parsedLockfile.importers?.[importerKey], packageName);
282
+ if (importerRange) {
283
+ candidates.push({
284
+ range: importerRange,
285
+ moduleRoot: importerKey
286
+ });
287
+ continue;
288
+ }
289
+ const manifestSpecifier = await getManifestDependencySpecifier(absoluteModuleRoot, packageName);
290
+ if (!manifestSpecifier) continue;
291
+ const installedVersion = await getPackageVersionFromNodeModules(absoluteModuleRoot, packageName);
292
+ if (!installedVersion) continue;
293
+ candidates.push({
294
+ range: manifestSpecifier.startsWith('npm:') ? manifestSpecifier : `^${installedVersion}`,
295
+ moduleRoot: importerKey
296
+ });
297
+ }
298
+ return candidates;
299
+ }
300
+ async function getManifestDependencySpecifier(moduleRoot, packageName) {
301
+ const manifestPath = path.join(moduleRoot, 'package.json');
302
+ if (!fse.existsSync(manifestPath)) return null;
303
+ const manifest = await fse.readJson(manifestPath);
304
+ for (const section of dependencySections) {
305
+ const sectionDependencies = manifest[section];
306
+ if (!sectionDependencies || typeof sectionDependencies !== 'object') continue;
307
+ const specifier = sectionDependencies[packageName];
308
+ if (typeof specifier === 'string') return specifier;
309
+ }
310
+ return null;
311
+ }
312
+ function getDistinctCandidates(candidates) {
313
+ const distinctCandidates = new Map();
314
+ for (const candidate of candidates) {
315
+ if (!distinctCandidates.has(candidate.range)) {
316
+ distinctCandidates.set(candidate.range, candidate);
317
+ }
318
+ }
319
+ return Array.from(distinctCandidates.values());
320
+ }
321
+ async function getPackageVersionFromNodeModules(packageRoot, packageName) {
322
+ const pkgFile = path.join(packageRoot, 'node_modules', packageName, 'package.json');
323
+ if (!fse.existsSync(pkgFile)) return null;
324
+ const pkgContent = await fse.readJson(pkgFile);
325
+ return typeof pkgContent.version === 'string' && pkgContent.version ? pkgContent.version : null;
326
+ }
327
+ function getImporterKey(projectPath, moduleRoot) {
328
+ const relativePath = path.relative(projectPath, moduleRoot);
329
+ return relativePath.replaceAll(path.sep, '/') || '.';
330
+ }
331
+ function getRepoRelativePath(projectPath, filePath) {
332
+ const relativePath = path.relative(projectPath, filePath).replaceAll(path.sep, '/');
333
+ return relativePath || '.';
334
+ }
335
+ function createConflictingCandidatesError(packageName, candidates) {
336
+ const details = candidates.map(candidate => `${candidate.moduleRoot} (${candidate.range})`).join(', ');
337
+ return new Error(`Conflicting REST dependency versions for ${packageName} in module importers: ${details}. ` + `Align the dependency versions or declare ${packageName} in the project root package.json ` + '(pnpm-lock.yaml).');
338
+ }
339
+ function createUnresolvedDependencyError(projectPath, packageName, moduleRoots) {
340
+ const checkedModuleRoots = moduleRoots.length ? moduleRoots.map(moduleRoot => getRepoRelativePath(projectPath, path.resolve(projectPath, moduleRoot))).join(', ') : '(none)';
341
+ return new Error(`Unable to resolve REST dependency version for ${packageName}. Add it to the project or an owning ` + `module and install it; checked module roots: ${checkedModuleRoots}; lockfile: pnpm-lock.yaml.`);
342
+ }
343
+
154
344
  function getControllerFileName(module, type, beanName) {
155
345
  const controllerFile = path.join(module.root, `src/${type}/${beanName}/controller.ts`);
156
346
  if (fs.existsSync(controllerFile)) {
@@ -260,11 +450,13 @@ class CliBinBuildRest extends BeanCliBase {
260
450
  const modules = [];
261
451
  const bundleModules = [];
262
452
  const sideEffectImportModuleNames = [];
453
+ const moduleRoots = [];
263
454
  for (const module of modulesMeta.modulesArray) {
264
455
  const moduleName = module.info.fullName;
265
456
  modules.push(`import '${moduleName}';`);
266
457
  modules.push(`export * from '${moduleName}';`);
267
458
  const moduleRoot = module.root.replaceAll('\\', '/');
459
+ moduleRoots.push(moduleRoot);
268
460
  if (moduleRoot.includes('/src/module/') || moduleRoot.includes('/src/suite/')) {
269
461
  bundleModules.push(moduleName);
270
462
  } else {
@@ -273,6 +465,7 @@ class CliBinBuildRest extends BeanCliBase {
273
465
  }
274
466
  context.bundleModules = bundleModules;
275
467
  context.sideEffectImportModuleNames = sideEffectImportModuleNames;
468
+ context.moduleRoots = moduleRoots;
276
469
  argv.Modules = modules.join('\n');
277
470
  // Name/Version
278
471
  argv.Name = bundleName;
@@ -324,8 +517,7 @@ class CliBinBuildRest extends BeanCliBase {
324
517
  tsconfig: 'tsconfig.rest.json',
325
518
  clean: false,
326
519
  dts: {
327
- // resolve: true,
328
- resolver: 'tsc',
520
+ resolver: 'oxc',
329
521
  tsgo: true,
330
522
  eager: true,
331
523
  tsconfig: 'tsconfig.rest.json',
@@ -357,14 +549,15 @@ class CliBinBuildRest extends BeanCliBase {
357
549
  flavor,
358
550
  bundleNameCopy,
359
551
  outDir,
360
- packageJson
552
+ packageJson,
553
+ moduleRoots
361
554
  } = buildContext;
362
555
  // build
363
556
  await this._buildTs(buildContext);
364
557
  await this._buildDts(buildContext);
365
558
  // deps
366
559
  const deps = await _extractDeps(path.join(outDir, 'index.d.mts'));
367
- const depsVersion = await _extractDepsVersion(projectPath, deps);
560
+ const depsVersion = await extractRestDependencyVersions(projectPath, deps, moduleRoots);
368
561
  // package.json
369
562
  const pkgContent = {
370
563
  ...packageJson,
@@ -392,83 +585,8 @@ function _copyToTarget(outDir, target, bundleNameCopy, projectPath) {
392
585
  }
393
586
  }
394
587
  async function _extractDeps(filePath) {
395
- let content = (await fse.readFile(filePath)).toString();
396
- const pos = content.indexOf('//#region');
397
- content = content.substring(0, pos);
398
- const packageNames = new Set();
399
- const re = /import\s+(?:[\s\S]*?\s+from\s+)?['"]([^'"]+)['"]/g;
400
- let match;
401
- while (true) {
402
- match = re.exec(content);
403
- if (!match) break;
404
- let pkg = match[1];
405
- if (pkg.startsWith('.') || pkg.startsWith('/')) continue;
406
- if (pkg.startsWith('@')) {
407
- if (pkg.split('/').length > 2) {
408
- pkg = pkg.split('/').slice(0, 2).join('/');
409
- }
410
- } else {
411
- if (pkg.split('/').length > 1) {
412
- pkg = pkg.split('/').slice(0, 1).join('/');
413
- }
414
- }
415
- packageNames.add(pkg);
416
- }
417
- return Array.from(packageNames).sort();
418
- }
419
- async function _extractDepsVersion(projectPath, deps) {
420
- const lockfilePath = path.join(projectPath, 'pnpm-lock.yaml');
421
- const lockfileContent = (await fse.readFile(lockfilePath)).toString();
422
- const parsedLockfile = yaml.parse(lockfileContent);
423
- const depsVersion = {};
424
- for (const dep of deps) {
425
- let dependencyRange = getPackageDependencyRangeFromLock(parsedLockfile, dep);
426
- if (!dependencyRange) {
427
- const version = await getPackageVersionFromNodeModules(projectPath, dep);
428
- dependencyRange = version ? `^${version}` : null;
429
- }
430
- if (!dependencyRange) {
431
- console.warn('dep version not found: ', dep);
432
- continue;
433
- }
434
- depsVersion[dep] = dependencyRange;
435
- }
436
- return depsVersion;
437
- }
438
- function getPackageDependencyRangeFromLock(parsedLockfile, packageName) {
439
- const importer = parsedLockfile.importers?.['.'];
440
- const sections = ['dependencies', 'devDependencies', 'optionalDependencies'];
441
- for (const section of sections) {
442
- const dependencyRange = getImporterDependencyRange(importer?.[section]?.[packageName]);
443
- if (dependencyRange) return dependencyRange;
444
- }
445
- return null;
446
- }
447
- function getImporterDependencyRange(importerDependency) {
448
- if (!importerDependency) return null;
449
- const specifier = importerDependency.specifier;
450
- if (specifier?.startsWith('npm:')) return specifier;
451
- const version = normalizeLockfileVersion(importerDependency.version);
452
- if (!version) return null;
453
- return `^${version}`;
454
- }
455
- function normalizeLockfileVersion(version) {
456
- if (!version) return null;
457
- if (version.startsWith('link:') || version.startsWith('file:')) return null;
458
- const versionWithoutPeers = version.split('(')[0];
459
- if (!versionWithoutPeers) return null;
460
- if (/^\d/.test(versionWithoutPeers)) return versionWithoutPeers;
461
- const aliasSeparator = versionWithoutPeers.lastIndexOf('@');
462
- if (aliasSeparator === -1) return null;
463
- const aliasedVersion = versionWithoutPeers.slice(aliasSeparator + 1);
464
- if (!aliasedVersion || !/^\d/.test(aliasedVersion)) return null;
465
- return aliasedVersion;
466
- }
467
- async function getPackageVersionFromNodeModules(projectPath, packageName) {
468
- const pkgFile = path.join(projectPath, 'node_modules', packageName, 'package.json');
469
- if (!fse.existsSync(pkgFile)) return null;
470
- const pkgContent = await loadJSONFile(pkgFile);
471
- return pkgContent.version;
588
+ const content = await fse.readFile(filePath, 'utf8');
589
+ return extractRestDependencies(content);
472
590
  }
473
591
 
474
592
  let __snippetsPathPrefix;
@@ -23,6 +23,7 @@ interface IBinBuildRestContext {
23
23
  outDir: string;
24
24
  bundleModules?: string[];
25
25
  sideEffectImportModuleNames?: string[];
26
+ moduleRoots?: string[];
26
27
  packageJson?: IBinBuildRestPackageJson;
27
28
  }
28
29
  export declare class CliBinBuildRest extends BeanCliBase {
@@ -0,0 +1 @@
1
+ export declare function extractRestDependencies(content: string): string[];
@@ -0,0 +1 @@
1
+ export declare function extractRestDependencyVersions(projectPath: string, deps: string[], moduleRoots?: string[]): Promise<Record<string, string>>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "zova-cli-set-front",
3
- "version": "1.2.112",
4
- "gitHead": "07074482abf76e38f9e3ccc3505f6d546e5c6f8d",
3
+ "version": "1.2.114",
4
+ "gitHead": "d658b653ccdc3a1856d71a932ff7a2c005005293",
5
5
  "description": "zova cli-set-front",
6
6
  "keywords": [
7
7
  "framework",
@@ -56,15 +56,15 @@
56
56
  "globby": "^14.1.0",
57
57
  "gogocode": "^1.0.55",
58
58
  "rimraf": "^5.0.5",
59
- "rolldown": "^1.0.0-rc.11",
59
+ "rolldown": "^1.2.14",
60
60
  "rolldown-plugin-dts": "^0.22.5",
61
61
  "svgo": "^3.2.0",
62
62
  "tsdown": "^0.21.7",
63
63
  "typescript": "^5.9.3",
64
64
  "urllib": "^4.6.11",
65
- "vite": "^8.0.14",
65
+ "vite": "^8.3.4",
66
66
  "yaml": "^2.8.3",
67
67
  "zova-openapi": "^1.1.17",
68
- "zova-vite": "^1.1.48"
68
+ "zova-vite": "^1.1.49"
69
69
  }
70
70
  }