UNPKG

3.97 kBJavaScriptView Raw
1#!/usr/bin/env node
2"use strict";
3var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5 return new (P || (P = Promise))(function (resolve, reject) {
6 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9 step((generator = generator.apply(thisArg, _arguments || [])).next());
10 });
11};
12Object.defineProperty(exports, "__esModule", { value: true });
13const program = require("commander");
14const fs_1 = require("fs");
15const path = require("path");
16const _1 = require("./");
17function init(dir = '.') {
18 return __awaiter(this, void 0, void 0, function* () {
19 const resolvedPath = path.resolve(dir);
20 try {
21 yield fs_1.promises.access(resolvedPath, fs_1.constants.F_OK | fs_1.constants.R_OK);
22 const xkcd = new _1.XKCD();
23 return [resolvedPath, xkcd];
24 }
25 catch (error) {
26 throw new Error(`The specified path does not exist or is not writable.`);
27 }
28 });
29}
30function save(filePath, imageResult) {
31 return __awaiter(this, void 0, void 0, function* () {
32 const { data, num, safe_title } = imageResult;
33 const extension = data.mimeType ? data.mimeType.replace('image/', '') : 'png';
34 const resolvedFilePath = path.resolve(filePath, `xkcd #${num} - ${safe_title}.${extension}`);
35 yield fs_1.promises.writeFile(resolvedFilePath, data.data);
36 console.info(`Saved image to "${resolvedFilePath}".`);
37 });
38}
39const { description, name, version } = require('../package.json');
40program.on('command:*', () => program.help());
41program
42 .name(name.replace(/^@[^/]+\//, ''))
43 .version(version, '-v, --version')
44 .description(description)
45 .option('-o, --output <dir>', 'Specify the output directory', path.resolve('.'));
46program
47 .command('latest')
48 .description('Save the latest comic')
49 .action((command) => __awaiter(void 0, void 0, void 0, function* () {
50 try {
51 const [resolvedPath, xkcd] = yield init(command.parent.output);
52 const imageData = yield xkcd.api.getLatest({ withData: true });
53 yield save(resolvedPath, imageData);
54 }
55 catch (error) {
56 console.error(`Error: ${error.message}`);
57 program.outputHelp();
58 process.exit(1);
59 }
60}));
61program
62 .command('random')
63 .description('Save a random comic')
64 .action((command) => __awaiter(void 0, void 0, void 0, function* () {
65 try {
66 const [resolvedPath, xkcd] = yield init(command.parent.output);
67 const imageData = yield xkcd.api.getRandom({ withData: true });
68 yield save(resolvedPath, imageData);
69 }
70 catch (error) {
71 console.error(`Error: ${error.message}`);
72 program.outputHelp();
73 process.exit(1);
74 }
75}));
76program
77 .command('number <index>')
78 .description('Save comic by index number')
79 .action((index, command) => __awaiter(void 0, void 0, void 0, function* () {
80 let parsedIndex;
81 try {
82 parsedIndex = parseInt(index, 10);
83 }
84 catch (error) {
85 throw new Error('Invalid number specified.');
86 }
87 try {
88 const [resolvedPath, xkcd] = yield init(command.parent.output);
89 const imageData = yield xkcd.api.getByIndex(parsedIndex, { withData: true });
90 yield save(resolvedPath, imageData);
91 }
92 catch (error) {
93 console.error(`Error: ${error.message}`);
94 program.outputHelp();
95 process.exit(1);
96 }
97}));
98program.parse(process.argv);
99if (!process.argv.slice(2).length) {
100 program.outputHelp();
101 process.exit(1);
102}
103//# sourceMappingURL=cli.js.map
\No newline at end of file