Skip to content

Commit

Permalink
[config] Add parser for yaml files #4
Browse files Browse the repository at this point in the history
- only simple parse is support, $ref in current document and cross
  document is not supported yet
  • Loading branch information
at15 committed Sep 7, 2016
1 parent cb6b63b commit db7136c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 6 deletions.
14 changes: 14 additions & 0 deletions example/data/people.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
at15:
mail: [email protected]
github: at15
twitter: at1510086
arrowrowe:
mail: [email protected]
github: arrowrowe
twitter: arrowrowe
ComMouse:
mail: [email protected]
github: ComMouse
# TODO: use json pointer ie: https://github.com/whitlockjc/json-refs
sway:
- $ref: 'sway.yml'
3 changes: 3 additions & 0 deletions example/data/sway.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mail: [email protected]
github: swaylq
twitter: swaylq
1 change: 1 addition & 0 deletions lib/config/locator.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const logger = require('../logger');
const fsUtil = require('../util/fs');
const DEFAULT_CONFIG_FILE = '.about.yml';

// TODO: use sinon for test, I guess stub can handle the file problem
function detectConfigFile(flags) {
if (typeof flags.config === 'string') {
let configFile = flags.config;
Expand Down
59 changes: 59 additions & 0 deletions lib/config/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Created by at15 on 2016/9/6.
*
* Enhanced yaml parser that support $ref
*/
'use strict';
const logger = require('../logger');
const fsUtil = require('../util/fs');
const yaml = require('js-yaml');

/**
* @property files {Array}
* @property parsed {Object}
*/
class Parser {
constructor() {
// all the files this parser has loaded
// TODO: may use object for faster lookup
this.files = [];
// Set of file -> parsed pairs
this.parsed = {};
this.mainEntryFile = '';
}

/** @param filePath {string} */
addFile(filePath) {
if (!fsUtil.fileExists(filePath)) {
logger.warn('file not found', {file: filePath});
return false;
}
// TODO: check if parse fail
this.parsed[filePath] = Parser.shallowParse(filePath);
return true;
}

/**
* Use specified file as main config entry, call this before call entry
*
* @param filePath {string}
*/
setMainEntry(filePath) {

}

/**
* shallowParse does not resolve $ref
* It doesn't check file existence and will return empty object when fail
*/
static shallowParse(filePath) {
try {
return yaml.safeLoad(fsUtil.readAsString(filePath));
} catch (e) {
logger.error('error when parse file', e);
return {};
}
}
}

module.exports = Parser;
8 changes: 7 additions & 1 deletion lib/util/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ function fileExists(path) {
}
}

/** @param path {string} */
function readAsString(path) {
return fs.readFileSync(path, 'utf8');
}

module.exports = {
fileExists
fileExists,
readAsString
};
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "About and Blog page generator for team and individual",
"main": "index.js",
"scripts": {
"test": "mocha test/*-spec.js",
"cover": "istanbul cover node_modules/mocha/bin/_mocha test/*-spec.js -- -R spec && istanbul check-coverage",
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha test/*-spec.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
"test": "mocha test/**/*-spec.js",
"cover": "istanbul cover node_modules/mocha/bin/_mocha test/**/*-spec.js -- -R spec && istanbul check-coverage",
"coveralls": "istanbul cover ./node_modules/mocha/bin/_mocha test/**/*-spec.js --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"repository": {
"type": "git",
Expand All @@ -26,6 +26,7 @@
"dependencies": {
"chalk": "^1.1.3",
"js-yaml": "^3.6.1",
"lodash": "^4.15.0",
"meow": "^3.7.0",
"winston": "^2.2.0"
},
Expand Down
34 changes: 34 additions & 0 deletions test/config/parser-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Created by at15 on 2016/9/7.
*/
'use strict';
const expect = require('chai').expect;
const Parser = require('../../lib/config/parser');

describe('Parser', () => {

it('can load yaml file', () => {
expect(Parser.shallowParse('example/data/sway.yml')).to.eql({
mail: '[email protected]',
github: 'swaylq',
twitter: 'swaylq'
});
});

it('can add file', () => {
let parser = new Parser();
let file = 'example/data/people.yml';
parser.addFile(file);
expect(parser.parsed).to.have.ownProperty(file);
});

it('can add several files', () => {
let parser = new Parser();
let file1 = 'example/data/people.yml';
let file2 = 'example/data/sway.yml';
parser.addFile(file1);
parser.addFile((file2));
expect(parser.parsed).to.have.all.keys(file1, file2);
});

});
4 changes: 2 additions & 2 deletions test/foo-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Created by at15 on 2016/9/5.
*/

var expect = require('chai').expect;
var foo = require('../lib/foo');
const expect = require('chai').expect;
const foo = require('../lib/foo');

describe('foo', function () {

Expand Down

0 comments on commit db7136c

Please sign in to comment.