forked from yarnpkg/yarn
/
offline-mirror.js
353 lines (295 loc) 路 17.3 KB
/
offline-mirror.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* @flow */
import {runInstall, getPackageVersion, explodeLockfile} from '../_helpers.js';
import {Install} from '../../../src/cli/commands/install.js';
import Lockfile from '../../../src/lockfile';
import {parse} from '../../../src/lockfile';
import * as fs from '../../../src/util/fs.js';
const path = require('path');
const semver = require('semver');
jest.mock('../../../src/util/package-name-utils');
const nameUtils = jest.requireMock('../../../src/util/package-name-utils');
beforeEach(() => {
// doing one time mock for one test is tricky,
// found this workaround https://github.com/facebook/jest/issues/2649#issuecomment-360467278
nameUtils.getSystemParams.mockImplementation(
jest.requireActual('../../../src/util/package-name-utils').getSystemParams,
);
nameUtils.getPlatformSpecificPackageFilename.mockImplementation(
jest.requireActual('../../../src/util/package-name-utils').getPlatformSpecificPackageFilename,
);
});
jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;
test.concurrent(
'install with offline mirror and pack-built-packages setting should run install' +
' scripts on first call and not run on second while producing the same node_modules',
(): Promise<void> => {
return runInstall({ignoreScripts: true}, 'install-offline-built-artifacts', async (config, reporter) => {
// install scripts were not run
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(false);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(false);
// enable packing of built artifacts
config.packBuiltPackages = true;
config.ignoreScripts = false;
// after first run we observe both package and global side effects
let reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(true);
// after second run we observe only package side effects because offline mirror was used
await fs.unlink(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'));
await fs.unlink(path.join(config.cwd, 'module-a-build.log'));
reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(false);
});
},
);
test.concurrent(
'install with offline mirror and pack-built-packages setting should not ignore ".npmignore"',
(): Promise<void> => {
return runInstall({ignoreScripts: true}, 'install-offline-built-artifacts-no-ignores', async (config, reporter) => {
// install scripts were not run
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'build', 'build-artifact.so'))).toEqual(
false,
);
// enable packing of built artifacts
config.packBuiltPackages = true;
config.ignoreScripts = false;
// after first run we observe package side effects
let reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'build', 'module-a-build.log'))).toEqual(
true,
);
// after second run we observe only package side effects because offline mirror was used
await fs.unlink(path.join(config.cwd, 'node_modules'));
reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'build', 'module-a-build.log'))).toEqual(
true,
);
});
},
);
test.concurrent('install without pack-built-packages should keep running install scripts', (): Promise<void> => {
return runInstall({ignoreScripts: true}, 'install-offline-built-artifacts', async (config, reporter) => {
// install scripts were not run
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(false);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(false);
config.ignoreScripts = false;
// after first run we observe both package and global side effects
let reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(true);
// after second run we observe both package and global side effects
await fs.unlink(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'));
await fs.unlink(path.join(config.cwd, 'module-a-build.log'));
reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(true);
});
});
test.concurrent('removing prebuilt package .tgz file falls back to running scripts', (): Promise<void> => {
return runInstall({ignoreScripts: true}, 'install-offline-built-artifacts', async (config, reporter) => {
// install scripts were not run
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(false);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(false);
// enable packing of built artifacts
config.packBuiltPackages = true;
config.ignoreScripts = false;
// after first run we observe both package and global side effects
let reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(true);
// after second run we observe both package and global side effects
const tgzFiles = await fs.readdir(path.join(config.cwd, 'mirror-for-offline', 'prebuilt'));
const packageTgz = tgzFiles.filter(f => f !== 'dep-a-v1.0.0.tgz')[0];
await fs.unlink(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'));
await fs.unlink(path.join(config.cwd, 'module-a-build.log'));
await fs.unlink(path.join(config.cwd, 'mirror-for-offline', 'prebuilt', packageTgz));
reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(true);
});
});
// This test is not run concurrently because we mock some internal module
test('switching platform for installed node_modules should trigger rebuild / using another prebuilt tgz', (): Promise<
void,
> => {
return runInstall({}, 'install-offline-built-artifacts-multiple-platforms', async (config, reporter) => {
let tgzFiles = await fs.readdir(path.join(config.cwd, 'mirror-for-offline', 'prebuilt'));
expect(tgzFiles.length).toBe(1);
// running install with platform 2 (artifacts get rewritten and install scripts rerun)
await fs.unlink(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'));
await fs.unlink(path.join(config.cwd, 'module-a-build.log'));
nameUtils.getSystemParams.mockImplementation(pkg => {
return `${process.platform}-${process.arch}-22`;
});
nameUtils.getPlatformSpecificPackageFilename.mockImplementation(pkg => {
const normaliseScope = name => (name[0] === '@' ? name.substr(1).replace('/', '-') : name);
const suffix = `${process.platform}-${process.arch}-22`;
return `${normaliseScope(pkg.name)}-v${pkg.version}-${suffix}`;
});
let reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
tgzFiles = await fs.readdir(path.join(config.cwd, 'mirror-for-offline', 'prebuilt'));
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(true);
expect(tgzFiles.length).toBe(2);
// running install with platform 1 again (no global side effects)
await fs.unlink(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'));
await fs.unlink(path.join(config.cwd, 'module-a-build.log'));
nameUtils.getSystemParams.mockImplementation(
jest.requireActual('../../../src/util/package-name-utils').getSystemParams,
);
nameUtils.getPlatformSpecificPackageFilename.mockImplementation(
jest.requireActual('../../../src/util/package-name-utils').getPlatformSpecificPackageFilename,
);
reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
tgzFiles = await fs.readdir(path.join(config.cwd, 'mirror-for-offline', 'prebuilt'));
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'module-a-build.log'))).toEqual(true);
expect(await fs.exists(path.join(config.cwd, 'module-a-build.log'))).toEqual(false);
expect(tgzFiles.length).toBe(2);
});
});
test('creates the file in the mirror when fetching a git repository', async () => {
await runInstall({}, 'install-git', async (config, reporter): Promise<void> => {
const lockfile = await Lockfile.fromDirectory(config.cwd);
expect(await fs.glob('example-yarn-package.git-*', {cwd: `${config.cwd}/offline-mirror`})).toHaveLength(1);
await fs.unlink(path.join(config.cwd, 'offline-mirror'));
await fs.unlink(path.join(config.cwd, 'node_modules'));
const firstReinstall = new Install({}, config, reporter, lockfile);
await firstReinstall.init();
expect(await fs.glob('example-yarn-package.git-*', {cwd: `${config.cwd}/offline-mirror`})).toHaveLength(1);
});
});
test.concurrent('install from offline mirror', (): Promise<void> => {
return runInstall({}, 'install-from-offline-mirror', async (config): Promise<void> => {
const allFiles = await fs.walk(config.cwd);
expect(
allFiles.findIndex((file): boolean => {
return file.relative === path.join('node_modules', 'fake-dependency', 'package.json');
}),
).toBeGreaterThanOrEqual(0);
expect(
allFiles.findIndex((file): boolean => {
return file.relative === path.join('node_modules', '@fakescope', 'fake-dependency', 'package.json');
}),
).toBeGreaterThanOrEqual(0);
});
});
test.concurrent('install should add missing deps to yarn and mirror (PR import scenario)', (): Promise<void> => {
return runInstall({}, 'install-import-pr', async config => {
expect(await getPackageVersion(config, 'mime-types')).toEqual('2.0.0');
expect(semver.satisfies(await getPackageVersion(config, 'mime-db'), '~1.0.1')).toEqual(true);
expect(await getPackageVersion(config, 'fake-yarn-dependency')).toEqual('1.0.1');
const mirror = await fs.walk(path.join(config.cwd, 'mirror-for-offline'));
expect(mirror).toHaveLength(3);
expect(mirror[0].relative).toEqual('fake-yarn-dependency-1.0.1.tgz');
expect(mirror[1].relative.indexOf('mime-db-1.0.')).toEqual(0);
expect(mirror[2].relative).toEqual('mime-types-2.0.0.tgz');
const lockFileContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const lockFileLines = explodeLockfile(lockFileContent);
expect(lockFileLines).toHaveLength(14);
expect(lockFileLines[4].indexOf('mime-db@')).toEqual(0);
expect(lockFileLines[8].indexOf('mime-types@2.0.0')).toEqual(0);
});
});
test.concurrent('install should update a dependency to yarn and mirror (PR import scenario 2)', (): Promise<void> => {
// mime-types@2.0.0 is gets updated to mime-types@2.1.11 via
// a change in package.json,
// files in mirror, yarn.lock, package.json and node_modules should reflect that
return runInstall({}, 'install-import-pr-2', async (config, reporter): Promise<void> => {
expect(semver.satisfies(await getPackageVersion(config, 'mime-db'), '~1.0.1')).toEqual(true);
expect(await getPackageVersion(config, 'mime-types')).toEqual('2.0.0');
await fs.copy(path.join(config.cwd, 'package.json.after'), path.join(config.cwd, 'package.json'), reporter);
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(semver.satisfies(await getPackageVersion(config, 'mime-db'), '~1.23.0')).toEqual(true);
expect(await getPackageVersion(config, 'mime-types')).toEqual('2.1.11');
const lockFileWritten = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const lockFileLines = explodeLockfile(lockFileWritten);
expect(lockFileLines[0]).toEqual('mime-db@~1.23.0:');
expect(lockFileLines[2]).toMatch(/resolved "https:\/\/registry\.yarnpkg\.com\/mime-db\/-\/mime-db-/);
expect(lockFileLines[4]).toEqual('mime-types@2.1.11:');
expect(lockFileLines[6]).toMatch(
/resolved "https:\/\/registry\.yarnpkg\.com\/mime-types\/-\/mime-types-2\.1\.11\.tgz#[a-f0-9]+"/,
);
const mirror = await fs.walk(path.join(config.cwd, 'mirror-for-offline'));
expect(mirror).toHaveLength(4);
const newFilesInMirror = mirror.filter((elem): boolean => {
return elem.relative !== 'mime-db-1.0.3.tgz' && elem.relative !== 'mime-types-2.0.0.tgz';
});
expect(newFilesInMirror).toHaveLength(2);
});
});
test.concurrent('offline mirror can be enabled from parent dir', (): Promise<void> => {
const fixture = {
source: 'offline-mirror-configuration',
cwd: 'enabled-from-parent',
};
return runInstall({}, fixture, async (config, reporter) => {
const rawLockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const {object: lockfile} = parse(rawLockfile);
expect(lockfile['mime-types@2.1.14'].resolved).toEqual(
'https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.14.tgz#f7ef7d97583fcaf3b7d282b6f8b5679dab1e94ee',
);
expect(await fs.exists(path.join(config.cwd, '../offline-mirror/mime-types-2.1.14.tgz'))).toBe(true);
});
});
test.concurrent('offline mirror can be enabled from parent dir, with merging of own .yarnrc', (): Promise<void> => {
const fixture = {
source: 'offline-mirror-configuration',
cwd: 'enabled-from-parent-merge',
};
return runInstall({}, fixture, async (config, reporter) => {
const rawLockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const {object: lockfile} = parse(rawLockfile);
expect(lockfile['mime-types@2.1.14'].resolved).toEqual(
'https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.14.tgz#f7ef7d97583fcaf3b7d282b6f8b5679dab1e94ee',
);
expect(await fs.exists(path.join(config.cwd, '../offline-mirror/mime-types-2.1.14.tgz'))).toBe(true);
});
});
test.concurrent('offline mirror can be disabled locally', (): Promise<void> => {
const fixture = {
source: 'offline-mirror-configuration',
cwd: 'disabled-locally',
};
return runInstall({}, fixture, async (config, reporter) => {
const rawLockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const {object: lockfile} = parse(rawLockfile);
expect(lockfile['mime-types@2.1.14'].resolved).toEqual(
'https://registry.yarnpkg.com/mime-types/-/mime-types-2.1.14.tgz#f7ef7d97583fcaf3b7d282b6f8b5679dab1e94ee',
);
expect(await fs.exists(path.join(config.cwd, '../offline-mirror/mime-types-2.1.14.tgz'))).toBe(false);
});
});
test.concurrent('prunes the offline mirror tarballs after pruning is enabled', (): Promise<void> => {
return runInstall({}, 'prune-offline-mirror', async (config): Promise<void> => {
const mirrorPath = 'mirror-for-offline';
// Scenario:
// dep-a 1.0.0 was originally installed, and it depends on dep-b 1.0.0, so
// both of these were added to the offline mirror. Then dep-a was upgraded
// to 1.1.0 which doesn't depend on dep-b. After this, pruning was enabled,
// so the next install should remove dep-a-1.0.0.tgz and dep-b-1.0.0.tgz.
expect(await fs.exists(path.join(config.cwd, `${mirrorPath}/dep-a-1.0.0.tgz`))).toEqual(false);
expect(await fs.exists(path.join(config.cwd, `${mirrorPath}/dep-b-1.0.0.tgz`))).toEqual(false);
expect(await fs.exists(path.join(config.cwd, `${mirrorPath}/dummy.txt`))).toEqual(true);
});
});
test.concurrent('scoped packages remain in offline mirror after pruning is enabled', (): Promise<void> => {
return runInstall({}, 'prune-offline-mirror-scoped', async (config): Promise<void> => {
const mirrorPath = 'mirror-for-offline';
// scoped package exists
expect(await fs.exists(path.join(config.cwd, `${mirrorPath}/@fakescope-fake-dependency-1.0.1.tgz`))).toEqual(true);
// unscoped package exists
expect(await fs.exists(path.join(config.cwd, `${mirrorPath}/fake-dependency-1.0.1.tgz`))).toEqual(true);
});
});