forked from lerna/lerna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (63 loc) · 2.49 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"use strict";
const path = require("path");
const pMap = require("p-map");
const slash = require("slash");
const { Command } = require("@lerna/command");
const { PackageGraph } = require("@lerna/package-graph");
const { symlinkDependencies } = require("@lerna/symlink-dependencies");
module.exports = factory;
function factory(argv) {
return new LinkCommand(argv);
}
class LinkCommand extends Command {
get requiresGit() {
return false;
}
initialize() {
this.allPackages = this.packageGraph.rawPackageList;
if (this.options.contents) {
// globally override directory to link
for (const pkg of this.allPackages) {
pkg.contents = this.options.contents;
}
}
this.targetGraph = this.options.forceLocal
? new PackageGraph(this.allPackages, "allDependencies", "forceLocal")
: this.packageGraph;
}
execute() {
if (this.options._.pop() === "convert") {
return this.convertLinksToFileSpecs();
}
return symlinkDependencies(this.allPackages, this.targetGraph, this.logger.newItem("link dependencies"));
}
convertLinksToFileSpecs() {
const rootPkg = this.project.manifest;
const rootDependencies = {};
const hoisted = {};
const changed = new Set();
const savePrefix = "file:";
for (const targetNode of this.targetGraph.values()) {
const resolved = { name: targetNode.name, type: "directory" };
// install root file: specifiers to avoid bootstrap
rootDependencies[targetNode.name] = targetNode.pkg.resolved.saveSpec;
for (const depNode of targetNode.localDependents.values()) {
const depVersion = slash(path.relative(depNode.pkg.location, targetNode.pkg.location));
// console.log("\n%s\n %j: %j", depNode.name, name, `${savePrefix}${depVersion}`);
depNode.pkg.updateLocalDependency(resolved, depVersion, savePrefix);
changed.add(depNode);
}
if (targetNode.pkg.devDependencies) {
// hoist _all_ devDependencies to the root
Object.assign(hoisted, targetNode.pkg.devDependencies);
targetNode.pkg.set("devDependencies", {});
changed.add(targetNode);
}
}
// mutate project manifest, completely overwriting existing dependencies
rootPkg.set("dependencies", rootDependencies);
rootPkg.set("devDependencies", Object.assign(rootPkg.get("devDependencies") || {}, hoisted));
return pMap(changed, (node) => node.pkg.serialize()).then(() => rootPkg.serialize());
}
}
module.exports.LinkCommand = LinkCommand;