Skip to content

Commit

Permalink
[lib] use winston for logger
Browse files Browse the repository at this point in the history
- related to #3 #2 give up on making code completion work, at least
  project level module got proper hint
- enable color by default
  • Loading branch information
at15 committed Sep 7, 2016
1 parent d2d64f0 commit 38eb8a3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
27 changes: 9 additions & 18 deletions bin/about.js
Original file line number Diff line number Diff line change
@@ -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 <input>
$ about <input>
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);
console.log(cli.input[0], cli.flags);
configLocator.detectConfigFile(cli.flags);
21 changes: 21 additions & 0 deletions lib/config/locator.js
Original file line number Diff line number Diff line change
@@ -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
};
22 changes: 22 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -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
};
18 changes: 18 additions & 0 deletions lib/util/fs.js
Original file line number Diff line number Diff line change
@@ -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
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 38eb8a3

Please sign in to comment.