@teambit/workspace.modules.node-modules-linker 0.0.253 → 0.0.255
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/codemod-components.js +147 -115
- package/dist/codemod-components.js.map +1 -1
- package/dist/index.js +55 -9
- package/dist/index.js.map +1 -1
- package/dist/node-modules-linker.d.ts +1 -1
- package/dist/node-modules-linker.js +335 -296
- package/dist/node-modules-linker.js.map +1 -1
- package/dist/package-json-transformer.js +75 -63
- package/dist/package-json-transformer.js.map +1 -1
- package/package.json +15 -15
- package/types/asset.d.ts +15 -3
- package/dist/preview-1738120666692.js +0 -7
|
@@ -1,325 +1,364 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
17
5
|
});
|
|
18
|
-
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
-
};
|
|
37
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
-
exports.linkToNodeModulesWithCodemod = linkToNodeModulesWithCodemod;
|
|
39
|
-
exports.linkToNodeModulesByIds = linkToNodeModulesByIds;
|
|
6
|
+
exports.default = void 0;
|
|
40
7
|
exports.linkToNodeModulesByComponents = linkToNodeModulesByComponents;
|
|
8
|
+
exports.linkToNodeModulesByIds = linkToNodeModulesByIds;
|
|
9
|
+
exports.linkToNodeModulesWithCodemod = linkToNodeModulesWithCodemod;
|
|
41
10
|
exports.removeLinksFromNodeModules = removeLinksFromNodeModules;
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
11
|
+
function _fsExtra() {
|
|
12
|
+
const data = _interopRequireDefault(require("fs-extra"));
|
|
13
|
+
_fsExtra = function () {
|
|
14
|
+
return data;
|
|
15
|
+
};
|
|
16
|
+
return data;
|
|
17
|
+
}
|
|
18
|
+
function _pMapSeries() {
|
|
19
|
+
const data = _interopRequireDefault(require("p-map-series"));
|
|
20
|
+
_pMapSeries = function () {
|
|
21
|
+
return data;
|
|
22
|
+
};
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
function path() {
|
|
26
|
+
const data = _interopRequireWildcard(require("path"));
|
|
27
|
+
path = function () {
|
|
28
|
+
return data;
|
|
29
|
+
};
|
|
30
|
+
return data;
|
|
31
|
+
}
|
|
32
|
+
function _workspace() {
|
|
33
|
+
const data = require("@teambit/workspace.root-components");
|
|
34
|
+
_workspace = function () {
|
|
35
|
+
return data;
|
|
36
|
+
};
|
|
37
|
+
return data;
|
|
38
|
+
}
|
|
39
|
+
function _legacy() {
|
|
40
|
+
const data = require("@teambit/legacy.constants");
|
|
41
|
+
_legacy = function () {
|
|
42
|
+
return data;
|
|
43
|
+
};
|
|
44
|
+
return data;
|
|
45
|
+
}
|
|
46
|
+
function _component() {
|
|
47
|
+
const data = require("@teambit/component.sources");
|
|
48
|
+
_component = function () {
|
|
49
|
+
return data;
|
|
50
|
+
};
|
|
51
|
+
return data;
|
|
52
|
+
}
|
|
53
|
+
function _legacy2() {
|
|
54
|
+
const data = require("@teambit/legacy.logger");
|
|
55
|
+
_legacy2 = function () {
|
|
56
|
+
return data;
|
|
57
|
+
};
|
|
58
|
+
return data;
|
|
59
|
+
}
|
|
60
|
+
function _pkgModules() {
|
|
61
|
+
const data = require("@teambit/pkg.modules.component-package-name");
|
|
62
|
+
_pkgModules = function () {
|
|
63
|
+
return data;
|
|
64
|
+
};
|
|
65
|
+
return data;
|
|
66
|
+
}
|
|
67
|
+
function _componentPackageVersion() {
|
|
68
|
+
const data = require("@teambit/component-package-version");
|
|
69
|
+
_componentPackageVersion = function () {
|
|
70
|
+
return data;
|
|
71
|
+
};
|
|
72
|
+
return data;
|
|
73
|
+
}
|
|
74
|
+
function _packageJsonTransformer() {
|
|
75
|
+
const data = require("./package-json-transformer");
|
|
76
|
+
_packageJsonTransformer = function () {
|
|
77
|
+
return data;
|
|
78
|
+
};
|
|
79
|
+
return data;
|
|
80
|
+
}
|
|
81
|
+
function _codemodComponents() {
|
|
82
|
+
const data = require("./codemod-components");
|
|
83
|
+
_codemodComponents = function () {
|
|
84
|
+
return data;
|
|
85
|
+
};
|
|
86
|
+
return data;
|
|
87
|
+
}
|
|
88
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
89
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
90
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
53
91
|
/**
|
|
54
92
|
* link given components to node_modules, so it's possible to use absolute link instead of relative
|
|
55
93
|
* for example, require('@bit/remote-scope.bar.foo)
|
|
56
94
|
*/
|
|
57
95
|
class NodeModuleLinker {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const workspacePath = this.workspace.path;
|
|
79
|
-
links.addBasePath(workspacePath);
|
|
80
|
-
yield links.persistAllToFS();
|
|
81
|
-
// Only clear cache if new package.json of components were created
|
|
82
|
-
if (this.packageJsonCreated) {
|
|
83
|
-
yield Promise.all(this.components.map((component) => { var _a; return (_a = this.consumer) === null || _a === void 0 ? void 0 : _a.componentFsCache.deleteDependenciesDataCache(component.id.toString()); }));
|
|
84
|
-
// if this cache is not cleared, then when asking workspace.get again to the same component, it returns it with
|
|
85
|
-
// component-issues like "MissingLinksFromNodeModulesToSrc" incorrectly.
|
|
86
|
-
this.workspace.clearAllComponentsCache();
|
|
87
|
-
}
|
|
88
|
-
yield (0, workspace_root_components_1.linkPkgsToRootComponents)({
|
|
89
|
-
rootComponentsPath: this.workspace.rootComponentsPath,
|
|
90
|
-
workspacePath,
|
|
91
|
-
}, this.components.map((comp) => (0, pkg_modules_component_package_name_1.componentIdToPackageName)(comp.state._consumer)));
|
|
92
|
-
return linksResults;
|
|
93
|
-
});
|
|
96
|
+
// preparation for the capsule, which is going to have only BitMap with no Consumer
|
|
97
|
+
|
|
98
|
+
constructor(components, workspace) {
|
|
99
|
+
this.components = components;
|
|
100
|
+
this.workspace = workspace;
|
|
101
|
+
this.consumer = this.workspace.consumer;
|
|
102
|
+
this.bitMap = this.consumer.bitMap;
|
|
103
|
+
this.dataToPersist = new (_component().DataToPersist)();
|
|
104
|
+
this.existingLinks = [];
|
|
105
|
+
this.packageJsonCreated = false;
|
|
106
|
+
}
|
|
107
|
+
async link() {
|
|
108
|
+
this.components = this.components.filter(component => this.bitMap.getComponentIfExist(component.id));
|
|
109
|
+
const links = await this.getLinks();
|
|
110
|
+
const linksResults = this.getLinksResults();
|
|
111
|
+
if (!linksResults.length) {
|
|
112
|
+
// avoid clearing the cache if it ends up with no links. (e.g. happens when mistakenly generating links for a
|
|
113
|
+
// component not in the workspace)
|
|
114
|
+
// or when all links are already exist.
|
|
115
|
+
return [];
|
|
94
116
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
});
|
|
117
|
+
const workspacePath = this.workspace.path;
|
|
118
|
+
links.addBasePath(workspacePath);
|
|
119
|
+
await links.persistAllToFS();
|
|
120
|
+
// Only clear cache if new package.json of components were created
|
|
121
|
+
if (this.packageJsonCreated) {
|
|
122
|
+
await Promise.all(this.components.map(component => this.consumer?.componentFsCache.deleteDependenciesDataCache(component.id.toString())));
|
|
123
|
+
// if this cache is not cleared, then when asking workspace.get again to the same component, it returns it with
|
|
124
|
+
// component-issues like "MissingLinksFromNodeModulesToSrc" incorrectly.
|
|
125
|
+
this.workspace.clearAllComponentsCache();
|
|
105
126
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
127
|
+
await (0, _workspace().linkPkgsToRootComponents)({
|
|
128
|
+
rootComponentsPath: this.workspace.rootComponentsPath,
|
|
129
|
+
workspacePath
|
|
130
|
+
}, this.components.map(comp => (0, _pkgModules().componentIdToPackageName)(comp.state._consumer)));
|
|
131
|
+
return linksResults;
|
|
132
|
+
}
|
|
133
|
+
async getLinks() {
|
|
134
|
+
this.dataToPersist = new (_component().DataToPersist)();
|
|
135
|
+
await (0, _pMapSeries().default)(this.components, async component => {
|
|
136
|
+
const componentId = component.id.toString();
|
|
137
|
+
_legacy2().logger.debug(`linking component to node_modules: ${componentId}`);
|
|
138
|
+
await this._populateComponentsLinks(component);
|
|
139
|
+
});
|
|
140
|
+
return this.dataToPersist;
|
|
141
|
+
}
|
|
142
|
+
addLinkResult(linksResults, id, from, to, existsBefore) {
|
|
143
|
+
if (!id) return;
|
|
144
|
+
const existingLinkResult = linksResults.find(linkResult => linkResult.id.isEqual(id));
|
|
145
|
+
if (existingLinkResult) {
|
|
146
|
+
existingLinkResult.bound.push({
|
|
147
|
+
from,
|
|
148
|
+
to,
|
|
149
|
+
existsBefore
|
|
150
|
+
});
|
|
151
|
+
} else {
|
|
152
|
+
linksResults.push({
|
|
153
|
+
id,
|
|
154
|
+
bound: [{
|
|
155
|
+
from,
|
|
156
|
+
to,
|
|
157
|
+
existsBefore
|
|
158
|
+
}]
|
|
159
|
+
});
|
|
116
160
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
161
|
+
}
|
|
162
|
+
getLinksResults() {
|
|
163
|
+
const linksResults = [];
|
|
164
|
+
this.dataToPersist.symlinks.forEach(symlink => {
|
|
165
|
+
this.addLinkResult(linksResults, symlink.componentId, symlink.src, symlink.dest, false);
|
|
166
|
+
});
|
|
167
|
+
this.existingLinks.forEach(link => {
|
|
168
|
+
const componentId = link.id;
|
|
169
|
+
link.bound.forEach(bound => {
|
|
170
|
+
this.addLinkResult(linksResults, componentId, bound.from, bound.to, true);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
this.components.forEach(component => {
|
|
174
|
+
const existingLinkResult = linksResults.find(linkResult => linkResult.id.isEqual(component.id));
|
|
175
|
+
if (!existingLinkResult) {
|
|
176
|
+
linksResults.push({
|
|
177
|
+
id: component.id,
|
|
178
|
+
bound: []
|
|
133
179
|
});
|
|
134
|
-
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return linksResults;
|
|
183
|
+
}
|
|
184
|
+
_getDefaultScope(component) {
|
|
185
|
+
if (component) {
|
|
186
|
+
return component.defaultScope;
|
|
135
187
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
188
|
+
return this.consumer ? this.consumer.config.defaultScope : null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* even when an authored component has rootDir, we can't just symlink that rootDir to
|
|
193
|
+
* node_modules/rootDir. it could work only when the main-file is index.js, not for other cases.
|
|
194
|
+
* node expects the module inside node_modules to have either package.json with valid "main"
|
|
195
|
+
* property or an index.js file. this main property can't be relative.
|
|
196
|
+
*/
|
|
197
|
+
async _populateComponentsLinks(component) {
|
|
198
|
+
const legacyComponent = component.state._consumer;
|
|
199
|
+
const linkPath = (0, _pkgModules().getNodeModulesPathOfComponent)(legacyComponent);
|
|
200
|
+
await this.symlinkComponentDir(component, linkPath);
|
|
201
|
+
this._deleteExistingLinksRootIfSymlink(linkPath);
|
|
202
|
+
await this.createPackageJson(component);
|
|
203
|
+
}
|
|
204
|
+
async symlinkComponentDir(component, linkPath) {
|
|
205
|
+
const componentMap = this.bitMap.getComponent(component.id);
|
|
206
|
+
const filesToBind = componentMap.getAllFilesPaths();
|
|
207
|
+
await Promise.all(filesToBind.map(async file => {
|
|
208
|
+
const fileWithRootDir = path().join(componentMap.rootDir, file);
|
|
209
|
+
const dest = path().join(linkPath, file);
|
|
210
|
+
let stat;
|
|
211
|
+
try {
|
|
212
|
+
stat = await _fsExtra().default.lstat(dest);
|
|
213
|
+
} catch {
|
|
214
|
+
// Ignore this error, it's probably because the file doesn't exist
|
|
215
|
+
}
|
|
216
|
+
if (stat && stat.isSymbolicLink()) {
|
|
217
|
+
this.addLinkResult(this.existingLinks, component.id, fileWithRootDir, dest, true);
|
|
218
|
+
} else {
|
|
219
|
+
this.dataToPersist.addSymlink(_component().Symlink.makeInstance(fileWithRootDir, dest, component.id, true));
|
|
220
|
+
}
|
|
221
|
+
}));
|
|
222
|
+
if (_legacy().IS_WINDOWS) {
|
|
223
|
+
// symlink the entire source directory into "_src" in node-modules.
|
|
224
|
+
this.dataToPersist.addSymlink(_component().Symlink.makeInstance(componentMap.rootDir, path().join(linkPath, _legacy().SOURCE_DIR_SYMLINK_TO_NM), component.id));
|
|
141
225
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Removing existing links root (the package path) - to handle cases it was linked by package manager for example
|
|
230
|
+
* this makes sure we are not affecting other places (like package manager cache) we shouldn't touch
|
|
231
|
+
* If you have a case when this deletes something created by the package manager and it's not the desired behavior,
|
|
232
|
+
* do not delete this code, but make sure the package manger nest the installed version into it's dependent
|
|
233
|
+
* @param component
|
|
234
|
+
*/
|
|
235
|
+
_deleteExistingLinksRootIfSymlink(linkPath) {
|
|
236
|
+
try {
|
|
237
|
+
const stat = _fsExtra().default.lstatSync(linkPath);
|
|
238
|
+
if (stat.isSymbolicLink()) {
|
|
239
|
+
this.dataToPersist.removePath(new (_component().RemovePath)(linkPath));
|
|
240
|
+
}
|
|
241
|
+
return undefined;
|
|
242
|
+
} catch {
|
|
243
|
+
return undefined; // probably file does not exist
|
|
156
244
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
})));
|
|
178
|
-
if (legacy_constants_1.IS_WINDOWS) {
|
|
179
|
-
// symlink the entire source directory into "_src" in node-modules.
|
|
180
|
-
this.dataToPersist.addSymlink(component_sources_1.Symlink.makeInstance(componentMap.rootDir, path.join(linkPath, legacy_constants_1.SOURCE_DIR_SYMLINK_TO_NM), component.id));
|
|
181
|
-
}
|
|
182
|
-
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* create package.json on node_modules/@bit/component-name/package.json with a property 'main'
|
|
249
|
+
* pointing to the component's main file.
|
|
250
|
+
* It is needed for Authored components only.
|
|
251
|
+
* Since an authored component doesn't have rootDir, it's impossible to symlink to the component directory.
|
|
252
|
+
* It makes it easier for Author to use absolute syntax between their own components.
|
|
253
|
+
*/
|
|
254
|
+
async createPackageJson(component) {
|
|
255
|
+
const legacyComp = component.state._consumer;
|
|
256
|
+
const hasPackageJsonAsComponentFile = legacyComp.files.some(file => file.relative === _legacy().PACKAGE_JSON);
|
|
257
|
+
if (hasPackageJsonAsComponentFile) return; // don't generate package.json on top of the user package.json
|
|
258
|
+
const dest = path().join((0, _pkgModules().getNodeModulesPathOfComponent)({
|
|
259
|
+
...legacyComp,
|
|
260
|
+
id: legacyComp.id
|
|
261
|
+
}));
|
|
262
|
+
const packageJsonExist = await _fsExtra().default.pathExists(path().join(dest, 'package.json'));
|
|
263
|
+
if (!packageJsonExist) {
|
|
264
|
+
this.packageJsonCreated = true;
|
|
183
265
|
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
_deleteExistingLinksRootIfSymlink(linkPath) {
|
|
192
|
-
try {
|
|
193
|
-
const stat = fs_extra_1.default.lstatSync(linkPath);
|
|
194
|
-
if (stat.isSymbolicLink()) {
|
|
195
|
-
this.dataToPersist.removePath(new component_sources_1.RemovePath(linkPath));
|
|
196
|
-
}
|
|
197
|
-
return undefined;
|
|
198
|
-
}
|
|
199
|
-
catch (_a) {
|
|
200
|
-
return undefined; // probably file does not exist
|
|
201
|
-
}
|
|
266
|
+
const packageJson = _component().PackageJsonFile.createFromComponent(dest, legacyComp, true);
|
|
267
|
+
await this._applyTransformers(component, packageJson);
|
|
268
|
+
if (_legacy().IS_WINDOWS) {
|
|
269
|
+
// in the workspace, override the "types" and add the "src" prefix.
|
|
270
|
+
// otherwise, the navigation and auto-complete won't work on the IDE.
|
|
271
|
+
// this is for Windows only. For Linux, we use symlinks for the files.
|
|
272
|
+
packageJson.addOrUpdateProperty('types', `${_legacy().SOURCE_DIR_SYMLINK_TO_NM}/${legacyComp.mainFile}`);
|
|
202
273
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
* Since an authored component doesn't have rootDir, it's impossible to symlink to the component directory.
|
|
208
|
-
* It makes it easier for Author to use absolute syntax between their own components.
|
|
209
|
-
*/
|
|
210
|
-
createPackageJson(component) {
|
|
211
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
212
|
-
const legacyComp = component.state._consumer;
|
|
213
|
-
const hasPackageJsonAsComponentFile = legacyComp.files.some((file) => file.relative === legacy_constants_1.PACKAGE_JSON);
|
|
214
|
-
if (hasPackageJsonAsComponentFile)
|
|
215
|
-
return; // don't generate package.json on top of the user package.json
|
|
216
|
-
const dest = path.join((0, pkg_modules_component_package_name_1.getNodeModulesPathOfComponent)(Object.assign(Object.assign({}, legacyComp), { id: legacyComp.id })));
|
|
217
|
-
const packageJsonExist = yield fs_extra_1.default.pathExists(path.join(dest, 'package.json'));
|
|
218
|
-
if (!packageJsonExist) {
|
|
219
|
-
this.packageJsonCreated = true;
|
|
220
|
-
}
|
|
221
|
-
const packageJson = component_sources_1.PackageJsonFile.createFromComponent(dest, legacyComp, true);
|
|
222
|
-
yield this._applyTransformers(component, packageJson);
|
|
223
|
-
if (legacy_constants_1.IS_WINDOWS) {
|
|
224
|
-
// in the workspace, override the "types" and add the "src" prefix.
|
|
225
|
-
// otherwise, the navigation and auto-complete won't work on the IDE.
|
|
226
|
-
// this is for Windows only. For Linux, we use symlinks for the files.
|
|
227
|
-
packageJson.addOrUpdateProperty('types', `${legacy_constants_1.SOURCE_DIR_SYMLINK_TO_NM}/${legacyComp.mainFile}`);
|
|
228
|
-
}
|
|
229
|
-
if (packageJson.packageJsonObject.version === 'latest') {
|
|
230
|
-
packageJson.packageJsonObject.version = '0.0.1-new';
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
packageJson.packageJsonObject.version = (0, component_package_version_1.snapToSemver)(packageJson.packageJsonObject.version);
|
|
234
|
-
}
|
|
235
|
-
// indicate that this component exists locally and it is symlinked into the workspace. not a normal package.
|
|
236
|
-
packageJson.packageJsonObject._bit_local = true;
|
|
237
|
-
packageJson.packageJsonObject.source = component.mainFile.relative;
|
|
238
|
-
// This is a hack because we have in the workspace package.json types:index.ts
|
|
239
|
-
// but also exports for core aspects
|
|
240
|
-
// TS can't find the types
|
|
241
|
-
// in order to solve it we copy the types to exports.types
|
|
242
|
-
// this will be applied only to aspects to minimize how it affects users
|
|
243
|
-
const envsData = component.state.aspects.get('teambit.envs/envs');
|
|
244
|
-
const isAspect = (envsData === null || envsData === void 0 ? void 0 : envsData.data.type) === 'aspect';
|
|
245
|
-
if (isAspect && packageJson.packageJsonObject.types && packageJson.packageJsonObject.exports) {
|
|
246
|
-
const exports = packageJson.packageJsonObject.exports['.']
|
|
247
|
-
? packageJson.packageJsonObject.exports['.']
|
|
248
|
-
: packageJson.packageJsonObject.exports;
|
|
249
|
-
if (!exports.types) {
|
|
250
|
-
const defaultModule = exports.default;
|
|
251
|
-
if (defaultModule)
|
|
252
|
-
delete exports.default;
|
|
253
|
-
exports.types = `./${packageJson.packageJsonObject.types}`;
|
|
254
|
-
exports.default = defaultModule;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
// packageJson.mergePropsFromExtensions(component);
|
|
258
|
-
// TODO: we need to have an hook here to get the transformer from the pkg extension
|
|
259
|
-
// don't delete the "version" prop, because in some scenarios, it's needed for the component to work properly.
|
|
260
|
-
// an example is when developing a vscode extension, vscode expects to have a valid package.json during the development.
|
|
261
|
-
this.dataToPersist.addFile(packageJson.toVinylFile());
|
|
262
|
-
const injectedDirs = yield this.workspace.getInjectedDirs(component);
|
|
263
|
-
const src = path.join(dest, 'package.json');
|
|
264
|
-
for (const injectedDir of injectedDirs) {
|
|
265
|
-
this.dataToPersist.addSymlink(component_sources_1.Symlink.makeInstance(src, path.join(injectedDir, 'package.json')));
|
|
266
|
-
}
|
|
267
|
-
});
|
|
274
|
+
if (packageJson.packageJsonObject.version === 'latest') {
|
|
275
|
+
packageJson.packageJsonObject.version = '0.0.1-new';
|
|
276
|
+
} else {
|
|
277
|
+
packageJson.packageJsonObject.version = (0, _componentPackageVersion().snapToSemver)(packageJson.packageJsonObject.version);
|
|
268
278
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
279
|
+
|
|
280
|
+
// indicate that this component exists locally and it is symlinked into the workspace. not a normal package.
|
|
281
|
+
packageJson.packageJsonObject._bit_local = true;
|
|
282
|
+
packageJson.packageJsonObject.source = component.mainFile.relative;
|
|
283
|
+
|
|
284
|
+
// This is a hack because we have in the workspace package.json types:index.ts
|
|
285
|
+
// but also exports for core aspects
|
|
286
|
+
// TS can't find the types
|
|
287
|
+
// in order to solve it we copy the types to exports.types
|
|
288
|
+
// this will be applied only to aspects to minimize how it affects users
|
|
289
|
+
const envsData = component.state.aspects.get('teambit.envs/envs');
|
|
290
|
+
const isAspect = envsData?.data.type === 'aspect';
|
|
291
|
+
if (isAspect && packageJson.packageJsonObject.types && packageJson.packageJsonObject.exports) {
|
|
292
|
+
const exports = packageJson.packageJsonObject.exports['.'] ? packageJson.packageJsonObject.exports['.'] : packageJson.packageJsonObject.exports;
|
|
293
|
+
if (!exports.types) {
|
|
294
|
+
const defaultModule = exports.default;
|
|
295
|
+
if (defaultModule) delete exports.default;
|
|
296
|
+
exports.types = `./${packageJson.packageJsonObject.types}`;
|
|
297
|
+
exports.default = defaultModule;
|
|
298
|
+
}
|
|
276
299
|
}
|
|
300
|
+
|
|
301
|
+
// packageJson.mergePropsFromExtensions(component);
|
|
302
|
+
// TODO: we need to have an hook here to get the transformer from the pkg extension
|
|
303
|
+
|
|
304
|
+
// don't delete the "version" prop, because in some scenarios, it's needed for the component to work properly.
|
|
305
|
+
// an example is when developing a vscode extension, vscode expects to have a valid package.json during the development.
|
|
306
|
+
|
|
307
|
+
this.dataToPersist.addFile(packageJson.toVinylFile());
|
|
308
|
+
const injectedDirs = await this.workspace.getInjectedDirs(component);
|
|
309
|
+
const src = path().join(dest, 'package.json');
|
|
310
|
+
for (const injectedDir of injectedDirs) {
|
|
311
|
+
this.dataToPersist.addSymlink(_component().Symlink.makeInstance(src, path().join(injectedDir, 'package.json')));
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* these are changes made by aspects
|
|
317
|
+
*/
|
|
318
|
+
async _applyTransformers(component, packageJson) {
|
|
319
|
+
return _packageJsonTransformer().PackageJsonTransformer.applyTransformers(component, packageJson);
|
|
320
|
+
}
|
|
277
321
|
}
|
|
278
322
|
exports.default = NodeModuleLinker;
|
|
279
|
-
function linkToNodeModulesWithCodemod(workspace, bitIds, changeRelativeToModulePaths) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
323
|
+
async function linkToNodeModulesWithCodemod(workspace, bitIds, changeRelativeToModulePaths) {
|
|
324
|
+
let codemodResults;
|
|
325
|
+
if (changeRelativeToModulePaths) {
|
|
326
|
+
codemodResults = await (0, _codemodComponents().changeCodeFromRelativeToModulePaths)(workspace, bitIds);
|
|
327
|
+
}
|
|
328
|
+
const linksResults = await linkToNodeModulesByIds(workspace, bitIds);
|
|
329
|
+
return {
|
|
330
|
+
linksResults,
|
|
331
|
+
codemodResults
|
|
332
|
+
};
|
|
288
333
|
}
|
|
289
|
-
function linkToNodeModulesByIds(
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
loadSeedersAsAspects: false,
|
|
300
|
-
loadExtensions: false,
|
|
301
|
-
});
|
|
302
|
-
});
|
|
303
|
-
const components = yield getComponents();
|
|
304
|
-
const nodeModuleLinker = new NodeModuleLinker(components, workspace);
|
|
305
|
-
return nodeModuleLinker.link();
|
|
334
|
+
async function linkToNodeModulesByIds(workspace, componentsIds, loadFromScope = false) {
|
|
335
|
+
if (!componentsIds.length) return [];
|
|
336
|
+
const getComponents = async () => {
|
|
337
|
+
if (loadFromScope) {
|
|
338
|
+
return workspace.scope.getMany(componentsIds);
|
|
339
|
+
}
|
|
340
|
+
return workspace.getMany(componentsIds, {
|
|
341
|
+
idsToNotLoadAsAspects: componentsIds.map(id => id.toString()),
|
|
342
|
+
loadSeedersAsAspects: false,
|
|
343
|
+
loadExtensions: false
|
|
306
344
|
});
|
|
345
|
+
};
|
|
346
|
+
const components = await getComponents();
|
|
347
|
+
const nodeModuleLinker = new NodeModuleLinker(components, workspace);
|
|
348
|
+
return nodeModuleLinker.link();
|
|
307
349
|
}
|
|
308
|
-
function linkToNodeModulesByComponents(components, workspace) {
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
return nodeModuleLinker.link();
|
|
312
|
-
});
|
|
350
|
+
async function linkToNodeModulesByComponents(components, workspace) {
|
|
351
|
+
const nodeModuleLinker = new NodeModuleLinker(components, workspace);
|
|
352
|
+
return nodeModuleLinker.link();
|
|
313
353
|
}
|
|
314
|
-
function removeLinksFromNodeModules(component, workspace, files) {
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
legacy_logger_1.logger.debug(`removeLinksFromNodeModules, deleting the following files:
|
|
354
|
+
async function removeLinksFromNodeModules(component, workspace, files) {
|
|
355
|
+
const absoluteCompDir = workspace.componentDir(component.id); // os format
|
|
356
|
+
const relativeFilesInsideCompDir = files.map(file => path().relative(absoluteCompDir, file));
|
|
357
|
+
const pkgDir = await workspace.getComponentPackagePath(component);
|
|
358
|
+
const pathsToRemove = relativeFilesInsideCompDir.map(file => path().join(pkgDir, file));
|
|
359
|
+
_legacy2().logger.debug(`removeLinksFromNodeModules, deleting the following files:
|
|
321
360
|
${pathsToRemove.join('\n')}`);
|
|
322
|
-
|
|
323
|
-
});
|
|
361
|
+
await Promise.all(pathsToRemove.map(file => _fsExtra().default.remove(file)));
|
|
324
362
|
}
|
|
363
|
+
|
|
325
364
|
//# sourceMappingURL=node-modules-linker.js.map
|