From 8960e8bc994b63cee01ba39fe2b7e1a5c36002a8 Mon Sep 17 00:00:00 2001 From: yumetodo Date: Sat, 7 Mar 2020 18:41:27 +0900 Subject: [PATCH] feat: now you can specify userid, output path --- bin/index.js | 36 ++++++++++++++++++++++-------------- package-lock.json | 5 +++++ package.json | 1 + 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/bin/index.js b/bin/index.js index f937ac9..e89eaed 100644 --- a/bin/index.js +++ b/bin/index.js @@ -2,8 +2,9 @@ "use strict"; +const path = require("path"); const mkdirp = require("mkdirp"); - +const program = require("commander"); const QiitaApi = require("./qiita"); const Item = require("./item.js"); // eslint-disable-next-line no-unused-vars @@ -11,27 +12,29 @@ const Comment = require("./comment.js"); const ImageManager = require("./image-manager.js"); const token = process.env.QIITA_ACCESS_TOKEN; -const imageDirectoryPath = "img"; -const rootItemPath = "items"; +const imagePathBase = "img"; +const itemPathBase = "items"; const relativeCommentPath = "comments"; const relativePathFromItemFileToRootDirectory = "../../"; const relativePathFromCommentFileToRootDirectory = "../../../.../"; -const main = async () => { +/** + * @param {string|undefined} userId + * @param {string|undefined} output + */ +const main = async (userId, output) => { + const imagePath = output == null ? imagePathBase : path.join(output, imagePathBase); + const itemPath = output == null ? itemPathBase : path.join(output, itemPathBase); console.log("info: Requesting items..."); const qiita = new QiitaApi(token, true); - const items = await qiita - .GetAllAuthenticatedUserItems() - .then(rawItems => rawItems.map(i => new Item(i))) - .catch(er => { - throw er; - }); + const rawItems = await (userId == null ? qiita.GetAllAuthenticatedUserItems() : qiita.GetAllUserItems(userId)); + const items = rawItems.map(i => new Item(i)); console.log(`info: ${items.length} items found.`); console.log("info: creating image save directory..."); - await mkdirp(imageDirectoryPath); + await mkdirp(imagePath); console.log("info: created."); - const imageManager = new ImageManager(imageDirectoryPath); + const imageManager = new ImageManager(imagePath); console.log("info: Requesting comments/images..."); for (const i of items) { // eslint-disable-next-line no-await-in-loop @@ -55,14 +58,19 @@ const main = async () => { console.log("info: Replace finished."); console.log("info: Writing items/comments..."); - await Promise.all(items.map(i => i.WriteFiles(rootItemPath, relativeCommentPath))); + await Promise.all(items.map(i => i.WriteFiles(itemPath, relativeCommentPath))); console.log("write finished."); }; if (token == null) { console.error("Fail to find QIITA_ACCESS_TOKEN env"); } else { - main().catch(er => { + program + .version("1.3.1") + .option("-u, --user-id ", "user id") + .option("-o, --output ", "Write output to instead of current directory.") + .parse(process.argv); + main(program.userId, program.output).catch(er => { console.error(er.stack, er); }); } diff --git a/package-lock.json b/package-lock.json index 00e522f..fa89771 100644 --- a/package-lock.json +++ b/package-lock.json @@ -812,6 +812,11 @@ "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", "dev": true }, + "commander": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", + "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==" + }, "common-path-prefix": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/common-path-prefix/-/common-path-prefix-3.0.0.tgz", diff --git a/package.json b/package.json index 2832238..257bc12 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "prettier": "^1.19.1" }, "dependencies": { + "commander": "^4.1.1", "fs-extra": "^8.1.0", "gaxios": "^2.3.2", "mime": "^2.4.4",