Skip to content

Commit

Permalink
Add "downloadChromium.withDefaults()" to download API
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Apr 11, 2022
1 parent 0376487 commit 2fb72b2
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 34 deletions.
48 changes: 27 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ E.g.:

## Use as API

rusted-chromium can be used as API as well. See more examples [here](https://github.com/BuZZ-T/rusted-chromium/blob/main/examples/README.md).

### Import store

To import a store file, use `importAndMergeStore`:
Expand All @@ -302,32 +304,42 @@ To download a chromium version, use `downloadChromium`:
import { ComparableVersion, downloadChromium } from 'rusted-chromium';

downloadChromium({
min: new ComparableVersion(0, 0, 0, 0),
max: new ComparableVersion(1000, 0, 0, 0),
results: 10,
os: 'linux',
arch: 'x64',
onFail: 'nothing',
autoUnzip: false,
interactive: false,
store: true,
download: true,
hideNegativeHits: boolean
hideNegativeHits: false,
interactive: true,
inverse: false,
max: new ComparableVersion(95, 0, 0, 0),
min: new ComparableVersion(0,0,0,0),
onFail: 'nothing',
onlyNewestMajor: false,
os: 'linux',
quiet: false,
store: true,
results: 10,
downloadFolder: null,
onlyNewestMajor: false
single: null,
inverse: false,
});
})
```

### Download a specific version of chromium
### Directly pass CLI flags
If you want to directly pass `process.argv` and extend or restrict the available flags, directly import `rusted`:
```ts
import { rusted } from 'rusted-chromium'

rusted(process.argv, 'linux')

```

### Use defaults and override specific settings

This is probably the most useful version in a CI envionment. This requires less config options, as many of them are not regarded when using `single`.

```ts
import { downloadChromium } from 'rusted-chromium';

downloadChromium({
downloadChromium.withDefaults({
arch: 'x64',
single: "10.0.0.0",
os: 'linux',
Expand All @@ -337,13 +349,7 @@ downloadChromium({
});
```

### Directly pass CLI flags
If you want to directly pass `process.argv` and extend or restrict the available flags, directly import `rusted`:
```ts
import { rusted } from 'rusted-chromium';

rusted(process.argv);
```
**See more examples to use the API [here](https://github.com/BuZZ-T/rusted-chromium/blob/main/examples/README.md).**

## FAQ

Expand Down Expand Up @@ -371,4 +377,4 @@ A session with the newer version was still running. In this case, chrom(e/ium) d
There are two possible solutions for this:

* Make sure to first close all running instances of chrom(e/ium)!
* Start your downloaded version of chromium with the flag `--user-data-dir=<folder>`. See [chromium.googlesource.com](https://chromium.googlesource.com/chromium/src.git/+/master/docs/user_data_dir.md#overriding-the-user-data-directory) for more information.
* Start your downloaded version of chromium with the flag `--user-data-dir=<folder>`. See [chromium.googlesource.com](https://chromium.googlesource.com/chromium/src.git/+/master/docs/user_data_dir.md#overriding-the-user-data-directory) for more information.
101 changes: 101 additions & 0 deletions download.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ describe('download', () => {
})

it('should fetch the zip and create the dest folder', async () => {
mapVersionsMock.mockReturnValue([new MappedVersion(10, 0, 0, 1, false)])
getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn())

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)
Expand Down Expand Up @@ -181,6 +182,36 @@ describe('download', () => {

expect(fetchChromeZipFileMock).toHaveBeenCalledTimes(1)
expect(fetchChromeZipFileMock).toHaveBeenCalledWith('chromeUrl')

expect(getChromeDownloadUrlMock).toHaveBeenCalledTimes(1)
expect(getChromeDownloadUrlMock).toHaveBeenCalledWith({
arch: 'x64',
autoUnzip: false,
download: true,
downloadFolder: 'down_folder',
hideNegativeHits: false,
interactive: true,
inverse: false,
max: new ComparableVersion({
major: 10000,
minor: 0,
branch: 0,
patch: 0,
}),
min: new ComparableVersion({
branch: 0,
major: 0,
minor: 0,
patch: 0,
}),
onFail: 'nothing',
onlyNewestMajor: false,
os: 'linux',
quiet: false,
results: 10,
single: null,
store: true,
}, [new MappedVersion(10, 0, 0, 1, false)])
})

it('should fetch the zip and create the dest folder on finish', async () => {
Expand Down Expand Up @@ -243,6 +274,76 @@ describe('download', () => {
expect(fetchChromeZipFileMock).toHaveBeenCalledWith('chromeUrl')
})

it('should fetch the zip with defaults', async () => {
mapVersionsMock.mockReturnValue([new MappedVersion(10, 0, 0, 2, false)])
getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn())

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)

// Act
await downloadChromium.withDefaults()

expect(progressConstructorMock).toHaveBeenCalledTimes(1)
expect(progressConstructorMock).toHaveBeenCalledWith(zipFileResource, { throttle: 100 })

expect(progressMock.start).toHaveBeenCalledTimes(0)
expect(progressMock.fraction).toHaveBeenCalledTimes(0)

expect(fetchChromeZipFileMock).toHaveBeenCalledTimes(1)
expect(fetchChromeZipFileMock).toHaveBeenCalledWith('chromeUrl')

expect(getChromeDownloadUrlMock).toHaveBeenCalledTimes(1)
expect(getChromeDownloadUrlMock).toHaveBeenCalledWith({
arch: 'x64',
autoUnzip: false,
download: true,
downloadFolder: null,
hideNegativeHits: false,
interactive: true,
inverse: false,
max: new ComparableVersion({
major: 10000,
minor: 0,
branch: 0,
patch: 0,
}),
min: new ComparableVersion({
branch: 0,
major: 0,
minor: 0,
patch: 0,
}),
onFail: 'nothing',
onlyNewestMajor: false,
os: 'linux',
quiet: false,
results: 10,
single: null,
store: true,
}, [new MappedVersion(10, 0, 0, 2, false)])

})

it('should fetch the zip with defaults for single', async () => {
getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn())

fetchChromeZipFileMock.mockResolvedValue(zipFileResource)

// Act
await downloadChromium.withDefaults({
single: new ComparableVersion(10, 0, 0, 0),
})

expect(progressConstructorMock).toHaveBeenCalledTimes(1)
expect(progressConstructorMock).toHaveBeenCalledWith(zipFileResource, { throttle: 100 })

expect(progressMock.start).toHaveBeenCalledTimes(0)
expect(progressMock.fraction).toHaveBeenCalledTimes(0)

expect(fetchChromeZipFileMock).toHaveBeenCalledTimes(1)
expect(fetchChromeZipFileMock).toHaveBeenCalledWith('chromeUrl')
})

it('should fetch and exract the zip', async () => {
getChromeDownloadUrlMock.mockResolvedValue(createGetChromeDownloadUrlReturn())

Expand Down
22 changes: 19 additions & 3 deletions download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ async function extractZip(downloadPath: string) {
* @see DEFAULT_SINGLE_CONFIG
* @param additionalConfig Manually set config, which will override the settings in the default config
*/
export async function downloadChromium(additionalConfig?: Partial<IChromeConfig>): Promise<void> {

const config = enrichAdditionalConfig(additionalConfig)
async function downloadForConfig(config: IChromeConfig): Promise<void> {

const versions = await loadVersions()
const store = await loadStore()
Expand Down Expand Up @@ -143,3 +141,21 @@ export async function downloadChromium(additionalConfig?: Partial<IChromeConfig>
throw new NoChromiumDownloadError()
}
}

/**
* Downlodas a chromium zip file with default config, which can be partially overridden
* @param config IChromeConfig to override the default config. May omit fields and can be ommited entirely
* @returns
*/
const withDefaults = (config: Partial<IChromeConfig> = {}) => downloadForConfig(enrichAdditionalConfig(config))

/**
* Downloads a chromium zip file based on the given config
* @see DEFAULT_FULL_CONFIG
* @see DEFAULT_SINGLE_CONFIG
* @param additionalConfig Manually set config, which will override the settings in the default config
*/
export const downloadChromium = Object.assign(
downloadForConfig,
{ withDefaults }
)
19 changes: 15 additions & 4 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,36 @@ downloadChromium({

Downloads a specific version (98.0.4707.2) for Linux 64-Bit.

```
```ts
import { ComparableVersion, downloadChromium } from 'rusted-chromium'

downloadChromium({
downloadChromium.withDefaults({
single: new ComparableVersion(98, 0, 4707, 2),
os: 'linux',
arch: 'x64',
})

```

### example-download.ts
Opens the cli prompt with the default configuration, only setting os and arch.
```ts
import { downloadChromium } from 'rusted-chromium'

downloadChromium({
downloadChromium.withDefaults({
arch: 'x64',
os: 'linux',
})

```

### example-download-default-config.ts
Opens the cli prompt with the default configuration.
```ts
import { downloadChromium } from 'rusted-chromium'

downloadChromium()
downloadChromium.withDefaults()

```

### example-export.ts
Expand All @@ -108,3 +111,11 @@ importAndMergeLocalstore({
url: 'https://rusted.buzz-t.eu/localstore.json'
})
```

### example-pass-cli-flags.ts
Directly pass the cli flags to `rusted-chromium` and it interpret it.
```ts
import { rusted } from 'rusted-chromium'

rusted(process.argv, 'linux')
```
2 changes: 1 addition & 1 deletion examples/example-download-default-config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { downloadChromium } from 'rusted-chromium'

downloadChromium()
downloadChromium.withDefaults()
2 changes: 1 addition & 1 deletion examples/example-download-single.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComparableVersion, downloadChromium } from 'rusted-chromium'

downloadChromium({
downloadChromium.withDefaults({
single: new ComparableVersion(98, 0, 4707, 2),
os: 'linux',
arch: 'x64',
Expand Down
2 changes: 1 addition & 1 deletion examples/example-download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { downloadChromium } from 'rusted-chromium'

downloadChromium({
downloadChromium.withDefaults({
arch: 'x64',
os: 'linux',
})
3 changes: 3 additions & 0 deletions examples/example-pass-cli-flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { rusted } from 'rusted-chromium'

rusted(process.argv, 'linux')
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"start": "ts-node start-local.ts",
"start:debug": "node --inspect --inspect-brk node_modules/.bin/ts-node start-local.ts",
"clean": "find -maxdepth 2 \\( -iname \"*.js\" -or -iname \"*.js.map\" \\) -and ! -name \"jest.config.js\" -and ! -path \"./bin/*\" -and ! -path \"./node_modules/*\" -delete",
"clean": "find -maxdepth 2 \\( -iname \"*.js\" -or -iname \"*.js.map\" -or -iname \"*.d.ts\" \\) -and ! -name \"jest.config.js\" -and ! -path \"./bin/*\" -and ! -path \"./node_modules/*\" -delete",
"lint": "eslint **/*.ts *.ts",
"pretest": "npm run clean",
"test": "jest --group=unit",
Expand Down
24 changes: 23 additions & 1 deletion utils/typeguards.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* @group unit/utils/typeguard
*/

import { isTextFunction, isIVersion, isIVersionWithDisabled } from './typeguards'
import { ComparableVersion } from '../commons/ComparableVersion'
import { isTextFunction, isIVersion, isIVersionWithDisabled, isChromeSingleConfig } from './typeguards'

describe('typeguards', () => {

Expand Down Expand Up @@ -140,4 +141,25 @@ describe('typeguards', () => {
})).toBe(false)
})
})

describe('isChromeSingleConfig', () => {
it('should correctly identify an IChromeSingleConfig', () => {
expect(isChromeSingleConfig({
single: new ComparableVersion(1,2,3,4),
})).toBe(true)
})

it('should return false on single === null', () => {
expect(isChromeSingleConfig({
hideNegativeHits: true,
single: null,
})).toBe(false)
})

it('should return false on single === undefined', () => {
expect(isChromeSingleConfig({
inverse: false,
})).toBe(false)
})
})
})
2 changes: 1 addition & 1 deletion utils/typeguards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function isIVersionWithDisabled(value: unknown): value is IVersionWithDis
}

export function isChromeSingleConfig(value: Partial<IChromeConfig>): value is Partial<IChromeSingleConfig> {
return value.single !== null
return value.single !== null && value.single !== undefined
}

0 comments on commit 2fb72b2

Please sign in to comment.