Created
June 19, 2020 21:19
-
-
Save gingeleski/68c10f5b3747484c98a108e63406c467 to your computer and use it in GitHub Desktop.
Part of a Node program I wrote, just dealing with argument parsing, to show my sister how to do that.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parse command line arguments with the Minimist library | |
const argv = require('minimist')(process.argv.slice(2)); | |
// Main browser automation dependencies | |
const { chromium, firefox, webkit } = require('playwright'); | |
// Other dependencies | |
const crypto = require('crypto'); | |
const fse = require('fs-extra'); | |
const path = require('path'); | |
const url = require('url'); | |
const validUrl = require('valid-url'); | |
// Global constants | |
const _VERSION = '1.0.0'; | |
const hashingAlgorithm = 'md5'; | |
const jsDir = 'js'; | |
const lastRuntimeOutputFilename = 'lastrun.txt'; | |
const snapshotsDir = path.join('.', 'snapshots') | |
const targetUrlOutputFilename = 'url.txt'; | |
// See if we should print help | |
if (argv['h'] || argv['help']) { | |
console.info(' Usage:'); | |
console.info(' jswatch <target_url> [options]') | |
console.info(' Options:'); | |
console.info(' --local-storage Pass Base64 JSON string of how to set browser local storage') | |
console.info(' -h, --help Print this help') | |
console.info(' -v, --version Print version') | |
process.exit(0); | |
} | |
// Print version information | |
if (argv['v'] || argv['version']) { | |
console.info(_VERSION); | |
process.exit(0); | |
} | |
// Need at least one argument for the target URL | |
if (0 == argv['_'].length) { | |
console.error('Missing mandatory target (URL) argument. Pass -h or --help for info.'); | |
process.exit(1); | |
} | |
// Validate target URL | |
const targetUrl = argv['_'][0]; | |
if (false == validUrl.isUri(targetUrl)) { | |
console.error('Given argument is not a valid URL.'); | |
process.exit(1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment