-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
executable file
·50 lines (43 loc) · 1.37 KB
/
util.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
"use strict";
const findParentDir = require('find-parent-dir');
const fs = require('fs');
require('shelljs/global');
function packageDir() {
const packageDir = findParentDir.sync(process.cwd(), 'package.json');
cd(packageDir);
}
function ensureCleanMaster(branch) {
branch = branch || 'master';
if (exec('git symbolic-ref HEAD').stdout.trim() !== `refs/heads/${branch}`)
throw new Error(`Not on ${branch} branch, aborting`);
if (exec('git status --porcelain').stdout.trim() !== '')
throw new Error('Working copy is dirty, aborting');
}
function _exec(command, silent) {
if (!silent) {
echo(command);
echo();
}
var result = exec(command, { silent: !!silent });
if (result.code === 0) return result;
echo(`cwd: ${process.cwd()}`);
echo(`Aborting; non-zero return value (${result.code}) from: ${command}`);
console.error(result.stderr);
exit(result.code)
}
function asJson (obj) { return JSON.stringify(obj, null, 2); }
let ensure = (type) => (path) => {
let is = false;
try { is = fs.lstatSync(path)['is' + type](); } catch (e) { console.log(e); }
if (!is) echo(`Not a ${type}: ${path}`) && exit(-3);
};
let assertDir = ensure('Directory');
let assertFile = ensure('File');
module.exports = {
ensureCleanMaster: ensureCleanMaster,
_exec: _exec,
asJson: asJson,
assertDir: assertDir,
assertFile: assertFile,
packageDir: packageDir,
};