-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use yaml to store local config
- Loading branch information
Showing
5 changed files
with
65 additions
and
0 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,8 @@ | ||
'use strict'; | ||
|
||
// Feflow default configs, developer will receive inquiry when first time use it. | ||
// Develop can be modify it later by $ feflow config <key> <value> | ||
|
||
module.exports = { | ||
npm: 'npm' // Set default npm tool, it can be npm, cnpm, tnpm. | ||
}; |
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,39 @@ | ||
'use strict'; | ||
|
||
const yaml = require('js-yaml'); | ||
const fs = require('hexo-fs'); | ||
|
||
// Get document, or throw exception on error | ||
function parseYaml(path) { | ||
let config; | ||
|
||
if (fs.existsSync(path)) { | ||
try { | ||
config = yaml.safeLoad(fs.readFileSync(path)); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
|
||
return config; | ||
} | ||
|
||
|
||
function safeDump(obj, path) { | ||
let doc; | ||
try { | ||
doc = yaml.safeDump(obj, { | ||
'styles': { | ||
'!!null': 'canonical' // dump null as ~ | ||
}, | ||
'sortKeys': true // sort object keys | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
|
||
return fs.writeFileSync(path, doc, 'utf-8'); | ||
} | ||
|
||
exports.parseYaml = parseYaml; | ||
exports.safeDump = safeDump; |
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