这是一个很棒的功能,我们可以使用" npm以编程方式,"但是我遇到了一些问题。功能" npm.load"似乎没有解雇。我没有得到我的" npm.load"中的任何控制台日志。或" npm.commands.install"功能
var npm = require('npm');
// There is another promise here
.then(function(path) {
// this is working the way I intend it to
cache[requestId].package = JSON.parse(path);
// This is firing
if (cache[requestId].package.name && cache[requestId].package.version && cache[requestId].package.scripts.start) {
// console logs and an array [ 'keystone', 'async', 'underscore', 'swig', 'node-sass', 'node-sass-middleware', 'dotenv' ]
console.log(Object.keys(cache[requestId].package.dependencies));
// console logs as /Users/207004/Desktop/github/mothership/server/app/routes/tractor-beam/ms-apps/my_site
console.log(localPath);
// console logs as a [Function]
console.log(npm.load);
// *** Here is the issue! This is not firing! ***
npm.load({}, function(err) {
// no console log
console.log(npm.commands.install);
// no console log
console.log(err);
npm.commands.install(localPath, Object.keys(cache[requestId].package.dependencies), function(err, done) {
// no console log
console.log('loaded');
// no console log
console.log(err, done);
// I am assuming that this is not firing, but my code does fire the console log in the next promise
return PM2.connectAsync();
});
});
} else {
console.log('else');
}
})
// Another promise chained here. A console log inside of this promise is firing.
任何帮助将不胜感激。如果您有任何问题,请与我们联系。
谢谢,
答案 0 :(得分:0)
我花了几天时间,但我想了很多。
这是我提出的解决方案,它是承诺:
var Promise = require('bluebird');
// This will create promise functions for all the methods in the "child_process" module.
// Created "exec.execAsync" below.
var exec = Promise.promisifyAll(require('child_process'));
// Function to make package names one long string for the command line.
var getDependencies = function(dependencies) {
var deps = '';
Object.keys(dependencies).forEach(function(el){
deps = deps + ' ' + el;
});
return deps;
};
// Promise before this reads the package.json file
.then(function(packageJson){
var deps;
var pack = JSON.parse(packageJson);
if(pack && pack.dependencies) {
deps = getDependencies(pack.dependencies);
// I used the "--prefix" flag because I wanted to install the dependencies in a different directory.
// This part takes a while. Plan your promises before and after accordingly.
// The command below console logs into this "npm install --prefix /Users/Max/Desktop/github/mothership/server/app/routes/tractor-beam/ms-apps/my_site keystone async underscore swig node-sass node-sass-middleware dotenv"
return exec.execAsync('npm install --prefix ' + localPath + deps);
}
})
// Continues to next promise
如果您有任何问题,请与我们联系。