Skip to content

Commit

Permalink
feat(cli): add update notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Nov 8, 2019
1 parent 2dc0f90 commit 7bcd7f2
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 99 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/sequelize": "^4.27.34",
"@types/tmp": "^0.1.0",
"@types/umzug": "^2.2.2",
"@types/update-notifier": "^2.5.0",
"@types/uuid": "^3.4.4",
"@types/yargs": "^12.0.8",
"babel-eslint": "^10.0.2",
Expand Down
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"lighthouse": "5.5.0",
"open": "^6.4.0",
"tmp": "^0.1.0",
"update-notifier": "^3.0.1",
"yargs": "^12.0.5"
},
"peerDependencies": {
Expand Down
7 changes: 5 additions & 2 deletions packages/cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
'use strict';

const yargs = require('yargs');
const updateNotifier = require('update-notifier');
const loadAndParseRcFile = require('@lhci/utils/src/lighthouserc.js').loadAndParseRcFile;
const getVersion = require('@lhci/utils/src/version.js').getVersion;
const assertCmd = require('./assert/assert.js');
const autorunCmd = require('./autorun/autorun.js');
const healthcheckCmd = require('./healthcheck/healthcheck.js');
Expand All @@ -17,12 +17,15 @@ const collectCmd = require('./collect/collect.js');
const serverCmd = require('./server/server.js');
const wizardCmd = require('./wizard/wizard.js');
const openCmd = require('./open/open.js');
const pkg = require('../package.json');

updateNotifier({pkg}).notify({defer: false});

async function run() {
/** @type {any} */
const argv = yargs
.help('help')
.version(getVersion())
.version(pkg.version)
.usage('lhci <command> <options>')
.env('LHCI')
.config('rc-file', loadAndParseRcFile)
Expand Down
96 changes: 18 additions & 78 deletions packages/cli/test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@

const fs = require('fs');
const path = require('path');
const {spawn, spawnSync} = require('child_process');
const {spawn} = require('child_process');
const fetch = require('isomorphic-fetch');
const log = require('lighthouse-logger');
const puppeteer = require('puppeteer');
const {startServer, waitForCondition, CLI_PATH} = require('./test-utils.js');
const {startServer, waitForCondition, CLI_PATH, runCLI} = require('./test-utils.js');

describe('Lighthouse CI CLI', () => {
const rcFile = path.join(__dirname, 'fixtures/lighthouserc.json');
const rcExtendedFile = path.join(__dirname, 'fixtures/lighthouserc-extended.json');
const budgetsFile = path.join(__dirname, 'fixtures/budgets.json');
const buildDir = path.join(__dirname, 'fixtures');
const cleanEnv = {...process.env, LHCI_GITHUB_TOKEN: '', LHCI_GITHUB_APP_TOKEN: ''};

let server;
let projectToken;
Expand Down Expand Up @@ -91,15 +90,9 @@ describe('Lighthouse CI CLI', () => {
it('should pass when things are good', async () => {
const LHCI_TOKEN = projectToken;
const LHCI_SERVER_BASE_URL = `http://localhost:${server.port}`;
let {stdout = '', stderr = '', status = -1} = spawnSync(
CLI_PATH,
['healthcheck', `--fatal`],
{env: {...cleanEnv, LHCI_TOKEN, LHCI_SERVER_BASE_URL}}
);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;
const {stdout, stderr, status} = runCLI(['healthcheck', `--fatal`], {
env: {LHCI_TOKEN, LHCI_SERVER_BASE_URL},
});

expect(stdout).toMatchInlineSnapshot(`
"✅ .lighthouseci/ directory writable
Expand All @@ -118,16 +111,11 @@ describe('Lighthouse CI CLI', () => {
it('should fail when things are bad', async () => {
const LHCI_TOKEN = projectToken;
const LHCI_SERVER_BASE_URL = `http://localhost:${server.port}`;
let {stdout = '', stderr = '', status = -1} = spawnSync(
CLI_PATH,
const {stdout, stderr, status} = runCLI(
['healthcheck', `--rc-file=${rcFile}`, `--fatal`, '--checks=githubToken'],
{env: {...cleanEnv, LHCI_TOKEN, LHCI_SERVER_BASE_URL}}
{env: {LHCI_TOKEN, LHCI_SERVER_BASE_URL}}
);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

expect(stdout).toMatchInlineSnapshot(`
"✅ .lighthouseci/ directory writable
✅ Ancestor hash determinable
Expand All @@ -145,16 +133,12 @@ describe('Lighthouse CI CLI', () => {

describe('collect', () => {
it('should collect results from buildDir', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(CLI_PATH, [
const {stdout, stderr, status} = runCLI([
'collect',
`--rc-file=${rcFile}`,
`--build-dir=${buildDir}`,
]);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

const stdoutClean = stdout.replace(/:\d{4,6}/g, ':XXXX').replace(/port \d{4,6}/, 'port XXXX');
expect(stdoutClean).toMatchInlineSnapshot(`
"Started a web server on port XXXX...
Expand All @@ -172,16 +156,12 @@ describe('Lighthouse CI CLI', () => {
}, 90000);

it('should collect results from explicit urls', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(CLI_PATH, [
const {stdout, stderr, status} = runCLI([
'collect',
`--rc-file=${rcFile}`,
`--url=${urlToCollect}`,
]);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

const stdoutClean = stdout.replace(/:\d{4,6}/g, ':XXXX');
expect(stdoutClean).toMatchInlineSnapshot(`
"Running Lighthouse 2 time(s) on http://localhost:XXXX/app/
Expand All @@ -198,16 +178,11 @@ describe('Lighthouse CI CLI', () => {
describe('upload', () => {
let uuids;
it('should read LHRs from folders', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(
CLI_PATH,
const {stdout, stderr, status} = runCLI(
['upload', `--serverBaseUrl=http://localhost:${server.port}`],
{env: {...cleanEnv, LHCI_TOKEN: projectToken}}
{env: {LHCI_TOKEN: projectToken}}
);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

const UUID_REGEX = /[0-9a-f-]{36}/gi;
uuids = stdout.match(UUID_REGEX);
const cleansedStdout = stdout.replace(UUID_REGEX, '<UUID>').replace(/:\d+/g, '<PORT>');
Expand Down Expand Up @@ -255,15 +230,7 @@ describe('Lighthouse CI CLI', () => {
});

it('should support target=temporary-public-storage', async () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(
CLI_PATH,
['upload', `--target=temporary-public-storage`],
{env: {...cleanEnv}}
);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;
const {stdout, stderr, status} = runCLI(['upload', `--target=temporary-public-storage`]);

expect(stdout).toContain('...success!');
expect(stdout).toContain('Open the report at');
Expand All @@ -272,16 +239,11 @@ describe('Lighthouse CI CLI', () => {
});

it('should fail repeated attempts', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(
CLI_PATH,
const {stdout, stderr, status} = runCLI(
['upload', `--serverBaseUrl=http://localhost:${server.port}`],
{env: {...cleanEnv, LHCI_TOKEN: projectToken}}
{env: {LHCI_TOKEN: projectToken}}
);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

expect(stdout).toEqual('');
expect(stderr).toContain('Unexpected status code 422');
expect(stderr).toContain('Build already exists for hash');
Expand All @@ -291,14 +253,7 @@ describe('Lighthouse CI CLI', () => {

describe('assert', () => {
it('should assert failures', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(CLI_PATH, [
'assert',
`--assertions.works-offline=error`,
]);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;
const {stdout, stderr, status} = runCLI(['assert', `--assertions.works-offline=error`]);

const stderrClean = stderr.replace(/:\d{4,6}/g, ':XXXX');
expect(stdout).toMatchInlineSnapshot(`""`);
Expand All @@ -322,18 +277,14 @@ describe('Lighthouse CI CLI', () => {
});

it('should assert failures from an rcfile', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(CLI_PATH, [
const {stdout, stderr, status} = runCLI([
'assert',
`--assertions.first-contentful-paint=off`,
`--assertions.speed-index=off`,
`--assertions.interactive=off`,
`--rc-file=${rcFile}`,
]);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

const stderrClean = stderr.replace(/\d{4,8}(\.\d{1,8})?/g, 'XXXX');
expect(stdout).toMatchInlineSnapshot(`""`);
expect(stderrClean).toMatchInlineSnapshot(`
Expand All @@ -356,17 +307,13 @@ describe('Lighthouse CI CLI', () => {
});

it('should assert failures from an extended rcfile', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(CLI_PATH, [
const {stdout, stderr, status} = runCLI([
'assert',
`--assertions.speed-index=off`,
`--assertions.interactive=off`,
`--rc-file=${rcExtendedFile}`,
]);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

const stderrClean = stderr.replace(/\d{4,}(\.\d{1,})?/g, 'XXXX');
expect(stdout).toMatchInlineSnapshot(`""`);
expect(stderrClean).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -398,14 +345,7 @@ describe('Lighthouse CI CLI', () => {
});

it('should assert failures from a budgets file', () => {
let {stdout = '', stderr = '', status = -1} = spawnSync(CLI_PATH, [
'assert',
`--budgets-file=${budgetsFile}`,
]);

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;
const {stdout, stderr, status} = runCLI(['assert', `--budgets-file=${budgetsFile}`]);

const stderrClean = stderr.replace(/\d{4,}(\.\d{1,})?/g, 'XXXX');
expect(stdout).toMatchInlineSnapshot(`""`);
Expand Down
26 changes: 25 additions & 1 deletion packages/cli/test/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
'use strict';

const path = require('path');
const {spawn} = require('child_process');
const {spawn, spawnSync} = require('child_process');
const testingLibrary = require('@testing-library/dom');

const CLI_PATH = path.join(__dirname, '../src/cli.js');
Expand All @@ -32,8 +32,32 @@ function waitForCondition(fn, label) {
});
}

/**
* @param {string[]} args
* @param {{cwd?: string, env?: Record<string, string>}} [overrides]
* @return {{stdout: string, stderr: string, status: number}}
*/
function runCLI(args, overrides = {}) {
const {env: extraEnvVars, ...options} = overrides;
const cleanEnv = {
...process.env,
LHCI_GITHUB_TOKEN: '',
LHCI_GITHUB_APP_TOKEN: '',
NO_UPDATE_NOTIFIER: '1',
};
const env = {...cleanEnv, ...extraEnvVars};
let {stdout = '', stderr = '', status = -1} = spawnSync(CLI_PATH, args, {...options, env});

stdout = stdout.toString();
stderr = stderr.toString();
status = status || 0;

return {stdout, stderr, status};
}

module.exports = {
CLI_PATH,
runCLI,
startServer,
waitForCondition,
};
2 changes: 1 addition & 1 deletion packages/server/src/ui/components/gauge.css
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

width: var(--indicator-width);
height: var(--indicator-height);
border-radius: var(--indicator-width);
border-radius: 25%;

background: var(--neutral-color);
}
Expand Down
15 changes: 0 additions & 15 deletions packages/utils/src/version.js

This file was deleted.

Loading

0 comments on commit 7bcd7f2

Please sign in to comment.