From 391d1abaa7743302cf36135ecaa82139a672c057 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Fri, 8 Dec 2017 15:14:28 -0800 Subject: [PATCH] chore: generalize node6 transpilation (#1560) This patch unifies node6 transpilation: - instead of generating multiple top-level directories, prefixed with `node6-`, all transpiled code gets placed under single `node6/` folder - transpilation doesn't change require paths of transpiled modules any more --- .gitignore | 2 -- .npmignore | 7 ++++++- index.js | 2 +- lib/Downloader.js | 3 ++- package.json | 2 +- test/test.js | 19 ++++++++++------- utils/node6-transform/index.js | 37 ++++++++++++++++++++++------------ 7 files changed, 46 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index d3a1eb9707a..0c166745492 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,3 @@ .vscode package-lock.json /node6 -/node6-test -/node6-testrunner diff --git a/.npmignore b/.npmignore index 801ec108a36..e8eface14c6 100644 --- a/.npmignore +++ b/.npmignore @@ -11,4 +11,9 @@ node_modules *.pyc .vscode package-lock.json -/node6-test +/node6/test +/node6/utils +/test +/utils +/docs +yarn.lock diff --git a/index.js b/index.js index 5c98c26e4dd..506dcb2b08d 100644 --- a/index.js +++ b/index.js @@ -25,4 +25,4 @@ try { if (asyncawait) module.exports = require('./lib/Puppeteer'); else - module.exports = require('./node6/Puppeteer'); + module.exports = require('./node6/lib/Puppeteer'); diff --git a/lib/Downloader.js b/lib/Downloader.js index 1c927adf6ac..cb1a3d34c25 100644 --- a/lib/Downloader.js +++ b/lib/Downloader.js @@ -34,7 +34,8 @@ const downloadURLs = { win64: '%s/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip', }; -const PROJECT_ROOT = path.join(__dirname, '..'); +// Project root will be different for node6-transpiled code. +const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..'); class Downloader { /** diff --git a/package.json b/package.json index a1eec7d917a..afc00f57f30 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "coverage": "cross-env COVERAGE=true npm run unit", "test-node6-transformer": "node utils/node6-transform/test/test.js", "build": "node utils/node6-transform/index.js", - "unit-node6": "node node6-test/test.js", + "unit-node6": "node node6/test/test.js", "tsc": "tsc -p ." }, "author": "The Chromium Authors", diff --git a/test/test.js b/test/test.js index 7b687106faa..b9be433bc35 100644 --- a/test/test.js +++ b/test/test.js @@ -19,8 +19,12 @@ const path = require('path'); const {helper} = require('../lib/helper'); if (process.env.COVERAGE) helper.recordPublicAPICoverage(); -console.log('Testing on Node', process.version); -const puppeteer = require('..'); + +const PROJECT_ROOT = fs.existsSync(path.join(__dirname, '..', 'package.json')) ? path.join(__dirname, '..') : path.join(__dirname, '..', '..'); + +const puppeteer = require(PROJECT_ROOT); +const DeviceDescriptors = require(path.join(PROJECT_ROOT, 'DeviceDescriptors')); + const SimpleServer = require('./server/SimpleServer'); const GoldenUtils = require('./golden-utils'); @@ -40,8 +44,12 @@ const HTTPS_PREFIX = 'https://localhost:' + HTTPS_PORT; const headless = (process.env.HEADLESS || 'true').trim().toLowerCase() === 'true'; const slowMo = parseInt((process.env.SLOW_MO || '0').trim(), 10); const executablePath = process.env.CHROME; + +console.log('Testing on Node', process.version); if (executablePath) console.warn(`${YELLOW_COLOR}WARN: running tests with ${executablePath}${RESET_COLOR}`); +// Make sure the `npm install` was run after the chromium roll. +console.assert(fs.existsSync(puppeteer.executablePath()), `Chromium is not Downloaded. Run 'npm install' and try to re-run tests`); const defaultBrowserOptions = { executablePath, @@ -50,9 +58,6 @@ const defaultBrowserOptions = { args: ['--no-sandbox'] }; -// Make sure the `npm install` was run after the chromium roll. -console.assert(fs.existsSync(puppeteer.executablePath()), `Chromium is not Downloaded. Run 'npm install' and try to re-run tests`); - const timeout = process.env.DEBUG_TEST || slowMo ? 0 : 10 * 1000; const {TestRunner, Reporter, Matchers} = require('../utils/testrunner/'); @@ -228,8 +233,8 @@ describe('Puppeteer', function() { }); describe('Page', function() { - const iPhone = require('../DeviceDescriptors')['iPhone 6']; - const iPhoneLandscape = require('../DeviceDescriptors')['iPhone 6 landscape']; + const iPhone = DeviceDescriptors['iPhone 6']; + const iPhoneLandscape = DeviceDescriptors['iPhone 6 landscape']; let browser; let page; diff --git a/utils/node6-transform/index.js b/utils/node6-transform/index.js index eaf4f9af1c2..22bc3f07e3d 100644 --- a/utils/node6-transform/index.js +++ b/utils/node6-transform/index.js @@ -19,9 +19,17 @@ const path = require('path'); const removeRecursive = require('rimraf').sync; const transformAsyncFunctions = require('./TransformAsyncFunctions'); -copyFolder(path.join(__dirname, '..', '..', 'lib'), path.join(__dirname, '..', '..', 'node6')); -copyFolder(path.join(__dirname, '..', '..', 'test'), path.join(__dirname, '..', '..', 'node6-test')); -copyFolder(path.join(__dirname, '..', '..', 'utils', 'testrunner'), path.join(__dirname, '..', '..', 'node6-testrunner')); +const root = path.join(__dirname, '..', '..'); +const dest = path.join(__dirname, '..', '..', 'node6'); + +if (fs.existsSync(dest)) + removeRecursive(dest); +fs.mkdirSync(dest); +fs.mkdirSync(path.join(dest, 'utils')); + +copyFolder(path.join(root, 'lib'), path.join(dest, 'lib')); +copyFolder(path.join(root, 'test'), path.join(dest, 'test')); +copyFolder(path.join(root, 'utils'), path.join(dest, 'utils')); function copyFolder(source, target) { if (fs.existsSync(target)) @@ -31,16 +39,19 @@ function copyFolder(source, target) { fs.readdirSync(source).forEach(file => { const from = path.join(source, file); const to = path.join(target, file); - if (fs.lstatSync(from).isDirectory()) { + if (fs.lstatSync(from).isDirectory()) copyFolder(from, to); - } else { - let text = fs.readFileSync(from); - if (file.endsWith('.js')) { - text = transformAsyncFunctions(text.toString()); - text = text.replace(/require\('\.\.\/lib\//g, `require('../node6/`); - text = text.replace(/require\('\.\.\/utils\/testrunner\//g, `require('../node6-testrunner/`); - } - fs.writeFileSync(to, text); - } + else + copyFile(from, to); }); } + +function copyFile(from, to) { + let text = fs.readFileSync(from); + if (from.endsWith('.js')) { + text = text.toString(); + const prefix = text.startsWith('#!') ? text.substring(0, text.indexOf('\n')) : ''; + text = prefix + transformAsyncFunctions(text.substring(prefix.length)); + } + fs.writeFileSync(to, text); +}