Skip to content

Commit

Permalink
add --list to log all versions matching the criteria
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Jul 10, 2022
1 parent 6221d63 commit e3edf7f
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 182 deletions.
3 changes: 3 additions & 0 deletions commons/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const ALL_FALSE_FULL_CONFIG: IChromeFullConfig = {
hideNegativeHits: false,
interactive: false,
inverse: false,
list: false,
max: new ComparableVersion(Infinity, 0, 0, 0),
min: new ComparableVersion(-Infinity, 0, 0, 0),
onFail: 'nothing',
Expand Down Expand Up @@ -46,6 +47,7 @@ export const DEFAULT_CONFIG_OPTIONS: IConfigOptions = {
hideNegativeHits: false,
increaseOnFail: false,
inverse: false,
list: false,
nonInteractive: false,
onlyNewestMajor: false,
quiet: false,
Expand All @@ -62,6 +64,7 @@ export const DEFAULT_FULL_CONFIG: IChromeFullConfig = {
hideNegativeHits: false,
interactive: true,
inverse: false,
list: false,
max: new ComparableVersion(10000, 0, 0, 0),
min: new ComparableVersion(0, 0, 0, 0),
onFail: 'nothing',
Expand Down
26 changes: 14 additions & 12 deletions config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function readConfig(args: string[], platform: NodeJS.Platform): ConfigWra
.option('-O, --only-newest-major', 'Show only the newest major version in user selection', DEFAULT_CONFIG_OPTIONS.onlyNewestMajor)
.option('-v, --inverse', 'Sort the selectable versions ascending', DEFAULT_CONFIG_OPTIONS.inverse)
.option('-s, --single <version>', 'Download a specific version in non-interactive mode, even if the file is listed in the localstore.json. Several other flags have no effect.')
.option('--list', 'List versions matching the criteria, doing nothing more', DEFAULT_CONFIG_OPTIONS.list)
.option('-q, --quiet', 'Suppress any logging output', DEFAULT_CONFIG_OPTIONS.quiet)
.option('--debug', 'Activates the debug mode (extended logging)', DEFAULT_CONFIG_OPTIONS.debug)
.parse(args)
Expand Down Expand Up @@ -100,23 +101,24 @@ export function readConfig(args: string[], platform: NodeJS.Platform): ConfigWra
return {
action: 'loadChrome',
config: {
autoUnzip: options.unzip,
min: new ComparableVersion(options.min),
max: new ComparableVersion(options.max),
results: minIsSet && !maxResultsIsSet ? Infinity : (parseInt(options.maxResults as string, 10) || 10),
os,
arch,
onFail: options.increaseOnFail ? 'increase' : options.decreaseOnFail ? 'decrease' : 'nothing',
interactive: !options.nonInteractive,
store: options.store,
autoUnzip: options.unzip,
debug: options.debug,
download: options.download,
hideNegativeHits: options.hideNegativeHits,
downloadFolder: options.folder || null,
onlyNewestMajor: options.onlyNewestMajor,
single: null,
hideNegativeHits: options.hideNegativeHits,
interactive: !options.nonInteractive,
inverse: options.inverse,
list: options.list,
max: new ComparableVersion(options.max),
min: new ComparableVersion(options.min),
onFail: options.increaseOnFail ? 'increase' : options.decreaseOnFail ? 'decrease' : 'nothing',
onlyNewestMajor: options.onlyNewestMajor,
os,
quiet: options.quiet,
debug: options.debug,
results: minIsSet && !maxResultsIsSet ? Infinity : (parseInt(options.maxResults as string, 10) || 10),
single: null,
store: options.store,
},
}
}
1 change: 1 addition & 0 deletions download/download-fluent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const allFalseConfig: IChromeFullConfig = {
hideNegativeHits: false,
interactive: false,
inverse: false,
list: false,
max: new ComparableVersion(Infinity, 0, 0, 0),
min: new ComparableVersion(-Infinity, 0, 0, 0),
onFail: 'nothing',
Expand Down
1 change: 1 addition & 0 deletions download/download.api.int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe('[int] download API', () => {
downloadFolder: null,
single: null,
debug: false,
list: false,
})

chromeZipStream.push('asdf')
Expand Down
39 changes: 35 additions & 4 deletions download/download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { fetchChromeZipFile } from '../api'
import { ComparableVersion } from '../commons/ComparableVersion'
import { MappedVersion } from '../commons/MappedVersion'
import { NoChromiumDownloadError } from '../errors'
import { IChromeConfig } from '../interfaces/interfaces'
import { Logger, logger, DebugMode } from '../log/logger'
import { progress, ProgressBar } from '../log/progress'
import { spinner, Spinner } from '../log/spinner'
Expand Down Expand Up @@ -190,7 +191,8 @@ describe('download', () => {
expect(fetchChromeZipFileMock).toHaveBeenCalledWith('chromeUrl')

expect(getChromeDownloadUrlMock).toHaveBeenCalledTimes(1)
expect(getChromeDownloadUrlMock).toHaveBeenCalledWith({

const expected: IChromeConfig = {
arch: 'x64',
autoUnzip: false,
debug: false,
Expand Down Expand Up @@ -218,7 +220,10 @@ describe('download', () => {
results: 10,
single: null,
store: true,
}, [new MappedVersion(10, 0, 0, 1, false)])
list: false,
}

expect(getChromeDownloadUrlMock).toHaveBeenCalledWith(expected, [new MappedVersion(10, 0, 0, 1, false)])
})

it('should fetch the zip and create the dest folder on finish', async () => {
Expand Down Expand Up @@ -300,7 +305,8 @@ describe('download', () => {
expect(fetchChromeZipFileMock).toHaveBeenCalledWith('chromeUrl')

expect(getChromeDownloadUrlMock).toHaveBeenCalledTimes(1)
expect(getChromeDownloadUrlMock).toHaveBeenCalledWith({

const expected: IChromeConfig = {
arch: 'x64',
autoUnzip: false,
debug: false,
Expand Down Expand Up @@ -328,7 +334,10 @@ describe('download', () => {
results: 10,
single: null,
store: true,
}, [new MappedVersion(10, 0, 0, 2, false)])
list: false,
}

expect(getChromeDownloadUrlMock).toHaveBeenCalledWith(expected, [new MappedVersion(10, 0, 0, 2, false)])

})

Expand Down Expand Up @@ -878,5 +887,27 @@ describe('download', () => {
expect(loggerMock.setDebugMode).toHaveBeenCalledWith(DebugMode.DEBUG)
})

it('should log the files and quit on config.list', async () => {
mapVersionsMock.mockReturnValue([new MappedVersion(20, 0, 0, 1, true), new MappedVersion(10, 0, 0, 1, false)])
getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn())

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)
existsSyncMock.mockReturnValue(false)

mkdirMock.mockImplementation((path, options, callback) => {
callback(null)
})

// Act
const config = createChromeFullConfig({
list: true
})
await downloadChromium(config)

expect(loggerMock.info).toHaveBeenCalledTimes(2)
expect(loggerMock.warn).toHaveBeenCalledTimes(1)
expect(loggerMock.info.mock.calls).toEqual([['versions:'], ['10.0.0.1']])
expect(loggerMock.warn).toHaveBeenCalledWith('20.0.0.1')
})
})
})
12 changes: 12 additions & 0 deletions download/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ async function downloadForConfig(config: IChromeConfig): Promise<DownloadReportE

const mappedVersions = mapVersions(versions, config, store)

if (!isChromeSingleConfig(config) && config.list) {
logger.info('versions:')
mappedVersions.forEach(v => {
if (v.disabled) {
logger.warn(v.value)
} else {
logger.info(v.value)
}
})
return []
}

logger.debug(`total number of versions: ${versions.length}, filtered versions: ${mappedVersions.length}`)

const { chromeUrl, filenameOS, report, selectedVersion } = await getChromeDownloadUrl(config, mappedVersions)
Expand Down
1 change: 1 addition & 0 deletions examples/example-download-all-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ downloadChromium({
downloadFolder: null,
single: null,
debug: false,
list: false,
})
2 changes: 2 additions & 0 deletions interfaces/config.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ export interface IConfigOptions {
single?: string,
inverse: boolean,

list: boolean,

debug: boolean,

quiet: boolean,
Expand Down
38 changes: 17 additions & 21 deletions interfaces/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,32 @@ export interface IExportConfigWrapper {

export type IChromeConfig = IChromeFullConfig | IChromeSingleConfig

export interface IChromeFullConfig {
min: ComparableVersion
max: ComparableVersion
results: number
os: OS
export interface IChromeCoreConfig {
arch: Arch
onFail: 'nothing' | 'increase' | 'decrease'
autoUnzip: boolean
interactive: boolean
store: boolean
debug: boolean
download: boolean
hideNegativeHits: boolean
downloadFolder: string | null
os: OS
quiet: boolean
store: boolean
}

export interface IChromeFullConfig extends IChromeCoreConfig {
hideNegativeHits: boolean
interactive: boolean
inverse: boolean
list: boolean
max: ComparableVersion
min: ComparableVersion
onFail: 'nothing' | 'increase' | 'decrease'
onlyNewestMajor: boolean
results: number
single: null
inverse: boolean
quiet: boolean
debug: boolean
}

export interface IChromeSingleConfig {
os: OS
arch: Arch
export interface IChromeSingleConfig extends IChromeCoreConfig {
single: ComparableVersion
store: boolean
autoUnzip: boolean
download: boolean
downloadFolder: string | null
quiet: boolean
debug: boolean
}

export interface IStoreConfig {
Expand Down
120 changes: 0 additions & 120 deletions select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,124 +149,4 @@ describe('userSelectedVersion', () => {
expect(loggerMock.warn).toHaveBeenCalledTimes(1)
expect(loggerMock.warn).toHaveBeenCalledWith('All versions in the range are disabled, try a different range and amount!')
})

it('should filter out not-newest major versions on --only-newest-major', async () => {
const mappedVersion10 = new MappedVersion({
major: 10,
minor: 0,
branch: 0,
patch: 0,
disabled: false
})
const mappedVersion10_2 = new MappedVersion({
major: 10,
minor: 0,
branch: 0,
patch: 1,
disabled: false
})
const mappedVersion60 = new MappedVersion({
major: 60,
minor: 1,
branch: 2,
patch: 3,
disabled: false
})
const mappedVersion60_2 = new MappedVersion({
major: 60,
minor: 1,
branch: 2,
patch: 104,
disabled: false
})

promptsMock.mockReturnValue({ version: '10.0.0.0' })
const config = createChromeFullConfig({
results: 10,
onlyNewestMajor: true,
})

// TODO: expect ?
await userSelectedVersion([mappedVersion60_2, mappedVersion60, mappedVersion10_2, mappedVersion10], config)
expect(promptsMock).toHaveBeenCalledWith({
type: 'select',
name: 'version',
message: 'Select a version',
warn: 'This version seems to not have a binary',
choices: [mappedVersion60_2, mappedVersion10_2],
hint: `for ${config.os} ${config.arch}`
})
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})

it('should strip the amount of versions passed to prompts on --only-newest-major', async () => {
const mappedVersion10 = new MappedVersion({
major: 10,
minor: 0,
branch: 0,
patch: 0,
disabled: false
})
const mappedVersion20 = new MappedVersion({
major: 20,
minor: 0,
branch: 0,
patch: 1,
disabled: false
})
const mappedVersion40 = new MappedVersion({
major: 40,
minor: 1,
branch: 2,
patch: 3,
disabled: false
})
const mappedVersion60 = new MappedVersion({
major: 60,
minor: 1,
branch: 2,
patch: 104,
disabled: false
})

promptsMock.mockReturnValue({ version: '10.0.0.0' })
const config = createChromeFullConfig({
results: 3,
onlyNewestMajor: true,
})

// TODO: expect ?
await userSelectedVersion([mappedVersion60, mappedVersion40, mappedVersion20, mappedVersion10], config)
expect(promptsMock).toHaveBeenCalledWith({
type: 'select',
name: 'version',
message: 'Select a version',
warn: 'This version seems to not have a binary',
choices: [mappedVersion60, mappedVersion40, mappedVersion20],
hint: `for ${config.os} ${config.arch}`
})
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})

it('should select the next version is newest major is disabled on --only-newest-major', async () => {
const mappedVersion20 = new MappedVersion(10, 0, 0, 0, false)
const mappedVersion201 = new MappedVersion(20, 0, 0, 1, true)

const config = createChromeFullConfig({
onlyNewestMajor: true,
results: 2,
})

// TODO: expect ?
await userSelectedVersion([mappedVersion20, mappedVersion201], config)
expect(promptsMock).toHaveBeenCalledWith({
type: 'select',
name: 'version',
message: 'Select a version',
warn: 'This version seems to not have a binary',
choices: [mappedVersion20],
hint: `for ${config.os} ${config.arch}`
})
expect(loggerMock.warn).toHaveBeenCalledTimes(0)
})
})
9 changes: 0 additions & 9 deletions select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ export async function userSelectedVersion(versions: MappedVersion[], config: ICh
return versions[0]
}

if (config.onlyNewestMajor) {
versions = versions.filter((version, index, versionArray) => {
const previous = versionArray[index - 1]
const previousMajor = previous?.value?.split('.')[0]
const currentMajor = version.value.split('.')[0]
return (currentMajor !== previousMajor || previous.disabled) && !version.disabled
}).slice(0, Number(config.results))
}

const { version } = await prompts({
type: 'select',
name: 'version',
Expand Down
Loading

0 comments on commit e3edf7f

Please sign in to comment.