cloud-ide-cide 2.0.34

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,94 @@
1
+ /**
2
+ * Resolve the angular.json project *key* for `ng build --project <key>`.
3
+ *
4
+ * Angular CLI only accepts keys listed in angular.json (often `@scope/lib-name`).
5
+ * On disk, libs may live at `projects/lib-name` while `root` in angular.json may be stale.
6
+ * Always prefer the registered project key that matches `package.json` "name" when present.
7
+ */
8
+ const fs = require('fs');
9
+ const path = require('path');
10
+
11
+ function readJsonSafe(p) {
12
+ try {
13
+ return JSON.parse(fs.readFileSync(p, 'utf8'));
14
+ } catch {
15
+ return null;
16
+ }
17
+ }
18
+
19
+ let _projectsCache = null;
20
+ let _projectsCacheRoot = null;
21
+
22
+ function getAngularProjects(angularRoot) {
23
+ const root = path.resolve(angularRoot);
24
+ if (_projectsCache && _projectsCacheRoot === root) return _projectsCache;
25
+ _projectsCacheRoot = root;
26
+ _projectsCache = {};
27
+ const ajPath = path.join(root, 'angular.json');
28
+ if (!fs.existsSync(ajPath)) return _projectsCache;
29
+ const aj = readJsonSafe(ajPath);
30
+ if (aj && aj.projects && typeof aj.projects === 'object') {
31
+ _projectsCache = aj.projects;
32
+ }
33
+ return _projectsCache;
34
+ }
35
+
36
+ /**
37
+ * @param {string} angularRoot - Workspace root (folder containing angular.json).
38
+ * For sibling Angular workspaces this is the sibling's root (e.g. LMS-UI/), NOT the
39
+ * cwd where `cide publish` was invoked. The correct value is stored in entry.workspaceRoot
40
+ * and is passed here as `entryWorkspaceRoot` from publishPackage.js.
41
+ * @param {string} absDir - Absolute path to the library folder (where package.json lives)
42
+ * @param {string} packageJsonPath - Path to that package.json
43
+ * @returns {string} Project key to pass to `ng build --project`
44
+ */
45
+ function resolveNgProjectNameForPackageDir(angularRoot, absDir, packageJsonPath) {
46
+ // Source of truth: the angular.json at angularRoot (may be a sibling workspace, not the cwd).
47
+ const projects = getAngularProjects(angularRoot);
48
+ const entryNorm = path.normalize(path.resolve(absDir));
49
+ const pkg = readJsonSafe(packageJsonPath) || {};
50
+ const folderName = path.basename(entryNorm);
51
+
52
+ console.log(` [resolver] angular.json from: ${angularRoot}`);
53
+
54
+ // 1) Authoritative: project whose `root` matches this folder on disk
55
+ for (const [key, cfg] of Object.entries(projects)) {
56
+ if (!cfg || cfg.root === undefined || cfg.root === null) continue;
57
+ if (cfg.root === '') continue;
58
+ const abs = path.normalize(path.resolve(angularRoot, cfg.root));
59
+ if (abs === entryNorm) {
60
+ console.log(` [resolver] matched by root path → "${key}"`);
61
+ return key;
62
+ }
63
+ }
64
+
65
+ // 2) npm "name" === angular project key (e.g. @cloudidesys/cloud-ide-academics) — required for ng CLI
66
+ if (pkg.name && projects[pkg.name]) {
67
+ console.log(` [resolver] matched by package name → "${pkg.name}"`);
68
+ return pkg.name;
69
+ }
70
+
71
+ // 3) Unscoped key matching folder name (e.g. "cloud-ide-accounts")
72
+ if (projects[folderName]) {
73
+ console.log(` [resolver] matched by folder name → "${folderName}"`);
74
+ return folderName;
75
+ }
76
+
77
+ // 4) Root path basename matches folder (stale/moved paths): return angular *key*, not basename
78
+ for (const [key, cfg] of Object.entries(projects)) {
79
+ if (!cfg || !cfg.root) continue;
80
+ const base = path.basename(path.normalize(cfg.root).replace(/[/\\]+$/, ''));
81
+ if (base === folderName) {
82
+ console.log(` [resolver] matched by root basename → "${key}"`);
83
+ return key;
84
+ }
85
+ }
86
+
87
+ // Fallback: use disk folder name. This will fail if the angular.json key is scoped
88
+ // (e.g. @cloudidesys/cloud-ide-academics) — check that angularRoot points to the
89
+ // correct workspace and that angular.json is up to date.
90
+ console.warn(` [resolver] WARNING: no match found in ${angularRoot}/angular.json — falling back to folder name "${folderName}"`);
91
+ return folderName;
92
+ }
93
+
94
+ module.exports = { resolveNgProjectNameForPackageDir, getAngularProjects };