forked from alexgorbatchev/run-when-changed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat-cmd.js
29 lines (27 loc) · 995 Bytes
/
format-cmd.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
import { dirname, basename, sep } from 'path';
import find from 'fs-find-root';
export default function formatCmd(cmd, fullFilepath) {
const filepath = fullFilepath.replace(process.cwd() + sep, '');
const filedir = dirname(filepath);
const fullFiledir = dirname(fullFilepath);
const filename = basename(filepath);
const values = {
filedir,
filename,
filepath,
s: filepath,
'full-filedir': fullFiledir,
'full-filepath': fullFilepath,
};
// chances are we don't always have to look up package.json folder, but
// performance hit of this is probably so insignificant, it's not worth
// the time to optimize this... PRs welcome of course :)
return find
.file('package.json', fullFiledir)
.then(rootDir => values['package-json-dir'] = dirname(rootDir))
.then(() => {
const keys = Object.keys(values).join('|');
let results = cmd.replace(new RegExp(`%(${keys})`, 'g'), (match, key) => values[key]);
return results;
});
}