From dfa8649f34ae85aaab923427b82905e6b658b64d Mon Sep 17 00:00:00 2001 From: Jb Landry Date: Mon, 27 Mar 2017 10:17:40 -0400 Subject: [PATCH] Tasks management --- cli.sublime-project | 3 ++- index.js | 47 ++++++++++++++++++++++++++++++++++++++++++++- package.json | 5 +++-- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/cli.sublime-project b/cli.sublime-project index 1b62a1e..55e9b93 100644 --- a/cli.sublime-project +++ b/cli.sublime-project @@ -14,7 +14,8 @@ ".gitignore", ".npmignore", ".sublimelinterrc", - ".travis.yml" + ".travis.yml", + "license" ] } ] diff --git a/index.js b/index.js index e164a28..e4b8c9f 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ const echo = console.log; // eslint-disable-line no-console const os = require('os'); const path = require('path'); +const glob = require('glob'); const readPkgUp = require('read-pkg-up'); const chalk = require('chalk'); const pad = require('pad'); @@ -27,7 +28,11 @@ const STATIC = global.___AbsolunetCli___ ? global.___AbsolunetCli___ : global.__ commands: {}, taskWidth: {}, fullUsage: {}, - baseWidth: 0 + baseWidth: 0, + tasks: { + path: '', + list: [] + } }; @@ -152,6 +157,46 @@ module.exports = class Cli { + //-- List tasks files + static initTasksList(tasksPath) { + STATIC.tasks.path = tasksPath; + + const tasks = []; + + glob.sync(`${STATIC.tasks.path}/!(default).js`).forEach((task) => { + tasks.push(task.split(STATIC.tasks.path).slice(-1).pop().substring(1).slice(0, -3)); + }); + + STATIC.tasks.list = tasks; + } + + + //-- Route to good task + static tasksRouter(meowCli) { + const [task] = meowCli.input; + + if (task) { + if (STATIC.tasks.list.includes(task)) { + require(`${STATIC.tasks.path}/${task}`)(meowCli); // eslint-disable-line global-require + } else { + meowCli.showHelp(); + } + } else { + require(`${STATIC.tasks.path}/default`)(meowCli); // eslint-disable-line global-require + } + } + + + //-- Tasks + static get tasks() { + return STATIC.tasks.list; + } + + + + + + //-- Is root static isRoot() { const user = os.userInfo(); diff --git a/package.json b/package.json index cba398d..cd54366 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ - { +{ "name": "@absolunet/cli", "version": "0.0.2", "description": "CLI utilities", @@ -10,7 +10,7 @@ "repository": { "url": "git://github.com/absolunet/node-cli.git", "type":"git" }, "bugs": { "url": "https://github.com/absolunet/node-cli/issues" }, - "engines": { "node": ">= 7.7.3" }, + "engines": { "node": ">= 7.7.4" }, "scripts": { "test": "mocha" }, @@ -21,6 +21,7 @@ "dependencies": { "chalk": "^1.1.3", + "glob": "^7.1.1", "pad": "^1.1.0", "read-pkg-up": "^2.0.0" }