-
Notifications
You must be signed in to change notification settings - Fork 16
/
lib.js
67 lines (58 loc) · 1.6 KB
/
lib.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
const util = require('util')
const promisify = util.promisify
const fs = require('fs')
const path = require('path')
const {minify} = require('terser');
const {serialize} = require('serialize-to-js');
exports.readFile = promisify(fs.readFile)
exports.writeFile = promisify(fs.writeFile)
exports.stat = promisify(fs.stat)
exports.readdir = promisify(fs.readdir)
exports.unlink = promisify(fs.unlink)
exports.realpath = async function(path) {
const realpath = promisify(fs.realpath)
try {
path = await realpath(path)
} catch (e) {
// do nothing
}
return path
}
exports.lstat = promisify(fs.lstat)
const fsExists = function(filename) {
return new Promise(resolve => {
fs.exists(filename, resolve)
})
}
exports.exists = fsExists
const existsCache = new Map()
const resolve = async (filepath, filenames, root = path.parse(filepath).root) => {
filepath = path.dirname(filepath)
if (filepath === root || path.basename(filepath) === 'node_modules') {
return null
}
for (const filename of filenames) {
let file = path.join(filepath, filename)
let exists = existsCache.has(file)
? existsCache.get(file)
: await fsExists(file)
if (exists) {
existsCache.set(file, true)
return file
}
}
return resolve(filepath, filenames, root)
}
exports.resolve = resolve
function serializeObject(obj, shouldMinify = false) {
let code = `module.exports = ${serialize(obj)};`
if (shouldMinify) {
let minified = minify(code)
if (minified.error) {
throw minified.error
}
code = minified.code
}
return code
}
exports.serializeObject = serializeObject