diff --git a/.gitignore b/.gitignore index f98bced..49e73ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules/ *.bcup +*.git-label-maker-config diff --git a/bin/index.js b/bin/index.js index a36c9b5..41741fb 100755 --- a/bin/index.js +++ b/bin/index.js @@ -4,6 +4,7 @@ // EXTERNAL DEPENDENCIES const fs = require("fs"), iq = require("inquirer"), + pathTool = require("path"), gitLabel = require("git-label"); // UTILS ARE STANDALONE METHODS WITH NO DEPENDENCIES const alertDeletes = require("./utils/alertDeletes"), @@ -103,6 +104,20 @@ const addFromPackage = (repo, token, path) => { .catch(console.warn); }; +const setGlobalPackage = (path, token) => { + fs.writeFile('.git-label-maker-config', pathTool.resolve(__dirname, '..', path), (err) => { + if (err) throw err; + console.log("\n"); + console.log("Saved " + path + " as global. When you specify no file when adding from a package, the global will be used instead."); + console.log("\n"); + gitLabelmaker(token); + }); +}; + +const getGlobalPackage = () => { + return fs.readFileSync('.git-label-maker-config', 'utf-8'); +} + // removeLabels function const removeLabels = (repo, token, answers) => { // Tell the user what they're about to lose @@ -145,7 +160,28 @@ const handleMainPrompts = (repo, token, ans) => { validate: validateAddPackages }]) .then((ans)=>{ - return addFromPackage( repo, token, ans.path ); + if (ans.path !== "") { + return addFromPackage( repo, token, ans.path ); + } else { + try { + addFromPackage( repo, token, getGlobalPackage() ); + } catch (e) { + console.log("Either an incorrect global was used or no global package has been set yet."); + } + } + }) + .catch(console.warn); + break; + + case "add global package": + prompt([{ + name: "path", + type: "input", + message: "What is the pathname you want to use for your package? (Must be valid json)", + validate: validateAddPackages + }]) + .then((ans)=>{ + return setGlobalPackage( ans.path, token ); }) .catch(console.warn); break; diff --git a/bin/modules/validateAddPackages.js b/bin/modules/validateAddPackages.js index ea131cf..598a53c 100644 --- a/bin/modules/validateAddPackages.js +++ b/bin/modules/validateAddPackages.js @@ -13,6 +13,8 @@ const isJsonString = require("../utils/isJsonString"); module.exports = function (jsonPath) { // Declare function as asynchronous, and save the done callback let done = this.async(); + // this is clunky, but this way we can just use a default + if (jsonPath === "") done(true); try { if (jsonPath.indexOf(".json") < 0) { done("Not a JSON file"); diff --git a/bin/prompts/mainMenu.js b/bin/prompts/mainMenu.js index 9cbaa43..202e0a2 100644 --- a/bin/prompts/mainMenu.js +++ b/bin/prompts/mainMenu.js @@ -7,6 +7,6 @@ module.exports = [ type: "list", name: "main", message: "Welcome to git-labelmaker!\nWhat would you like to do?", - choices: [ "Add Custom Labels", "Add Labels From Package", "Create a Package From Labels", "Remove Labels", "Remove All Labels", "Reset Token", "Quit" ] + choices: [ "Add Custom Labels", "Add Labels From Package", "Add Global Package", "Create a Package From Labels", "Remove Labels", "Remove All Labels", "Reset Token", "Quit" ] } ]; diff --git a/bin/utils/banners.js b/bin/utils/banners.js index e7584fe..23bdba8 100644 --- a/bin/utils/banners.js +++ b/bin/utils/banners.js @@ -14,6 +14,7 @@ module.exports = { welcome: printBanner(" Welcome to git-labelmaker "), addCustom: printBanner(" Adding Custom Labels "), addFromPackage: printBanner(" Adding Labels From Package "), + addGlobalPackage: printBanner(" Add Global Package "), createPkgFromLabels: printBanner(" Creating Package From Labels "), removeLabels: printBanner(" Removing Labels "), resetToken: printBanner(" Resetting Token "),