Welcome to mirror list, hosted at ThFree Co, Russian Federation.

common-tap.js « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2efca30c48107e0d06c0ba89f757c0447edf535c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
var spawn = require("child_process").spawn
var path = require("path")
var fs = require("fs")

var port = exports.port = 1337
exports.registry = "http://localhost:" + port
process.env.npm_config_loglevel = "error"

var npm_config_cache = path.resolve(__dirname, "npm_cache")
exports.npm_config_cache = npm_config_cache

var bin = exports.bin = require.resolve("../bin/npm-cli.js")
var once = require("once")

exports.npm = function (cmd, opts, cb) {
  cb = once(cb)
  cmd = [bin].concat(cmd)
  opts = opts || {}

  opts.env = opts.env ? opts.env : process.env
  if (!opts.env.npm_config_cache) {
    opts.env.npm_config_cache = npm_config_cache
  }

  var stdout = ""
    , stderr = ""
    , node = process.execPath
    , child = spawn(node, cmd, opts)

  if (child.stderr) child.stderr.on("data", function (chunk) {
    stderr += chunk
  })

  if (child.stdout) child.stdout.on("data", function (chunk) {
    stdout += chunk
  })

  child.on("error", cb)

  child.on("close", function (code) {
    cb(null, code, stdout, stderr)
  })
  return child
}

// based on http://bit.ly/1tkI6DJ
function deleteNpmCacheRecursivelySync(cache) {
  cache = cache ? cache : npm_config_cache
  var files = []
  var res
  if( fs.existsSync(cache) ) {
    files = fs.readdirSync(cache)
    files.forEach(function(file,index) {
      var curPath = path.resolve(cache, file)
      if(fs.lstatSync(curPath).isDirectory()) { // recurse
        deleteNpmCacheRecursivelySync(curPath)
      } else { // delete file
        if (res = fs.unlinkSync(curPath))
          throw Error("Failed to delete file " + curPath + ", error " + res)
      }
    })
    if (res = fs.rmdirSync(cache))
      throw Error("Failed to delete directory " + cache + ", error " + res)
  }
  return 0
}
exports.deleteNpmCacheRecursivelySync = deleteNpmCacheRecursivelySync