From de1614ee06fb7bccf95c70045050e996bf93c6b2 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 14 Dec 2023 00:28:21 +0000 Subject: [PATCH] feat(cli): add static dir file discovery depth flag (#940) Co-authored-by: Connor Clark --- docs/configuration.md | 22 +++++++++++++++++++++ packages/cli/src/collect/collect.js | 9 ++++++++- packages/cli/src/collect/fallback-server.js | 17 +++++++++++++--- packages/cli/test/collect-puppeteer.test.js | 6 +++--- types/collect.d.ts | 1 + 5 files changed, 48 insertions(+), 7 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 789c607dd..bed81d864 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -232,6 +232,9 @@ Options: --maxAutodiscoverUrls The maximum number of pages to collect when using the staticDistDir option with no specified URL. Disable this limit by setting to 0. [number] [default: 5] + --staticDirFileDiscoveryDepth The maximum depth of nested folders Lighthouse will look into to discover + URLs on a static file folder. + [number] [default: 2] ``` #### `method` @@ -422,6 +425,25 @@ lhci collect --url=https://example-1.com --url=https://example-2.com lhci collect --start-server-command="yarn serve" --url=http://localhost:8080/ --puppeteer-script=./path/to/login-with-puppeteer.js ``` +### `staticDirFileDiscoveryDepth` + +The maximum depth level of nested folders that Lighthouse will look into to discover URLs. If not set, this will default to 2. + +### Example +```text + +public/ +├── index.html #level 0 +├── contact/ +│ └── index.html #level 1 +├── projects/ +│ ├──index.html #level 1 +│ └── crisis/ +│ ├──index.html #level 2 +│ └── earthquake/ +│ └── index.html #level 3 +``` + --- ### `upload` diff --git a/packages/cli/src/collect/collect.js b/packages/cli/src/collect/collect.js index 048420398..5dc0710fe 100644 --- a/packages/cli/src/collect/collect.js +++ b/packages/cli/src/collect/collect.js @@ -94,6 +94,12 @@ function buildCommand(yargs) { default: 5, type: 'number', }, + staticDirFileDiscoveryDepth: { + description: + 'The maximum depth level of nested folders that Lighthouse will look into to discover URLs. If not set, this will default to 2.', + default: 2, + type: 'number', + }, }); } @@ -187,7 +193,8 @@ async function startServerAndDetermineUrls(options) { : options.autodiscoverUrlBlocklist ? [options.autodiscoverUrlBlocklist] : []; - const availableUrls = server.getAvailableUrls(); + const maxStaticDirFileDiscoveryDepth = options.staticDirFileDiscoveryDepth || 2; + const availableUrls = server.getAvailableUrls(maxStaticDirFileDiscoveryDepth); const normalizedBlocklist = autodiscoverUrlBlocklistAsArray.map(rawUrl => { const url = new URL(rawUrl, 'http://localhost'); url.port = server.port.toString(); diff --git a/packages/cli/src/collect/fallback-server.js b/packages/cli/src/collect/fallback-server.js index a7c4007b5..33c21d7a5 100644 --- a/packages/cli/src/collect/fallback-server.js +++ b/packages/cli/src/collect/fallback-server.js @@ -72,9 +72,20 @@ class FallbackServer { ); } - /** @return {string[]} */ - getAvailableUrls() { - const htmlFiles = FallbackServer.readHtmlFilesInDirectory(this._pathToBuildDir, 2); + /** + * @param {number} maxDepth + * @return {string[]} + */ + getAvailableUrls(maxDepth) { + if (maxDepth >= 0) { + maxDepth = Math.floor(maxDepth); + } else { + process.stderr.write( + `WARNING: staticDirFileDiscoveryDepth must be greater than 0. Defaulting to a discovery depth of 2\n` + ); + maxDepth = 2; + } + const htmlFiles = FallbackServer.readHtmlFilesInDirectory(this._pathToBuildDir, maxDepth); return htmlFiles.map(({file}) => `http://localhost:${this._port}/${file}`); } diff --git a/packages/cli/test/collect-puppeteer.test.js b/packages/cli/test/collect-puppeteer.test.js index d2fa75d0c..7cf01a123 100644 --- a/packages/cli/test/collect-puppeteer.test.js +++ b/packages/cli/test/collect-puppeteer.test.js @@ -91,9 +91,9 @@ describe('Lighthouse CI collect CLI with puppeteer', () => { const chromePathHelp = stdout.match(/--chromePath.*\n.*\n.*/); expect(chromePathHelp).toMatchInlineSnapshot(` [ - "--chromePath The path to the Chrome or Chromium executable to use for collection. - --puppeteerScript The path to a script that manipulates the browser with puppeteer before running Lighthouse, used for auth. - --puppeteerLaunchOptions The object of puppeteer launch options", + "--chromePath The path to the Chrome or Chromium executable to use for collection. + --puppeteerScript The path to a script that manipulates the browser with puppeteer before running Lighthouse, used for auth. + --puppeteerLaunchOptions The object of puppeteer launch options", ] `); expect(stderr).toMatchInlineSnapshot(`""`); diff --git a/types/collect.d.ts b/types/collect.d.ts index 491bd728f..f0be5c731 100644 --- a/types/collect.d.ts +++ b/types/collect.d.ts @@ -63,6 +63,7 @@ declare global { additive: boolean; settings?: LighthouseSettings; maxAutodiscoverUrls?: number; + staticDirFileDiscoveryDepth?: number; } } }