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: generalize node6 transpilation #1560

Merged
merged 3 commits into from
Dec 8, 2017
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
2 changes: 0 additions & 2 deletions .gitignore
Expand Up @@ -9,5 +9,3 @@
.vscode
package-lock.json
/node6
/node6-test
/node6-testrunner
7 changes: 6 additions & 1 deletion .npmignore
Expand Up @@ -11,4 +11,9 @@ node_modules
*.pyc
.vscode
package-lock.json
/node6-test
/node6/test
/node6/utils
/test
/utils
/docs
yarn.lock
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -25,4 +25,4 @@ try {
if (asyncawait)
module.exports = require('./lib/Puppeteer');
else
module.exports = require('./node6/Puppeteer');
module.exports = require('./node6/lib/Puppeteer');
3 changes: 2 additions & 1 deletion lib/Downloader.js
Expand Up @@ -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 {
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
19 changes: 12 additions & 7 deletions test/test.js
Expand Up @@ -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');

Expand All @@ -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,
Expand All @@ -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/');
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 23 additions & 13 deletions utils/node6-transform/index.js
Expand Up @@ -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))
Expand All @@ -31,16 +39,18 @@ 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, 'utf8');
if (from.endsWith('.js')) {
const prefix = text.startsWith('#!') ? text.substring(0, text.indexOf('\n')) : '';
text = prefix + transformAsyncFunctions(text.substring(prefix.length))
}
fs.writeFileSync(to, text);
}