Skip to content

Commit

Permalink
fix(cli): improved CLI option resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Jan 17, 2022
1 parent 1c8c892 commit 57696ce
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Expand Up @@ -40,6 +40,7 @@
"@unlighthouse/server": "workspace:*",
"cac": "^6.7.12",
"consola": "^2.15.3",
"defu": "^5.0.0",
"fs-extra": "^10.0.0",
"globby": "^12.0.2",
"lodash-es": "^4.17.21",
Expand Down
28 changes: 16 additions & 12 deletions packages/cli/src/createCli.ts
Expand Up @@ -4,17 +4,21 @@ import { version } from '../package.json'
export default function createCli() {
const cli = cac('unlighthouse')

cli
.help()
.version(version)
.example('unlighthouse --site harlanzw.com')

cli.option('--site <site>', 'Host URL to scan')
cli.option('--scanner.samples <samples>', 'Specify the amount of samples to run.')
cli.option('--enable-javascript', 'When inspecting the HTML wait for the javascript to execute. Useful for SPAs.')
cli.option('--disable-javascript', 'When inspecting the HTML, don\'t wait for the javascript to execute.')
cli.option('--output-path <output-path>', 'Path to save the contents of the client and reports to.')
cli.option('--root <root>', 'Root ro run lighthouse. Useful for changing where the config is read from or setting up sampling.')
cli.option('--config-file <config-file>', 'Config File Path. Where to load the configuration file from.')
cli.option('--no-cache', 'Disable the caching.')
cli.option('--cache', 'Enable the caching.')
cli.option('-d, --debug', 'Debug. Enable debugging in the logger.')

return cli
.help()
.version(version)
.option('--site <site>', 'Host URL to scan')
.option('--site <site>', 'Alias for --site')
.option('--samples <scanner.samples>', 'Specify the amount of samples to run.')
.example('unlighthouse --site harlanzw.com')
.option('--output-path <output-path>', 'Path to save the contents of the client and reports to.')
.option('--root <root>', 'Root ro run lighthouse. Useful for changing where the config is read from or setting up sampling.')
.option('--config-file <config-file>', 'Config File Path. Where to load the configuration file from.')
.option('--no-cache', 'Disable the caching.')
.option('--cache', 'Enable the caching.')
.option('-d, --debug', 'Debug. Enable debugging in the logger.')
}
2 changes: 2 additions & 0 deletions packages/cli/src/types.ts
Expand Up @@ -9,6 +9,8 @@ export interface CliOptions {
configFile?: string
debug?: boolean
['scanner.samples']?: number
enableJavascript?: boolean
disableJavascript?: boolean
}

export interface CiOptions extends CliOptions {
Expand Down
37 changes: 26 additions & 11 deletions packages/cli/src/util.ts
Expand Up @@ -3,6 +3,7 @@ import type { UserConfig } from '@unlighthouse/core'
import { pick } from 'lodash-es'
import { handleError } from './errors'
import type { CiOptions, CliOptions } from './types'
import defu from 'defu'

export const isValidUrl = (s: string) => {
try {
Expand All @@ -23,17 +24,31 @@ export const validateOptions = (resolvedOptions: UserConfig) => {
}

export function pickOptions(options: CiOptions|CliOptions): UserConfig {
const picked : Record<string, any> = {}
if (options.noCache)
options.cache = true
picked.cache = true

return pick(options, [
// root level options
'scanner.samples',
'site',
'root',
'configFile',
'debug',
'cache',
'outputPath',
])
if (options.enableJavascript) {
picked.scanner = {
skipJavascript: false
}
} else if (options.disableJavascript) {
picked.scanner = {
skipJavascript: true
}
}

return defu(
pick(options, [
// root level options
'scanner.samples',
'site',
'root',
'configFile',
'debug',
'cache',
'outputPath',
]),
picked,
)
}

0 comments on commit 57696ce

Please sign in to comment.