-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
60 lines (47 loc) · 1.17 KB
/
index.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
const fs = require('fs')
const path = require('path')
// Update package.json
function updatePkg(dir, fn) {
// Pkg path
const file = path.join(dir, 'package.json')
// Read pkg
let data = fs.readFileSync(file, 'utf-8')
const pkg = JSON.parse(data)
// Update pkg object
fn(pkg)
// Stringify pkg
const regex = /^[ ]+|\t+/m
const res = regex.exec(data)
const indent = res ? res[0] : null
data = JSON.stringify(pkg, null, indent)
// Write pkg
fs.writeFileSync(file, `${data}\n`)
}
// Update pkg.scripts names
function updateScripts(pkg, fn) {
pkg.scripts = Object.fromEntries(
Object.entries(pkg.scripts).map(([key, value]) => [fn(key), value])
)
}
function enable(name) {
if (['_install', '_postinstall'].includes(name)) {
return name.substring(1)
}
return name
}
function disable(name) {
if (['install', 'postinstall'].includes(name)) {
return `_${name}`
}
return name
}
function enableAndSave(dir = process.cwd()) {
updatePkg(dir, (pkg) => updateScripts(pkg, enable))
}
function disableAndSave(dir = process.cwd()) {
updatePkg(dir, (pkg) => updateScripts(pkg, disable))
}
module.exports = {
enableAndSave,
disableAndSave,
}