From c9a843baa0f5112d6dbe0754f7505d7736c3c6fb Mon Sep 17 00:00:00 2001 From: Joel Einbinder Date: Tue, 29 May 2018 15:23:32 -0700 Subject: [PATCH] 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. --- install.js | 35 ++++++++++++++++++------- package.json | 2 +- utils/protocol-types-generator/index.js | 8 ++++-- 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/install.js b/install.js index a4da083fdc0..3870985dcf2 100644 --- a/install.js +++ b/install.js @@ -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'); +} diff --git a/package.json b/package.json index af7b44fd95a..6c2be60dd2e 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/utils/protocol-types-generator/index.js b/utils/protocol-types-generator/index.js index 94e73c67202..3923cc24a85 100644 --- a/utils/protocol-types-generator/index.js +++ b/utils/protocol-types-generator/index.js @@ -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)}`); });