feat(toggle-scripts): CLI to enable/disable package.json scripts
This commit is contained in:
parent
1b97cb263c
commit
a75e1c52b7
59
@vates/toggle-scripts/README.md
Normal file
59
@vates/toggle-scripts/README.md
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
<!-- DO NOT EDIT MANUALLY, THIS FILE HAS BEEN GENERATED -->
|
||||||
|
|
||||||
|
# @vates/toggle-scripts
|
||||||
|
|
||||||
|
[](https://npmjs.org/package/@vates/toggle-scripts)  [](https://bundlephobia.com/result?p=@vates/toggle-scripts) [](https://npmjs.org/package/@vates/toggle-scripts)
|
||||||
|
|
||||||
|
> Easily enable/disable scripts in package.json
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
Installation of the [npm package](https://npmjs.org/package/@vates/toggle-scripts):
|
||||||
|
|
||||||
|
```
|
||||||
|
> npm install --save @vates/toggle-scripts
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
Usage: toggle-scripts options...
|
||||||
|
|
||||||
|
Easily enable/disable scripts in package.json
|
||||||
|
|
||||||
|
Options
|
||||||
|
+<script> Enable the script <script>, ie prefix it with `_`
|
||||||
|
-<script> Disable the script <script>, ie remove the prefix `_`
|
||||||
|
|
||||||
|
Examples
|
||||||
|
toggle-scripts +postinstall +preuninstall
|
||||||
|
toggle-scripts -postinstall -preuninstall
|
||||||
|
```
|
||||||
|
|
||||||
|
For example, if you want `postinstall` hook only in dev:
|
||||||
|
|
||||||
|
```json
|
||||||
|
// package.json
|
||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"postinstall": "<some dev only command>",
|
||||||
|
"prepublishOnly": "toggle-scripts -postinstall",
|
||||||
|
"postpublish": "toggle-scripts +postinstall"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Contributions
|
||||||
|
|
||||||
|
Contributions are _very_ welcomed, either on the documentation or on
|
||||||
|
the code.
|
||||||
|
|
||||||
|
You may:
|
||||||
|
|
||||||
|
- report any [issue](https://github.com/vatesfr/xen-orchestra/issues)
|
||||||
|
you've encountered;
|
||||||
|
- fork and create a pull request.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[ISC](https://spdx.org/licenses/ISC) © [Vates SAS](https://vates.fr)
|
26
@vates/toggle-scripts/USAGE.md
Normal file
26
@vates/toggle-scripts/USAGE.md
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
```
|
||||||
|
Usage: toggle-scripts options...
|
||||||
|
|
||||||
|
Easily enable/disable scripts in package.json
|
||||||
|
|
||||||
|
Options
|
||||||
|
+<script> Enable the script <script>, ie prefix it with `_`
|
||||||
|
-<script> Disable the script <script>, ie remove the prefix `_`
|
||||||
|
|
||||||
|
Examples
|
||||||
|
toggle-scripts +postinstall +preuninstall
|
||||||
|
toggle-scripts -postinstall -preuninstall
|
||||||
|
```
|
||||||
|
|
||||||
|
For example, if you want `postinstall` hook only in dev:
|
||||||
|
|
||||||
|
```json
|
||||||
|
// package.json
|
||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"postinstall": "<some dev only command>",
|
||||||
|
"prepublishOnly": "toggle-scripts -postinstall",
|
||||||
|
"postpublish": "toggle-scripts +postinstall"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
59
@vates/toggle-scripts/index.js
Executable file
59
@vates/toggle-scripts/index.js
Executable file
@ -0,0 +1,59 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const fs = require('fs')
|
||||||
|
|
||||||
|
const mapKeys = (object, iteratee) => {
|
||||||
|
const result = {}
|
||||||
|
for (const key of Object.keys(object)) {
|
||||||
|
result[iteratee(key, object)] = object[key]
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
const pkgPath = process.env.npm_package_json
|
||||||
|
if (pkgPath === undefined) {
|
||||||
|
const { description, name, version } = require('./package.json')
|
||||||
|
const bin = 'toggle-scripts'
|
||||||
|
process.stdout.write(`Usage: ${bin} options...
|
||||||
|
|
||||||
|
${description}
|
||||||
|
|
||||||
|
Options
|
||||||
|
+<script> Enable the script <script>, ie prefix it with \`_\`
|
||||||
|
-<script> Disable the script <script>, ie remove the prefix \`_\`
|
||||||
|
|
||||||
|
Examples
|
||||||
|
${bin} +postinstall +preuninstall
|
||||||
|
${bin} -postinstall -preuninstall
|
||||||
|
|
||||||
|
${name} v${version}
|
||||||
|
`)
|
||||||
|
process.exit()
|
||||||
|
}
|
||||||
|
|
||||||
|
const plan = { __proto__: null }
|
||||||
|
for (const arg of process.argv.slice(2)) {
|
||||||
|
const action = arg[0]
|
||||||
|
const script = arg.slice(1)
|
||||||
|
|
||||||
|
if (action === '+') {
|
||||||
|
plan['_' + script] = script
|
||||||
|
} else if (action === '-') {
|
||||||
|
plan[script] = '_' + script
|
||||||
|
} else {
|
||||||
|
throw new Error('invalid param: ' + arg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf8'))
|
||||||
|
pkg.scripts = mapKeys(pkg.scripts, (name, scripts) => {
|
||||||
|
const newName = plan[name]
|
||||||
|
if (newName === undefined) {
|
||||||
|
return name
|
||||||
|
}
|
||||||
|
if (newName in scripts) {
|
||||||
|
throw new Error('script already defined: ' + name)
|
||||||
|
}
|
||||||
|
return newName
|
||||||
|
})
|
||||||
|
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
|
41
@vates/toggle-scripts/package.json
Normal file
41
@vates/toggle-scripts/package.json
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
{
|
||||||
|
"private": false,
|
||||||
|
"name": "@vates/toggle-scripts",
|
||||||
|
"description": "Easily enable/disable scripts in package.json",
|
||||||
|
"keywords": [
|
||||||
|
"dev",
|
||||||
|
"disable",
|
||||||
|
"enable",
|
||||||
|
"lifecycle",
|
||||||
|
"npm",
|
||||||
|
"package.json",
|
||||||
|
"pinst",
|
||||||
|
"postinstall",
|
||||||
|
"script",
|
||||||
|
"scripts",
|
||||||
|
"toggle"
|
||||||
|
],
|
||||||
|
"homepage": "https://github.com/vatesfr/xen-orchestra/tree/master/@vates/toggle-scripts",
|
||||||
|
"bugs": "https://github.com/vatesfr/xen-orchestra/issues",
|
||||||
|
"repository": {
|
||||||
|
"directory": "@vates/toggle-scripts",
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/vatesfr/xen-orchestra.git"
|
||||||
|
},
|
||||||
|
"author": {
|
||||||
|
"name": "Vates SAS",
|
||||||
|
"url": "https://vates.fr"
|
||||||
|
},
|
||||||
|
"license": "ISC",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"index.j"
|
||||||
|
],
|
||||||
|
"bin": "./index.js",
|
||||||
|
"scripts": {
|
||||||
|
"postversion": "npm publish --access public"
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user