From 79ceb0c439a979a702960835ec999a2fe53c4599 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 13 Jun 2017 21:09:28 -0700 Subject: [PATCH] Consolidate phantom shim code in the phantom-shim folder This patch: - renames phantomjs folder into phantom_shim - moves bin/puppeteer into a phantom_shim/runner.js and merges the file with phantomjs/index.js - removes "bin" field from the package.json - it is confusing to have phantom shim installable --- package.json | 3 - {phantomjs => phantom_shim}/FileSystem.js | 0 {phantomjs => phantom_shim}/Phantom.js | 0 {phantomjs => phantom_shim}/System.js | 0 {phantomjs => phantom_shim}/WebPage.js | 0 {phantomjs => phantom_shim}/WebServer.js | 0 bin/puppeteer.js => phantom_shim/runner.js | 56 +++++++++++++++-- {phantomjs => phantom_shim}/utilities.js | 0 phantomjs/index.js | 71 ---------------------- third_party/phantomjs/test/run-tests.py | 2 +- 10 files changed, 53 insertions(+), 79 deletions(-) rename {phantomjs => phantom_shim}/FileSystem.js (100%) rename {phantomjs => phantom_shim}/Phantom.js (100%) rename {phantomjs => phantom_shim}/System.js (100%) rename {phantomjs => phantom_shim}/WebPage.js (100%) rename {phantomjs => phantom_shim}/WebServer.js (100%) rename bin/puppeteer.js => phantom_shim/runner.js (50%) rename {phantomjs => phantom_shim}/utilities.js (100%) delete mode 100644 phantomjs/index.js diff --git a/package.json b/package.json index 2ac58b59..67185285 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,6 @@ "progress": "^2.0.0", "rimraf": "^2.6.1" }, - "bin": { - "puppeteer": "./bin/puppeteer.js" - }, "puppeteer": { "chromium_revision": "478524" }, diff --git a/phantomjs/FileSystem.js b/phantom_shim/FileSystem.js similarity index 100% rename from phantomjs/FileSystem.js rename to phantom_shim/FileSystem.js diff --git a/phantomjs/Phantom.js b/phantom_shim/Phantom.js similarity index 100% rename from phantomjs/Phantom.js rename to phantom_shim/Phantom.js diff --git a/phantomjs/System.js b/phantom_shim/System.js similarity index 100% rename from phantomjs/System.js rename to phantom_shim/System.js diff --git a/phantomjs/WebPage.js b/phantom_shim/WebPage.js similarity index 100% rename from phantomjs/WebPage.js rename to phantom_shim/WebPage.js diff --git a/phantomjs/WebServer.js b/phantom_shim/WebServer.js similarity index 100% rename from phantomjs/WebServer.js rename to phantom_shim/WebServer.js diff --git a/bin/puppeteer.js b/phantom_shim/runner.js similarity index 50% rename from bin/puppeteer.js rename to phantom_shim/runner.js index 6816bd39..19842223 100755 --- a/bin/puppeteer.js +++ b/phantom_shim/runner.js @@ -16,10 +16,15 @@ */ var vm = require('vm'); -var fs = require('fs'); var path = require('path'); +var fs = require('fs'); +var Phantom = require('./Phantom'); +var FileSystem = require('./FileSystem'); +var System = require('./System'); +var WebPage = require('./WebPage'); +var WebServer = require('./WebServer'); +var child_process = require('child_process'); var Browser = require('..').Browser; -var PhatomJs = require('../phantomjs'); var version = require('../package.json').version; var argv = require('minimist')(process.argv.slice(2), { alias: { v: 'version' }, @@ -40,7 +45,7 @@ if (argv['ssl-certificates-path']) { var scriptArguments = argv._; if (!scriptArguments.length) { - console.log('puppeteer [scriptfile]'); + console.log(__filename.split('/').pop() + ' [scriptfile]'); return; } @@ -56,6 +61,49 @@ var browser = new Browser({ headless: argv.headless, }); -var context = PhatomJs.createContext(browser, scriptPath, argv); +var context = createPhantomContext(browser, scriptPath, argv); var scriptContent = fs.readFileSync(scriptPath, 'utf8'); vm.runInContext(scriptContent, context); + +/** + * @param {!Browser} browser + * @param {string} scriptPath + * @param {!Array} argv + * @return {!Object} + */ +function createPhantomContext(browser, scriptPath, argv) { + var context = {}; + context.setInterval = setInterval; + context.setTimeout = setTimeout; + context.clearInterval = clearInterval; + context.clearTimeout = clearTimeout; + + context.phantom = Phantom.create(context, scriptPath); + context.console = console; + context.window = context; + context.WebPage = options => new WebPage(browser, scriptPath, options); + + vm.createContext(context); + + var nativeExports = { + fs: new FileSystem(), + system: new System(argv._), + webpage: { + create: context.WebPage, + }, + webserver: { + create: () => new WebServer(), + }, + cookiejar: { + create: () => {}, + }, + child_process: child_process + }; + var bootstrapPath = path.join(__dirname, '..', 'third_party', 'phantomjs', 'bootstrap.js'); + var bootstrapCode = fs.readFileSync(bootstrapPath, 'utf8'); + vm.runInContext(bootstrapCode, context, { + filename: 'bootstrap.js' + })(nativeExports); + return context; +} + diff --git a/phantomjs/utilities.js b/phantom_shim/utilities.js similarity index 100% rename from phantomjs/utilities.js rename to phantom_shim/utilities.js diff --git a/phantomjs/index.js b/phantomjs/index.js deleted file mode 100644 index e563ce37..00000000 --- a/phantomjs/index.js +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright 2017 Google Inc. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -var vm = require('vm'); -var path = require('path'); -var fs = require('fs'); -var Phantom = require('./Phantom'); -var FileSystem = require('./FileSystem'); -var System = require('./System'); -var WebPage = require('./WebPage'); -var WebServer = require('./WebServer'); -var child_process = require('child_process'); - -var bootstrapPath = path.join(__dirname, '..', 'third_party', 'phantomjs', 'bootstrap.js'); -var bootstrapCode = fs.readFileSync(bootstrapPath, 'utf8'); - -module.exports = { - /** - * @param {!Browser} browser - * @param {string} scriptPath - * @param {!Array} argv - * @return {!Object} - */ - createContext(browser, scriptPath, argv) { - var context = {}; - context.setInterval = setInterval; - context.setTimeout = setTimeout; - context.clearInterval = clearInterval; - context.clearTimeout = clearTimeout; - - context.phantom = Phantom.create(context, scriptPath); - context.console = console; - context.window = context; - context.WebPage = options => new WebPage(browser, scriptPath, options); - - vm.createContext(context); - - var nativeExports = { - fs: new FileSystem(), - system: new System(argv._), - webpage: { - create: context.WebPage, - }, - webserver: { - create: () => new WebServer(), - }, - cookiejar: { - create: () => {}, - }, - child_process: child_process - }; - vm.runInContext(bootstrapCode, context, { - filename: 'bootstrap.js' - })(nativeExports); - return context; - } -}; - diff --git a/third_party/phantomjs/test/run-tests.py b/third_party/phantomjs/test/run-tests.py index 99e375a8..e535c51f 100755 --- a/third_party/phantomjs/test/run-tests.py +++ b/third_party/phantomjs/test/run-tests.py @@ -977,7 +977,7 @@ class TestRunner(object): def init(): base_path = os.path.normpath(os.path.dirname(os.path.abspath(__file__))) - phantomjs_exe = os.path.normpath(base_path + '/../../../bin/puppeteer.js') + phantomjs_exe = os.path.normpath(base_path + '/../../../phantom_shim/runner.js') if not os.path.isfile(phantomjs_exe): sys.stdout.write("{} is unavailable, cannot run tests.\n" .format(phantomjs_exe))