Skip to content

Commit

Permalink
cli: convert to ES modules (#13045)
Browse files Browse the repository at this point in the history
  • Loading branch information
connorjclark authored Sep 14, 2021
1 parent 8eab19a commit 0e89735
Show file tree
Hide file tree
Showing 23 changed files with 665 additions and 542 deletions.
45 changes: 37 additions & 8 deletions build/build-smokehouse-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,47 @@
*/
'use strict';

const browserify = require('browserify');
const fs = require('fs');
const rollup = require('rollup');

/**
* Rollup plugins don't export types that work with commonjs.
* @template T
* @param {T} module
* @return {T['default']}
*/
function rollupPluginTypeCoerce(module) {
// @ts-expect-error
return module;
}

const nodeResolve = rollupPluginTypeCoerce(require('rollup-plugin-node-resolve'));
const commonjs = rollupPluginTypeCoerce(require('rollup-plugin-commonjs'));
// @ts-expect-error: no types
const shim = require('rollup-plugin-shim');
const {LH_ROOT} = require('../root.js');

const distDir = `${LH_ROOT}/dist`;
const bundleOutFile = `${distDir}/smokehouse-bundle.js`;
const smokehouseLibFilename = './lighthouse-cli/test/smokehouse/frontends/lib.js';
const smokehouseCliFilename =
require.resolve('../lighthouse-cli/test/smokehouse/lighthouse-runners/cli.js');

browserify(smokehouseLibFilename, {standalone: 'Lighthouse.Smokehouse'})
.ignore('./lighthouse-cli/test/smokehouse/lighthouse-runners/cli.js')
.transform('@wardpeet/brfs', {global: true, parserOpts: {ecmaVersion: 12}})
.bundle((err, src) => {
if (err) throw err;
fs.writeFileSync(bundleOutFile, src.toString());
async function build() {
const bundle = await rollup.rollup({
input: smokehouseLibFilename,
context: 'globalThis',
plugins: [
nodeResolve(),
commonjs(),
shim({
[smokehouseCliFilename]: 'export default {}',
}),
],
});

await bundle.write({
file: bundleOutFile,
});
}

build();
27 changes: 27 additions & 0 deletions lighthouse-cli/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license Copyright 2021 The Lighthouse Authors. 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.
*/
'use strict';

module.exports = {
env: {
browser: true,
},
rules: {
// TODO(esmodules): move to root eslint when all code is ESM
// or when this is resolved: https://github.com/import-js/eslint-plugin-import/issues/2214
'import/order': [2, {
'groups': [
'builtin',
'external',
['sibling', 'parent'],
'index',
'object',
'type',
],
'newlines-between': 'always',
}],
},
};
52 changes: 31 additions & 21 deletions lighthouse-cli/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@
* cli-flags lh-core/index
*/

const fs = require('fs');
const path = require('path');
const commands = require('./commands/commands.js');
const printer = require('./printer.js');
const {getFlags} = require('./cli-flags.js');
const {runLighthouse} = require('./run.js');
const {generateConfig} = require('../lighthouse-core/index.js');
const log = require('lighthouse-logger');
const pkg = require('../package.json');
const Sentry = require('../lighthouse-core/lib/sentry.js');
const updateNotifier = require('update-notifier');
const {askPermission} = require('./sentry-prompt.js');
const {LH_ROOT} = require('../root.js');
import fs from 'fs';
import path from 'path';
import url from 'url';

import log from 'lighthouse-logger';
import updateNotifier from 'update-notifier';

import * as commands from './commands/commands.js';
import * as Printer from './printer.js';
import {getFlags} from './cli-flags.js';
import {runLighthouse} from './run.js';
import lighthouse from '../lighthouse-core/index.js';
import * as Sentry from '../lighthouse-core/lib/sentry.js';
import {askPermission} from './sentry-prompt.js';
import {LH_ROOT} from '../root.js';

const pkg = JSON.parse(fs.readFileSync(LH_ROOT + '/package.json', 'utf-8'));

/**
* @return {boolean}
Expand Down Expand Up @@ -58,16 +62,22 @@ async function begin() {
commands.listTraceCategories();
}

const url = cliFlags._[0];
const urlUnderTest = cliFlags._[0];

/** @type {LH.Config.Json|undefined} */
let configJson;
if (cliFlags.configPath) {
// Resolve the config file path relative to where cli was called.
cliFlags.configPath = path.resolve(process.cwd(), cliFlags.configPath);
configJson = require(cliFlags.configPath);

if (cliFlags.configPath.endsWith('.json')) {
configJson = JSON.parse(fs.readFileSync(cliFlags.configPath, 'utf-8'));
} else {
const configModuleUrl = url.pathToFileURL(cliFlags.configPath).href;
configJson = (await import(configModuleUrl)).default;
}
} else if (cliFlags.preset) {
configJson = require(`../lighthouse-core/config/${cliFlags.preset}-config.js`);
configJson = (await import(`../lighthouse-core/config/${cliFlags.preset}-config.js`)).default;
}

if (cliFlags.budgetPath) {
Expand All @@ -88,7 +98,7 @@ async function begin() {

if (
cliFlags.output.length === 1 &&
cliFlags.output[0] === printer.OutputMode.json &&
cliFlags.output[0] === Printer.OutputMode.json &&
!cliFlags.outputPath
) {
cliFlags.outputPath = 'stdout';
Expand All @@ -106,7 +116,7 @@ async function begin() {
}

if (cliFlags.printConfig) {
const config = generateConfig(configJson, cliFlags);
const config = lighthouse.generateConfig(configJson, cliFlags);
process.stdout.write(config.getPrintString());
return;
}
Expand All @@ -119,7 +129,7 @@ async function begin() {
}
if (cliFlags.enableErrorReporting) {
Sentry.init({
url,
url: urlUnderTest,
flags: cliFlags,
environmentData: {
name: 'redacted', // prevent sentry from using hostname
Expand All @@ -132,9 +142,9 @@ async function begin() {
});
}

return runLighthouse(url, cliFlags, configJson);
return runLighthouse(urlUnderTest, cliFlags, configJson);
}

module.exports = {
export {
begin,
};
19 changes: 12 additions & 7 deletions lighthouse-cli/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@

/* eslint-disable max-len */

const yargs = require('yargs');
const fs = require('fs');
const {isObjectOfUnknownValues} = require('../lighthouse-core/lib/type-verifiers.js');
import fs from 'fs';

import yargs from 'yargs';
import * as yargsHelpers from 'yargs/helpers';

import {isObjectOfUnknownValues} from '../lighthouse-core/lib/type-verifiers.js';

/**
* @param {string=} manualArgv
* @param {{noExitOnFailure?: boolean}=} options
* @return {LH.CliFlags}
*/
function getFlags(manualArgv, options = {}) {
// @ts-expect-error - undocumented, but yargs() supports parsing a single `string`.
const y = manualArgv ? yargs(manualArgv) : yargs;
const y = manualArgv ?
// @ts-expect-error - undocumented, but yargs() supports parsing a single `string`.
yargs(manualArgv) :
yargs(yargsHelpers.hideBin(process.argv));

let parser = y.help('help')
.showHelpOnFail(false, 'Specify --help for available options')
Expand Down Expand Up @@ -318,7 +323,7 @@ function getFlags(manualArgv, options = {}) {
throw new Error('Please provide a url');
})
.epilogue('For more information on Lighthouse, see https://developers.google.com/web/tools/lighthouse/.')
.wrap(yargs.terminalWidth());
.wrap(y.terminalWidth());

if (options.noExitOnFailure) {
// Silence console.error() logging and don't process.exit().
Expand Down Expand Up @@ -499,6 +504,6 @@ function coerceScreenEmulation(value) {
return screenEmulationSettings;
}

module.exports = {
export {
getFlags,
};
9 changes: 2 additions & 7 deletions lighthouse-cli/commands/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
*/
'use strict';

const listAudits = require('./list-audits.js');
const listTraceCategories = require('./list-trace-categories.js');

module.exports = {
listAudits,
listTraceCategories,
};
export {listAudits} from './list-audits.js';
export {listTraceCategories} from './list-trace-categories.js';
4 changes: 2 additions & 2 deletions lighthouse-cli/commands/list-audits.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
'use strict';

const lighthouse = require('../../lighthouse-core/index.js');
import lighthouse from '../../lighthouse-core/index.js';

function listAudits() {
const audits = lighthouse.getAuditList().map((i) => i.replace(/\.js$/, ''));
process.stdout.write(JSON.stringify({audits}, null, 2));
process.exit(0);
}

module.exports = listAudits;
export {listAudits};
4 changes: 2 additions & 2 deletions lighthouse-cli/commands/list-trace-categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/
'use strict';

const lighthouse = require('../../lighthouse-core/index.js');
import lighthouse from '../../lighthouse-core/index.js';

function listTraceCategories() {
const traceCategories = lighthouse.traceCategories;
process.stdout.write(JSON.stringify({traceCategories}));
process.exit(0);
}

module.exports = listTraceCategories;
export {listTraceCategories};
4 changes: 3 additions & 1 deletion lighthouse-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
*/
'use strict';

require('./bin.js').begin().catch(err => {
import {begin} from './bin.js';

begin().catch(err => {
process.stderr.write(err.stack);
process.exit(1);
});
File renamed without changes.
7 changes: 4 additions & 3 deletions lighthouse-cli/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
'use strict';

const fs = require('fs');
const log = require('lighthouse-logger');
import fs from 'fs';

import log from 'lighthouse-logger';

/**
* An enumeration of acceptable output modes:
Expand Down Expand Up @@ -91,7 +92,7 @@ function getValidOutputOptions() {
return Object.keys(OutputMode);
}

module.exports = {
export {
checkOutputPath,
write,
OutputMode,
Expand Down
29 changes: 14 additions & 15 deletions lighthouse-cli/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@

/* eslint-disable no-console */

const path = require('path');
const psList = require('ps-list');
import path from 'path';

const Printer = require('./printer.js');
const ChromeLauncher = require('chrome-launcher');
import psList from 'ps-list';
import * as ChromeLauncher from 'chrome-launcher';
import yargsParser from 'yargs-parser';
import log from 'lighthouse-logger';
import open from 'open';

const yargsParser = require('yargs-parser');
const lighthouse = require('../lighthouse-core/index.js');
const log = require('lighthouse-logger');
const getFilenamePrefix = require('../report/generator/file-namer.js').getFilenamePrefix;
const assetSaver = require('../lighthouse-core/lib/asset-saver.js');
const URL = require('../lighthouse-core/lib/url-shim.js');

const open = require('open');
import * as Printer from './printer.js';
import lighthouse from '../lighthouse-core/index.js';
import {getFilenamePrefix} from '../report/generator/file-namer.js';
import * as assetSaver from '../lighthouse-core/lib/asset-saver.js';
import URL from '../lighthouse-core/lib/url-shim.js';

/** @typedef {Error & {code: string, friendlyMessage?: string}} ExitError */

Expand Down Expand Up @@ -203,8 +202,8 @@ async function potentiallyKillChrome(launchedChrome) {
* @return {Promise<LH.RunnerResult|undefined>}
*/
async function runLighthouseWithFraggleRock(url, flags, config, launchedChrome) {
const fraggleRock = require('../lighthouse-core/fraggle-rock/api.js');
const puppeteer = require('puppeteer');
const fraggleRock = (await import('../lighthouse-core/fraggle-rock/api.js')).default;
const puppeteer = (await import('puppeteer')).default;
const browser = await puppeteer.connect({browserURL: `http://localhost:${launchedChrome.port}`});
const page = await browser.newPage();
flags.channel = 'fraggle-rock-cli';
Expand Down Expand Up @@ -272,7 +271,7 @@ async function runLighthouse(url, flags, config) {
}
}

module.exports = {
export {
parseChromeFlags,
saveResults,
runLighthouse,
Expand Down
11 changes: 5 additions & 6 deletions lighthouse-cli/sentry-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
*/
'use strict';

const Configstore = require('configstore');
const {Confirm} = require('enquirer');

const log = require('lighthouse-logger');
import Configstore from 'configstore';
import Confirm from 'enquirer';
import log from 'lighthouse-logger';

const MAXIMUM_WAIT_TIME = 20 * 1000;

Expand All @@ -29,7 +28,7 @@ function prompt() {
/** @type {NodeJS.Timer|undefined} */
let timeout;

const prompt = new Confirm({
const prompt = new Confirm.Confirm({
name: 'isErrorReportingEnabled',
initial: false,
message: MESSAGE,
Expand Down Expand Up @@ -75,6 +74,6 @@ function askPermission() {
}).catch(_ => false);
}

module.exports = {
export {
askPermission,
};
Loading

0 comments on commit 0e89735

Please sign in to comment.