-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (104 loc) · 3.61 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
const path = require('path')
module.exports = options => {
const {only, without, search, as, toRoot, install, verbose, lazy, patch} = options = Object.assign({
only: null,
without: [],
search: [process.cwd()], //__dirname
global: false,
as: {},
toRoot: [],
install: true,
verbose: false,
require: null,
lazy: true,
patch: {},
}, options)
let initialization = new Date().getTime()
const packages = options.global ? global : {}
const V = verbose ? console.error : () => {}
for (let dir of search) {
dir = path.resolve(dir)
const packageJSON = require(path.resolve(dir, 'package.json'))
if (only) {
for (const i in only) {
only[only[i]] = only[i]
delete only[i]
}
}
if (options.require) {
for (const i in options.require) {
options.require[options.require[i]] = options.require[i]
delete options.require[i]
}
}
const deps = only || Object.assign(packageJSON.dependencies || {}, packageJSON.devDependencies || {}, options.require)
for (const package in deps) {
if (without.indexOf(package) > -1 || package == 'fast-require') continue
const name = as[package] || package.replace(/(\.|-)([^-])/g, (all, dash, symbol) => symbol.toUpperCase())
let _path = null
const notInstalled = []
try {
_path = require.resolve(package, {paths: [dir]})
} catch (e) {
if (install) {
for (const package in deps) {
try {
require.resolve(package, {paths: [dir]})
} catch (e) {
notInstalled.push(package)
}
}
const cmd = `npm install ${notInstalled.join(' ')} --production`
V(`fast-require:\t${cmd}`)
require.cache = {}
require('child_process').execSync(cmd, {cwd: dir, stdio: 'inherit'})
// if (options.require)
// require('child_process').execSync(`npm install ${options.require.join(' ')}`, {cwd: dir, stdio: 'inherit'})
_path = require.resolve(package, {paths: [dir]})
}
}
const p = patch[name] ? patch[name] : x => x
if (toRoot.indexOf(package) > -1) {
const start = new Date()
const module = p(require(_path))
for (const f in module) {
Object.defineProperty(packages, f, {
set: () => {
throw new Error(`fast-require:\tDo not override ${f}! Reserved by ${package} package`)
},
get: () => module[f],
})
}
const toRootTime = new Date() - start
initialization += toRootTime
V(`fast-require:\t${toRootTime}ms\t${name}`)
} else if (!lazy)
packages[name] = p(require(package))
else {
let module
if (packages[name]) {
try {
packages[name] = 'test'
} catch (e) {
if (new RegExp(`fast-require.*${package}`).test(e)) return
}
throw new Error(`fast-require:\tTrying to override ${package} which was already defined`)
}
Object.defineProperty(packages, name, {
set: () => {
throw new Error(`fast-require:\tDo not override ${name}! Reserved by ${package} package`)
},
get: () => {
if (!module) {
const start = new Date()
module = p(require(_path))
V(`fast-require:\t${new Date() - start}ms\t${name}`)
}
return module
}})
}
}
}
V(`fast-require: \t${new Date().getTime() - initialization}ms\tinit`)
return packages
}