-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
33 lines (29 loc) · 814 Bytes
/
util.js
File metadata and controls
33 lines (29 loc) · 814 Bytes
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
const exec = require('child_process').exec;
const fs = require('fs');
async function execSyncWithOutput(cmd) {
return new Promise((resolve, reject) => {
const proc = exec(cmd, err => {
reject(err);
});
proc.stdout.pipe(process.stdout);
proc.stderr.pipe(process.stderr);
proc.on('exit', () => resolve());
});
}
async function modifyJson(jsonPath, fn) {
const jsonStr = fs.readFileSync(jsonPath, 'utf8');
const obj = JSON.parse(jsonStr);
fn(obj);
fs.writeFileSync(jsonPath, JSON.stringify(obj, null, 2), 'utf8');
}
// pattern must not contain double quotes
async function runSed(filepath, pattern) {
const cmd = `sed -i "${pattern}" '${filepath}'`;
console.log(cmd);
await execSyncWithOutput(cmd);
}
module.exports = {
execSyncWithOutput,
modifyJson,
runSed,
};