|
| 1 | +# Clean Package |
| 2 | + |
| 3 | +This `clean-package` tool is used for removing development configuration from 'package.json' before publishing the package to NPM. |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/clean-package) |
| 6 | +[](https://opensource.org/licenses/MIT) |
| 7 | + |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install clean-package --save |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +## Integrated Usage |
| 17 | + |
| 18 | +The `clean-package` tool works directly on the 'package.json' file, to avoid breaking the NPM lifecycle. This allow you to add a script to the 'package.json' to clean the file during packing. |
| 19 | + |
| 20 | +```json |
| 21 | +{ |
| 22 | + "name": "my-package", |
| 23 | + "version": "1.0.0", |
| 24 | + "scripts": { |
| 25 | + "prepack": "clean-package", |
| 26 | + "postpack": "clean-package restore" |
| 27 | + } |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +See [CLI Usage](#command-line-usage 'Command Line Usage') for independent usage instructions. |
| 32 | + |
| 33 | +#### JSON Configuration Files |
| 34 | + |
| 35 | +Options can be configured in `clean-package.config.json` at the root of your project (where the `package.json` is). |
| 36 | + |
| 37 | +```json |
| 38 | +{ |
| 39 | + "indent": 2, |
| 40 | + "remove": [ |
| 41 | + "eslintConfig", |
| 42 | + "jest" |
| 43 | + ] |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Alternatively, you can choose to specify your configuration from within `package.json` using the `clean-package` key like so: |
| 48 | + |
| 49 | +```json |
| 50 | +{ |
| 51 | + "name": "my-package", |
| 52 | + "version": "1.0.0", |
| 53 | + "clean-package": { |
| 54 | + "indent": 2, |
| 55 | + "remove": [ |
| 56 | + "eslintConfig", |
| 57 | + "jest" |
| 58 | + ] |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +#### JavaScript Configuration File |
| 64 | + |
| 65 | +You can also create the configuration using JavaScript in the `clean-package.config.js` at the root of your project: |
| 66 | + |
| 67 | +```js |
| 68 | +module.exports = { |
| 69 | + indent: '\t', |
| 70 | + replace: { |
| 71 | + 'config.port': '8080' |
| 72 | + } |
| 73 | +}; |
| 74 | +``` |
| 75 | + |
| 76 | + |
| 77 | +### Options |
| 78 | + |
| 79 | +##### backupPath |
| 80 | + |
| 81 | +Default: `'./package.json.backup'` |
| 82 | + |
| 83 | +A `String` specifying the location and filename to which the `package.json` will be backed up. |
| 84 | + |
| 85 | +##### indent |
| 86 | + |
| 87 | +Default: `2` |
| 88 | + |
| 89 | +A `String` or `Number` defining the indentation that's used to format the cleaned `package.json`. See the `space` parameter of `JSON.stringify` for [more information](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Parameters). |
| 90 | + |
| 91 | +##### remove |
| 92 | + |
| 93 | +Default: `['clean-package']` |
| 94 | + |
| 95 | +A `String[]` specifying the keys to be removed from the cleaned `package.json`; otherwise, `null` when nothing is to be removed. |
| 96 | + |
| 97 | +Deeper keys can be accessed using a dot (e.g., `'key.keyInsideKey'`). Likewise, arrays are accessible using brackets (e.g., `'key.arrKey[0]'`). |
| 98 | + |
| 99 | +##### replace |
| 100 | + |
| 101 | +Default: `null` |
| 102 | + |
| 103 | +An `Object` specifying the keys to be replaced in the cleaned `package.json`; otherwise, `null` when nothing is to be replaced. |
| 104 | + |
| 105 | +Deeper keys and arrays are accessible in the same manner. Additionally, the replaced keys may receive any valid JSON value, including objects. |
| 106 | + |
| 107 | +## Command Line Usage |
| 108 | + |
| 109 | +``` |
| 110 | +clean-package [<backup-path>] [<option>...] |
| 111 | +
|
| 112 | +where <option> is one of: |
| 113 | +
|
| 114 | + -i, --indent <value> Specify the indentation overriding existing configuration. |
| 115 | +
|
| 116 | + -rm, --remove <key>... Specify the keys to remove overriding existing configuration. |
| 117 | +
|
| 118 | + --removeAdd <key>... Same as --remove without overriding existing configuration. |
| 119 | +
|
| 120 | + -r, --replace <key>=<value>... Specify the keys to remove overriding existing configuration. |
| 121 | +
|
| 122 | + --replaceAdd <key>=<value>... Same as --replace without overriding existing configuration. |
| 123 | +``` |
| 124 | + |
| 125 | +``` |
| 126 | +clean-package restore [<backup-path>] |
| 127 | +
|
| 128 | +alias: r |
| 129 | +``` |
| 130 | + |
| 131 | +### How do I remove package scripts and use `clean-package restore`? |
| 132 | + |
| 133 | +If you're integrating `clean-package` into the NPM lifecycle, removing the all the `package.json` scripts with `clean-package` will also remove them from the current execution. This is just how NPM works. |
| 134 | + |
| 135 | +For example, this configuration will remove the `postpack` script before it is every requested by `npm pack` or `npm publish`, thereby effectively removing the event from the executing lifecycle. |
| 136 | + |
| 137 | +```json |
| 138 | +{ |
| 139 | + "scripts": { |
| 140 | + "prepack": "clean-package", |
| 141 | + "postpack": "clean-package restore" |
| 142 | + }, |
| 143 | + "clean-package": { |
| 144 | + "remove": [ |
| 145 | + "clean-package", |
| 146 | + "scripts" |
| 147 | + ] |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +There are multiple ways to work around this (more than are offered here). One solution might be to manual run the command with `npx clean-package restore`. Another might be to define a custom script that would call `pack` and `clean-package` in sequence: |
| 153 | + |
| 154 | +```json |
| 155 | +{ |
| 156 | + "scripts": { |
| 157 | + "prepack": "clean-package", |
| 158 | + "new:pack": "npm pack && clean-package restore", |
| 159 | + "new:publish": "npm publish && clean-package restore" |
| 160 | + } |
| 161 | +} |
| 162 | +``` |
0 commit comments