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 <mathias@qiwi.be>
This commit is contained in:
Jack Franklin 2020-04-28 13:58:42 +01:00 committed by GitHub
parent 06d62c0165
commit 3bf9bd199d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 6 deletions

View File

@ -43,6 +43,7 @@ jobs:
env: env:
- CHROMIUM=true - CHROMIUM=true
script: script:
- npm run compare-protocol-d-ts
- npm run test-install - npm run test-install
- npm run lint - npm run lint
- npm run test-doclint - npm run test-doclint

View File

@ -28,7 +28,8 @@
"tsc": "tsc --version && tsc -p . && cp src/protocol.d.ts lib/ && cp src/externs.d.ts lib/", "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", "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/", "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" "test-install": "scripts/test-install.sh"
}, },
"files": [ "files": [

View File

@ -1,7 +1,8 @@
// @ts-check // @ts-check
const path = require('path'); const path = require('path');
const puppeteer = require('../..'); const puppeteer = require('../..');
module.exports = puppeteer.launch({
const fetchAndGenerateProtocolDefinitions = () => puppeteer.launch({
pipe: false, pipe: false,
executablePath: process.env.BINARY, executablePath: process.env.BINARY,
}).then(async browser => { }).then(async browser => {
@ -72,12 +73,47 @@ declare global {
export default Protocol; export default Protocol;
`; `;
const outputPath = path.join(__dirname, '..', '..', 'src', 'protocol.d.ts');
require('fs').writeFileSync(outputPath, output); return {output, version};
console.log(`Wrote protocol.d.ts for ${version} to ${path.relative(process.cwd(), outputPath)}`);
console.log(`You should commit the changes.`);
}); });
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 * @typedef {Object} Property
* @property {string=} $ref * @property {string=} $ref