From 3bf9bd199dee90b166982972e291e4787aac58fa Mon Sep 17 00:00:00 2001 From: Jack Franklin Date: Tue, 28 Apr 2020 13:58:42 +0100 Subject: [PATCH] chore: enforce src/protocol.d.ts is in sync (#5762) * chore: enforce src/protocol.d.ts is in sync On CI we run `npm run compare-protocol-d-ts` which checks that the file on disk is up to date with the protocol we fetch from the browser. Co-authored-by: Mathias Bynens --- .travis.yml | 1 + package.json | 3 +- utils/protocol-types-generator/index.js | 46 ++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index a64306bf..95a685a5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,6 +43,7 @@ jobs: env: - CHROMIUM=true script: + - npm run compare-protocol-d-ts - npm run test-install - npm run lint - npm run test-doclint diff --git a/package.json b/package.json index 4e756347..2b13a5e3 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,8 @@ "tsc": "tsc --version && tsc -p . && cp src/protocol.d.ts lib/ && cp src/externs.d.ts lib/", "apply-next-version": "node utils/apply_next_version.js", "test-types": "node utils/doclint/generate_types && tsc --version && tsc -p utils/doclint/generate_types/test/", - "update-protocol-d-ts": "node utils/protocol-types-generator", + "update-protocol-d-ts": "node utils/protocol-types-generator update", + "compare-protocol-d-ts": "node utils/protocol-types-generator compare", "test-install": "scripts/test-install.sh" }, "files": [ diff --git a/utils/protocol-types-generator/index.js b/utils/protocol-types-generator/index.js index 89568ee7..c5e2c960 100644 --- a/utils/protocol-types-generator/index.js +++ b/utils/protocol-types-generator/index.js @@ -1,7 +1,8 @@ // @ts-check const path = require('path'); const puppeteer = require('../..'); -module.exports = puppeteer.launch({ + +const fetchAndGenerateProtocolDefinitions = () => puppeteer.launch({ pipe: false, executablePath: process.env.BINARY, }).then(async browser => { @@ -72,12 +73,47 @@ declare global { export default Protocol; `; - const outputPath = path.join(__dirname, '..', '..', 'src', 'protocol.d.ts'); - require('fs').writeFileSync(outputPath, output); - console.log(`Wrote protocol.d.ts for ${version} to ${path.relative(process.cwd(), outputPath)}`); - console.log(`You should commit the changes.`); + + return {output, version}; }); +const protocolOutputPath = path.join(__dirname, '..', '..', 'src', 'protocol.d.ts'); +const relativeProtocolOutputPath = path.relative(process.cwd(), protocolOutputPath); + +const writeOutputToDisk = ({output, version}) => { + require('fs').writeFileSync(protocolOutputPath, output); + console.log(`Wrote protocol.d.ts for ${version} to ${relativeProtocolOutputPath}`); + console.log(`You should commit the changes.`); +}; + +const cli = async() => { + const scriptToRun = process.argv[2]; + + if (scriptToRun === 'update') { + writeOutputToDisk(await fetchAndGenerateProtocolDefinitions()); + } else if (scriptToRun === 'compare') { + const {output} = await fetchAndGenerateProtocolDefinitions(); + const outputOnDisk = require('fs').readFileSync(protocolOutputPath, {encoding: 'utf8'}); + if (output === outputOnDisk) { + console.log(`Success: ${relativeProtocolOutputPath} is up to date.`); + } else { + console.log(`Error: ${relativeProtocolOutputPath} is out of date.`); + console.log('You should run `npm run update-protocol-d-ts` and commit the changes.'); + process.exit(1); + } + + } else { + console.log(`Unknown protocol script ${scriptToRun}.`); + console.log(`Valid scripts are: + - update: fetch and update ${relativeProtocolOutputPath} + - compare: check ${relativeProtocolOutputPath} is up to date with the latest CDP. + `); + process.exit(1); + } +}; + +cli(); + /** * @typedef {Object} Property * @property {string=} $ref