Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
npm-programmatic/index.js /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
101 lines (91 sloc)
2.83 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const Promise = require('bluebird'); | |
| const exec = require('child_process').exec; | |
| module.exports = { | |
| install: function(packages, opts){ | |
| if(packages.length == 0 || !packages || !packages.length){return Promise.reject("No packages found");} | |
| if(typeof packages == "string") packages = [packages]; | |
| if(!opts) opts = {}; | |
| var cmdString = "npm install " + packages.join(" ") + " " | |
| + (opts.global ? " -g":"") | |
| + (opts.save ? " --save":" --no-save") | |
| + (opts.saveDev? " --save-dev":"") | |
| + (opts.legacyBundling? " --legacy-bundling":"") | |
| + (opts.noOptional? " --no-optional":"") | |
| + (opts.ignoreScripts? " --ignore-scripts":""); | |
| return new Promise(function(resolve, reject){ | |
| var cmd = exec(cmdString, {cwd: opts.cwd?opts.cwd:"/", maxBuffer: opts.maxBuffer?opts.maxBuffer:200 * 1024},(error, stdout, stderr) => { | |
| if (error) { | |
| reject(error); | |
| } else { | |
| resolve(true); | |
| } | |
| }); | |
| if(opts.output) { | |
| var consoleOutput = function(msg) { | |
| console.log('npm: ' + msg); | |
| }; | |
| cmd.stdout.on('data', consoleOutput); | |
| cmd.stderr.on('data', consoleOutput); | |
| } | |
| }); | |
| }, | |
| uninstall: function(packages, opts){ | |
| if(packages.length == 0 || !packages || !packages.length){return Promise.reject(new Error("No packages found"));} | |
| if(typeof packages == "string") packages = [packages]; | |
| if(!opts) opts = {}; | |
| var cmdString = "npm uninstall " + packages.join(" ") + " " | |
| + (opts.global ? " -g":"") | |
| + (opts.save ? " --save":" --no-save") | |
| + (opts.saveDev? " --saveDev":""); | |
| return new Promise(function(resolve, reject){ | |
| var cmd = exec(cmdString, {cwd: opts.cwd?opts.cwd:"/"},(error, stdout, stderr) => { | |
| if (error) { | |
| reject(error); | |
| } else { | |
| resolve(true); | |
| } | |
| }); | |
| if(opts.output) { | |
| var consoleOutput = function(msg) { | |
| console.log('npm: ' + msg); | |
| }; | |
| cmd.stdout.on('data', consoleOutput); | |
| cmd.stderr.on('data', consoleOutput); | |
| } | |
| }); | |
| }, | |
| list:function(path){ | |
| var global = false; | |
| if(!path) global = true; | |
| var cmdString = "npm ls --depth=0 " + (global?"-g ":" "); | |
| return new Promise(function(resolve, reject){ | |
| exec(cmdString, {cwd: path?path:"/"},(error, stdout, stderr) => { | |
| if(stderr !== ""){ | |
| if (stderr.indexOf("missing")== -1 && stderr.indexOf("required") == -1) { | |
| return reject(error); | |
| } | |
| } | |
| var packages = []; | |
| packages = stdout.split('\n'); | |
| packages = packages.filter(function(item){ | |
| if(item.match(/^├──.+/g) != null){ | |
| return true | |
| } | |
| if(item.match(/^└──.+/g) != null){ | |
| return true | |
| } | |
| return undefined; | |
| }); | |
| packages = packages.map(function(item){ | |
| if(item.match(/^├──.+/g) != null){ | |
| return item.replace(/^├──\s/g, ""); | |
| } | |
| if(item.match(/^└──.+/g) != null){ | |
| return item.replace(/^└──\s/g, ""); | |
| } | |
| }) | |
| resolve(packages); | |
| }); | |
| }); | |
| } | |
| } |