1 | var spawn = require("child_process").spawn
|
2 | var path = require("path")
|
3 | var fs = require("fs")
|
4 |
|
5 | var port = exports.port = 1337
|
6 | exports.registry = "http://localhost:" + port
|
7 | process.env.npm_config_loglevel = "error"
|
8 |
|
9 | var npm_config_cache = path.resolve(__dirname, "npm_cache")
|
10 | exports.npm_config_cache = npm_config_cache
|
11 |
|
12 | var bin = exports.bin = require.resolve("../bin/npm-cli.js")
|
13 | var once = require("once")
|
14 |
|
15 | exports.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 |
|
47 | function 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()) {
|
56 | deleteNpmCacheRecursivelySync(curPath)
|
57 | } else {
|
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 | }
|
67 | exports.deleteNpmCacheRecursivelySync = deleteNpmCacheRecursivelySync |
\ | No newline at end of file |