-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
40 lines (32 loc) · 1.05 KB
/
utils.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
const { mkdir, readFile, writeFile } = require('fs');
const { exec } = require('child_process');
const { promisify } = require('util');
const mkdirAwaitable = (path) => promisify(mkdir)(path, { recursive: true });
const readFileAwaitable = async (filename) => {
try {
const res = await promisify(readFile)(filename);
return res.toString('ascii');
} catch {
return Promise.resolve('');
}
};
const writeFileAwaitable = (filename, content) => promisify(writeFile)(filename, content);
const execAwaitable = (command) => {
try {
return promisify(exec)(command, { maxBuffer: 1024 * 2048 });
} catch {
console.warn(`stdout maxBuffer length (${2048}) exceeded. Try increasing it whit --buffer-length`);
}
};
const getTreeFiles = async (hash) => {
const { stderr, stdout } = await execAwaitable(`git ls-tree ${hash}`);
if (stderr) throw new Error(stderr);
return stdout.split('\n');
};
module.exports = {
createDir: mkdirAwaitable,
readFile: readFileAwaitable,
writeFile: writeFileAwaitable,
exec: execAwaitable,
getTreeFiles,
};