-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
288 lines (242 loc) · 8.17 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import _ from 'lodash'
import globby from 'globby'
import readFile from 'fs-readfile-promise'
import writeFile from 'fs-writefile-promise'
import mkdirp from 'mkdirp-promise'
const error = (componentName, errorMessage) => new Error(`💥 ${componentName} Panic! ${errorMessage}`)
/**
* Reads bower files provided a working directory.
*
* @param {object} options
* @param {string} options.workingDir
* @param {array} options.excludePaths
*/
export function reader({ workingDir, excludePaths } = {}) {
const name = 'Reader'
const _formatExcludes = (globList) => {
return globList.map((glob) => `${workingDir}/${glob}/**`)
}
if (!workingDir) {
return Promise.reject(error(name, 'Please provide a workingDirectory.'))
}
if (excludePaths && !Array.isArray(excludePaths)) {
// TODO: make this fool-proof
let parseExcludeAsString
try {
parseExcludeAsString = JSON.parse(excludePaths.replace(/'/g, '"'))
} catch (e) {
return Promise.reject(error(name, 'Excludes should be an array of strings, like ["glob/to/exclude"]'))
}
excludePaths = parseExcludeAsString
}
return globby(
[
`${workingDir}/**/*.html`
],
{
ignore: [...excludePaths ? _formatExcludes(excludePaths) : [] ]
}
)
}
/**
* Parses html files provided as a file list and returns a list containing path and matches.
*
* @param {object} options
* @param {string} options.workingDir
* @param {array} options.fileList
* @param {string} options.search The path to replace.
* @param {string} options.replace Will replace search.
* @param {array} options.excludePatterns A list of regular expression that shouldn't be matched by the parser.
*/
// TODO: worth splitting this up.
export async function parser({ workingDir, fileList, search, replace, excludePatterns } = {}) {
const name = 'Parser'
const _readFiles = (paths) => {
return Promise.all(
paths.map(async (path) => {
return {
content: await readFile(path, { encoding: 'utf8'}),
path: path
}
})
)
}
const _getFilePathDepth = (files, prefix) => {
return files.map((file) => {
const depth = file.path.split('/').length
return {
...file,
depth
}
})
}
const _adjustPathForDepth = (depth, lowestDepth, stringToAdjust) => {
if (depth > lowestDepth) {
let difference = depth - lowestDepth
let formattedString = stringToAdjust
while (difference) {
formattedString = `../${formattedString}`
difference--
}
return formattedString
} else {
return stringToAdjust
}
}
if (excludePatterns && !Array.isArray(excludePatterns)) {
// TODO: make this fool-proof
let parseExcludeAsString
try {
parseExcludeAsString = JSON.parse(excludePatterns.replace(/'/g, '"'))
} catch (e) {
return Promise.reject(error(name, 'Exclude patterns should be an array of strings, like ["regex1", "regex2"]'))
}
excludePatterns = parseExcludeAsString
}
if (!fileList) {
return Promise.reject(error(name, 'Please provide a fileList to parse.'))
}
if (fileList && !Array.isArray(fileList)) {
return Promise.reject(error(name, 'Filelist should be an array.'))
}
const files = _getFilePathDepth(await _readFiles(fileList), workingDir)
try {
const lowestDepth = files.reduce((acc, { depth }) => {
if (depth < acc) {
return depth
}
return acc
}, Infinity)
const results = files
.map((file) => {
// 1. Get all matches.
// Regex finds all lines that have a string that contains link or script.
const allRegExp = /<(link|script).*>/g
let matches = []
let match = allRegExp.exec(file.content)
while (match) {
matches.push({
full: match[0],
type: match[1]
})
match = allRegExp.exec(file.content)
}
/**
* 2. Exclude patterns if provided.
*/
if (excludePatterns) {
matches = matches.filter((m) => {
const excludeRegExps = excludePatterns.map((exclude) => new RegExp(exclude, 'g'))
return excludeRegExps.reduce((acc, cur) => {
return acc && !cur.exec(m.full)
}, true)
})
}
/**
* 3. Adjust search & replace strings.
* Each match is compared against the lowest depth in the provided filelist.
* If a file has a depth bigger than that, we append `../` according to the depth difference to the search string. This way we deal with nested matches.
*/
const adjustedSearch = _adjustPathForDepth(file.depth, lowestDepth, search)
const adjustedReplace = _adjustPathForDepth(file.depth, lowestDepth, replace)
// 4. Filter by search, if provided
if (adjustedSearch) {
matches = matches.filter((m) => {
/* Regex finds all lines that have a href or src that contain the search word in it, followed by a letter.
*
* Example for *search = '../'*
* ===================================
* Run against:
* 1. <script src="lib/file.js"
* 2. <script src="../lib/file.js"
* 3. <script src="../../lib/file.js"
* -----------------------------------
* Matches 2.:
* <script src=" ~../~ lib/file.js"
*/
const searchRegExp = new RegExp(`(href|src)="(${adjustedSearch}[a-zA-Z].*)"`, 'g')
return searchRegExp.exec(m.full)
})
}
// 5. Format matches to line + suggestion objects.
matches = matches.map((m) => {
const lastIndex = m.full.lastIndexOf(adjustedSearch)
const suggestion =
m.full.substring(0, lastIndex) +
adjustedReplace +
m.full.substring(lastIndex + adjustedSearch.length)
return {
line: m.full,
suggestion
}
})
return {
path: file.path,
matches
}
})
return results
} catch (e) {
console.error(e)
throw error(name, `There was a problem parsing one or more files ${JSON.stringify(files)}`)
}
}
/**
* Replaces contents in a file.
*
* @param {object} options
* @param {string} options.path File path to replace lines in.
* @param {object[]} options.lines A list of lines to replace.
* @param {string} options.lines[].line Original line (to replace).
* @param {string} options.lines[].suggestion New line (will replace original).
*/
export async function replacer ({ path, lines }) {
const name = 'Replacer'
const _replaceLines = (content, lineArray) => {
let output = `${content}`
lineArray.forEach((l) => {
const lineRegExp = new RegExp(l.line, 'g')
output = output.replace(lineRegExp, l.suggestion)
})
return output
}
if (!path) {
return Promise.reject(error(name, `File path to replace is required.`))
}
if (!lines || !Array.isArray(lines)) {
console.error(`Lines failed ${JSON.stringify(lines)}`)
return Promise.reject(error(name, `Lines to replace cannot be empty and must be a valid array`))
}
const file = await readFile(path, { encoding: 'utf8'})
return _replaceLines(file, lines)
}
/**
* Outputs content to a file.
*
* @param {object} options
* @param {*} options.content The content to output.
* @param {string} options.outputFile File path to output to.
*/
export async function outputer({ content, outputFile } = {}) {
const name = 'Outputer'
const _outputMessage = (result, path, message) => {
const icon = result === 'success' ? '✅ ' : '⚠ '
return {
result,
message: `${icon} ${message} to ${path}`
}
}
if (!outputFile) {
return Promise.reject(error(name, `Output file is required.`))
}
if (!content) {
return _outputMessage('warning', outputFile, 'Did nothing, because there was nothing to output')
}
try {
await mkdirp(outputFile.substring(0, outputFile.lastIndexOf('/')))
await writeFile(outputFile, content)
} catch (e) {
return Promise.reject(error(name, `Failed to write to ${outputFile}`))
}
return _outputMessage('success', outputFile, 'Outputed content')
}