123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- const fs = require('fs')
- const path = require('path')
- function updatePkg(dir, fn) {
-
- const file = path.join(dir, 'package.json')
-
- let data = fs.readFileSync(file, 'utf-8')
- const pkg = JSON.parse(data)
-
- fn(pkg)
-
- const regex = /^[ ]+|\t+/m
- const res = regex.exec(data)
- const indent = res ? res[0] : null
- data = JSON.stringify(pkg, null, indent)
-
- fs.writeFileSync(file, `${data}\n`)
- }
- 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,
- }
|