npm-programmatic
Advanced tools
Comparing version 0.0.1 to 0.0.2
34
index.js
@@ -43,3 +43,37 @@ const Promise = require('bluebird'); | ||
}); | ||
}, | ||
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 (error) { | ||
reject(error); | ||
} else { | ||
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); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
{ | ||
"name": "npm-programmatic", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Use NPM commands programmatically", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -60,4 +60,25 @@ # npm-programmatic | ||
## List Installed Packages | ||
``` | ||
npm.list(path).then(function) | ||
``` | ||
| Name | Type | Value | | ||
| ------------- |:-------------:| -----:| | ||
| path | String | path at which to look | | ||
### Example | ||
``` | ||
var npm = require('npm-programmatic'); | ||
npm.list('/path/to/project') | ||
.then(function(arrayOfPackages){ | ||
console.log(arrayOfPackages); | ||
}) | ||
.catch(function(){ | ||
console.log("Unable to uninstall package"); | ||
}); | ||
``` | ||
## Tests | ||
install mocha and dev dependencies. Then run | ||
``` npm test ``` |
9134
8
177
84