Allow preInstall hook to return false to skip npm install

This commit is contained in:
Nick O'Leary 2021-04-19 20:29:30 +01:00
parent d2432716ea
commit b4a03a56b4
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
4 changed files with 38 additions and 8 deletions

View File

@ -196,11 +196,15 @@ async function installModule(moduleDetails) {
"version": moduleDetails.version,
"dir": installDir,
}
return hooks.trigger("preInstall", triggerPayload).then(() => {
return hooks.trigger("preInstall", triggerPayload).then((result) => {
// preInstall passed
// - run install
log.trace(NPM_COMMAND + JSON.stringify(args));
return exec.run(NPM_COMMAND, args, { cwd: installDir },true)
if (result !== false) {
log.trace(NPM_COMMAND + JSON.stringify(args));
return exec.run(NPM_COMMAND, args, { cwd: installDir },true)
} else {
log.trace("skipping npm install");
}
}).then(() => {
return hooks.trigger("postInstall", triggerPayload)
}).then(() => {

View File

@ -178,11 +178,15 @@ async function installModule(module,version,url) {
"isUpgrade": isUpgrade
}
return hooks.trigger("preInstall", triggerPayload).then(() => {
return hooks.trigger("preInstall", triggerPayload).then((result) => {
// preInstall passed
// - run install
log.trace(npmCommand + JSON.stringify(args));
return exec.run(npmCommand,args,{ cwd: installDir}, true)
if (result !== false) {
log.trace(npmCommand + JSON.stringify(args));
return exec.run(npmCommand,args,{ cwd: installDir}, true)
} else {
log.trace("skipping npm install");
}
}).then(() => {
return hooks.trigger("postInstall", triggerPayload)
}).then(() => {

View File

@ -123,6 +123,25 @@ describe("externalModules api", function() {
fs.existsSync(path.join(homeDir,"externalModules")).should.be.true();
})
it("skips npm install if preInstall returns false", async function() {
externalModules.init({userDir: homeDir});
externalModules.register("function", "libs");
let receivedPreEvent,receivedPostEvent;
hooks.add("preInstall", function(event) { receivedPreEvent = event; return false })
hooks.add("postInstall", function(event) { receivedPostEvent = event; })
await externalModules.checkFlowDependencies([
{type: "function", libs:[{module: "foo"}]}
])
exec.run.called.should.be.false();
receivedPreEvent.should.have.property("module","foo")
receivedPreEvent.should.have.property("version")
receivedPreEvent.should.have.property("dir")
receivedPreEvent.should.eql(receivedPostEvent)
fs.existsSync(path.join(homeDir,"externalModules")).should.be.true();
})
it("installs missing modules from inside subflow module", async function() {
externalModules.init({userDir: homeDir});
externalModules.register("function", "libs");

View File

@ -291,16 +291,19 @@ describe('nodes/registry/installer', function() {
}).catch(done);
});
it("fails install if preInstall hook fails", function(done) {
it("skips invoking npm if preInstall returns false", function(done) {
let receivedEvent;
hooks.add("preInstall", function(event) { throw new Error("preInstall-error"); })
hooks.add("preInstall", function(event) { return false })
hooks.add("postInstall", function(event) { receivedEvent = event; })
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
installer.installModule("this_wont_exist","1.2.3").catch(function(err) {
exec.run.called.should.be.false();
should.exist(receivedEvent);
done();
}).catch(done);
});
it("rollsback install if postInstall hook fails", function(done) {
hooks.add("postInstall", function(event) { throw new Error("fail"); })
installer.installModule("this_wont_exist","1.2.3").catch(function(err) {