UNPKG

@ffflorian/xkcdjs

Version:
136 lines (135 loc) 5.51 kB
#!/usr/bin/env node "use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 node_fs_1 = require("node:fs"); const path = __importStar(require("node:path")); const package_json_1 = require("../package.json"); const _1 = require("./"); function init() { return __awaiter(this, arguments, void 0, function* (dir = '.') { const resolvedPath = path.resolve(dir); try { yield node_fs_1.promises.access(resolvedPath, node_fs_1.constants.F_OK | node_fs_1.constants.R_OK); const xkcd = new _1.XKCD(); return [resolvedPath, xkcd]; } catch (_a) { throw new Error('The specified path does not exist or is not writable.'); } }); } function save(filePath, imageResult) { return __awaiter(this, void 0, void 0, function* () { const { data, num, safe_title } = imageResult; const extension = data.mimeType ? data.mimeType.replace('image/', '') : 'png'; const resolvedFilePath = path.resolve(filePath, `xkcd #${num} - ${safe_title}.${extension}`); yield node_fs_1.promises.writeFile(resolvedFilePath, data.data); console.info(`Saved image to "${resolvedFilePath}".`); }); } commander_1.program.on('command:*', () => commander_1.program.help()); commander_1.program .name(package_json_1.name.replace(/^@[^/]+\//, '')) .version(package_json_1.version, '-v, --version') .description(package_json_1.description) .option('-o, --output <dir>', 'Specify the output directory', path.resolve('.')); commander_1.program .command('latest') .description('Save the latest comic') .action((command) => __awaiter(void 0, void 0, void 0, function* () { try { const [resolvedPath, xkcd] = yield init(command.parent.output); const imageData = yield xkcd.api.getLatest({ withData: true }); yield save(resolvedPath, imageData); } catch (error) { console.error(`Error: ${error.message}`); commander_1.program.outputHelp(); process.exit(1); } })); commander_1.program .command('random') .description('Save a random comic') .action((command) => __awaiter(void 0, void 0, void 0, function* () { try { const [resolvedPath, xkcd] = yield init(command.parent.output); const imageData = yield xkcd.api.getRandom({ withData: true }); yield save(resolvedPath, imageData); } catch (error) { console.error(`Error: ${error.message}`); commander_1.program.outputHelp(); process.exit(1); } })); commander_1.program .command('number <index>') .description('Save comic by index number') .action((index, command) => __awaiter(void 0, void 0, void 0, function* () { let parsedIndex; try { parsedIndex = parseInt(index, 10); } catch (_a) { throw new Error('Invalid number specified.'); } try { const [resolvedPath, xkcd] = yield init(command.parent.output); const imageData = yield xkcd.api.getByIndex(parsedIndex, { withData: true }); yield save(resolvedPath, imageData); } catch (error) { console.error(`Error: ${error.message}`); commander_1.program.outputHelp(); process.exit(1); } })); commander_1.program.parse(process.argv); if (!process.argv.slice(2).length) { commander_1.program.outputHelp(); process.exit(1); }