1 | 'use strict'
|
2 | var fs = require('graceful-fs')
|
3 | var readCmdShim = require('read-cmd-shim')
|
4 | var isWindows = require('../lib/utils/is-windows.js')
|
5 | var extend = Object.assign || require('util')._extend
|
6 | var Bluebird = require('bluebird')
|
7 |
|
8 |
|
9 | if (!global.setImmediate || !require('timers').setImmediate) {
|
10 | require('timers').setImmediate = global.setImmediate = function () {
|
11 | var args = [arguments[0], 0].concat([].slice.call(arguments, 1))
|
12 | setTimeout.apply(this, args)
|
13 | }
|
14 | }
|
15 |
|
16 | var spawn = require('child_process').spawn
|
17 | var path = require('path')
|
18 |
|
19 | var port = exports.port = 1337
|
20 | exports.registry = 'http://localhost:' + port
|
21 | const ourenv = {}
|
22 | ourenv.npm_config_loglevel = 'error'
|
23 | ourenv.npm_config_progress = 'false'
|
24 |
|
25 | var npm_config_cache = path.resolve(__dirname, 'npm_cache')
|
26 | ourenv.npm_config_cache = exports.npm_config_cache = npm_config_cache
|
27 | ourenv.npm_config_userconfig = exports.npm_config_userconfig = path.join(__dirname, 'fixtures', 'config', 'userconfig')
|
28 | ourenv.npm_config_globalconfig = exports.npm_config_globalconfig = path.join(__dirname, 'fixtures', 'config', 'globalconfig')
|
29 | ourenv.npm_config_global_style = 'false'
|
30 | ourenv.npm_config_legacy_bundling = 'false'
|
31 | ourenv.npm_config_fetch_retries = '0'
|
32 | ourenv.random_env_var = 'foo'
|
33 |
|
34 | ourenv.npm_config_node_version = process.version.replace(/-.*$/, '')
|
35 | for (let key of Object.keys(ourenv)) process.env[key] = ourenv[key]
|
36 |
|
37 | var bin = exports.bin = require.resolve('../bin/npm-cli.js')
|
38 |
|
39 | var chain = require('slide').chain
|
40 | var once = require('once')
|
41 |
|
42 | var nodeBin = exports.nodeBin = process.env.npm_node_execpath || process.env.NODE || process.execPath
|
43 |
|
44 | exports.npm = function (cmd, opts, cb) {
|
45 | if (!cb) {
|
46 | var prom = new Bluebird((resolve, reject) => {
|
47 | cb = function (err, code, stdout, stderr) {
|
48 | if (err) return reject(err)
|
49 | return resolve([code, stdout, stderr])
|
50 | }
|
51 | })
|
52 | }
|
53 | cb = once(cb)
|
54 | cmd = [bin].concat(cmd)
|
55 | opts = extend({}, opts || {})
|
56 |
|
57 | opts.env = opts.env || process.env
|
58 | if (opts.env._storage) opts.env = Object.assign({}, opts.env._storage)
|
59 | if (!opts.env.npm_config_cache) {
|
60 | opts.env.npm_config_cache = npm_config_cache
|
61 | }
|
62 | if (!opts.env.npm_config_send_metrics) {
|
63 | opts.env.npm_config_send_metrics = 'false'
|
64 | }
|
65 |
|
66 | nodeBin = opts.nodeExecPath || nodeBin
|
67 |
|
68 | var stdout = ''
|
69 | var stderr = ''
|
70 | var child = spawn(nodeBin, cmd, opts)
|
71 |
|
72 | if (child.stderr) {
|
73 | child.stderr.on('data', function (chunk) {
|
74 | stderr += chunk
|
75 | })
|
76 | }
|
77 |
|
78 | if (child.stdout) {
|
79 | child.stdout.on('data', function (chunk) {
|
80 | stdout += chunk
|
81 | })
|
82 | }
|
83 |
|
84 | child.on('error', cb)
|
85 |
|
86 | child.on('close', function (code) {
|
87 | cb(null, code, stdout, stderr)
|
88 | })
|
89 | return prom || child
|
90 | }
|
91 |
|
92 | exports.makeGitRepo = function (params, cb) {
|
93 |
|
94 | var git = require('../lib/utils/git.js')
|
95 |
|
96 | var root = params.path || process.cwd()
|
97 | var user = params.user || 'PhantomFaker'
|
98 | var email = params.email || 'nope@not.real'
|
99 | var added = params.added || ['package.json']
|
100 | var message = params.message || 'stub repo'
|
101 |
|
102 | var opts = { cwd: root, env: { PATH: process.env.PATH } }
|
103 | var commands = [
|
104 | git.chainableExec(['init'], opts),
|
105 | git.chainableExec(['config', 'user.name', user], opts),
|
106 | git.chainableExec(['config', 'user.email', email], opts),
|
107 | git.chainableExec(['add'].concat(added), opts),
|
108 | git.chainableExec(['commit', '-m', message], opts)
|
109 | ]
|
110 |
|
111 | if (Array.isArray(params.commands)) {
|
112 | commands = commands.concat(params.commands)
|
113 | }
|
114 |
|
115 | chain(commands, cb)
|
116 | }
|
117 |
|
118 | exports.readBinLink = function (path) {
|
119 | if (isWindows) {
|
120 | return readCmdShim.sync(path)
|
121 | } else {
|
122 | return fs.readlinkSync(path)
|
123 | }
|
124 | }
|
125 |
|
126 | exports.skipIfWindows = function (why) {
|
127 | if (!isWindows) return
|
128 | console.log('1..1')
|
129 | if (!why) why = 'this test not available on windows'
|
130 | console.log('ok 1 # skip ' + why)
|
131 | process.exit(0)
|
132 | }
|
133 |
|
134 | exports.pendIfWindows = function (why) {
|
135 | if (!isWindows) return
|
136 | console.log('1..1')
|
137 | if (!why) why = 'this test is pending further changes on windows'
|
138 | console.log('not ok 1 # todo ' + why)
|
139 | process.exit(0)
|
140 | }
|
141 |
|
142 | exports.newEnv = function () {
|
143 | return new Environment(process.env)
|
144 | }
|
145 |
|
146 | exports.emptyEnv = function () {
|
147 | const filtered = {}
|
148 | for (let key of Object.keys(process.env)) {
|
149 | if (!/^npm_/.test(key)) filtered[key] = process.env[key]
|
150 | }
|
151 | for (let key of Object.keys(ourenv)) {
|
152 | filtered[key] = ourenv[key]
|
153 | }
|
154 | return new Environment(filtered)
|
155 | }
|
156 |
|
157 | function Environment (env) {
|
158 | if (env instanceof Environment) return env.clone()
|
159 |
|
160 | Object.defineProperty(this, '_storage', {
|
161 | value: extend({}, env)
|
162 | })
|
163 | }
|
164 | Environment.prototype = {}
|
165 |
|
166 | Environment.prototype.delete = function (key) {
|
167 | var args = Array.isArray(key) ? key : arguments
|
168 | var ii
|
169 | for (ii = 0; ii < args.length; ++ii) {
|
170 | delete this._storage[args[ii]]
|
171 | }
|
172 | return this
|
173 | }
|
174 |
|
175 | Environment.prototype.clone = function () {
|
176 | return new Environment(this._storage)
|
177 | }
|
178 |
|
179 | Environment.prototype.extend = function (env) {
|
180 | var self = this.clone()
|
181 | var args = Array.isArray(env) ? env : arguments
|
182 | var ii
|
183 | for (ii = 0; ii < args.length; ++ii) {
|
184 | var arg = args[ii]
|
185 | if (!arg) continue
|
186 | Object.keys(arg).forEach(function (name) {
|
187 | self._storage[name] = arg[name]
|
188 | })
|
189 | }
|
190 | return self
|
191 | }
|