source: trip-planner-front/node_modules/node-gyp-build/index.js@ e29cc2e

Last change on this file since e29cc2e was e29cc2e, checked in by Ema <ema_spirova@…>, 3 years ago

primeNG components

  • Property mode set to 100644
File size: 5.7 KB
Line 
1var fs = require('fs')
2var path = require('path')
3var os = require('os')
4
5// Workaround to fix webpack's build warnings: 'the request of a dependency is an expression'
6var runtimeRequire = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require // eslint-disable-line
7
8var vars = (process.config && process.config.variables) || {}
9var prebuildsOnly = !!process.env.PREBUILDS_ONLY
10var abi = process.versions.modules // TODO: support old node where this is undef
11var runtime = isElectron() ? 'electron' : 'node'
12var arch = os.arch()
13var platform = os.platform()
14var libc = process.env.LIBC || (isAlpine(platform) ? 'musl' : 'glibc')
15var armv = process.env.ARM_VERSION || (arch === 'arm64' ? '8' : vars.arm_version) || ''
16var uv = (process.versions.uv || '').split('.')[0]
17
18module.exports = load
19
20function load (dir) {
21 return runtimeRequire(load.path(dir))
22}
23
24load.path = function (dir) {
25 dir = path.resolve(dir || '.')
26
27 try {
28 var name = runtimeRequire(path.join(dir, 'package.json')).name.toUpperCase().replace(/-/g, '_')
29 if (process.env[name + '_PREBUILD']) dir = process.env[name + '_PREBUILD']
30 } catch (err) {}
31
32 if (!prebuildsOnly) {
33 var release = getFirst(path.join(dir, 'build/Release'), matchBuild)
34 if (release) return release
35
36 var debug = getFirst(path.join(dir, 'build/Debug'), matchBuild)
37 if (debug) return debug
38 }
39
40 var prebuild = resolve(dir)
41 if (prebuild) return prebuild
42
43 var nearby = resolve(path.dirname(process.execPath))
44 if (nearby) return nearby
45
46 var target = [
47 'platform=' + platform,
48 'arch=' + arch,
49 'runtime=' + runtime,
50 'abi=' + abi,
51 'uv=' + uv,
52 armv ? 'armv=' + armv : '',
53 'libc=' + libc,
54 'node=' + process.versions.node,
55 process.versions.electron ? 'electron=' + process.versions.electron : '',
56 typeof __webpack_require__ === 'function' ? 'webpack=true' : '' // eslint-disable-line
57 ].filter(Boolean).join(' ')
58
59 throw new Error('No native build was found for ' + target + '\n loaded from: ' + dir + '\n')
60
61 function resolve (dir) {
62 // Find matching "prebuilds/<platform>-<arch>" directory
63 var tuples = readdirSync(path.join(dir, 'prebuilds')).map(parseTuple)
64 var tuple = tuples.filter(matchTuple(platform, arch)).sort(compareTuples)[0]
65 if (!tuple) return
66
67 // Find most specific flavor first
68 var prebuilds = path.join(dir, 'prebuilds', tuple.name)
69 var parsed = readdirSync(prebuilds).map(parseTags)
70 var candidates = parsed.filter(matchTags(runtime, abi))
71 var winner = candidates.sort(compareTags(runtime))[0]
72 if (winner) return path.join(prebuilds, winner.file)
73 }
74}
75
76function readdirSync (dir) {
77 try {
78 return fs.readdirSync(dir)
79 } catch (err) {
80 return []
81 }
82}
83
84function getFirst (dir, filter) {
85 var files = readdirSync(dir).filter(filter)
86 return files[0] && path.join(dir, files[0])
87}
88
89function matchBuild (name) {
90 return /\.node$/.test(name)
91}
92
93function parseTuple (name) {
94 // Example: darwin-x64+arm64
95 var arr = name.split('-')
96 if (arr.length !== 2) return
97
98 var platform = arr[0]
99 var architectures = arr[1].split('+')
100
101 if (!platform) return
102 if (!architectures.length) return
103 if (!architectures.every(Boolean)) return
104
105 return { name, platform, architectures }
106}
107
108function matchTuple (platform, arch) {
109 return function (tuple) {
110 if (tuple == null) return false
111 if (tuple.platform !== platform) return false
112 return tuple.architectures.includes(arch)
113 }
114}
115
116function compareTuples (a, b) {
117 // Prefer single-arch prebuilds over multi-arch
118 return a.architectures.length - b.architectures.length
119}
120
121function parseTags (file) {
122 var arr = file.split('.')
123 var extension = arr.pop()
124 var tags = { file: file, specificity: 0 }
125
126 if (extension !== 'node') return
127
128 for (var i = 0; i < arr.length; i++) {
129 var tag = arr[i]
130
131 if (tag === 'node' || tag === 'electron' || tag === 'node-webkit') {
132 tags.runtime = tag
133 } else if (tag === 'napi') {
134 tags.napi = true
135 } else if (tag.slice(0, 3) === 'abi') {
136 tags.abi = tag.slice(3)
137 } else if (tag.slice(0, 2) === 'uv') {
138 tags.uv = tag.slice(2)
139 } else if (tag.slice(0, 4) === 'armv') {
140 tags.armv = tag.slice(4)
141 } else if (tag === 'glibc' || tag === 'musl') {
142 tags.libc = tag
143 } else {
144 continue
145 }
146
147 tags.specificity++
148 }
149
150 return tags
151}
152
153function matchTags (runtime, abi) {
154 return function (tags) {
155 if (tags == null) return false
156 if (tags.runtime !== runtime && !runtimeAgnostic(tags)) return false
157 if (tags.abi !== abi && !tags.napi) return false
158 if (tags.uv && tags.uv !== uv) return false
159 if (tags.armv && tags.armv !== armv) return false
160 if (tags.libc && tags.libc !== libc) return false
161
162 return true
163 }
164}
165
166function runtimeAgnostic (tags) {
167 return tags.runtime === 'node' && tags.napi
168}
169
170function compareTags (runtime) {
171 // Precedence: non-agnostic runtime, abi over napi, then by specificity.
172 return function (a, b) {
173 if (a.runtime !== b.runtime) {
174 return a.runtime === runtime ? -1 : 1
175 } else if (a.abi !== b.abi) {
176 return a.abi ? -1 : 1
177 } else if (a.specificity !== b.specificity) {
178 return a.specificity > b.specificity ? -1 : 1
179 } else {
180 return 0
181 }
182 }
183}
184
185function isElectron () {
186 if (process.versions && process.versions.electron) return true
187 if (process.env.ELECTRON_RUN_AS_NODE) return true
188 return typeof window !== 'undefined' && window.process && window.process.type === 'renderer'
189}
190
191function isAlpine (platform) {
192 return platform === 'linux' && fs.existsSync('/etc/alpine-release')
193}
194
195// Exposed for unit tests
196// TODO: move to lib
197load.parseTags = parseTags
198load.matchTags = matchTags
199load.compareTags = compareTags
200load.parseTuple = parseTuple
201load.matchTuple = matchTuple
202load.compareTuples = compareTuples
Note: See TracBrowser for help on using the repository browser.