@domainlang/cli 0.5.2 → 0.7.0

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 (38) hide show
  1. package/README.md +16 -16
  2. package/out/dependency-commands.d.ts +1 -1
  3. package/out/dependency-commands.js +52 -24
  4. package/out/dependency-commands.js.map +1 -1
  5. package/out/main.d.ts +1 -1
  6. package/out/main.js +1 -1
  7. package/out/main.js.map +1 -1
  8. package/out/services/dependency-analyzer.d.ts +59 -0
  9. package/out/services/dependency-analyzer.js +260 -0
  10. package/out/services/dependency-analyzer.js.map +1 -0
  11. package/out/services/dependency-resolver.d.ts +148 -0
  12. package/out/services/dependency-resolver.js +448 -0
  13. package/out/services/dependency-resolver.js.map +1 -0
  14. package/out/services/git-url-resolver.d.ts +158 -0
  15. package/out/services/git-url-resolver.js +408 -0
  16. package/out/services/git-url-resolver.js.map +1 -0
  17. package/out/services/governance-validator.d.ts +56 -0
  18. package/out/services/governance-validator.js +171 -0
  19. package/out/services/governance-validator.js.map +1 -0
  20. package/out/services/index.d.ts +12 -0
  21. package/out/services/index.js +13 -0
  22. package/out/services/index.js.map +1 -0
  23. package/out/services/semver.d.ts +98 -0
  24. package/out/services/semver.js +195 -0
  25. package/out/services/semver.js.map +1 -0
  26. package/out/services/types.d.ts +58 -0
  27. package/out/services/types.js +8 -0
  28. package/out/services/types.js.map +1 -0
  29. package/package.json +4 -3
  30. package/src/dependency-commands.ts +59 -24
  31. package/src/main.ts +1 -1
  32. package/src/services/dependency-analyzer.ts +329 -0
  33. package/src/services/dependency-resolver.ts +546 -0
  34. package/src/services/git-url-resolver.ts +504 -0
  35. package/src/services/governance-validator.ts +226 -0
  36. package/src/services/index.ts +13 -0
  37. package/src/services/semver.ts +213 -0
  38. package/src/services/types.ts +81 -0
@@ -0,0 +1,504 @@
1
+ /**
2
+ * Git Repository Resolver Service (CLI-only)
3
+ *
4
+ * Resolves git-based package imports to local cached repositories.
5
+ * Supports simplified GitHub syntax (owner/repo@version) and full URLs.
6
+ *
7
+ * Design: Repository-level imports (not individual files)
8
+ * - Imports load entire package
9
+ * - Package entry point defined in model.yaml
10
+ * - Version pinning at repository level
11
+ *
12
+ * This module contains network operations (git clone, git ls-remote) and
13
+ * should ONLY be used in CLI contexts, never in the LSP.
14
+ */
15
+
16
+ import path from 'node:path';
17
+ import fs from 'node:fs/promises';
18
+ import { exec } from 'node:child_process';
19
+ import { promisify } from 'node:util';
20
+ import YAML from 'yaml';
21
+ import type { GitImportInfo, ResolvingPackage, LockFile } from './types.js';
22
+
23
+ const execAsync = promisify(exec);
24
+
25
+ /**
26
+ * Parses import URLs into structured git import information.
27
+ *
28
+ * Supported formats:
29
+ * - owner/repo@version (GitHub assumed)
30
+ * - owner/repo (GitHub, defaults to main)
31
+ * - https://github.com/owner/repo@version
32
+ * - https://gitlab.com/owner/repo@version
33
+ * - https://git.example.com/owner/repo@version
34
+ */
35
+ export class GitUrlParser {
36
+ /**
37
+ * Determines if an import string is a git repository import.
38
+ */
39
+ static isGitUrl(importStr: string): boolean {
40
+ // GitHub shorthand: owner/repo or owner/repo@version
41
+ if (/^[a-zA-Z0-9-]+\/[a-zA-Z0-9-_.]+(@[^/]+)?$/.test(importStr)) {
42
+ return true;
43
+ }
44
+
45
+ // Full URLs
46
+ return (
47
+ importStr.startsWith('https://github.com/') ||
48
+ importStr.startsWith('https://gitlab.com/') ||
49
+ importStr.startsWith('https://bitbucket.org/') ||
50
+ importStr.startsWith('https://git.') ||
51
+ importStr.startsWith('git://')
52
+ );
53
+ }
54
+
55
+ /**
56
+ * Parses a git import URL into structured components.
57
+ *
58
+ * @param importStr - The import URL string
59
+ * @returns Parsed git import information
60
+ * @throws Error if URL format is invalid
61
+ */
62
+ static parse(importStr: string): GitImportInfo {
63
+ // Handle GitHub shorthand (owner/repo or owner/repo@version)
64
+ if (this.isGitHubShorthand(importStr)) {
65
+ return this.parseGitHubShorthand(importStr);
66
+ }
67
+
68
+ // Handle full URLs
69
+ if (importStr.startsWith('https://') || importStr.startsWith('git://')) {
70
+ return this.parseFullUrl(importStr);
71
+ }
72
+
73
+ throw new Error(
74
+ `Invalid git import URL: '${importStr}'.\n` +
75
+ `Hint: Use 'owner/repo' or 'owner/repo@version' format (e.g., 'domainlang/core@v1.0.0').`
76
+ );
77
+ }
78
+
79
+ /**
80
+ * Checks if string is GitHub shorthand format.
81
+ */
82
+ private static isGitHubShorthand(importStr: string): boolean {
83
+ return /^[a-zA-Z0-9-]+\/[a-zA-Z0-9-_.]+(@[^/]+)?$/.test(importStr);
84
+ }
85
+
86
+ /**
87
+ * Parses GitHub shorthand (owner/repo or owner/repo@version).
88
+ */
89
+ private static parseGitHubShorthand(importStr: string): GitImportInfo {
90
+ const match = importStr.match(/^([a-zA-Z0-9-]+)\/([a-zA-Z0-9-_.]+)(?:@([^/]+))?$/);
91
+ if (!match) {
92
+ throw new Error(
93
+ `Invalid GitHub shorthand format: '${importStr}'.\n` +
94
+ `Hint: Use 'owner/repo' or 'owner/repo@version' format.`
95
+ );
96
+ }
97
+
98
+ const [, owner, repo, version] = match;
99
+ const resolvedVersion = version || 'main';
100
+
101
+ return {
102
+ original: importStr,
103
+ platform: 'github',
104
+ owner,
105
+ repo,
106
+ version: resolvedVersion,
107
+ repoUrl: `https://github.com/${owner}/${repo}`,
108
+ entryPoint: 'index.dlang', // Default, will be resolved from model.yaml
109
+ };
110
+ }
111
+
112
+ /**
113
+ * Parses full git URLs (https://...).
114
+ *
115
+ * Supported:
116
+ * - https://github.com/owner/repo@version
117
+ * - https://gitlab.com/owner/repo@version
118
+ * - https://git.example.com/owner/repo@version
119
+ */
120
+ private static parseFullUrl(importStr: string): GitImportInfo {
121
+ // GitHub
122
+ const ghMatch = importStr.match(
123
+ /^https:\/\/github\.com\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
124
+ );
125
+ if (ghMatch) {
126
+ const [, owner, repo, version] = ghMatch;
127
+ return {
128
+ original: importStr,
129
+ platform: 'github',
130
+ owner,
131
+ repo,
132
+ version: version || 'main',
133
+ repoUrl: `https://github.com/${owner}/${repo}`,
134
+ entryPoint: 'index.dlang',
135
+ };
136
+ }
137
+
138
+ // GitLab
139
+ const glMatch = importStr.match(
140
+ /^https:\/\/gitlab\.com\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
141
+ );
142
+ if (glMatch) {
143
+ const [, owner, repo, version] = glMatch;
144
+ return {
145
+ original: importStr,
146
+ platform: 'gitlab',
147
+ owner,
148
+ repo,
149
+ version: version || 'main',
150
+ repoUrl: `https://gitlab.com/${owner}/${repo}`,
151
+ entryPoint: 'index.dlang',
152
+ };
153
+ }
154
+
155
+ // Bitbucket
156
+ const bbMatch = importStr.match(
157
+ /^https:\/\/bitbucket\.org\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
158
+ );
159
+ if (bbMatch) {
160
+ const [, owner, repo, version] = bbMatch;
161
+ return {
162
+ original: importStr,
163
+ platform: 'bitbucket',
164
+ owner,
165
+ repo,
166
+ version: version || 'main',
167
+ repoUrl: `https://bitbucket.org/${owner}/${repo}`,
168
+ entryPoint: 'index.dlang',
169
+ };
170
+ }
171
+
172
+ // Generic git URL
173
+ const genericMatch = importStr.match(
174
+ /^(?:https|git):\/\/([^/]+)\/([^/]+)\/([^/@]+)(?:@([^/]+))?$/
175
+ );
176
+ if (genericMatch) {
177
+ const [, host, owner, repo, version] = genericMatch;
178
+ return {
179
+ original: importStr,
180
+ platform: 'generic',
181
+ owner,
182
+ repo,
183
+ version: version || 'main',
184
+ repoUrl: `https://${host}/${owner}/${repo}`,
185
+ entryPoint: 'index.dlang',
186
+ };
187
+ }
188
+
189
+ throw new Error(
190
+ `Unsupported git URL format: '${importStr}'.\n` +
191
+ `Supported formats:\n` +
192
+ ` • owner/repo (GitHub shorthand)\n` +
193
+ ` • owner/repo@version\n` +
194
+ ` • https://github.com/owner/repo\n` +
195
+ ` • https://gitlab.com/owner/repo`
196
+ );
197
+ }
198
+ }
199
+
200
+ /**
201
+ * Resolves git repository imports to local entry point files.
202
+ *
203
+ * Implements a content-addressable cache:
204
+ * - Cache location: .dlang/packages/ (project-local, per PRS-010)
205
+ * - Cache key: {owner}/{repo}/{commit-hash}
206
+ * - Downloads entire repository on first use
207
+ * - Reads model.yaml to find entry point
208
+ * - Returns path to entry point file
209
+ */
210
+ export class GitUrlResolver {
211
+ private cacheDir: string;
212
+ private lockFile?: LockFile;
213
+
214
+ /**
215
+ * Creates a GitUrlResolver with a project-local cache directory.
216
+ *
217
+ * @param cacheDir - The cache directory path. Per PRS-010, this should be
218
+ * the project's `.dlang/packages/` directory for isolation
219
+ * and reproducibility (like node_modules).
220
+ */
221
+ constructor(cacheDir: string) {
222
+ this.cacheDir = cacheDir;
223
+ }
224
+
225
+ /**
226
+ * Sets the lock file for dependency resolution.
227
+ *
228
+ * When a lock file is set, all package imports will use
229
+ * the locked commit hashes instead of resolving versions.
230
+ * This ensures reproducible builds and handles transitive dependencies.
231
+ *
232
+ * @param lockFile - The parsed lock file from the workspace root
233
+ */
234
+ setLockFile(lockFile: LockFile | undefined): void {
235
+ this.lockFile = lockFile;
236
+ }
237
+
238
+ /**
239
+ * Resolves a git import URL to the package's entry point file path.
240
+ *
241
+ * Process:
242
+ * 1. Parse git URL
243
+ * 2. Check lock file for pinned version (transitive dependency support)
244
+ * 3. Resolve version to commit hash (if not locked)
245
+ * 4. Check cache
246
+ * 5. Download repository if not cached
247
+ * 6. Read model.yaml to find entry point
248
+ * 7. Return path to entry point file
249
+ *
250
+ * @param importUrl - The git import URL
251
+ * @returns Path to the package's entry point file
252
+ */
253
+ async resolve(importUrl: string): Promise<string> {
254
+ const gitInfo = GitUrlParser.parse(importUrl);
255
+
256
+ // Check lock file for pinned version (handles transitive dependencies)
257
+ let commitHash: string;
258
+ const packageKey = `${gitInfo.owner}/${gitInfo.repo}`;
259
+
260
+ if (this.lockFile?.dependencies[packageKey]) {
261
+ // Use locked commit hash (reproducible build)
262
+ commitHash = this.lockFile.dependencies[packageKey].commit;
263
+ } else {
264
+ // No lock file entry - resolve version via network
265
+ commitHash = await this.resolveCommit(gitInfo);
266
+ }
267
+
268
+ // Check cache
269
+ const cachedPath = this.getCachePath(gitInfo, commitHash);
270
+
271
+ if (!(await this.existsInCache(cachedPath))) {
272
+ // Download repository
273
+ await this.downloadRepo(gitInfo, commitHash, cachedPath);
274
+ }
275
+
276
+ // Read package metadata to get entry point
277
+ const entryPoint = await this.getEntryPoint(cachedPath);
278
+ const entryFile = path.join(cachedPath, entryPoint);
279
+
280
+ // Verify entry point exists
281
+ if (!(await this.existsInCache(entryFile))) {
282
+ throw new Error(
283
+ `Entry point '${entryPoint}' not found in package '${gitInfo.owner}/${gitInfo.repo}@${gitInfo.version}'.\n` +
284
+ `Hint: Ensure the package has an entry point file (default: index.dlang).`
285
+ );
286
+ }
287
+
288
+ return entryFile;
289
+ }
290
+
291
+ /**
292
+ * Reads model.yaml to get the package entry point.
293
+ * Falls back to index.dlang if no model.yaml found.
294
+ */
295
+ private async getEntryPoint(repoPath: string): Promise<string> {
296
+ const yamlPath = path.join(repoPath, 'model.yaml');
297
+
298
+ try {
299
+ const yamlContent = await fs.readFile(yamlPath, 'utf-8');
300
+ const metadata = this.parseYaml(yamlContent);
301
+ return metadata.entry ?? 'index.dlang';
302
+ } catch {
303
+ // No model.yaml or parse error, use default
304
+ return 'index.dlang';
305
+ }
306
+ }
307
+
308
+ /**
309
+ * Parses model.yaml content to extract entry point.
310
+ *
311
+ * Expected structure:
312
+ * model:
313
+ * entry: index.dlang
314
+ */
315
+ private parseYaml(content: string): ResolvingPackage {
316
+ const parsed = YAML.parse(content) as {
317
+ model?: {
318
+ name?: string;
319
+ version?: string;
320
+ entry?: string;
321
+ };
322
+ };
323
+
324
+ return {
325
+ entry: parsed.model?.entry,
326
+ name: parsed.model?.name,
327
+ version: parsed.model?.version,
328
+ };
329
+ }
330
+
331
+ /**
332
+ * Resolves a version (tag/branch) to a commit hash using git ls-remote.
333
+ */
334
+ async resolveCommit(gitInfo: GitImportInfo): Promise<string> {
335
+ try {
336
+ // Try to resolve as tag or branch
337
+ const { stdout } = await execAsync(
338
+ `git ls-remote ${gitInfo.repoUrl} ${gitInfo.version}`
339
+ );
340
+
341
+ if (stdout.trim()) {
342
+ const commitHash = stdout.split('\t')[0];
343
+ return commitHash;
344
+ }
345
+
346
+ // If not found, assume it's already a commit hash
347
+ if (/^[0-9a-f]{7,40}$/i.test(gitInfo.version)) {
348
+ return gitInfo.version;
349
+ }
350
+
351
+ throw new Error(
352
+ `Could not resolve version '${gitInfo.version}' for ${gitInfo.repoUrl}.\n` +
353
+ `Hint: Check that the version (tag, branch, or commit) exists in the repository.`
354
+ );
355
+ } catch (error) {
356
+ throw new Error(
357
+ `Failed to resolve git version '${gitInfo.version}' for ${gitInfo.repoUrl}.\n` +
358
+ `Error: ${error}\n` +
359
+ `Hint: Verify the repository URL is correct and accessible.`
360
+ );
361
+ }
362
+ }
363
+
364
+ /**
365
+ * Gets the local cache path for a git repository.
366
+ *
367
+ * Format: .dlang/packages/{owner}/{repo}/{version}/
368
+ *
369
+ * Per PRS-010: Project-local cache structure mirrors the Design Considerations
370
+ * section showing `.dlang/packages/{owner}/{repo}/{version}/` layout.
371
+ */
372
+ getCachePath(gitInfo: GitImportInfo, commitHash: string): string {
373
+ return path.join(
374
+ this.cacheDir,
375
+ gitInfo.owner,
376
+ gitInfo.repo,
377
+ commitHash
378
+ );
379
+ }
380
+
381
+ /**
382
+ * Checks if a file or directory exists in the cache.
383
+ */
384
+ private async existsInCache(filePath: string): Promise<boolean> {
385
+ try {
386
+ await fs.access(filePath);
387
+ return true;
388
+ } catch {
389
+ return false;
390
+ }
391
+ }
392
+
393
+ /**
394
+ * Downloads a git repository to the cache.
395
+ *
396
+ * Uses shallow clone for efficiency (only downloads the specific commit).
397
+ */
398
+ async downloadRepo(
399
+ gitInfo: GitImportInfo,
400
+ commitHash: string,
401
+ cachePath: string
402
+ ): Promise<void> {
403
+ const targetDir = path.resolve(cachePath);
404
+ const parentDir = path.dirname(targetDir);
405
+ await fs.mkdir(parentDir, { recursive: true });
406
+
407
+ try {
408
+ await execAsync(
409
+ `git clone ${gitInfo.repoUrl}.git "${targetDir}" --no-checkout`
410
+ );
411
+
412
+ await execAsync(
413
+ `git -C "${targetDir}" fetch --depth 1 origin ${commitHash}`
414
+ );
415
+
416
+ await execAsync(
417
+ `git -C "${targetDir}" checkout --force --detach ${commitHash}`
418
+ );
419
+
420
+ await fs.rm(path.join(targetDir, '.git'), { recursive: true, force: true });
421
+ } catch (error) {
422
+ await fs.rm(targetDir, { recursive: true, force: true });
423
+ const message = error instanceof Error ? error.message : String(error);
424
+ throw new Error(
425
+ `Failed to download package '${gitInfo.owner}/${gitInfo.repo}@${gitInfo.version}'.\n` +
426
+ `Error: ${message}\n` +
427
+ `Hint: Check your network connection and verify the repository URL is correct.`
428
+ );
429
+ }
430
+ }
431
+
432
+ /**
433
+ * Clears the entire cache.
434
+ */
435
+ async clearCache(): Promise<void> {
436
+ await fs.rm(this.cacheDir, { recursive: true, force: true });
437
+ }
438
+
439
+ /**
440
+ * Gets cache statistics (size, number of cached repos, etc.).
441
+ *
442
+ * Cache structure: .dlang/packages/{owner}/{repo}/{version}/
443
+ */
444
+ async getCacheStats(): Promise<{
445
+ totalSize: number;
446
+ repoCount: number;
447
+ cacheDir: string;
448
+ }> {
449
+ let totalSize = 0;
450
+ let repoCount = 0;
451
+
452
+ try {
453
+ const owners = await fs.readdir(this.cacheDir);
454
+ for (const owner of owners) {
455
+ const ownerPath = path.join(this.cacheDir, owner);
456
+ const ownerStat = await fs.stat(ownerPath);
457
+ if (!ownerStat.isDirectory()) continue;
458
+
459
+ const repos = await fs.readdir(ownerPath);
460
+ for (const repo of repos) {
461
+ const repoPath = path.join(ownerPath, repo);
462
+ const repoStat = await fs.stat(repoPath);
463
+ if (!repoStat.isDirectory()) continue;
464
+
465
+ const versions = await fs.readdir(repoPath);
466
+ repoCount += versions.length;
467
+
468
+ for (const version of versions) {
469
+ const versionPath = path.join(repoPath, version);
470
+ totalSize += await this.getDirectorySize(versionPath);
471
+ }
472
+ }
473
+ }
474
+ } catch {
475
+ // Cache directory doesn't exist yet
476
+ }
477
+
478
+ return {
479
+ totalSize,
480
+ repoCount,
481
+ cacheDir: this.cacheDir,
482
+ };
483
+ }
484
+
485
+ /**
486
+ * Gets the total size of a directory in bytes.
487
+ */
488
+ private async getDirectorySize(dirPath: string): Promise<number> {
489
+ let size = 0;
490
+ const entries = await fs.readdir(dirPath, { withFileTypes: true });
491
+
492
+ for (const entry of entries) {
493
+ const entryPath = path.join(dirPath, entry.name);
494
+ if (entry.isDirectory()) {
495
+ size += await this.getDirectorySize(entryPath);
496
+ } else {
497
+ const stats = await fs.stat(entryPath);
498
+ size += stats.size;
499
+ }
500
+ }
501
+
502
+ return size;
503
+ }
504
+ }