From a122bbb8b5b8853d3d2c159aa3b39bf1406e03ed Mon Sep 17 00:00:00 2001 From: Connor Clark Date: Mon, 8 Apr 2024 18:12:26 -0700 Subject: [PATCH] feat(cli): add --lhr to assert command to load LHRs from anywhere --- packages/cli/src/assert/assert.js | 6 +++++- packages/utils/src/saved-reports.js | 15 ++++++++++++--- types/assert.d.ts | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/cli/src/assert/assert.js b/packages/cli/src/assert/assert.js index e9bedabd6..5cabd9c85 100644 --- a/packages/cli/src/assert/assert.js +++ b/packages/cli/src/assert/assert.js @@ -32,6 +32,10 @@ function buildCommand(yargs) { type: 'boolean', description: 'Whether to include the results of passed assertions in the output.', }, + lhr: { + description: + 'Path to LHRs (either a folder or a single file path). Not recursive. If not provided, .lighthouseci is used', + }, }); } @@ -53,7 +57,7 @@ async function runCommand(options) { // If we have a budgets file, convert it to our assertions format. if (budgetsFile) options = await convertBudgetsToAssertions(readBudgets(budgetsFile)); - const lhrs = loadSavedLHRs().map(json => JSON.parse(json)); + const lhrs = loadSavedLHRs(options.lhr).map(json => JSON.parse(json)); const uniqueUrls = new Set(lhrs.map(lhr => lhr.finalUrl)); const allResults = getAllAssertionResults(options, lhrs); const groupedResults = _.groupBy(allResults, result => result.url); diff --git a/packages/utils/src/saved-reports.js b/packages/utils/src/saved-reports.js index 62c80a922..739f2da2a 100644 --- a/packages/utils/src/saved-reports.js +++ b/packages/utils/src/saved-reports.js @@ -18,14 +18,23 @@ function ensureDirectoryExists(baseDir = LHCI_DIR) { } /** + * @param {string} [directoryOrPath] * @return {string[]} */ -function loadSavedLHRs() { - ensureDirectoryExists(); +function loadSavedLHRs(directoryOrPath) { + directoryOrPath = directoryOrPath || LHCI_DIR; + + if (directoryOrPath === LHCI_DIR) { + ensureDirectoryExists(); + } + + if (fs.lstatSync(directoryOrPath).isFile()) { + return [fs.readFileSync(directoryOrPath, 'utf8')]; + } /** @type {string[]} */ const lhrs = []; - for (const file of fs.readdirSync(LHCI_DIR)) { + for (const file of fs.readdirSync(directoryOrPath)) { if (!LHR_REGEX.test(file)) continue; const filePath = path.join(LHCI_DIR, file); diff --git a/types/assert.d.ts b/types/assert.d.ts index fa44b41ae..6df881f71 100644 --- a/types/assert.d.ts +++ b/types/assert.d.ts @@ -38,6 +38,7 @@ declare global { includePassedAssertions?: boolean; budgetsFile?: string; assertMatrix?: BaseOptions[]; + lhr?: string; } /**