diff --git a/bin/process-git-diff.js b/bin/process-git-diff.js new file mode 100644 index 0000000000000..b80b117daa59e --- /dev/null +++ b/bin/process-git-diff.js @@ -0,0 +1,34 @@ +// npm will introduce changes to a `package-lock.json` file for optional +// dependencies varying on environment. If the only changes are the +// addition of an "optional" flag in `package-lock.json` file from +// `git diff`: we ignore the results. +// +// See: https://github.com/npm/npm/issues/17722 + +// Example usage: +// +// git diff -U0 | xargs -0 node bin/process-git-diff + +// Example input: +// +// diff --git a/package-lock.json b/package-lock.json +// index e8c8a25dc..251af8689 100644 +// --- a/package-lock.json +// +++ b/package-lock.json +// @@ -14373 +14373,2 @@ +// - "dev": true +// + "dev": true, +// + "optional": true +// @@ -14648 +14649,2 @@ +// - "dev": true +// + "dev": true, +// + "optional": true + +const hasNonOptionalDiff = !! ( process.argv[ 2 ] || '' ) + // Strip individual diffs of optional-only. + .replace( /@@ .+ @@\n(-.+\n\+.+,\n)?\+.+\"optional\": true,?\n/gm, '' ) + // If no more line diffs remain after above, remove diff heading for file. + .replace( /diff --git a\/package-lock.json b\/package-lock.json\nindex \w+..\w+ \d+\n--- a\/package-lock.json\n\+\+\+ b\/package-lock.json\n(?!@@)/, '' ); + +// Exit with error code if, after replace, changes still exist. +process.exit( hasNonOptionalDiff ? 1 : 0 ); diff --git a/package.json b/package.json index 7a31c83725846..1b8e0ce2e6c2c 100644 --- a/package.json +++ b/package.json @@ -151,7 +151,9 @@ "build": "npm run build:packages && cross-env NODE_ENV=production webpack", "check-engines": "check-node-version --package", "check-licenses": "concurrently \"wp-scripts check-licenses --prod --gpl2\" \"wp-scripts check-licenses --dev\"", - "ci": "concurrently \"npm run lint\" \"npm run test-unit:coverage-ci\"", + "precheck-local-changes": "npm run docs:build", + "check-local-changes": "( git diff -U0 | xargs -0 node bin/process-git-diff ) || ( echo \"There are local uncommitted changes after one or both of 'npm install' or 'npm run docs:build'!\" && exit 1 );", + "ci": "concurrently \"npm run lint\" \"npm run test-unit:coverage-ci\" \"npm run check-local-changes\"", "predev": "npm run check-engines", "dev": "npm run build:packages && concurrently \"cross-env webpack --watch\" \"npm run dev:packages\"", "dev:packages": "node ./bin/packages/watch.js",