Skip to content

Commit

Permalink
feat: use yaml to store local config
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed Sep 9, 2017
1 parent 499358d commit c7d3241
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/core/defaultConfig.js
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.
};
13 changes: 13 additions & 0 deletions lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const chalk = require('chalk');
const fs = require('hexo-fs');
const logger = require('./logger');
const Command = require('./command');
const utils = require('../utils');
const pkg = require('../../package.json');
const sep = pathFn.sep;

Expand All @@ -27,12 +28,16 @@ class Feflow {
args = args || {};

const base = pathFn.join(osenv.home(), './.feflow');
const rcPath = pathFn.join(base, '.feflowrc.yml');

this.version = pkg.version;
this.baseDir = base + sep;
this.rcPath = rcPath;
this.pkgPath = pathFn.join(base, 'package.json');
this.pluginDir = pathFn.join(base, 'node_modules') + sep;

this.config = utils.parseYaml(rcPath); // Read feflow local config.

this.log = logger({
debug: Boolean(args.debug)
});
Expand Down Expand Up @@ -65,6 +70,13 @@ class Feflow {

}

/**
* Init feflow home dir, package.json and .feflowrc.yml.
*/
initHome() {
const { baseDir, pkgPath, rcPath } = this;
}

/**
* Call a command in console.
* @param name
Expand Down Expand Up @@ -127,6 +139,7 @@ class Feflow {
return fn(module.exports, require, module, path, pathFn.dirname(path), self);
}).asCallback(callback);
}

}

module.exports = Feflow;
4 changes: 4 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
exports.spawnCommand = require('./spawnCommand');

exports.Loading = require('./loading');

exports.parseYaml = require('./yaml').parseYaml;

exports.safeDump = require('./yaml').safeDump;
39 changes: 39 additions & 0 deletions lib/utils/yaml.js
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;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"conventional-changelog-cli": "^1.2.0",
"husky": "^0.13.1",
"istanbul": "^0.4.5",
"js-yaml": "^3.9.1",
"mocha": "^3.5.0",
"sinon": "^3.2.1",
"validate-commit-msg": "^2.11.1"
Expand Down

0 comments on commit c7d3241

Please sign in to comment.