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

Commit

Permalink
feat: add --dry-run
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
dominykas committed Jan 29, 2019
1 parent 6169171 commit 5728a44
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
6 changes: 5 additions & 1 deletion bin/allow-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

const Allow = require('..');

Allow.run().catch((err) => {
const options = {
dryRun: process.argv.includes('--dry-run')
};

Allow.run(options).catch((err) => {

console.error(err);
process.exit(1);
Expand Down
29 changes: 17 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ internals.queue = (tree) => {
return topo.nodes;
};

internals.runScript = (stage, { pkg, path, cwd, unsafePerm }) => {
internals.runScript = (stage, { pkg, path, cwd, unsafePerm }, options) => {

console.log();
console.log(`==========> ${stage} ${path || pkg.name}...`);

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

console.log(`==========> ${stage} ${path || pkg.name}...`);
return Npm.runScript(pkg, stage, Path.join(cwd, path), {
dir: cwd,
unsafePerm, // @todo: find an official way to do this for top level package
Expand Down Expand Up @@ -95,7 +100,7 @@ internals.getLockFile = (cwd) => {
}
};

exports.run = async (cmd = 'install') => {
exports.run = async (options) => {

const cwd = process.cwd();
const pkg = require(Path.join(cwd, 'package.json'));
Expand All @@ -116,7 +121,7 @@ exports.run = async (cmd = 'install') => {
})
.filter(({ childPkg }) => {

return childPkg.scripts && (childPkg.scripts[cmd] || childPkg.scripts[`pre${cmd}`] || childPkg.scripts[`post${childPkg}`]);
return childPkg.scripts && (childPkg.scripts.install || childPkg.scripts.preinstall || childPkg.scripts.postinstall);
})
.filter(({ path, childPkg }) => {

Expand All @@ -133,22 +138,22 @@ exports.run = async (cmd = 'install') => {
return allowScripts[name];
});

await internals.runScript('preinstall', { pkg, path: '', cwd, unsafePerm: true });
await internals.runScript('preinstall', { pkg, path: '', cwd, unsafePerm: true }, options);

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

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

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

await internals.runScript('install', { pkg, path: '', cwd, unsafePerm: true });
await internals.runScript('postinstall', { pkg, path: '', cwd, unsafePerm: true });
await internals.runScript('prepublish', { pkg, path: '', cwd, unsafePerm: true });
await internals.runScript('prepare', { pkg, path: '', cwd, unsafePerm: true });
await internals.runScript('install', { pkg, path: '', cwd, unsafePerm: true }, options);
await internals.runScript('postinstall', { pkg, path: '', cwd, unsafePerm: true }, options);
await internals.runScript('prepublish', { pkg, path: '', cwd, unsafePerm: true }, options);
await internals.runScript('prepare', { pkg, path: '', cwd, unsafePerm: true }, options);
};

0 comments on commit 5728a44

Please sign in to comment.