UNPKG

jokkerr

Version:

Node package and CLI tool for saving web page as single HTML file

88 lines 3.2 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const commander_1 = require("commander"); const cairn_1 = require("./cairn"); const utils_1 = require("./utils"); const fs_1 = require("fs"); class Handler { constructor() { this.url = []; this.opt = {}; } main() { const program = this.parser(); if (this.url.length < 1) { // Output help information and exit immediately. program.help(); } let filepath = ''; if (program.output && program.output !== '-') { if (!fs_1.statSync(program.output)) { console.warn('custom output not exists, path: ' + program.output); process.exit(1); } filepath = program.output + '/'; } const output = (url, filename, content) => { if (program.output === '-') { console.info(content); } else { fs_1.writeFile(filename, content, (err) => { if (err) { console.warn(`${url} => ${err}`); return; } console.info(`${url} => ${filename}`); }); } }; const cairn = new cairn_1.Cairn(); for (const url of this.url) { if (!utils_1.isValidURL(url)) { console.info(`${url} => request url is not specified\n`); continue; } const filename = filepath + utils_1.createFileName(url); cairn .request({ url: url }) .options(this.opt) .archive() .then((webpage) => { output(url, filename, webpage); }) .catch((err) => console.warn(`${url} => ${JSON.stringify(err)}`)); } } parser() { const program = new commander_1.Command(); program .name('cairn') .usage('[options] url1 [url2]...[urlN]') .version('1.0.0', '-v, --version', 'output the current version') .description('CLI tool for saving web page as single HTML file'); program.option('-o, --output <string>', 'path to save archival result'); program.option('-u, --user-agent <string>', 'set custom user agent'); program .option('--no-js', 'disable JavaScript') .option('--no-css', 'disable CSS styling') .option('--no-embeds', 'remove embedded elements (e.g iframe)') .option('--no-medias', 'remove media elements (e.g img, audio)'); program.parse(process.argv); if (program.userAgent) this.opt.userAgent = program.userAgent; if (program.noJs) this.opt.disableJS = true; if (program.noCss) this.opt.disableCSS = true; if (program.noEmbeds) this.opt.disableEmbeds = true; if (program.noMedias) this.opt.disableMedias = true; this.url = program.args; return program; } } new Handler().main(); //# sourceMappingURL=cli.js.map