forked from tradle/rn-nodeify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
executable file
·268 lines (226 loc) · 6.61 KB
/
cmd.js
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var semver = require('semver')
var proc = require('child_process')
var pick = require('object.pick')
var extend = require('xtend/mutable')
var deepEqual = require('deep-equal')
var find = require('findit')
var minimist = require('minimist')
var parallel = require('run-parallel')
var allShims = require('./shims')
var coreList = require('./coreList')
var browser = require('./browser')
var pkgPath = path.join(process.cwd(), 'package.json')
var pkg = require(pkgPath)
var hackFiles = require('./pkg-hacks')
var argv = minimist(process.argv.slice(2), {
alias: {
i: 'install',
e: 'hack',
h: 'help'
}
})
if (argv.help) {
runHelp()
process.exit(0)
} else {
run()
}
function run () {
var toShim
if (argv.install) {
if (argv.install === true) {
toShim = coreList
} else {
toShim = argv.install.split(',')
.map(function (name) {
return name.trim()
})
}
} else {
toShim = coreList
}
if (toShim.indexOf('stream') !== -1) {
toShim.push(
'_stream_transform',
'_stream_readable',
'_stream_writable',
'_stream_duplex',
'_stream_passthrough',
'readable-stream'
)
}
if (toShim.indexOf('crypto') !== -1) {
toShim.push('react-native-randombytes')
}
if (argv.install) {
installShims(toShim, function (err) {
if (err) throw err
runHacks()
})
} else {
runHacks()
}
function runHacks () {
hackPackageJSONs(toShim, function (err) {
if (err) throw err
if (argv.hack) {
if (argv.hack === true) hackFiles()
else hackFiles([].concat(argv.hack))
}
})
}
}
function installShims (modulesToShim, done) {
var shimPkgNames = modulesToShim.map(function (m) {
return browser[m] || m
}).filter(function (shim) {
return !/^_/.test(shim) && shim.indexOf('/') === -1
})
var existence = []
parallel(shimPkgNames.map(function (name) {
var modPath = path.resolve('./node_modules/' + name)
return function (cb) {
fs.exists(modPath, function (exists) {
if (!exists) return cb()
var install = true
var pkgJson = require(modPath + '/package.json')
if (/^git\:\/\//.test(pkgJson._resolved)) {
var hash = allShims[name].split('#')[1]
if (hash && pkgJson.gitHead.indexOf(hash) === 0) {
install = false
}
} else {
var existingVer = pkgJson.version
if (semver.satisfies(existingVer, allShims[name])) {
install = false
}
}
if (!install) {
console.log('not reinstalling ' + name)
shimPkgNames.splice(shimPkgNames.indexOf(name), 1)
}
cb()
})
}
}), function (err) {
if (err) throw err
if (!shimPkgNames.length) {
return finish()
}
var installLine = 'npm install --save '
shimPkgNames.forEach(function (name) {
if (allShims[name].indexOf('/') === -1) {
console.log('installing from npm', name)
installLine += name + '@' + allShims[name]
} else {
// github url
console.log('installing from github', name)
installLine += allShims[name].match(/([^\/]+\/[^\/]+)$/)[1]
}
pkg.dependencies[name] = allShims[name]
installLine += ' '
})
fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2), function (err) {
if (err) throw err
proc.execSync(installLine, {
cwd: process.cwd(),
env: process.env,
stdio: 'inherit'
})
finish()
})
console.log('installing:', installLine)
function finish () {
copyShim(done)
}
})
}
function copyShim (cb) {
fs.exists('./shim.js', function (exists) {
if (exists) return cb()
fs.readFile(path.join(__dirname, 'shim.js'), { encoding: 'utf8' }, function (err, contents) {
if (err) return cb(err)
fs.writeFile('./shim.js', contents, cb)
})
})
}
function hackPackageJSONs (modules, done) {
fixPackageJSON(modules, './package.json', true)
var finder = find('./node_modules')
finder.on('file', function (file) {
if (path.basename(file) !== 'package.json') return
fixPackageJSON(modules, file, true)
})
finder.once('end', done)
}
function fixPackageJSON (modules, file, overwrite) {
if (file.split(path.sep).indexOf('react-native') >= 0) return
fs.readFile(path.resolve(file), { encoding: 'utf8' }, function (err, contents) {
if (err) throw err
// var browser = pick(baseBrowser, modules)
var pkgJson
try {
pkgJson = JSON.parse(contents)
} catch (err) {
console.warn('failed to parse', file)
return
}
// if (shims[pkgJson.name]) {
// console.log('skipping', pkgJson.name)
// return
// }
// if (pkgJson.name === 'readable-stream') debugger
var orgBrowser = pkgJson['react-native'] || pkgJson.browser || pkgJson.browserify || {}
if (typeof orgBrowser === 'string') {
orgBrowser = {}
orgBrowser[pkgJson.main || 'index.js'] = pkgJson['react-native'] || pkgJson.browser || pkgJson.browserify
}
var depBrowser = extend({}, orgBrowser)
for (var p in browser) {
if (modules.indexOf(p) === -1) continue
if (!(p in orgBrowser)) {
depBrowser[p] = browser[p]
} else {
if (!overwrite && orgBrowser[p] !== browser[p]) {
console.log('not overwriting mapping', p, orgBrowser[p])
} else {
depBrowser[p] = browser[p]
}
}
}
modules.forEach(function (p) {
if (depBrowser[p] === false) {
console.log('removing browser exclude', file, p)
delete depBrowser[p]
}
})
if (!deepEqual(orgBrowser, depBrowser)) {
pkgJson.browser = pkgJson['react-native'] = depBrowser
delete pkgJson.browserify
fs.writeFile(file, JSON.stringify(pkgJson, null, 2), rethrow)
}
})
}
function rethrow (err) {
if (err) throw err
}
function runHelp () {
console.log(function () {
/*
Usage:
rn-nodeify --install dns,stream,http,https
rn-nodeify --install # installs all core shims
rn-nodeify --hack # run all package-specific hacks
rn-nodeify --hack rusha,fssync # run some package-specific hacks
Options:
-h --help show usage
-e, --hack run package-specific hacks (list or leave blank to run all)
-i, --install install shims (list or leave blank to install all)
Please report bugs! https://github.com/mvayngrib/rn-nodeify/issues
*/
}.toString().split(/\n/).slice(2, -2).join('\n'))
process.exit(0)
}