Skip to content

Commit

Permalink
Testing; refactor check-local-changes script (#41074)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano authored and youknowriad committed Jun 3, 2022
1 parent 519d4dd commit e343c2e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
55 changes: 55 additions & 0 deletions bin/check-local-changes.js
Original file line number Diff line number Diff line change
@@ -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;
} );
36 changes: 0 additions & 36 deletions bin/process-git-diff.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e343c2e

Please sign in to comment.