From 61a853dcc6c893f06a143f9823d50bab51314ca6 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Wed, 30 Jun 2021 17:25:20 +0300 Subject: [PATCH] ci: generate diff for NPM package --- .github/workflows/ci.yml | 24 ++++++++ .gitignore | 1 + .prettierignore | 1 + cspell.yml | 1 + resources/diff-npm-package.js | 104 ++++++++++++++++++++++++++++++++++ 5 files changed, 131 insertions(+) create mode 100644 resources/diff-npm-package.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd3f3c1232..e7f32481c0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -209,6 +209,30 @@ jobs: - name: Run Benchmark run: 'npm run benchmark -- --revs HEAD HEAD~1' + diff-npm-package: + name: Diff content of NPM package + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v2 + with: + fetch-depth: 2 + + - name: Setup Node.js + uses: actions/setup-node@v2 + with: + node-version: ${{ env.NODE_VERSION_USED_FOR_DEVELOPMENT }} + + - name: Generate report + run: 'node resources/diff-npm-package.js ${{ github.event.pull_request.base.sha }} HEAD' + + - name: Upload generated report + uses: actions/upload-artifact@v2 + with: + name: npm-dist-diff.html + path: ./npm-dist-diff.html + if-no-files-found: ignore + deploy-to-npm-branch: name: Deploy to `npm` branch runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 9e07128cb8..28b46e673b 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ # https://help.github.com/articles/ignoring-files/#create-a-global-gitignore # https://www.gitignore.io/ +/diff-npm-package.html /.eslintcache /node_modules /coverage diff --git a/.prettierignore b/.prettierignore index 475f5e22fd..384c2585fb 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,4 +1,5 @@ # Copied from '.gitignore', please keep it in sync. +/diff-npm-package.html /.eslintcache /node_modules /coverage diff --git a/cspell.yml b/cspell.yml index 36a6cf6e0c..c244033cf7 100644 --- a/cspell.yml +++ b/cspell.yml @@ -1,6 +1,7 @@ language: en ignorePaths: # Copied from '.gitignore', please keep it in sync. + - diff-npm-package.html - .eslintcache - node_modules - coverage diff --git a/resources/diff-npm-package.js b/resources/diff-npm-package.js new file mode 100644 index 0000000000..98df6f3ed2 --- /dev/null +++ b/resources/diff-npm-package.js @@ -0,0 +1,104 @@ +'use strict'; + +const os = require('os'); +const fs = require('fs'); +const path = require('path'); +const cp = require('child_process'); + +const LOCAL = 'local'; +const localRepoDir = path.join(__dirname, '..'); +const tmpDir = path.join(os.tmpdir(), 'graphql-js-npm-diff'); +fs.rmSync(tmpDir, { recursive: true, force: true }); +fs.mkdirSync(tmpDir); + +const args = process.argv.slice(2); +let [fromRevision, toRevision] = args; +if (args.length < 2) { + fromRevision = fromRevision ?? 'HEAD'; + toRevision = toRevision ?? LOCAL; + console.warn( + `Assuming you meant: diff-npm-package ${fromRevision} ${toRevision}`, + ); +} + +console.log(`📦 Building NPM package for ${fromRevision}...`); +const fromPackage = prepareNPMPackage(fromRevision); + +console.log(`📦 Building NPM package for ${toRevision}...`); +const toPackage = prepareNPMPackage(toRevision); + +console.log('➖➕ Generating diff...'); +const diff = exec(`npm diff --diff=${fromPackage} --diff=${toPackage}`); + +if (diff === '') { + console.log('No changes found!'); +} else { + const reportPath = path.join(localRepoDir, 'npm-dist-diff.html'); + fs.writeFileSync(reportPath, generateReport(diff), 'utf-8'); + console.log('Report saved to: ', reportPath); +} + +function generateReport(diffString) { + return ` + + + + + + + + + + + +
+ + + `; +} +function prepareNPMPackage(revision) { + if (revision === LOCAL) { + exec('npm --quiet run build:npm', { cwd: localRepoDir }); + return path.join(localRepoDir, 'npmDist'); + } + + // Returns the complete git hash for a given git revision reference. + const hash = exec(`git rev-parse "${revision}"`); + + const repoDir = path.join(tmpDir, hash); + fs.rmSync(repoDir, { recursive: true, force: true }); + fs.mkdirSync(repoDir); + exec(`git archive "${hash}" | tar -xC "${repoDir}"`); + exec('npm --quiet ci', { cwd: repoDir }); + exec('npm --quiet run build:npm', { cwd: repoDir }); + return path.join(repoDir, 'npmDist'); +} + +function exec(command, options = {}) { + const result = cp.execSync(command, { + encoding: 'utf-8', + stdio: ['inherit', 'pipe', 'inherit'], + ...options, + }); + return result?.trimEnd(); +}