Skip to content

Commit

Permalink
Tooling: Ignore optional changes to lockfile
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Oct 12, 2018
1 parent 22c85e5 commit fa44d86
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 33 additions & 0 deletions bin/process-git-diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// npm will introduce changes to a `package-lock.json` file for optional
// dependencies varying on environment. Disregard from a git diff result when
// only changes are addition of "optional" flag in `package-lock.json` file.
//
// 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 );
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"check-engines": "check-node-version --package",
"check-licenses": "concurrently \"wp-scripts check-licenses --prod --gpl2\" \"wp-scripts check-licenses --dev\"",
"precheck-local-changes": "npm run docs:build",
"check-local-changes": "git diff --exit-code > /dev/null || ( echo \"There are local uncommitted changes after one or both of 'npm install' or 'npm run docs:build'!\" && exit 1 );",
"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\"",
Expand Down

0 comments on commit fa44d86

Please sign in to comment.