-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjectCommand.js
54 lines (41 loc) · 1.6 KB
/
injectCommand.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
const fs = require('fs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
let injectCommand = async function (
input_command,
target,
input_pattern = '',
force = true
) {
const command_parts = input_command.match(/^([^\s$]+)/i)
if (!input_command) throw Error('Please specify a command')
if (!input_pattern && (!command_parts || !command_parts[1]))
throw Error('Could not detect a pattern, please specify it manually')
if (!fs.existsSync(target))
throw Error('Please ensure your target file already exists')
const pattern_text =
input_pattern || `<!-- auto-${command_parts[1].toLowerCase()} -->`
const pattern_regex = new RegExp(
`(${pattern_text})[\\s\\S]*${pattern_text}`,
'i'
)
const target_content = fs.readFileSync(target, 'utf8')
const { stdout, stderr } = await exec(input_command)
if (stderr) throw Error(stderr)
// Remove any existing match of the pattern in the output
let output_content = stdout.replace(RegExp(pattern_text, 'ig'), '')
// Add the pattern at the begining and at the end of the partial
output_content = pattern_text + '\n' + output_content + '\n' + pattern_text
let new_target_content
if (!pattern_regex.test(target_content) && force == true) {
new_target_content = target_content + '\n' + output_content
} else {
new_target_content = target_content.replace(pattern_regex, output_content)
}
if (target_content == new_target_content) return [pattern_text, '']
else {
fs.writeFileSync(target, new_target_content)
return [pattern_text, target]
}
}
module.exports = injectCommand