@wabarc/cairn
Version:
Node package and CLI tool for saving web page as single HTML file
113 lines • 5.16 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const archiver_1 = require("./archiver");
const utils_1 = require("./utils");
const fs_1 = require("fs");
class Handler {
constructor() {
this.urls = [];
this.opts = {};
}
main() {
return __awaiter(this, void 0, void 0, function* () {
const program = this.parser();
const options = program.opts();
if (this.urls.length < 1) {
// Output help information and exit immediately.
program.help();
}
let filepath = '';
if (options.output && options.output !== '-') {
if (!(0, fs_1.statSync)(options.output)) {
console.warn('custom output not exists, path: ' + options.output);
process.exit(1);
}
filepath = options.output + '/';
}
const output = (url, filename, content) => __awaiter(this, void 0, void 0, function* () {
if (options.output === '-') {
console.info(content);
}
else {
(0, fs_1.writeFile)(filename, content, (err) => {
if (err) {
console.warn(`${url} => ${err}`);
return;
}
console.info(`${url} => ${filename}`);
});
}
});
const cairn = new archiver_1.Archiver();
for (const url of this.urls) {
if (!(0, utils_1.isValidURL)(url)) {
console.info(`${url} => request url is not specified\n`);
continue;
}
const filename = filepath + (0, utils_1.createFileName)(url);
yield cairn
.request({ url: url })
.options(this.opts)
.archive()
.then((archived) => __awaiter(this, void 0, void 0, function* () {
if (!archived.webpage || typeof archived.webpage.root !== 'function') {
return;
}
const html = archived.webpage.root() ? archived.webpage.root().html() : '';
if (!html) {
console.warn(`${url} => archival failure. [status: ${archived.status}]`);
return;
}
yield output(url, filename, html || '');
}))
.catch((err) => console.warn(`${url} => ${JSON.stringify(err)}`));
}
});
}
parser() {
const program = new commander_1.Command();
const version = process.env.npm_package_version || '0.0.1';
program
.name('cairn')
.usage('[options] url1 [url2]...[urlN]')
.version(version, '-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('-p, --proxy [protocol://]host[:port]', 'use this proxy');
program.option('-t, --timeout <number>', 'maximum time (in second) request timeout');
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);
const options = program.opts();
if (options.proxy)
this.opts.proxy = options.proxy;
if (options.userAgent)
this.opts.userAgent = options.userAgent;
if (options.timeout)
this.opts.timeout = parseInt(options.timeout);
// `no-` to set the option value to false when used.
this.opts.disableJS = !options.js;
this.opts.disableCSS = !options.css;
this.opts.disableEmbeds = !options.embeds;
this.opts.disableMedias = !options.medias;
this.urls = program.args;
return program;
}
}
new Handler().main();
//# sourceMappingURL=cli.js.map