chore(types): generate protocol.d.ts on install (#2625)

Previously protocol.d.ts was generated on `npm run tsc`. This was inconvenient because it meant that vscode checking was wrong until type checking was run manually, and was inefficient because it necessarily regenerated the types even if no new Chromium was downloaded. This patch generates the types when npm install is run from the github checkout, assuming a new Chromium revision was downloaded.
This commit is contained in:
Joel Einbinder 2018-05-29 15:23:32 -07:00 committed by Andrey Lushnikov
parent 7d64d40d66
commit c9a843baa0
3 changed files with 33 additions and 12 deletions

View File

@ -36,8 +36,10 @@ const revision = process.env.PUPPETEER_CHROMIUM_REVISION || process.env.npm_conf
const revisionInfo = browserFetcher.revisionInfo(revision);
// Do nothing if the revision is already downloaded.
if (revisionInfo.local)
if (revisionInfo.local) {
generateProtocolTypesIfNecessary(false /* updated */);
return;
}
// Override current environment proxy settings with npm configuration, if any.
const NPM_HTTPS_PROXY = process.env.npm_config_https_proxy || process.env.npm_config_proxy;
@ -65,7 +67,7 @@ function onSuccess(localRevisions) {
localRevisions = localRevisions.filter(revision => revision !== revisionInfo.revision);
// Remove previous chromium revisions.
const cleanupOldVersions = localRevisions.map(revision => browserFetcher.remove(revision));
return Promise.all(cleanupOldVersions);
return Promise.all([...cleanupOldVersions, generateProtocolTypesIfNecessary(true /* updated */)]);
}
/**
@ -107,16 +109,31 @@ function buildNode6IfNecessary() {
// folder.
if (!fs.existsSync(path.join('utils', 'node6-transform')))
return;
let asyncawait = true;
try {
new Function('async function test(){await 1}');
} catch (error) {
asyncawait = false;
}
// if async/await is supported, then node6 is not needed.
if (asyncawait)
if (supportsAsyncAwait())
return;
// Re-build node6/ folder.
console.log('Building Puppeteer for Node 6');
require(path.join(__dirname, 'utils', 'node6-transform'));
}
function supportsAsyncAwait() {
try {
new Function('async function test(){await 1}');
} catch (error) {
return false;
}
return true;
}
function generateProtocolTypesIfNecessary(updated) {
if (!supportsAsyncAwait())
return;
const fs = require('fs');
const path = require('path');
if (!fs.existsSync(path.join(__dirname, 'utils', 'protocol-types-generator')))
return;
if (!updated && fs.existsSync(path.join(__dirname, 'lib', 'protocol.d.ts')))
return;
return require('./utils/protocol-types-generator');
}

View File

@ -19,7 +19,7 @@
"test-node6-transformer": "node utils/node6-transform/test/test.js",
"build": "node utils/node6-transform/index.js",
"unit-node6": "node node6/test/test.js",
"tsc": "node utils/protocol-types-generator && tsc -p .",
"tsc": "tsc -p .",
"prepublishOnly": "npm run build",
"apply-next-version": "node utils/apply_next_version.js"
},

View File

@ -1,6 +1,7 @@
// @ts-check
const path = require('path');
const puppeteer = require('../..');
puppeteer.launch({
module.exports = puppeteer.launch({
pipe: false,
executablePath: process.env.CHROME,
args: ['--no-sandbox', '--disable-dev-shm-usage']
@ -9,6 +10,7 @@ puppeteer.launch({
const page = await browser.newPage();
await page.goto(`http://${origin}/json/protocol`);
const json = JSON.parse(await page.evaluate(() => document.documentElement.innerText));
const version = await browser.version();
await browser.close();
const output = `// This is generated from /utils/protocol-types-generator/index.js
declare global {
@ -70,7 +72,9 @@ declare global {
// empty export to keep file a module
export {}
`;
require('fs').writeFileSync(require('path').join(__dirname, '..', '..', 'lib', 'protocol.d.ts'), output);
const outputPath = path.join(__dirname, '..', '..', 'lib', 'protocol.d.ts');
require('fs').writeFileSync(outputPath, output);
console.log(`Wrote protocol.d.ts for ${version} to ${path.relative(process.cwd(), outputPath)}`);
});