Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Commit

Permalink
fix: only call runScript when relevant script actually exists
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Feb 10, 2019
1 parent d86370b commit d167ca5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internals.runScript = (stage, { pkg, path, cwd, unsafePerm }, options) => {
console.log();

if (options.dryRun) {
console.log(`DRY RUN ==> ${stage} ${path || pkg.name}...`);
console.log(`DRY RUN ==> ${stage} ${path || pkg.name}`);
return;
}

Expand Down Expand Up @@ -146,15 +146,21 @@ exports.run = async (options) => {
await internals.runScript('preinstall', { pkg, path: '', cwd, unsafePerm: true }, options);

for (const { path, childPkg } of allowedPackages) {
await internals.runScript('preinstall', { pkg: childPkg, path, cwd }, options);
if (childPkg.scripts.preinstall) {
await internals.runScript('preinstall', { pkg: childPkg, path, cwd }, options);
}
}

for (const { path, childPkg } of allowedPackages) {
await internals.runScript('install', { pkg: childPkg, path, cwd }, options);
if (childPkg.scripts.install) {
await internals.runScript('install', { pkg: childPkg, path, cwd }, options);
}
}

for (const { path, childPkg } of allowedPackages) {
await internals.runScript('postinstall', { pkg: childPkg, path, cwd }, options);
if (childPkg.scripts.postinstall) {
await internals.runScript('postinstall', { pkg: childPkg, path, cwd }, options);
}
}

await internals.runScript('install', { pkg, path: '', cwd, unsafePerm: true }, options);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/dominykas/allow-scripts.git"
},
"scripts": {
"test": "lab -L -t 93 -m 5000"
"test": "lab -L -t 96 -m 5000"
},
"bin": {
"allow-scripts": "./bin/allow-scripts.js"
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/basic.dry-run.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
DRY RUN ==> preinstall @example/basic

DRY RUN ==> preinstall node_modules/@example/with-preinstall-script

DRY RUN ==> install node_modules/@example/with-install-script

DRY RUN ==> postinstall node_modules/@example/with-postinstall-script

DRY RUN ==> install @example/basic

DRY RUN ==> postinstall @example/basic

DRY RUN ==> prepublish @example/basic

DRY RUN ==> prepare @example/basic
16 changes: 16 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ describe('allow-scripts', () => {
expect(fixture.getLog()).not.to.contain('without-install-scripts');
});

it('dry run: reports allowed scripts', async () => {

const fixture = Fixtures.setup('basic', [
'with-preinstall-script',
'with-install-script',
'with-postinstall-script',
'without-scripts',
'without-install-scripts'
]);

await Allow.run({ dryRun: true });

expect(fixture.getActualResult()).to.equal('');
expect(fixture.getLog()).to.equal(Fs.readFileSync(Path.join(__dirname, 'fixtures', 'basic.dry-run.txt')).toString().trim());
});

it('crashes on script not in allowed list', async () => {

const fixture = Fixtures.setup('not-in-allowed', [
Expand Down

0 comments on commit d167ca5

Please sign in to comment.