/
BootstrapCommand.js
161 lines (134 loc) · 4.65 KB
/
BootstrapCommand.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import FileSystemUtilities from "../FileSystemUtilities";
import NpmUtilities from "../NpmUtilities";
import PackageUtilities from "../PackageUtilities";
import Command from "../Command";
import semver from "semver";
import async from "async";
import find from "lodash.find";
import path from "path";
export default class BootstrapCommand extends Command {
initialize(callback) {
// Nothing to do...
callback(null, true);
}
execute(callback) {
this.linkDependencies(err => {
if (err) {
callback(err);
} else {
this.logger.success("Successfully bootstrapped " + this.packages.length + " packages.");
callback(null, true);
}
});
}
linkDependencies(callback) {
this.progressBar.init(this.packages.length);
this.logger.info("Linking all dependencies");
const ignore = this.flags.ignore || this.repository.bootstrapConfig.ignore;
async.parallelLimit(PackageUtilities.filterPackages(this.packages, ignore, true).map(pkg => done => {
async.series([
cb => FileSystemUtilities.mkdirp(pkg.nodeModulesLocation, cb),
cb => this.installExternalPackages(pkg, cb),
cb => this.linkDependenciesForPackage(pkg, cb)
], err => {
this.progressBar.tick(pkg.name);
done(err);
});
}), this.concurrency, err => {
this.progressBar.terminate();
callback(err);
});
}
linkDependenciesForPackage(pkg, callback) {
async.each(this.packages, (dependency, done) => {
if (!this.hasMatchingDependency(pkg, dependency, true)) return done();
const linkSrc = dependency.location;
const linkDest = path.join(pkg.nodeModulesLocation, dependency.name);
this.createLinkedDependency(linkSrc, linkDest, dependency.name, done);
}, callback);
}
createLinkedDependency(src, dest, name, callback) {
FileSystemUtilities.rimraf(dest, err => {
if (err) {
return callback(err);
}
FileSystemUtilities.mkdirp(dest, err => {
if (err) {
return callback(err);
}
this.createLinkedDependencyFiles(src, dest, name, callback);
});
});
}
createLinkedDependencyFiles(src, dest, name, callback) {
const srcPackageJsonLocation = path.join(src, "package.json");
const destPackageJsonLocation = path.join(dest, "package.json");
const destIndexJsLocation = path.join(dest, "index.js");
const packageJsonFileContents = JSON.stringify({
name: name,
version: require(srcPackageJsonLocation).version
}, null, " ");
const prefix = this.repository.linkedFiles.prefix || "";
const indexJsFileContents = prefix + "module.exports = require(" + JSON.stringify(src) + ");";
FileSystemUtilities.writeFile(destPackageJsonLocation, packageJsonFileContents, err => {
if (err) {
return callback(err);
}
FileSystemUtilities.writeFile(destIndexJsLocation, indexJsFileContents, callback);
});
}
installExternalPackages(pkg, callback) {
const allDependencies = pkg.allDependencies;
const externalPackages = Object.keys(allDependencies)
.filter(dependency => {
const match = find(this.packages, pkg => {
return pkg.name === dependency;
});
return !(match && this.hasMatchingDependency(pkg, match));
})
.filter(dependency => {
return !this.hasDependencyInstalled(pkg, dependency);
})
.map(dependency => {
// Needs "quotes" for versions like "^1.0.0 || ^2.0.0"
return `${dependency}@"${allDependencies[dependency]}"`;
});
if (externalPackages.length) {
NpmUtilities.installInDir(pkg.location, externalPackages, callback);
} else {
callback();
}
}
hasMatchingDependency(pkg, dependency, showWarning = false) {
const expectedVersion = pkg.allDependencies[dependency.name];
const actualVersion = dependency.version;
if (!expectedVersion) {
return false;
}
if (this.isCompatableVersion(actualVersion, expectedVersion)) {
return true;
}
if (showWarning) {
this.logger.warning(
`Version mismatch inside "${pkg.name}". ` +
`Depends on "${dependency.name}@${expectedVersion}" ` +
`instead of "${dependency.name}@${actualVersion}".`
);
}
return false;
}
hasDependencyInstalled(pkg, dependency) {
const packageJson = path.join(pkg.nodeModulesLocation, dependency, "package.json");
try {
return this.isCompatableVersion(
require(packageJson).version,
pkg.allDependencies[dependency]
);
} catch (e) {
return false;
}
}
isCompatableVersion(actual, expected) {
return semver.satisfies(actual, expected);
}
}