Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enforce src/protocol.d.ts is in sync #5762

Merged
merged 2 commits into from Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -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": [
Expand Down
46 changes: 41 additions & 5 deletions 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 => {
Expand Down Expand Up @@ -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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Readability:

Suggested change
const protocolOutputPath = path.join(__dirname, '..', '..', 'src', 'protocol.d.ts');
const protocolOutputPath = path.join(__dirname, '../../src/protocol.d.ts');

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather leave this as is so it works should anyone run this on Windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path.join normalizes, so it would still work on Windows. (I would not have suggested this change otherwise.)

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.');
jackfranklin marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down