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() ), } );