Skip to content

Commit

Permalink
Scripts: Add support for lint-pkg-json script (#128)
Browse files Browse the repository at this point in the history
* Scripts: Add support for lint-pkg-json script

* Scripts: Update documentation for `lint-pkg-json` command
  • Loading branch information
gziolo authored May 23, 2018
1 parent 4beeb1a commit 04093f5
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 15 deletions.
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
6 changes: 5 additions & 1 deletion packages/scripts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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))
49 changes: 40 additions & 9 deletions packages/scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,46 @@ 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": {
"lint:pkg-json": "wp-scripts lint-pkg-json .",
"test": "wp-scripts test-unit-js"
}
}
```

## 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` (as presented below) to view all of the available options.

_Example:_

```json
{
"scripts": {
Expand All @@ -28,14 +67,6 @@ This is how you execute those scripts using the presented setup:
* `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 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.

## Inspiration

This is inspired by [react-scripts](https://www.npmjs.com/package/react-scripts) and [kcd-scripts](https://www.npmjs.com/package/kcd-scripts).
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

0 comments on commit 04093f5

Please sign in to comment.