Skip to content

Commit

Permalink
Allow opn & update-notifier CLI deps to be optional. (#2150)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed May 4, 2017
1 parent 8652478 commit b2ccdfc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lighthouse-cli/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import * as Printer from './printer';
import * as randomPort from './random-port';
import {Results} from './types/types';
const yargs = require('yargs');
const opn = require('opn');
const updateNotifier = require('update-notifier');
const pkg = require('../package.json');

// accept noop modules for these, so the real dependency is optional.
import {opn, updateNotifier} from './shim-modules';

updateNotifier({pkg}).notify(); // Tell user if there's a newer version of LH.

interface LighthouseError extends Error {
Expand Down
3 changes: 2 additions & 1 deletion lighthouse-cli/performance-experiment/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@

const http = require('http');
const parse = require('url').parse;
const opn = require('opn');

const log = require('../../lighthouse-core/lib/log');
const lighthouse = require('../../lighthouse-core');
const ExperimentDatabase = require('./experiment-database/database');
const PerfXReportGenerator = require('./report/perf-x-report-generator');
const opn = require('../shim-modules').opn;

let database;
let fallbackReportId;
Expand Down
36 changes: 36 additions & 0 deletions lighthouse-cli/shim-modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license 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.
*/
'use strict';

/** @fileoverview Some users may be unable to install the full dependency tree,
* especially for the CLI. `opn` and `update-notifier` in particular have some
* uncommon transitive dependencies, so these shims will let the modules no-op
* if the real dependency is not installed.
*/

let opn;
try {
opn = require('opn');
} catch (e) {
opn = function shimOpn() {
console.error('module `opn` not installed. Not opening browser.');
};
}

let updateNotifier;
try {
updateNotifier = require('update-notifier');
} catch (e) {
updateNotifier = function shimUpdateNotifier() {
console.error('module `update-notifier` not installed. Not checking for new version.');
return {notify: () => false};
};
}

export {
opn,
updateNotifier
};

0 comments on commit b2ccdfc

Please sign in to comment.