Skip to content
This repository has been archived by the owner on Jul 9, 2018. It is now read-only.

Commit

Permalink
Scripts: Add support for lint-pkg-json script
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed May 22, 2018
1 parent 4beeb1a commit 0553afd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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",
Expand Down
4 changes: 4 additions & 0 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 3 additions & 0 deletions packages/scripts/config/npmpackagejsonlint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@wordpress/npm-package-json-lint-config"
}
4 changes: 3 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
"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"
"read-pkg-up": "^3.0.0",
"resolve-bin": "^0.4.0"
},
"publishConfig": {
"access": "public"
Expand Down
36 changes: 36 additions & 0 deletions packages/scripts/scripts/lint-pkg-json.js
Original file line number Diff line number Diff line change
@@ -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 );
4 changes: 4 additions & 0 deletions packages/scripts/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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` );

Expand Down Expand Up @@ -74,6 +77,7 @@ const spawnScript = ( scriptName, args = [] ) => {
};

module.exports = {
fromConfigRoot,
getCliArgs,
hasCliArg,
hasProjectFile,
Expand Down
4 changes: 2 additions & 2 deletions packages/scripts/utils/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() ),
} );

Expand Down

0 comments on commit 0553afd

Please sign in to comment.