-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
145 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { argv, Options } from 'yargs'; | ||
import { Options } from 'yargs'; | ||
import { getEnvPreset } from '../../../pre-set'; | ||
|
||
export const openReport = { | ||
alias: 'e', | ||
type: 'boolean', | ||
description: 'Opens browser automatically after the user-flow is collected. (true by default)', | ||
default: getEnvPreset().openReport as boolean, | ||
default: getEnvPreset().openReport, | ||
requiresArg: true | ||
} satisfies Options; | ||
|
||
export function get(): boolean { | ||
const { openReport } = argv as any as { openReport: boolean }; | ||
return openReport; | ||
} |
17 changes: 6 additions & 11 deletions
17
packages/cli/src/lib/commands/collect/processes/collect-reports.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 27 additions & 26 deletions
53
packages/cli/src/lib/commands/collect/utils/persist/open-report.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,34 @@ | ||
import { get as dryRun } from '../../../../commands/collect/options/dryRun'; | ||
import { get as openReport } from '../../options/openReport'; | ||
import { get as interactive } from '../../../../global/options/interactive'; | ||
import * as openReport from 'open'; | ||
import { logVerbose } from '../../../../core/loggin'; | ||
import * as openFileInBrowser from 'open'; | ||
import { CollectCommandOptions } from '../../options'; | ||
|
||
export async function openFlowReport(fileNames: string[]): Promise<void> { | ||
// open report if requested and not in executed in CI | ||
if (!dryRun() && openReport() && interactive()) { | ||
|
||
const htmlReport = fileNames.find(i => i.includes('.html')); | ||
if (htmlReport) { | ||
logVerbose('open HTML report in browser'); | ||
await openFileInBrowser(htmlReport, { wait: false }); | ||
return Promise.resolve(void 0); | ||
} | ||
export async function openFlowReports(fileNames: string[]): Promise<void> { | ||
const htmlReport = fileNames.find(i => i.includes('.html')); | ||
if (htmlReport) { | ||
logVerbose('open HTML report in browser'); | ||
await openReport(htmlReport, { wait: false }); | ||
return Promise.resolve(void 0); | ||
} | ||
|
||
const mdReport = fileNames.find(i => i.includes('.md')); | ||
if (mdReport) { | ||
logVerbose('open Markdown report in browser'); | ||
await openFileInBrowser(mdReport, { wait: false }); | ||
return Promise.resolve(void 0); | ||
} | ||
const mdReport = fileNames.find(i => i.includes('.md')); | ||
if (mdReport) { | ||
logVerbose('open Markdown report in browser'); | ||
await openReport(mdReport, { wait: false }); | ||
return Promise.resolve(void 0); | ||
} | ||
|
||
const jsonReport = fileNames.find(i => i.includes('.json')); | ||
if (jsonReport) { | ||
logVerbose('open JSON report in browser'); | ||
// @TODO if JSON is given open the file in https://googlechrome.github.io/lighthouse/viewer/ | ||
await openFileInBrowser(jsonReport, { wait: false }); | ||
} | ||
const jsonReport = fileNames.find(i => i.includes('.json')); | ||
if (jsonReport) { | ||
logVerbose('open JSON report in browser'); | ||
// @TODO if JSON is given open the file in https://googlechrome.github.io/lighthouse/viewer/ | ||
await openReport(jsonReport, { wait: false }); | ||
} | ||
return Promise.resolve(void 0); | ||
} | ||
|
||
export function handleOpenFlowReports({ dryRun, openReport, interactive}: CollectCommandOptions) { | ||
if (dryRun || !openReport || !interactive) { | ||
return; | ||
} | ||
return openFlowReports; | ||
} |
77 changes: 77 additions & 0 deletions
77
packages/cli/src/lib/commands/collect/utils/persist/open-report.unit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as openReport from 'open'; | ||
import { handleOpenFlowReports, openFlowReports } from './open-report'; | ||
import { logVerbose } from '../../../../core/loggin'; | ||
import { CollectCommandOptions } from '../../options'; | ||
|
||
jest.mock('open'); | ||
jest.mock('../../../../core/loggin'); | ||
|
||
describe('handleOpenFlowReport', () => { | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}) | ||
|
||
it('should return the openFlowReport function if openReport, interactive and not dryRun', async () => { | ||
const openReportsProcess = handleOpenFlowReports({ | ||
openReport: true, | ||
interactive: true, | ||
dryRun: false, | ||
} as CollectCommandOptions); | ||
expect(openReportsProcess).toEqual(expect.any(Function)); | ||
}); | ||
|
||
it('should return undefined if openReport is false', () => { | ||
const openReportsProcess = handleOpenFlowReports({ | ||
openReport: false, | ||
interactive: true, | ||
dryRun: false, | ||
} as CollectCommandOptions); | ||
expect(openReportsProcess).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if dryRun is true', () => { | ||
const openReportsProcess = handleOpenFlowReports({ | ||
openReport: true, | ||
interactive: true, | ||
dryRun: true, | ||
} as CollectCommandOptions); | ||
expect(openReportsProcess).toBeUndefined(); | ||
}); | ||
|
||
it('should return undefined if interactive is false', () => { | ||
const openReportsProcess = handleOpenFlowReports({ | ||
openReport: true, | ||
interactive: false, | ||
dryRun: false, | ||
} as CollectCommandOptions); | ||
expect(openReportsProcess).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe('openReports', () => { | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not open the report if no file name is passed', async () => { | ||
await openFlowReports([]); | ||
expect(openReport).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it.each(['html', 'json', 'md'])('should open the %s report', async (format) => { | ||
await openFlowReports([`example.${format}`]); | ||
expect(openReport).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not logVerbose if no file name is passed', async () => { | ||
await openFlowReports([]); | ||
expect(logVerbose).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should only open 1 time report if multiple report formats are passed', async () => { | ||
await openFlowReports(['example.html', 'example.json', 'example.md']); | ||
expect(openReport).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,14 @@ | ||
import { argv, Options } from 'yargs'; | ||
import { getEnvPreset } from '../../pre-set'; | ||
|
||
function getDefaultByCliMode(): boolean { | ||
return getEnvPreset().interactive as boolean; | ||
} | ||
export const interactive = { | ||
alias: 'i', | ||
type: 'boolean', | ||
description: 'When false questions are skipped with the values from the suggestions. This is useful for CI integrations.', | ||
default: getDefaultByCliMode() | ||
default: getEnvPreset().interactive, | ||
} satisfies Options; | ||
|
||
const defaultValue = interactive.default; | ||
|
||
// We don't rely on yargs option normalization features as this can happen before cli bootstrap | ||
export function get(): boolean { | ||
const { interactive, i } = argv as any as {interactive?: boolean, i?: boolean}; | ||
return interactive !== undefined ? Boolean(interactive) : i !== undefined ? Boolean(i) : defaultValue; | ||
const { interactive } = argv as any as { interactive: boolean }; | ||
return interactive; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters