Skip to content

Commit 05080da

Browse files
authored
fixes ignored folders in prebuilt packages feature of offline mirror (#5793)
* fixes #5755 * lint and duplication fixes * fix test
1 parent 08870c8 commit 05080da

File tree

7 files changed

+60
-12
lines changed

7 files changed

+60
-12
lines changed

__tests__/commands/install/offline-mirror.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,36 @@ test.concurrent(
5353
},
5454
);
5555

56+
test.concurrent(
57+
'install with offline mirror and pack-built-packages setting should not ignore ".npmignore"',
58+
(): Promise<void> => {
59+
return runInstall({ignoreScripts: true}, 'install-offline-built-artifacts-no-ignores', async (config, reporter) => {
60+
// install scripts were not run
61+
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'build', 'build-artifact.so'))).toEqual(
62+
false,
63+
);
64+
65+
// enable packing of built artifacts
66+
config.packBuiltPackages = true;
67+
68+
// after first run we observe package side effects
69+
let reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
70+
await reinstall.init();
71+
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'build', 'module-a-build.log'))).toEqual(
72+
true,
73+
);
74+
75+
// after second run we observe only package side effects because offline mirror was used
76+
await fs.unlink(path.join(config.cwd, 'node_modules'));
77+
reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
78+
await reinstall.init();
79+
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a', 'build', 'module-a-build.log'))).toEqual(
80+
true,
81+
);
82+
});
83+
},
84+
);
85+
5686
test.concurrent('install without pack-built-packages should keep running install scripts', (): Promise<void> => {
5787
return runInstall({ignoreScripts: true}, 'install-offline-built-artifacts', async (config, reporter) => {
5888
// install scripts were not run
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
yarn-offline-mirror "./mirror-for-offline"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": {
3+
"dep-a": "1.0.0"
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
6+
version "1.0.0"
7+
resolved "./mirror-for-offline/dep-a-v1.0.0.tgz#02b52818570e272083ef17a99f62ec7e30d0a1a5"

src/cli/commands/pack.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ export async function packTarball(
109109
// apply filters
110110
sortFilter(files, filters, keepFiles, possibleKeepFiles, ignoredFiles);
111111

112-
const packer = tar.pack(config.cwd, {
113-
ignore: name => {
112+
return packWithIgnoreAndHeaders(
113+
config.cwd,
114+
name => {
114115
const relative = path.relative(config.cwd, name);
115116
// Don't ignore directories, since we need to recurse inside them to check for unignored files.
116117
if (fs2.lstatSync(name).isDirectory()) {
@@ -120,6 +121,17 @@ export async function packTarball(
120121
// Otherwise, ignore a file if we're not supposed to keep it.
121122
return !keepFiles.has(relative);
122123
},
124+
{mapHeader},
125+
);
126+
}
127+
128+
export function packWithIgnoreAndHeaders(
129+
cwd: string,
130+
ignoreFunction?: string => boolean,
131+
{mapHeader}: {mapHeader?: Object => Object} = {},
132+
): Promise<stream$Duplex> {
133+
return tar.pack(cwd, {
134+
ignore: ignoreFunction,
123135
map: header => {
124136
const suffix = header.name === '.' ? '' : `/${header.name}`;
125137
header.name = `package${suffix}`;
@@ -128,8 +140,6 @@ export async function packTarball(
128140
return mapHeader ? mapHeader(header) : header;
129141
},
130142
});
131-
132-
return packer;
133143
}
134144

135145
export async function pack(config: Config): Promise<stream$Duplex> {

src/package-install-scripts.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import executeLifecycleScript from './util/execute-lifecycle-script.js';
99
import * as crypto from './util/crypto.js';
1010
import * as fsUtil from './util/fs.js';
1111
import {getPlatformSpecificPackageFilename} from './util/package-name-utils.js';
12-
import {pack} from './cli/commands/pack.js';
12+
import {packWithIgnoreAndHeaders} from './cli/commands/pack.js';
1313

1414
const fs = require('fs');
1515
const invariant = require('invariant');
@@ -320,13 +320,8 @@ export default class PackageInstallScripts {
320320
const ref = pkg._reference;
321321
invariant(ref, 'expected reference');
322322
const builtPackagePath = this.config.generateHardModulePath(ref);
323-
const pkgConfig = await Config.create(
324-
{
325-
cwd: builtPackagePath,
326-
},
327-
this.reporter,
328-
);
329-
const stream = await pack(pkgConfig);
323+
// don't use pack command, we want to avoid the file filters logic
324+
const stream = await packWithIgnoreAndHeaders(builtPackagePath);
330325

331326
const hash = await new Promise((resolve, reject) => {
332327
const validateStream = new crypto.HashStream();

0 commit comments

Comments
 (0)