UNPKG

1.82 kBJavaScriptView Raw
1var spawn = require("child_process").spawn
2var path = require("path")
3var fs = require("fs")
4
5var port = exports.port = 1337
6exports.registry = "http://localhost:" + port
7process.env.npm_config_loglevel = "error"
8
9var npm_config_cache = path.resolve(__dirname, "npm_cache")
10exports.npm_config_cache = npm_config_cache
11
12var bin = exports.bin = require.resolve("../bin/npm-cli.js")
13var once = require("once")
14
15exports.npm = function (cmd, opts, cb) {
16 cb = once(cb)
17 cmd = [bin].concat(cmd)
18 opts = opts || {}
19
20 opts.env = opts.env ? opts.env : process.env
21 if (!opts.env.npm_config_cache) {
22 opts.env.npm_config_cache = npm_config_cache
23 }
24
25 var stdout = ""
26 , stderr = ""
27 , node = process.execPath
28 , child = spawn(node, cmd, opts)
29
30 if (child.stderr) child.stderr.on("data", function (chunk) {
31 stderr += chunk
32 })
33
34 if (child.stdout) child.stdout.on("data", function (chunk) {
35 stdout += chunk
36 })
37
38 child.on("error", cb)
39
40 child.on("close", function (code) {
41 cb(null, code, stdout, stderr)
42 })
43 return child
44}
45
46// based on http://bit.ly/1tkI6DJ
47function deleteNpmCacheRecursivelySync(cache) {
48 cache = cache ? cache : npm_config_cache
49 var files = []
50 var res
51 if( fs.existsSync(cache) ) {
52 files = fs.readdirSync(cache)
53 files.forEach(function(file,index) {
54 var curPath = path.resolve(cache, file)
55 if(fs.lstatSync(curPath).isDirectory()) { // recurse
56 deleteNpmCacheRecursivelySync(curPath)
57 } else { // delete file
58 if (res = fs.unlinkSync(curPath))
59 throw Error("Failed to delete file " + curPath + ", error " + res)
60 }
61 })
62 if (res = fs.rmdirSync(cache))
63 throw Error("Failed to delete directory " + cache + ", error " + res)
64 }
65 return 0
66}
67exports.deleteNpmCacheRecursivelySync = deleteNpmCacheRecursivelySync
\No newline at end of file