From 981da448addba7a2a42dbaf15b22302a1086d708 Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Tue, 22 May 2018 10:31:20 +0200 Subject: [PATCH 1/2] Scripts: Add support for lint-pkg-json script --- package.json | 3 +- packages/scripts/README.md | 4 +++ .../scripts/config/npmpackagejsonlint.json | 3 ++ packages/scripts/package.json | 5 ++- packages/scripts/scripts/lint-pkg-json.js | 36 +++++++++++++++++++ packages/scripts/utils/index.js | 4 +++ packages/scripts/utils/package.js | 4 +-- 7 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 packages/scripts/config/npmpackagejsonlint.json create mode 100644 packages/scripts/scripts/lint-pkg-json.js diff --git a/package.json b/package.json index 1581c55..d6299f9 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "is-plain-obj": "^1.1.0", "lerna": "^2.9.0", "mkdirp": "^0.5.1", - "npm-package-json-lint": "^3.0.1", "npm-run-all": "^4.1.2", "path-type": "^3.0.0", "rimraf": "^2.6.1", @@ -56,7 +55,7 @@ "check-engines": "check-node-version --package", "create-symlinks": "node ./scripts/create-symlinks.js", "lerna-bootstrap": "lerna bootstrap --hoist", - "lint:pkg-json": "npmPkgJsonLint ./packages", + "lint:pkg-json": "wp-scripts lint-pkg-json ./packages", "postinstall": "npm-run-all lerna-bootstrap create-symlinks build", "pretest": "npm run lint:pkg-json", "test": "wp-scripts test-unit-js", diff --git a/packages/scripts/README.md b/packages/scripts/README.md index d5d970b..9a0af09 100644 --- a/packages/scripts/README.md +++ b/packages/scripts/README.md @@ -30,6 +30,10 @@ This is how you execute those scripts using the presented setup: ## Available Scripts +### `wp-scripts lint-pkg-json` + +Helps enforce standards for your package.json file. It uses [npm-package-json-lint](https://www.npmjs.com/package/npm-package-json-lint) with the set of default rules provided. You can override them with your own rules as described in [npm-package-json-lint wiki](https://github.com/tclindner/npm-package-json-lint/wiki). + ### `wp-scripts test-unit-js` _Alias_: `wp-scripts test-unit-jest` diff --git a/packages/scripts/config/npmpackagejsonlint.json b/packages/scripts/config/npmpackagejsonlint.json new file mode 100644 index 0000000..2d323d3 --- /dev/null +++ b/packages/scripts/config/npmpackagejsonlint.json @@ -0,0 +1,3 @@ +{ + "extends": "@wordpress/npm-package-json-lint-config" +} diff --git a/packages/scripts/package.json b/packages/scripts/package.json index 1ac7569..93442ce 100644 --- a/packages/scripts/package.json +++ b/packages/scripts/package.json @@ -33,9 +33,12 @@ "dependencies": { "@wordpress/babel-preset-default": "^1.3.0", "@wordpress/jest-preset-default": "^1.0.6", + "@wordpress/npm-package-json-lint-config": "^1.0.0", "cross-spawn": "^5.1.0", "jest": "^22.4.0", - "read-pkg-up": "^3.0.0" + "npm-package-json-lint": "^3.0.1", + "read-pkg-up": "^3.0.0", + "resolve-bin": "^0.4.0" }, "publishConfig": { "access": "public" diff --git a/packages/scripts/scripts/lint-pkg-json.js b/packages/scripts/scripts/lint-pkg-json.js new file mode 100644 index 0000000..c7856c3 --- /dev/null +++ b/packages/scripts/scripts/lint-pkg-json.js @@ -0,0 +1,36 @@ +/** + * External dependencies + */ +const { sync: spawn } = require( 'cross-spawn' ); +const { sync: resolveBin } = require( 'resolve-bin' ); + +/** + * Internal dependencies + */ +const { + fromConfigRoot, + getCliArgs, + hasCliArg, + hasProjectFile, + hasPackageProp, +} = require( '../utils' ); + +const args = getCliArgs(); + +const hasLintConfig = hasCliArg( '-c' ) || + hasCliArg( '--configFile' ) || + hasProjectFile( '.npmpackagejsonlintrc.json' ) || + hasProjectFile( 'npmpackagejsonlint.config.js' ) || + hasPackageProp( 'npmPackageJsonLintConfig' ); + +const config = ! hasLintConfig + ? [ '--configFile', fromConfigRoot( 'npmpackagejsonlint.json' ) ] + : []; + +const result = spawn( + resolveBin( 'npm-package-json-lint', { executable: 'npmPkgJsonLint' } ), + [ ...config, ...args ], + { stdio: 'inherit' } +); + +process.exit( result.status ); diff --git a/packages/scripts/utils/index.js b/packages/scripts/utils/index.js index 791341a..712516f 100644 --- a/packages/scripts/utils/index.js +++ b/packages/scripts/utils/index.js @@ -22,6 +22,9 @@ const fromProjectRoot = ( fileName ) => const hasProjectFile = ( fileName ) => existsSync( fromProjectRoot( fileName ) ); +const fromConfigRoot = ( fileName ) => + path.join( path.dirname( __dirname ), 'config', fileName ); + const fromScriptsRoot = ( scriptName ) => path.join( path.dirname( __dirname ), 'scripts', `${ scriptName }.js` ); @@ -74,6 +77,7 @@ const spawnScript = ( scriptName, args = [] ) => { }; module.exports = { + fromConfigRoot, getCliArgs, hasCliArg, hasProjectFile, diff --git a/packages/scripts/utils/package.js b/packages/scripts/utils/package.js index 88e90aa..4de586f 100644 --- a/packages/scripts/utils/package.js +++ b/packages/scripts/utils/package.js @@ -2,14 +2,14 @@ * External dependencies */ const { realpathSync } = require( 'fs' ); -const readPkgUp = require( 'read-pkg-up' ); +const { sync: readPkgUp } = require( 'read-pkg-up' ); /** * Internal dependencies */ const { getCurrentWorkingDirectory } = require( './process' ); -const { pkg, path: pkgPath } = readPkgUp.sync( { +const { pkg, path: pkgPath } = readPkgUp( { cwd: realpathSync( getCurrentWorkingDirectory() ), } ); From 670fc4b004c86456f5735e7e6af0fc84d77e063e Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Wed, 23 May 2018 09:32:22 +0200 Subject: [PATCH 2/2] Scripts: Update documentation for `lint-pkg-json` command --- packages/scripts/CHANGELOG.md | 6 ++++- packages/scripts/README.md | 47 +++++++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/packages/scripts/CHANGELOG.md b/packages/scripts/CHANGELOG.md index 011abcf..bdc966a 100644 --- a/packages/scripts/CHANGELOG.md +++ b/packages/scripts/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.0 (2018-05-23) + +- Added support for `lint-pkg-json` script ([#128](https://github.com/WordPress/packages/pull/128)) + ## 1.1.5 (2018-05-18) -- Fix: Standardized `package.json` format ([#119](https://github.com/WordPress/packages/pull/119)) +- Fix: Standardized `package.json` format ([#119](https://github.com/WordPress/packages/pull/119)) diff --git a/packages/scripts/README.md b/packages/scripts/README.md index 9a0af09..3d0d4b7 100644 --- a/packages/scripts/README.md +++ b/packages/scripts/README.md @@ -12,33 +12,60 @@ npm install @wordpress/scripts --save-dev ## Setup -This is a CLI and exposes a binary called `wp-scripts` so you can call it directly. However this module is designed to be configured using the `scripts` section in the `package.json` file of your project. Example configuration: +This is a CLI and exposes a binary called `wp-scripts` so you can call it directly. However this module is designed to be configured using the `scripts` section in the `package.json` file of your project. + +_Example:_ + ```json { "scripts": { - "test": "wp-scripts test-unit-js", - "test:help": "wp-scripts test-unit-js --help", - "test:watch": "wp-scripts test-unit-js --watch" + "lint:pkg-json": "wp-scripts lint-pkg-json .", + "test": "wp-scripts test-unit-js" } } ``` -This is how you execute those scripts using the presented setup: -* `npm run test` or `npm test` - runs all unit tests. -* `npm run test:help` - prints all available options to configure unit tests runner. -* `npm run test:watch` - runs all unit tests in the watch mode. - ## Available Scripts ### `wp-scripts lint-pkg-json` Helps enforce standards for your package.json file. It uses [npm-package-json-lint](https://www.npmjs.com/package/npm-package-json-lint) with the set of default rules provided. You can override them with your own rules as described in [npm-package-json-lint wiki](https://github.com/tclindner/npm-package-json-lint/wiki). +_Example:_ + +```json +{ + "scripts": { + "lint:pkg-json": "wp-scripts lint-pkg-json ." + } +} +``` + +This is how you execute those scripts using the presented setup: +* `npm run lint:pkg-jsont` - lints `package.json` file in the project's root folder. + ### `wp-scripts test-unit-js` _Alias_: `wp-scripts test-unit-jest` -Launches the test runner. It uses [Jest](https://facebook.github.io/jest/) behind the scenes and you are able to utilize all of its [CLI options](https://facebook.github.io/jest/docs/en/cli.html). You can also run `./node_modules/.bin/wp-scripts test-unit-js --help` or `npm run test:help` (if you use `package.json` setup shared above) to view all of the available options. +Launches the test runner. It uses [Jest](https://facebook.github.io/jest/) behind the scenes and you are able to utilize all of its [CLI options](https://facebook.github.io/jest/docs/en/cli.html). You can also run `./node_modules/.bin/wp-scripts test-unit-js --help` or `npm run test:help` (as presented below) to view all of the available options. + +_Example:_ + +```json +{ + "scripts": { + "test": "wp-scripts test-unit-js", + "test:help": "wp-scripts test-unit-js --help", + "test:watch": "wp-scripts test-unit-js --watch" + } +} +``` + +This is how you execute those scripts using the presented setup: +* `npm run test` or `npm test` - runs all unit tests. +* `npm run test:help` - prints all available options to configure unit tests runner. +* `npm run test:watch` - runs all unit tests in the watch mode. ## Inspiration