Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arg parse improvments #78

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: node index.js https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml
- run: node index.js https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --quite
- run: node index.js https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --quiet
- run: node index.js https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --no-css
- run: node index.js https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --no-js
- run: node index.js https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --no-images
Expand All @@ -81,7 +81,7 @@ jobs:
name: docker-image
- run: docker load -i sitemap-warmer.tar
- run: docker run sitemap-warmer:local https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml
- run: docker run sitemap-warmer:local https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --quite
- run: docker run sitemap-warmer:local https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --quiet
- run: docker run sitemap-warmer:local https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --no-css
- run: docker run sitemap-warmer:local https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --no-js
- run: docker run sitemap-warmer:local https://raw.githubusercontent.com/tdtgit/sitemap-warmer/master/tests/sitemap.xml --no-images
Expand Down
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,29 @@ You can also warm up multiple domains of course.
Usage:

```shell
warmup datuan.dev <URL> <parameter>
warmup <URL> <parameters>
```

| Parameter | Description | Default |
|------------------ |--------------------------------------------------------------------------------------------------------------------- |----------------- |
| `-a`, `--all` | Warm up all URLs in sitemap | False |
| `-r`, `--range` | Only warm up URLs with `lastMod` newer than X seconds.<br> This parameters is ignored if `-a` (`--all`) is provided | 300s (5 minutes) |
| `-d`, `--delay` | Delay (in milliseconds) between each warm up call.<br> If you using the low-end hosting, keep this value higher | 500 |
| `--no-images` | Disable images warm up | False |
| `--no-css` | Disable CSS warm up | False |
| `--no-js` | Disable Javascript warm up | False |
| `--no-brotli` | Disable Brotli compression warm up | False |
| `-q`, `--quite` | Suppress the debug log | False |
| `-h`, `--headers` | Add custom headers | None |
| Parameter | Description | Default |
|-------------------- |--------------------------------------------------------------------------------------------------------------------- |----------------- |
| `-a`, `--all` | Warm up all URLs in sitemap | False |
| `-r`, `--range` | Only warm up URLs with `lastMod` newer than X seconds.<br> This parameters is ignored if `-a` (`--all`) is provided | 300s (5 minutes) |
| `-d`, `--delay` | Delay (in milliseconds) between each warm up call.<br> If you using the low-end hosting, keep this value higher | 500 |
| `--img`, --images` | Enables images warm up | True |
| `--webp` | Enables webp images warm up | True |
| `--avif` | Enables avif images warm up | True |
| `--css` | Enables CSS warm up | True |
| `--js` | Enables Javascript warm up | True |
| `--brotli` | Enables Brotli compression warm up. Used by all modern browsers, "Accept Encoding: gzip, deflate, br". | True |
| `-q`, `--quiet` | Suppress the debug log | False |
| `-h`, `--headers` | Add custom headers, for instance Host, Authorization, User-Agent etc. | None |
| `-p`, `--purge` | Purge resource before warm up, 0 = purging disabled, 1 >= content, 2 >= images | 0 |

## Advanced options
### Custom request headers

```shell
warmup datuan.dev --headers.auth "Bearer super_secret" --headers.user-agent "My own crawler"
warmup datuan.dev --headers.authorization "Bearer super_secret" --headers.user-agent "My own crawler"
```
...

Expand Down
134 changes: 102 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,122 @@ import utils from './utilities.js'
import fetch from 'node-fetch'
import Logger from 'logplease'
import yargs from 'yargs'
const argv = yargs(process.argv.slice(2))
import { hideBin } from 'yargs/helpers'

const argv = yargs(hideBin(process.argv))
.usage('Usage: $0' + ' domain.com')

.alias('v', 'version')
.alias('h', 'help')
.alias('r', 'range')
.describe('range', 'Only warm up URLs with lastModified newer than this value (in seconds). Default: 300s (5 minutes)')
.default('range', 300)
.alias('d', 'delay')
.describe('delay', 'Delay (in milliseconds) between each warm up call. If you using the low-end hosting, keep this value higher. Default: 500ms')
.default('delay', 500)
.describe('images', 'Enable images warm up. Default: true')
.default('images', true)
.describe('css', 'Enable CSS warm up. Default: true')
.default('css', true)
.describe('js', 'Enable Javascript warm up. Default: true')
.default('js', true)
.describe('brotli', 'Enable Brotli compress warm up. Default: true')
.default('brotli', true)
.describe('webp', 'Enable WebP images warm up. Default: true')
.default('webp', true)
.describe('avif', 'Enable AVIF images warm up. Default: true')
.default('avif', true)
.alias('a', 'all')
.describe('all', 'Ignore --range parameter and warm up all URLs in sitemap')
.alias('q', 'quite')
.describe('quite', 'Disable debug logging if you feel it\'s too much')
.alias('p', 'purge')
.describe('purge', 'Enable purging the resources before warm up.')
.default('purge', 0)
.describe('headers', 'Add custom headers with warmup request. Example --headers.auth \'Bearer secret_token\'')
.default('headers', {})

.option('a', {
alias: 'all',
describe: 'Warm up all all URLs in sitemap, ignores --range parameter',
type: 'boolean',
coerce: toBoolean,
default: false
})
.option('r', {
alias: 'range',
describe: 'Only warm up URLs with lastModified newer than this value (in seconds). Default: 300s (5 minutes)',
type: 'number',
coerce: toInt,
default: 300
})
.option('d', {
alias: 'delay',
describe: 'Delay (in milliseconds) between each warm up call. If your using low-end hosting, keep this value higher.',
type: 'number',
coerce: toInt,
default: 500
})
.option('q', {
alias: ['quiet', 'quite', 'silent'],
describe: 'Disable debug logging if you feel it\'s too much.',
type: 'boolean',
coerce: toBoolean,
default: false
})

.option('img', {
alias: 'images',
describe: 'Enable images warm up.',
type: 'string',
coerce: toBoolean,
default: true
})

.option('css', {
describe: 'Enable CSS warm up.',
type: 'string',
coerce: toBoolean,
default: true
})
.option('js', {
describe: 'Enable Javascript warm up.',
type: 'string',
coerce: toBoolean,
default: true
})

.option('webp', {
describe: 'Enable WebP images warm up.',
type: 'string',
coerce: toBoolean,
default: true
})
.option('avif', {
describe: 'Enable AVIF images warm up.',
type: 'string',
coerce: toBoolean,
default: true
})

.option('brotli', {
describe: 'Enable Brotli compress warm up (Used by all modern browsers, "Accept Encoding: gzip, deflate, br")',
type: 'string',
coerce: toBoolean,
default: true
})

.option('p', {
alias: 'purge',
describe: 'Enable purging the resources before warm up (0 = no purging, 1 >= page content, 2 >= images).',
type: 'number',
coerce: toInt,
default: 0,
choices: [0, 1, 2]
})

.option('headers', {
default: {},
describe: 'Add custom headers with warmup request. For instance Host, Authorization, User-Agent etc.',
})
.example('$0 domain.com --headers.authorization "Bearer secret_token"', 'Add custom auth header')

.argv

const logger = Logger.create('main', {
useLocalTime: true,
})

if (argv.quite) {
if (argv.quiet) {
Logger.setLogLevel(Logger.LogLevels.INFO)
}

const settings = {
all: argv.all,
sitemap: process.argv[2],
domain: null,
newer_than: parseInt(argv.range) || 300,
delay: parseInt(argv.delay) || 500,
newer_than: argv.range,
delay: argv.delay,
warmup_images: argv.images,
warmup_css: argv.css,
warmup_js: argv.js,
warmup_brotli: argv.brotli,
warmup_webp: argv.webp,
warmup_avif: argv.avif,
purge: parseInt(argv.purge) || 0,
purge: argv.purge,
custom_headers: argv.headers,
}

Expand Down Expand Up @@ -98,3 +160,11 @@ fetch(settings.sitemap.href).then((res) => {
}).catch(error => {
logger.error(error)
})

function toBoolean(value) {
return ['1', 'true'].includes(`${value}`)
}

function toInt(value) {
return parseInt(value) || 0
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"node-fetch": "^3.2.3",
"node-html-parser": "^5.3.3",
"normalize-url": "^7.0.3",
"yargs": "^17.3.1"
"yargs": "^17.4.0"
},
"bin": {
"warmup": "index.js"
Expand Down