forked from lerna/lerna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BootstrapCommand.js
188 lines (146 loc) · 6.94 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import pathExists from "path-exists";
import assert from "assert";
import path from "path";
import fs from "fs";
import ChildProcessUtilities from "../src/ChildProcessUtilities";
import BootstrapCommand from "../src/commands/BootstrapCommand";
import exitWithCode from "./_exitWithCode";
import initFixture from "./_initFixture";
import stub from "./_stub";
import assertStubbedCalls from "./_assertStubbedCalls";
describe("BootstrapCommand", () => {
describe("dependencies between packages in the repo", () => {
let testDir;
beforeEach(done => {
testDir = initFixture("BootstrapCommand/basic", done);
});
it("should bootstrap files", done => {
const bootstrapCommand = new BootstrapCommand([], {});
bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
assertStubbedCalls([
[ChildProcessUtilities, "spawn", { nodeCallback: true }, [
{ args: ["npm", ["install", "package-1@^0.0.0"], { cwd: path.join(testDir, "packages/package-4"), stdio: "ignore" }] }
]]
]);
bootstrapCommand.runCommand(exitWithCode(0, err => {
if (err) return done(err);
try {
assert.ok(!pathExists.sync(path.join(testDir, "lerna-debug.log")), "lerna-debug.log should not exist");
assert.ok(pathExists.sync(path.join(testDir, "packages/package-1/node_modules")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-2/node_modules")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-3/node_modules")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-4/node_modules")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-2/node_modules/package-1")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-2/node_modules/package-1/index.js")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-2/node_modules/package-1/package.json")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-3/node_modules/package-2")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-3/node_modules/package-2/index.js")));
assert.ok(pathExists.sync(path.join(testDir, "packages/package-3/node_modules/package-2/package.json")));
// Should not exist because mis-matched version
assert.ok(!pathExists.sync(path.join(testDir, "packages/package-4/node_modules/package-1")));
assert.equal(fs.readFileSync(path.join(testDir, "packages/package-2/node_modules/package-1/index.js")).toString(), "/**\n * @prefix\n */\nmodule.exports = require(\"" + path.join(testDir, "packages/package-1") + "\");\n");
assert.equal(fs.readFileSync(path.join(testDir, "packages/package-2/node_modules/package-1/package.json")).toString(), "{\n \"name\": \"package-1\",\n \"version\": \"1.0.0\"\n}\n");
assert.equal(fs.readFileSync(path.join(testDir, "packages/package-3/node_modules/package-2/index.js")).toString(), "/**\n * @prefix\n */\nmodule.exports = require(\"" + path.join(testDir, "packages/package-2") + "\");\n");
assert.equal(fs.readFileSync(path.join(testDir, "packages/package-3/node_modules/package-2/package.json")).toString(), "{\n \"name\": \"package-2\",\n \"version\": \"1.0.0\",\n \"main\": \"lib/index.js\"\n}\n");
done();
} catch (err) {
done(err);
}
}));
});
it("should not bootstrap an ignored package", done => {
const bootstrapCommand = new BootstrapCommand([], {
ignore: "package-2"
});
bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
assertStubbedCalls([
[ChildProcessUtilities, "spawn", { nodeCallback: true }, [
{ args: ["npm", ["install", "package-1@^0.0.0"], { cwd: path.join(testDir, "packages/package-4"), stdio: "ignore" }] }
]]
]);
bootstrapCommand.runCommand(exitWithCode(0, err => {
if (err) return done(err);
try {
assert.ok(!pathExists.sync(path.join(testDir, "lerna-debug.log")), "lerna-debug.log should not exist");
assert.ok(!pathExists.sync(path.join(testDir, "packages/package-2/node_modules/package-1")));
done();
} catch (err) {
done(err);
}
}));
});
it("should not bootstrap ignored packages", done => {
const bootstrapCommand = new BootstrapCommand([], {
ignore: "package-@(3|4)"
});
bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
bootstrapCommand.runCommand(exitWithCode(0, err => {
if (err) return done(err);
try {
assert.ok(!pathExists.sync(path.join(testDir, "lerna-debug.log")), "lerna-debug.log should not exist");
assert.ok(!pathExists.sync(path.join(testDir, "packages/package-3/node_modules/package-2")));
done();
} catch (err) {
done(err);
}
}));
});
});
describe("external dependencies that haven't been installed", () => {
let testDir;
beforeEach(done => {
testDir = initFixture("BootstrapCommand/cold", done);
});
it("should get installed", done => {
const bootstrapCommand = new BootstrapCommand([], {});
bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
let installed = false;
stub(ChildProcessUtilities, "spawn", (command, args, options, callback) => {
assert.deepEqual(args, ["install", "external@^1.0.0"])
assert.deepEqual(options, { cwd: path.join(testDir, "packages/package-1"), stdio: "ignore" })
installed = true;
callback();
});
bootstrapCommand.runCommand(exitWithCode(0, err => {
if (err) return done(err);
try {
assert.ok(!pathExists.sync(path.join(testDir, "lerna-debug.log")), "lerna-debug.log should not exist");
assert.ok(installed, "The external dependency was installed");
done();
} catch (err) {
done(err);
}
}));
});
});
describe("external dependencies that have already been installed", () => {
let testDir;
beforeEach(done => {
testDir = initFixture("BootstrapCommand/warm", done);
});
it("should not get re-installed", done => {
const bootstrapCommand = new BootstrapCommand([], {});
bootstrapCommand.runValidations();
bootstrapCommand.runPreparations();
let installed = false;
stub(ChildProcessUtilities, "spawn", (command, args, options, callback) => {
installed = true;
callback();
});
bootstrapCommand.runCommand(exitWithCode(0, err => {
if (err) return done(err);
try {
assert.ok(!pathExists.sync(path.join(testDir, "lerna-debug.log")), "lerna-debug.log should not exist");
assert.ok(!installed, "The external dependency was not installed");
done();
} catch (err) {
done(err);
}
}));
});
});
});