-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
270 lines (229 loc) · 6.11 KB
/
index.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
269
270
/* eslint no-sync:0 */
'use strict'
// Import
const pathUtil = require('path')
const fsUtil = require('fs')
const safeps = require('safeps')
const extractOptsAndCallback = require('extract-opts')
const Errlop = require('errlop')
// Prepare
function complete(error, result, next) {
if (next) {
next(error, result)
return null
} else {
return error || result
}
}
// =====================================
// Define Module
// Require or install a package synchronously or asynchronously
// Also export this function as the default
function lazyRequire(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
// If we have a callback, then do async
return next
? lazyRequire.async(name, opts, next)
: lazyRequire.sync(name, opts)
}
// Require or install a package synchronously or asynchronously
lazyRequire.auto = function auto(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
// If we have no callback, or if we support sync, then do sync
if (!next || lazyRequire.canSyncInstall()) {
return lazyRequire.requireOrInstallSync(name, opts, next)
} else {
return lazyRequire.requireOrInstallAsync(name, opts, next)
}
}
// Require or install a package synchronously
lazyRequire.sync = function sync(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
let result = lazyRequire.require(name, opts)
let error = null
// Synchronous
if (result instanceof Error) {
result = lazyRequire.installSync(name, opts)
if (result instanceof Error) {
error = result
result = null
}
}
// Complete
return complete(error, result, next)
}
// Require or install a package asynchronously
lazyRequire.async = function async(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
// Asynchronous
lazyRequire.require(name, opts, function(error, result) {
if (result) return next(error, result)
lazyRequire.installAsync(name, opts, next)
})
// Exit
return null
}
// Attempt to require a module
lazyRequire.require = function _require(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
let result = null
let error = null
// Fetch
try {
result = require(name)
} catch (e1) {
error = e1
if (opts.cwd) {
try {
const path = pathUtil.join(opts.cwd, 'node_modules', name)
result = require(path)
error = null
} catch (e2) {
error = new Errlop(e2, error)
try {
const path = pathUtil.join(opts.cwd, 'node_modules', name)
const pkg = pathUtil.join(path, 'package.json')
const main = pathUtil.join(path, require(pkg).main)
result = require(main)
error = null
} catch (e3) {
error = new Errlop(e3, error)
}
}
}
}
// Complete
return complete(error, result, next)
}
// Can Save
lazyRequire.canSave = function canSave(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
let result = null
let error = null
// Options
if (opts.cwd == null) {
opts.cwd = process.cwd()
}
opts.packagePath = pathUtil.join(opts.cwd, 'package.json')
// Fetch
try {
result = fsUtil.existsSync(opts.packagePath) === true
} catch (err) {
error = err
}
// Complete
return complete(error, result, next)
}
// Can install synchronously
lazyRequire.canSyncInstall = function canSyncInstall(opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
let result = safeps.hasSpawnSync()
let error = null
// Fetch
if (result instanceof Error) {
error = result
result = null
}
// Complete
return complete(error, result, next)
}
// Attempt to require a module (will install if missing)
// Asynchronous with optional callback
lazyRequire.installAsync = function installAsync(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
let error = null
// Defaults
if (opts.save == null) {
opts.save = false
}
if (opts.cwd == null) {
opts.cwd = process.cwd()
}
// Check that we are not in the browser
if (process.browser === true) {
error = new Error('lazy-require: installing in the browser is not possible')
}
// Check saving
else if (opts.save === true && lazyRequire.canSave() === false) {
error = new Error(
'lazy-require: cannot save the module as `opts.cwd` did not contain a `package.json` file'
)
}
// Install
else {
// Arguments
const args = ['npm', 'install', name]
if (opts.save === true) {
args.push('--save')
opts.save = null // {delete opts.save} is very slow
}
// Install
safeps.spawn(args, opts, function(err) {
// Check
if (err) return next(err)
// Try to load the module
lazyRequire.require(name, opts, next)
})
// Exit
return null
}
// Complete
return complete(error, null, next)
}
// Attempt to require a module (will install if missing)
// Synchronous with optional callback
lazyRequire.installSync = function installSync(name, opts, next) {
// Prepare
;[opts, next] = extractOptsAndCallback(opts, next)
let error = null
// Defaults
if (opts.save == null) {
opts.save = false
}
if (opts.cwd == null) {
opts.cwd = process.cwd()
}
// Check that we are not in the browser
if (typeof process !== 'undefined' && process.browser === true) {
error = new Error('lazy-require: installing in the browser is not possible')
}
// Check that spawnSync exists, check that the project's package.json exists
else if (lazyRequire.canSyncInstall() === false) {
error = new Error('lazy-require: installing synchronously is not possible')
}
// Check saving
else if (opts.save === true && lazyRequire.canSave() === false) {
error = new Error(
'lazy-require: cannot save the module as `opts.cwd` did not contain a `package.json` file'
)
}
// Install
else {
// Arguments
const args = ['npm', 'install', name]
if (opts.save === true) {
args.push('--save')
opts.save = null // {delete opts.save} is very slow
}
// Install
const spawnResult = safeps.spawnSync(args, opts)
if (spawnResult instanceof Error) {
error = spawnResult
} else {
// Exit
return lazyRequire.require(name, opts, next)
}
}
// Complete
return complete(error, null, next)
}
// Export
module.exports = lazyRequire