zova-cli-set-front 1.2.113 → 1.2.115

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,213 @@ 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 = parseWorkspaceLockfile(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 parseWorkspaceLockfile(content) {
244
+ const documents = yaml.parseAllDocuments(content);
245
+ for (const document of documents) {
246
+ if (document.errors.length) throw document.errors[0];
247
+ }
248
+
249
+ // pnpm 12 places the workspace lock after its package-manager/config dependency lock.
250
+ const document = documents.findLast(({
251
+ contents
252
+ }) => {
253
+ return contents !== null && !(yaml.isScalar(contents) && contents.value === null && contents.source === '');
254
+ });
255
+ if (!document || !yaml.isMap(document.contents) || !yaml.isMap(document.get('importers', true))) {
256
+ throw new Error('Invalid pnpm-lock.yaml: expected a workspace document with an importers mapping.');
257
+ }
258
+ return document.toJS();
259
+ }
260
+ function getPackageDependencyRangeFromLock(parsedLockfile, packageName) {
261
+ const importer = parsedLockfile.importers?.['.'];
262
+ return getImporterDependencyRange(importer, packageName);
263
+ }
264
+ function getImporterDependencyRange(importer, packageName) {
265
+ if (!importer) return null;
266
+ for (const section of dependencySections) {
267
+ const dependencyRange = normalizeImporterDependency(importer[section]?.[packageName]);
268
+ if (dependencyRange) return dependencyRange;
269
+ }
270
+ return null;
271
+ }
272
+ function normalizeImporterDependency(importerDependency) {
273
+ if (!importerDependency) return null;
274
+ const specifier = importerDependency.specifier;
275
+ if (specifier?.startsWith('npm:')) return specifier;
276
+ const version = normalizeLockfileVersion(importerDependency.version);
277
+ if (!version) return null;
278
+ return `^${version}`;
279
+ }
280
+ function normalizeLockfileVersion(version) {
281
+ if (!version) return null;
282
+ if (version.startsWith('link:') || version.startsWith('file:')) return null;
283
+ const versionWithoutPeers = version.split('(')[0];
284
+ if (!versionWithoutPeers) return null;
285
+ if (/^\d/.test(versionWithoutPeers)) return versionWithoutPeers;
286
+ const aliasSeparator = versionWithoutPeers.lastIndexOf('@');
287
+ if (aliasSeparator === -1) return null;
288
+ const aliasedVersion = versionWithoutPeers.slice(aliasSeparator + 1);
289
+ if (!aliasedVersion || !/^\d/.test(aliasedVersion)) return null;
290
+ return aliasedVersion;
291
+ }
292
+ async function getModuleCandidates(projectPath, parsedLockfile, packageName, moduleRoots) {
293
+ const candidates = [];
294
+ for (const moduleRoot of moduleRoots) {
295
+ const absoluteModuleRoot = path.resolve(projectPath, moduleRoot);
296
+ if (absoluteModuleRoot === path.resolve(projectPath)) continue;
297
+ const importerKey = getImporterKey(projectPath, absoluteModuleRoot);
298
+ const importerRange = getImporterDependencyRange(parsedLockfile.importers?.[importerKey], packageName);
299
+ if (importerRange) {
300
+ candidates.push({
301
+ range: importerRange,
302
+ moduleRoot: importerKey
303
+ });
304
+ continue;
305
+ }
306
+ const manifestSpecifier = await getManifestDependencySpecifier(absoluteModuleRoot, packageName);
307
+ if (!manifestSpecifier) continue;
308
+ const installedVersion = await getPackageVersionFromNodeModules(absoluteModuleRoot, packageName);
309
+ if (!installedVersion) continue;
310
+ candidates.push({
311
+ range: manifestSpecifier.startsWith('npm:') ? manifestSpecifier : `^${installedVersion}`,
312
+ moduleRoot: importerKey
313
+ });
314
+ }
315
+ return candidates;
316
+ }
317
+ async function getManifestDependencySpecifier(moduleRoot, packageName) {
318
+ const manifestPath = path.join(moduleRoot, 'package.json');
319
+ if (!fse.existsSync(manifestPath)) return null;
320
+ const manifest = await fse.readJson(manifestPath);
321
+ for (const section of dependencySections) {
322
+ const sectionDependencies = manifest[section];
323
+ if (!sectionDependencies || typeof sectionDependencies !== 'object') continue;
324
+ const specifier = sectionDependencies[packageName];
325
+ if (typeof specifier === 'string') return specifier;
326
+ }
327
+ return null;
328
+ }
329
+ function getDistinctCandidates(candidates) {
330
+ const distinctCandidates = new Map();
331
+ for (const candidate of candidates) {
332
+ if (!distinctCandidates.has(candidate.range)) {
333
+ distinctCandidates.set(candidate.range, candidate);
334
+ }
335
+ }
336
+ return Array.from(distinctCandidates.values());
337
+ }
338
+ async function getPackageVersionFromNodeModules(packageRoot, packageName) {
339
+ const pkgFile = path.join(packageRoot, 'node_modules', packageName, 'package.json');
340
+ if (!fse.existsSync(pkgFile)) return null;
341
+ const pkgContent = await fse.readJson(pkgFile);
342
+ return typeof pkgContent.version === 'string' && pkgContent.version ? pkgContent.version : null;
343
+ }
344
+ function getImporterKey(projectPath, moduleRoot) {
345
+ const relativePath = path.relative(projectPath, moduleRoot);
346
+ return relativePath.replaceAll(path.sep, '/') || '.';
347
+ }
348
+ function getRepoRelativePath(projectPath, filePath) {
349
+ const relativePath = path.relative(projectPath, filePath).replaceAll(path.sep, '/');
350
+ return relativePath || '.';
351
+ }
352
+ function createConflictingCandidatesError(packageName, candidates) {
353
+ const details = candidates.map(candidate => `${candidate.moduleRoot} (${candidate.range})`).join(', ');
354
+ 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).');
355
+ }
356
+ function createUnresolvedDependencyError(projectPath, packageName, moduleRoots) {
357
+ const checkedModuleRoots = moduleRoots.length ? moduleRoots.map(moduleRoot => getRepoRelativePath(projectPath, path.resolve(projectPath, moduleRoot))).join(', ') : '(none)';
358
+ 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.`);
359
+ }
360
+
154
361
  function getControllerFileName(module, type, beanName) {
155
362
  const controllerFile = path.join(module.root, `src/${type}/${beanName}/controller.ts`);
156
363
  if (fs.existsSync(controllerFile)) {
@@ -260,11 +467,13 @@ class CliBinBuildRest extends BeanCliBase {
260
467
  const modules = [];
261
468
  const bundleModules = [];
262
469
  const sideEffectImportModuleNames = [];
470
+ const moduleRoots = [];
263
471
  for (const module of modulesMeta.modulesArray) {
264
472
  const moduleName = module.info.fullName;
265
473
  modules.push(`import '${moduleName}';`);
266
474
  modules.push(`export * from '${moduleName}';`);
267
475
  const moduleRoot = module.root.replaceAll('\\', '/');
476
+ moduleRoots.push(moduleRoot);
268
477
  if (moduleRoot.includes('/src/module/') || moduleRoot.includes('/src/suite/')) {
269
478
  bundleModules.push(moduleName);
270
479
  } else {
@@ -273,6 +482,7 @@ class CliBinBuildRest extends BeanCliBase {
273
482
  }
274
483
  context.bundleModules = bundleModules;
275
484
  context.sideEffectImportModuleNames = sideEffectImportModuleNames;
485
+ context.moduleRoots = moduleRoots;
276
486
  argv.Modules = modules.join('\n');
277
487
  // Name/Version
278
488
  argv.Name = bundleName;
@@ -324,8 +534,7 @@ class CliBinBuildRest extends BeanCliBase {
324
534
  tsconfig: 'tsconfig.rest.json',
325
535
  clean: false,
326
536
  dts: {
327
- // resolve: true,
328
- resolver: 'tsc',
537
+ resolver: 'oxc',
329
538
  tsgo: true,
330
539
  eager: true,
331
540
  tsconfig: 'tsconfig.rest.json',
@@ -357,14 +566,15 @@ class CliBinBuildRest extends BeanCliBase {
357
566
  flavor,
358
567
  bundleNameCopy,
359
568
  outDir,
360
- packageJson
569
+ packageJson,
570
+ moduleRoots
361
571
  } = buildContext;
362
572
  // build
363
573
  await this._buildTs(buildContext);
364
574
  await this._buildDts(buildContext);
365
575
  // deps
366
576
  const deps = await _extractDeps(path.join(outDir, 'index.d.mts'));
367
- const depsVersion = await _extractDepsVersion(projectPath, deps);
577
+ const depsVersion = await extractRestDependencyVersions(projectPath, deps, moduleRoots);
368
578
  // package.json
369
579
  const pkgContent = {
370
580
  ...packageJson,
@@ -392,83 +602,8 @@ function _copyToTarget(outDir, target, bundleNameCopy, projectPath) {
392
602
  }
393
603
  }
394
604
  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;
605
+ const content = await fse.readFile(filePath, 'utf8');
606
+ return extractRestDependencies(content);
472
607
  }
473
608
 
474
609
  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.113",
4
- "gitHead": "3dddb11c81f79753b36a886b3462cb401308f530",
3
+ "version": "1.2.115",
4
+ "gitHead": "c4185c759d9e8933a078182614a83fd43031659f",
5
5
  "description": "zova cli-set-front",
6
6
  "keywords": [
7
7
  "framework",