forked from CodingItWrong/jessup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
106 lines (90 loc) · 2.06 KB
/
commands.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
102
103
104
105
106
const {execSync} = require('child_process');
const fs = require('fs');
const {chdir} = require('process');
const jq = require('node-jq');
const DRY_RUN = false;
function cd(path) {
console.log(`$ cd ${path}`);
if (!DRY_RUN) {
chdir(path);
}
}
function mkdir(path) {
console.log(`$ mkdir -p ${path}`);
if (!DRY_RUN) {
fs.mkdirSync(path, {recursive: true});
}
}
function command(commandText) {
console.log(`$ ${commandText}`);
// TODO: use polymorphism instead of a conditional
if (!DRY_RUN) {
try {
execSync(commandText, {stdio: 'inherit'});
} catch (e) {
console.error(e.stdout.toString());
console.error(e.stderr.toString());
throw e;
}
}
}
function group(message, stepImplementation, {commit = true} = {}) {
console.log(message.toUpperCase());
console.log('');
stepImplementation();
if (commit) {
command('git add .');
command(`git commit -m "${message}"`);
}
console.log('');
}
async function groupAsync(
message,
stepImplementationAsync,
{commit = true} = {}
) {
console.log(message.toUpperCase());
console.log('');
await stepImplementationAsync();
if (commit) {
command('git add .');
command(`git commit -m "${message}"`);
}
console.log('');
}
async function modifyJson(jsonFile, query) {
const formattedJsonData = await jq.run(query, jsonFile);
writeFile(jsonFile, formattedJsonData);
}
function setScript(scriptName, implementation) {
command(`npm set-script ${scriptName} "${implementation}"`);
}
function writeFile(path, contents) {
console.log(`Write ${path}`);
console.log('');
console.log(contents);
console.log('');
console.log('(end of file)');
console.log('');
if (!DRY_RUN) {
fs.writeFileSync(path, contents);
}
}
function addNpmPackages({dev = false, packages}) {
command(`yarn add ${dev ? '-D ' : ''}${packages.join(' ')}`);
}
function displayMessage(message) {
console.log(message);
}
module.exports = {
addNpmPackages,
cd,
command,
displayMessage,
group,
groupAsync,
mkdir,
modifyJson,
setScript,
writeFile,
};