Skip to content

Commit 605a86a

Browse files
committed
Initial release
1 parent 35b1f4c commit 605a86a

13 files changed

+561
-0
lines changed

.editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
root = true
2+
3+
[*]
4+
insert_final_newline = true
5+
indent_size = 2
6+
indent_style = tab
7+
tab_width = 2
8+
trim_trailing_whitespace = true
9+
10+
[*.md]
11+
indent_style = space

.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# User-specific files
2+
.DS_Store
3+
*.env
4+
*.user
5+
*.vs
6+
7+
# Package Managers
8+
node_modules
9+
npm-debug.log
10+
11+
# Build Artifacts
12+
*.lock.json
13+
*-lock.json
14+
*.backup
15+
*.cache
16+
*.tgz

.npmignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# User-specific files
2+
.DS_Store
3+
*.editorconfig
4+
*.env
5+
*.user
6+
*.vs
7+
8+
# Package Managers
9+
node_modules
10+
npm-debug.log
11+
12+
# Build Artifacts
13+
*.lock.json
14+
*-lock.json
15+
*.backup
16+
*.cache
17+
*.tgz

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
* Initial release!

README.md

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
[![Release Version](https://img.shields.io/npm/v/clean-package.svg)](https://www.npmjs.com/package/clean-package)
6+
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](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+
```

bin/clean-package.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
require('../src');

package.json

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"name": "clean-package",
3+
"description": "Removing configuration keys in 'package.json' before creating an NPM package.",
4+
"version": "1.0.0",
5+
"author": "roydukkey",
6+
"license": "MIT",
7+
"repository": {
8+
"type": "git",
9+
"url": "git+https://[email protected]/roydukkey/clean-package.git"
10+
},
11+
"homepage": "https://github.com/roydukkey/clean-package#readme",
12+
"bugs": {
13+
"url": "https://github.com/roydukkey/clean-package/issues"
14+
},
15+
"keywords": [
16+
"npm",
17+
"pack",
18+
"package",
19+
"package.json",
20+
"publish",
21+
"clean"
22+
],
23+
"bin": {
24+
"clean-package": "./bin/clean-package.js"
25+
},
26+
"dependencies": {
27+
"dot-object": "^2.1.3"
28+
},
29+
"devDependencies": {
30+
"@types/node": "^14.6.1",
31+
"eslint": "^7.7.0"
32+
},
33+
"scripts": {
34+
"prepack": "$npm_package_bin_clean_package",
35+
"new:pack": "npm pack && $npm_package_bin_clean_package restore",
36+
"new:publish": "npm publish && $npm_package_bin_clean_package restore"
37+
},
38+
"clean-package": {
39+
"indent": "\t",
40+
"remove": [
41+
"clean-package",
42+
"eslintConfig",
43+
"scripts"
44+
]
45+
},
46+
"eslintConfig": {
47+
"env": {
48+
"node": true,
49+
"es6": true
50+
},
51+
"parserOptions": {
52+
"ecmaVersion": 2018
53+
},
54+
"rules": {
55+
"arrow-parens": [
56+
"error",
57+
"always"
58+
],
59+
"arrow-spacing": [
60+
"error",
61+
{
62+
"before": true,
63+
"after": true
64+
}
65+
],
66+
"brace-style": [
67+
"error",
68+
"stroustrup",
69+
{
70+
"allowSingleLine": false
71+
}
72+
],
73+
"comma-spacing": [
74+
"error",
75+
{
76+
"before": false,
77+
"after": true
78+
}
79+
],
80+
"curly": [
81+
"error"
82+
],
83+
"comma-dangle": [
84+
"error",
85+
"never"
86+
],
87+
"key-spacing": [
88+
"error"
89+
],
90+
"object-curly-spacing": [
91+
"error",
92+
"always"
93+
],
94+
"quotes": [
95+
"error",
96+
"single"
97+
],
98+
"semi": [
99+
"error"
100+
],
101+
"sort-imports": [
102+
"error",
103+
{
104+
"ignoreCase": false,
105+
"ignoreDeclarationSort": false,
106+
"ignoreMemberSort": false,
107+
"memberSyntaxSortOrder": [
108+
"single",
109+
"multiple",
110+
"all",
111+
"none"
112+
]
113+
}
114+
],
115+
"space-before-blocks": [
116+
"error",
117+
"always"
118+
],
119+
"space-before-function-paren": [
120+
"error",
121+
"always"
122+
]
123+
}
124+
}
125+
}

src/clean.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
const dot = require('dot-object');
4+
const fs = require('fs');
5+
const isDeepStrictEqual = require('util').isDeepStrictEqual;
6+
7+
8+
module.exports = (options, packageJson) => {
9+
10+
// Whether or not the contents of that 'package.json' has changed.
11+
let hasChanged = false;
12+
13+
// Delete keys which are not needed in release package
14+
options.remove.forEach((value) => {
15+
if (dot.delete(value, packageJson) !== undefined && !hasChanged) {
16+
hasChanged = true;
17+
}
18+
});
19+
20+
// Replace keys which should have different content in release package
21+
if (options.replace) {
22+
for (const [key, value] of Object.entries(options.replace)) {
23+
if (!isDeepStrictEqual(dot.pick(key, packageJson), value)) {
24+
dot.str(key, value, packageJson);
25+
hasChanged = true;
26+
}
27+
}
28+
}
29+
30+
// If there are changes, write files
31+
if (hasChanged) {
32+
33+
// Backup source file
34+
fs.renameSync(options.sourcePath, options.backupPath);
35+
36+
// Write changes to package
37+
fs.writeFileSync(options.sourcePath, JSON.stringify(packageJson, null, options.indent));
38+
39+
}
40+
};

src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const [options, packageJson] = require('./options');
4+
5+
6+
// Route command to 'restore' otherwise 'clean'
7+
require(options.isRestore ? './restore' : './clean.js')(options, packageJson);

0 commit comments

Comments
 (0)