diff --git a/bin/check-local-changes.js b/bin/check-local-changes.js new file mode 100644 index 0000000000000..03e4f475a5cdc --- /dev/null +++ b/bin/check-local-changes.js @@ -0,0 +1,55 @@ +/** + * External dependencies + */ +const SimpleGit = require( 'simple-git' ); + +SimpleGit() + .diff( [ '-U0' ] ) + .then( ( diff ) => { + // 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 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 nonOptionalDiff = diff + // 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(?!@@)/, + '' + ); + + if ( !! nonOptionalDiff ) { + console.error( + "There are local uncommitted changes after one or both of 'npm install' or 'npm run docs:build'!\n" + ); + console.log( nonOptionalDiff ); + process.exitCode = 1; + } + } ) + .catch( ( error ) => { + console.error( + 'Checking local changes failed!\n\n' + error.toString() + '\n' + ); + process.exitCode = 1; + } ); diff --git a/bin/process-git-diff.js b/bin/process-git-diff.js deleted file mode 100644 index cce2b0cffb344..0000000000000 --- a/bin/process-git-diff.js +++ /dev/null @@ -1,36 +0,0 @@ -// 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 cfd1686305617..7e000a6d4581d 100755 --- a/package.json +++ b/package.json @@ -252,7 +252,7 @@ "changelog": "node ./bin/plugin/cli.js changelog", "check-licenses": "concurrently \"wp-scripts check-licenses --prod --gpl2 --ignore=@react-native-community/cli,@react-native-community/cli-platform-ios\" \"wp-scripts check-licenses --dev\"", "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'!\" && git diff --exit-code && exit 1 );", + "check-local-changes": "node ./bin/check-local-changes.js", "dev": "npm run build:packages && concurrently \"wp-scripts start\" \"npm run dev:packages\"", "dev:packages": "concurrently \"node ./bin/packages/watch.js\" \"tsc --build --watch\"", "distclean": "rimraf node_modules packages/*/node_modules",