-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[config] Add parser for yaml files #4
- only simple parse is support, $ref in current document and cross document is not supported yet
- Loading branch information
Showing
8 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mail: [email protected] | ||
github: swaylq | ||
twitter: swaylq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters