Skip to content

Commit

Permalink
feat(cli): add static dir file discovery depth flag (#940)
Browse files Browse the repository at this point in the history
Co-authored-by: Connor Clark <[email protected]>
  • Loading branch information
DanielSwarup and connorjclark authored Dec 14, 2023
1 parent 2cf4dc0 commit de1614e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 7 deletions.
22 changes: 22 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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`
Expand Down
9 changes: 8 additions & 1 deletion packages/cli/src/collect/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
});
}

Expand Down Expand Up @@ -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();
Expand Down
17 changes: 14 additions & 3 deletions packages/cli/src/collect/fallback-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

Expand Down
6 changes: 3 additions & 3 deletions packages/cli/test/collect-puppeteer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`""`);
Expand Down
1 change: 1 addition & 0 deletions types/collect.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ declare global {
additive: boolean;
settings?: LighthouseSettings;
maxAutodiscoverUrls?: number;
staticDirFileDiscoveryDepth?: number;
}
}
}
Expand Down

0 comments on commit de1614e

Please sign in to comment.