diff --git a/bin/about.js b/bin/about.js index 47c5724..7fb0eb5 100644 --- a/bin/about.js +++ b/bin/about.js @@ -1,32 +1,23 @@ #!/usr/bin/env node + 'use strict'; const meow = require('meow'); -const chalk = require('chalk'); -const error = chalk.bold.red; -console.log(error('Error!')); +const configLocator = require('../lib/config/locator'); const cli = meow(` Usage - $ foo + $ about Options - --rainbow, -r Include a rainbow + --config, -c Specify config file Examples - $ foo unicorns --rainbow - 🌈 unicorns 🌈 + $ about --config ./example/config.yaml + $ about -c ./example/config.yaml `, { alias: { - r: 'rainbow' + c: 'config' } }); -/* - { - input: ['unicorns'], - flags: {rainbow: true}, - ... - } - */ -// node about.js unicorns --rainbow -// unicorns { rainbow: true, r: true } -console.log(cli.input[0], cli.flags); \ No newline at end of file +console.log(cli.input[0], cli.flags); +configLocator.detectConfigFile(cli.flags); \ No newline at end of file diff --git a/lib/config/locator.js b/lib/config/locator.js new file mode 100644 index 0000000..fe850f4 --- /dev/null +++ b/lib/config/locator.js @@ -0,0 +1,21 @@ +/** + * Created by at15 on 2016/9/6. + */ +const logger = require('../logger'); +const fsUtil = require('../util/fs'); + +function detectConfigFile(flags) { + if (typeof flags.config === 'string') { + let configFile = flags.config; + logger.debug(`config file set as ${configFile}`); + // it must exist, if not we fallback to the default one and throw a warning + if (!fsUtil.fileExists(configFile)) { + logger.warn(`config file can't be found ${configFile}`); + } + } + logger.debug('using default config file'); +} + +module.exports = { + detectConfigFile +}; \ No newline at end of file diff --git a/lib/logger.js b/lib/logger.js new file mode 100644 index 0000000..b637e78 --- /dev/null +++ b/lib/logger.js @@ -0,0 +1,22 @@ +/** + * Created by at15 on 2016/9/6. + * + */ + +const winston = require('winston'); + +let logger = new (winston.Logger)({ + transports: [ + new (winston.transports.Console)({ level: 'debug', colorize: true }) + ] +}); + +module.exports = { + // FIXME: got no type hint for the function parameters + // TODO: add color using chalk, don't know if winston has color by default + // winston is using colors instead of chalk (colors seems to have problems which are solved by chalk) + debug: logger.debug, + info: logger.info, + warn: logger.warn, + error: logger.error +}; \ No newline at end of file diff --git a/lib/util/fs.js b/lib/util/fs.js new file mode 100644 index 0000000..41ef84a --- /dev/null +++ b/lib/util/fs.js @@ -0,0 +1,18 @@ +/** + * Created by at15 on 2016/9/6. + */ +var fs = require('fs'); + +/** @param path {string} */ +function fileExists(path) { + try { + fs.accessSync(path, fs.constants.F_OK) + return true; + } catch (e) { + return false; + } +} + +module.exports = { + fileExists +}; \ No newline at end of file diff --git a/package.json b/package.json index 86c7e18..1b743a8 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,9 @@ "homepage": "https://github.com/tongquhq/about#readme", "dependencies": { "chalk": "^1.1.3", - "meow": "^3.7.0" + "js-yaml": "^3.6.1", + "meow": "^3.7.0", + "winston": "^2.2.0" }, "devDependencies": { "chai": "3.5.0",