-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
90 lines (83 loc) · 2 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
const path = require('path')
const { readFile, readFileSync, writeFile, writeFileSync } = require('fs')
const DEFAULT_SEP = '/'
/**
* @param {string} input
* @param {string=} [sep=DEFAULT_SEP]
* @return {string}
*/
function normalize(input, sep = DEFAULT_SEP) {
const _input = path.normalize(input)
return _input.split(path.sep).join(sep)
}
/**
* given localPath, return string to form matching RegExp
* @param {string} localPath
* @returns {string}
*/
function generateLocalPathStr(localPath) {
const pathArr = localPath.split(DEFAULT_SEP)
const len = pathArr.length
return pathArr
.map((part, index) => {
part = escapeDot(part)
if (index === len - 1) {
return `${part}`
} else {
return `\\.*(${part})?`
}
})
.join(`\\${DEFAULT_SEP}?`)
}
/**
* @param {string} input
*/
function escapeDot(input) {
if (input.includes('.')) {
return input.replace(/\./g, '\\.')
}
return input
}
/**
* produce RegExp to match local path
* @param {string} localPath
* @return {RegExp}
*/
function generateLocalPathReg(localPath) {
const content = generateLocalPathStr(localPath)
const prefix = `([(=+,\\n\\t]\\s*['"]?)`
// using prefix to strictly match resource reference
// like src="", url(""), a = "", srcset="xxx.jpg 100w, xxx@2.jpg 200w"
return new RegExp(`${prefix}${content}`, 'g')
}
// read file
const read = (location) => readFileSync(location, 'utf-8')
// write file
const write = (location) => (content) => writeFileSync(location, content)
const readAsync = (location) =>
new Promise((resolve, reject) => {
readFile(location, 'utf-8', (err, data) => {
if (err) {
reject(err)
}
resolve(data)
})
})
const writeAsync = (location) => async (content) =>
new Promise((resolve, reject) => {
writeFile(location, content, (err) => {
if (err) {
reject(err)
}
resolve()
})
})
module.exports = {
DEFAULT_SEP,
normalize,
generateLocalPathReg,
read,
write,
readAsync,
writeAsync,
}