format-package
Advanced tools
Comparing version 5.0.0 to 5.1.0
#!/usr/bin/env node | ||
const handleFile = ({ write, verbose }, config) => async filePath => { | ||
const path = require('path'); | ||
const fs = require('../fs'); | ||
const format = require('../'); | ||
const path = require('path'); | ||
const Timer = require('../utils/timer'); | ||
const fs = require('fs-extra'); | ||
const globby = require('globby'); | ||
const format = require('../'); | ||
const Timer = require('../utils/timer'); | ||
const parser = require('./parse'); | ||
const configSearch = require('./config'); | ||
const error = require('./error'); | ||
const handleFile = ({ write, verbose }, config) => async filePath => { | ||
const timer = new Timer(); | ||
timer.start(); | ||
const prevPkgContent = await fs.read(filePath, 'utf8'); | ||
const prevPkg = JSON.parse(prevPkgContent); | ||
const prevPkg = await fs.readJson(filePath, 'utf8'); | ||
const nextPkg = await format(prevPkg, config); | ||
if (write) { | ||
await fs.write(filePath, nextPkg, { encoding: 'utf8' }); | ||
await fs.writeFile(filePath, nextPkg, { encoding: 'utf8' }); | ||
} | ||
@@ -33,18 +39,10 @@ | ||
try { | ||
const path = require('path'); | ||
const Timer = require('../utils/timer'); | ||
const timer = new Timer(); | ||
timer.start(); | ||
const { | ||
globs, | ||
config: configPath, | ||
ignore, | ||
...options | ||
} = require('./parse')(argv); | ||
const { globs, config: configPath, ignore, ...options } = parser(argv); | ||
const config = configPath ? require(configPath) : {}; | ||
const { config } = await configSearch.search({ configPath }); | ||
const files = await require('globby')(globs, { | ||
const files = await globby(globs, { | ||
onlyFiles: true, | ||
@@ -59,2 +57,3 @@ ignore, | ||
/* istanbul ignore next */ | ||
console.log( | ||
@@ -64,7 +63,12 @@ `✏️ Formatted ${files.length} file${files.length === 1 ? '' : 's'}` | ||
} catch (err) { | ||
require('./error')(err); | ||
error(err); | ||
} | ||
} | ||
/* istanbul ignore next */ | ||
if (require.main === module) execute(process.argv.slice(2)); | ||
module.exports = execute; | ||
module.exports = { | ||
handleFile, | ||
execute, | ||
}; |
@@ -1,3 +0,1 @@ | ||
const path = require('path'); | ||
const parser = require('yargs')('run --help') | ||
@@ -12,2 +10,3 @@ .command(['format [files..]', '*'], 'Format files', yargs => { | ||
}) | ||
.env('FORMAT_PACKAGE') | ||
.options({ | ||
@@ -19,3 +18,2 @@ c: { | ||
type: 'string', | ||
transform: path.resolve, | ||
}, | ||
@@ -44,4 +42,5 @@ w: { | ||
}) | ||
.strict() | ||
.help('h'); | ||
module.exports = parser.parse.bind(parser); |
@@ -18,5 +18,6 @@ [ | ||
"main", | ||
"directories", | ||
"module", | ||
"browser", | ||
"files", | ||
"directories", | ||
"config", | ||
@@ -23,0 +24,0 @@ "publishConfig", |
@@ -0,4 +1,6 @@ | ||
const sortScripts = require('sort-scripts'); | ||
const transformations = { | ||
scripts(key, prevValue) { | ||
const nextValue = require('sort-scripts')(prevValue).reduce( | ||
const nextValue = sortScripts(prevValue).reduce( | ||
(obj, [name, value]) => Object.assign({}, obj, { [name]: value }), | ||
@@ -5,0 +7,0 @@ {} |
{ | ||
"name": "format-package", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Sensible formatting and ordering of package.json", | ||
@@ -32,8 +32,8 @@ "license": "MIT", | ||
"format": "npm-run-all format:*", | ||
"format:pkg": "./lib/cli/index.js -wp", | ||
"format:src": "prettier --write \"**/*.{md,js,json}\"", | ||
"format:package": "./lib/cli/index.js -w package.json", | ||
"format:source": "prettier --write \"**/*.{md,js,json}\"", | ||
"gamut": "npm-run-all reset docs format lint test", | ||
"lint": "eslint \"**/*.js\"", | ||
"prepublishOnly": "yarn gamut", | ||
"reset": "cross-env rimraf node_modules && yarn install", | ||
"reset": "rimraf node_modules && yarn install", | ||
"test": "jest" | ||
@@ -43,3 +43,3 @@ }, | ||
"**/package.json": [ | ||
"yarn format:pkg", | ||
"yarn format:package", | ||
"git add" | ||
@@ -57,6 +57,19 @@ ], | ||
}, | ||
"jest": { | ||
"snapshotSerializers": [ | ||
"./scripts/snapshot-serializers-ansi.js" | ||
], | ||
"testMatch": [ | ||
"**/lib/**/?(*.)+(spec|test).[jt]s?(x)" | ||
] | ||
}, | ||
"dependencies": { | ||
"@hapi/joi": "^15.1.0", | ||
"chalk": "^2.4.2", | ||
"convert-hrtime": "^3.0.0", | ||
"cosmiconfig": "^5.2.1", | ||
"fs-extra": "^8.1.0", | ||
"globby": "10.0.1", | ||
"json5": "^2.1.0", | ||
"resolve-from": "^5.0.0", | ||
"sort-scripts": "^1.0.1", | ||
@@ -69,4 +82,8 @@ "yargs": "^13.2.4" | ||
"devDependencies": { | ||
"@types/cosmiconfig": "^5.0.3", | ||
"@types/fs-extra": "^8.0.0", | ||
"@types/hapi__joi": "^15.0.2", | ||
"@types/jest": "^24.0.15", | ||
"@types/node": "^12.0.12", | ||
"codecov": "^3.5.0", | ||
"cross-env": "^5.2.0", | ||
"eslint": "^6.0.1", | ||
@@ -78,6 +95,7 @@ "eslint-config-airbnb-base": "13.2.0", | ||
"glob": "^7.1.4", | ||
"has-ansi": "^3.0.0", | ||
"husky": "3.0.0", | ||
"install-deps-postmerge": "^1.0.3", | ||
"jest": "^24.8.0", | ||
"lint-staged": "9.0.2", | ||
"lint-staged": "9.1.0", | ||
"markdown-magic": "^0.1.25", | ||
@@ -89,4 +107,5 @@ "markdown-magic-engines": "^1.0.2", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^1.18.2" | ||
"prettier": "^1.18.2", | ||
"strip-ansi": "^5.2.0" | ||
} | ||
} |
136
README.md
@@ -27,2 +27,9 @@ # Format Package [![codecov](https://codecov.io/gh/camacho/format-package/branch/master/graph/badge.svg)](https://codecov.io/gh/camacho/format-package) | ||
- [CLI](#cli) | ||
- [Configuration Files](#configuration-files) | ||
- [Configuration Schema](#configuration-schema) | ||
- [Configuration Examples](#configuration-examples) | ||
- [with package.json](#with-packagejson) | ||
- [with format-package.json](#with-format-packagejson) | ||
- [with `format-package.js` or `format-package.config.js`](#with-format-packagejs-or-format-packageconfigjs) | ||
- [with format-package.{yml,yaml}, format-package.config.{yml,yaml}](#with-format-packageymlyaml-format-packageconfigymlyaml) | ||
- [Integrating](#integrating) | ||
@@ -190,5 +197,6 @@ - [Development](#development) | ||
"main", | ||
"directories", | ||
"module", | ||
"browser", | ||
"files", | ||
"directories", | ||
"config", | ||
@@ -268,5 +276,7 @@ "publishConfig", | ||
```js | ||
const sortScripts = require('sort-scripts'); | ||
const transformations = { | ||
scripts(key, prevValue) { | ||
const nextValue = require('sort-scripts')(prevValue).reduce( | ||
const nextValue = sortScripts(prevValue).reduce( | ||
(obj, [name, value]) => Object.assign({}, obj, { [name]: value }), | ||
@@ -360,10 +370,19 @@ {} | ||
| **Option** | **Alias** | **Description** | **Default** | | ||
| ---------- | --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | | ||
| `config` | `c` | Path to a custom configuration to use. This configuration can be JavaScript, `JSON`, or any other format that your configuration of node can `require`. The default configuration can be found [here](lib/defaults/index.js). | | | ||
| `write` | `w` | Write the output to the location of the found `package.json` | **false** | | ||
| `ignore` | `i` | Patterns for ignoring matching files | **`['**/node_modules/**']`** | | ||
| `verbose` | `v` | Print the output of the formatting | **false** | | ||
| `help` | `h` | Print help menu | | | ||
Options can also be passed as environment variables and are used in the following order of precedence: | ||
1. Command line options | ||
2. Env vars | ||
``` | ||
FORMAT_PACKAGE_VERBOSE=true | ||
``` | ||
| **Option** | **Alias** | **ENV** | **Description** | **Default** | | ||
| ---------- | --------- | ---------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------- | | ||
| `config` | `c` | FORMAT_PACKAGE_CONFIG | Path to a custom configuration to use. This configuration can be JavaScript, `JSON`, or any other format that your configuration of node can `require`. The default configuration can be found [here](lib/defaults/index.js). | | | ||
| `write` | `w` | FORMAT_PACKAGE_WRITE | Write the output to the location of the found `package.json` | **false** | | ||
| `ignore` | `i` | FORMAT_PACKAGE_IGNORE | Patterns for ignoring matching files | **`['**/node_modules/**']`** | | ||
| `verbose` | `v` | FORMAT_PACKAGE_VERBOSE | Print the output of the formatting | **false** | | ||
| `help` | `h` | | Print help menu | | | ||
You can also see the available options in the terminal by running: | ||
@@ -375,2 +394,97 @@ | ||
## Configuration Files | ||
`format-package` will search for a valid configuration file in the following order of precedence. | ||
1. If the option `--config [path | module id]` or a `FORMAT_PACKAGE_CONFIG` | ||
environment variable is provided: | ||
a. check if the value resolves to a module id, else | ||
b. check if value resolves to an existing path | ||
If either `a` or `b` are valid configuration, then use the configuration, else continue searching. | ||
2. [`format-package.js`](#with-format-packagejs-or-format-packageconfigjs) | ||
3. [`format-package.yaml` or `format-package.yml`](#with-format-packageymlyaml-format-packageconfigymlyaml) | ||
4. [`format-package.json`](#with-format-packagejson) | ||
5. [`format-package.config.js`](#with-format-packagejs-or-format-packageconfigjs) | ||
6. [`format-package.config.yaml` or `format-package.config.yml`](#with-format-packageymlyaml-format-packageconfigymlyaml) | ||
7. [`format-package`](#with-packagejson) property in `package.json` | ||
If there are no valid configuration, it will use the [default](lib/defaults/index.js). | ||
### Configuration Schema | ||
<!-- AUTO-GENERATED-CONTENT:START (REGION:src=./lib/cli/config-schema.js®ion='Joi Schema') --> | ||
<!-- The below code snippet is automatically added from ./lib/cli/config-schema.js --> | ||
```js | ||
const JoiConfigSchema = Joi.object({ | ||
order: Joi.array() | ||
.min(0) | ||
.unique(), | ||
transformations: Joi.object().optional(), | ||
formatter: Joi.func().optional(), | ||
}); | ||
``` | ||
<!-- AUTO-GENERATED-CONTENT:END --> | ||
### Configuration Examples | ||
Supported configuration formats: JSON, JSON5, JS, and YAML. | ||
### with package.json | ||
<!-- AUTO-GENERATED-CONTENT:START (JSONPROP:src=./examples/format-package-property/package.json&prop=format-package) --> | ||
<!-- The below code snippet is automatically added from ./examples/format-package-property/package.json --> | ||
```json | ||
{ | ||
"order": ["name", "version"] | ||
} | ||
``` | ||
<!-- AUTO-GENERATED-CONTENT:END --> | ||
### with format-package.json | ||
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/format-package-json/format-package.json) --> | ||
<!-- The below code snippet is automatically added from ./examples/format-package-json/format-package.json --> | ||
```json | ||
{ | ||
"order": ["name", "description", "..."] | ||
} | ||
``` | ||
<!-- AUTO-GENERATED-CONTENT:END --> | ||
### with `format-package.js` or `format-package.config.js` | ||
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/format-package-config-js/format-package.config.js) --> | ||
<!-- The below code snippet is automatically added from ./examples/format-package-config-js/format-package.config.js --> | ||
```js | ||
module.exports = { | ||
order: ['name', 'description', '...'], | ||
}; | ||
``` | ||
<!-- AUTO-GENERATED-CONTENT:END --> | ||
### with format-package.{yml,yaml}, format-package.config.{yml,yaml} | ||
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/format-package-config-yml/format-package.config.yml) --> | ||
<!-- The below code snippet is automatically added from ./examples/format-package-config-yml/format-package.config.yml --> | ||
```yml | ||
order: | ||
- name | ||
- description | ||
- ... | ||
``` | ||
<!-- AUTO-GENERATED-CONTENT:END --> | ||
## Integrating | ||
@@ -431,4 +545,4 @@ | ||
| `format` | format the application code | | ||
| `format:pkg` | format package.json | | ||
| `format:src` | format source content using [prettier](https://github.com/prettier/prettier) | | ||
| `format:package` | `./lib/cli/index.js -w package.json` | | ||
| `format:source` | `prettier --write "**/*.{md,js,json}"` | | ||
| `gamut` | run the full gamut of checks - reset environment, generate docs, format and lint code, and run tests | | ||
@@ -435,0 +549,0 @@ | `lint` | lint the application code | |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
35097
34
405
551
2
11
25
+ Added@hapi/joi@^15.1.0
+ Addedcosmiconfig@^5.2.1
+ Addedfs-extra@^8.1.0
+ Addedjson5@^2.1.0
+ Addedresolve-from@^5.0.0
+ Added@hapi/address@2.1.4(transitive)
+ Added@hapi/bourne@1.3.2(transitive)
+ Added@hapi/hoek@8.5.1(transitive)
+ Added@hapi/joi@15.1.1(transitive)
+ Added@hapi/topo@3.1.6(transitive)
+ Addedargparse@1.0.10(transitive)
+ Addedcaller-callsite@2.0.0(transitive)
+ Addedcaller-path@2.0.0(transitive)
+ Addedcallsites@2.0.0(transitive)
+ Addedcosmiconfig@5.2.1(transitive)
+ Addederror-ex@1.3.2(transitive)
+ Addedesprima@4.0.1(transitive)
+ Addedfs-extra@8.1.0(transitive)
+ Addedgraceful-fs@4.2.11(transitive)
+ Addedimport-fresh@2.0.0(transitive)
+ Addedis-arrayish@0.2.1(transitive)
+ Addedis-directory@0.3.1(transitive)
+ Addedjs-yaml@3.14.1(transitive)
+ Addedjson-parse-better-errors@1.0.2(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedjsonfile@4.0.0(transitive)
+ Addedparse-json@4.0.0(transitive)
+ Addedresolve-from@3.0.05.0.0(transitive)
+ Addedsprintf-js@1.0.3(transitive)
+ Addeduniversalify@0.1.2(transitive)