-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.js
78 lines (59 loc) 路 1.49 KB
/
init.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
'use strict';
const CWD = process.cwd();
const {
readFileSync,
writeFileSync,
existsSync,
} = require('fs');
const tryCatch = require('try-catch');
const nupdate = require('nupdate');
const info = require(`${CWD}/package`);
const {scripts} = info;
const madrun =
`'use strict';
const {
run,
series,
parallel,
} = require('madrun');
module.exports = ${JSON.stringify(scripts, null, 4)};
`;
module.exports.create = () => {
if (!existsSync('./madrun.js'))
writeFileSync('./madrun.js', madrun);
};
module.exports.patchPackage = () => {
const updatedScripts = patchPackage(require(`${CWD}/madrun`));
writeFileSync('./package.json', preparePackage(info, updatedScripts));
};
module.exports.patchNpmIgnore = () => {
const [, file = ''] = tryCatch(readFileSync, './.npmignore');
if (file.includes('madrun.js'))
return;
const data = file + 'madrun.js\n\n';
writeFileSync('./.npmignore', data);
}
module.exports.addMadrunToPackage = () => {
return nupdate('madrun', {
install: true,
dev: true,
add: true,
});
};
function preparePackage(info, scripts) {
const data = {
...info,
scripts: {
...scripts,
},
};
return JSON.stringify(data, null, 2) + '\n';
}
function patchPackage(scripts) {
const result = {};
const keys = Object.keys(scripts);
for (const key of keys) {
result[key] = `madrun ${key}`;
}
return result;
}