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

Scripts: Add support for lint-pkg-json script #128

Merged
merged 2 commits into from
May 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
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).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's include an example here:

  "scripts": {
    "lint:pkg-json": "wp-scripts lint-pkg-json ."
  },

Without the . at the end of the command this error is displayed: No lint targets provided

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good call. We can make sure it is provided as default instead. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default is a good idea if it can be overridden using packages/ for example with Lerna repos.

Regardless of that though we should include some basic docs here in the readme

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's start with your example and iterate :)

### `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"
}
5 changes: 4 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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