-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
101 lines (86 loc) · 2.98 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
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
const util = require("util");
const fs = require("fs");
const bytes = require("bytes");
const glob = require("glob");
const gzipSize = require("gzip-size");
const parseGitConfig = require("parse-git-config");
const sme = require("source-map-explorer");
const { spawn } = require("child_process");
const fsReadFile = util.promisify(fs.readFile);
const fsStat = util.promisify(fs.stat);
function getFileSize(path, gzipped = false) {
if (gzipped) {
return fsReadFile(path, "utf8").then(gzipSize);
}
return fsStat(path).then(stats => stats.size);
}
function execCommand(command) {
return new Promise((resolve, reject) => {
const bufs = [];
const errBufs = [];
const proc = spawn("/bin/sh", ["-o", "pipefail", "-c", command]);
proc.on("error", error => {
reject(error);
}).on("exit", code => {
if (code) {
reject(new Error(`"${command}" exited with code ${code}:\n${Buffer.concat(errBufs).toString()}`));
} else {
resolve(Buffer.concat(bufs).toString());
}
});
proc.stdout.on("data", data => {
bufs.push(data);
}).on("error", error => {
reject(error);
});
proc.stderr.on("data", data => {
errBufs.push(data);
}).on("error", error => {
reject(error);
});
});
}
module.exports = {
readFiles(config) {
return Promise.all(config.map(fileConfig => {
const paths = glob.sync(fileConfig.path);
if (!paths.length) {
console.log(`There is no matching file for ${fileConfig.path} in ${process.cwd()}`);
return [];
}
return Promise.all(paths.map(path => {
const gzippedSize = "gzip" in fileConfig ? fileConfig.gzip : true;
return getFileSize(path, gzippedSize).then(size => ({
limit: bytes(fileConfig.limit) || Infinity,
size,
path
}));
}));
})).then(results => results.reduce((prev, cur) => prev.concat(cur), []));
},
generateBundleStats(filepath) {
return new Promise((resolve, reject) => {
resolve(sme(filepath));
});
},
getRepoName() {
return new Promise((resolve, reject) => {
parseGitConfig({ cwd: process.cwd() }, (err, config) => {
if (err) {
reject(err);
return;
}
const m = config['remote "origin"'].url.match(/[/:]((?:[^/]+)\/(?:[^/]+)).git$/);
if (!m) {
reject(new Error("Repository name couldn't be parsed"));
return;
}
const [, repoName] = m;
resolve(repoName);
});
});
},
getRepoHeadHash() {
return execCommand("git rev-parse HEAD").then(output => output.trim());
}
};