Skip to content

Commit

Permalink
feat: init client before load external plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
cpselvis committed Sep 10, 2017
1 parent c7d3241 commit 2982f3d
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 31 deletions.
8 changes: 0 additions & 8 deletions lib/core/defaultConfig.js

This file was deleted.

3 changes: 2 additions & 1 deletion lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@ class Feflow {
require('../internal/generator')(this);
require('../internal/install')(this);

// Load external plugins
// Init client and load external plugins
return Promise.each([
'initClient',
'loadPlugins'
], function(name) {
return require('./' + name)(self);
Expand Down
69 changes: 69 additions & 0 deletions lib/core/initClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict';

const fs = require('hexo-fs');
const Promise = require('bluebird');

/**
* Init feflow client, including ~/.feflow, ~/.feflow/package.json, ~/.feflow/.feflowrc.yml
*/
class Client {

constructor(ctx) {
this.ctx = ctx;
}

initHome() {
const ctx = this.ctx;
const baseDir = ctx.baseDir;

return new Promise(function(resolve) {
if (fs.existsSync(baseDir) && fs.statSync(baseDir).isFile()) {
fs.unlinkSync(baseDir);
}

if (!fs.existsSync(baseDir)) {
fs.mkdirsSync(baseDir);
}
resolve(ctx);
});
}

initPkg() {
const ctx = this.ctx;
const pkgPath = ctx.pkgPath;

return new Promise(function(resolve) {
if (!fs.existsSync(pkgPath)) {
fs.writeFileSync(pkgPath, JSON.stringify({
"name": "feflow-home",
"version": "0.0.0",
"private": true
}, null, 4));
}
resolve(ctx);
});
}

initLocalRc() {
const ctx = this.ctx;
const rcPath = ctx.rcPath;

return new Promise(function(resolve) {
if (!fs.existsSync(rcPath)) {
console.log('rc file not exists');
}
resolve(ctx);
});
}
}


module.exports = function(ctx) {
const client = new Client(ctx);

return Promise.each([
client.initHome(),
client.initPkg(),
client.initLocalRc()
]);
};
2 changes: 1 addition & 1 deletion lib/core/loadPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ module.exports = function(ctx) {
return Promise.all([
plugin.loadModules()
]);
};
};
4 changes: 2 additions & 2 deletions lib/internal/install/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const Promise = require('bluebird');
const fs = require('hexo-fs');
const rp = require('request-promise');
const Table = require('easy-table');
const spawn = require('cross-spawn');
const Loading = require('../../utils/index').Loading;
const spawnCommand = require('../../utils/index').spawnCommand;

class Plugin {

Expand Down Expand Up @@ -76,7 +76,7 @@ class Plugin {
execNpmCommand(cmd, modules, where) {
return new Promise((resolve, reject) => {
const args = [cmd].concat(modules).concat('--color=always');
const tnpm = spawnCommand('tnpm', args, {cwd: where});
const tnpm = spawn('tnpm', args, {cwd: where});

let output = '';
tnpm.stdout.on('data', (data) => {
Expand Down
6 changes: 1 addition & 5 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
'use strict';

exports.spawnCommand = require('./spawnCommand');

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

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

exports.safeDump = require('./yaml').safeDump;
exports.safeDump = require('./yaml').safeDump;
13 changes: 0 additions & 13 deletions lib/utils/spawnCommand.js

This file was deleted.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"abbrev": "^1.1.0",
"bluebird": "^3.5.0",
"bunyan": "^1.8.12",
"chalk": "^2.0.1",
"chalk": "^2.1.0",
"co": "^4.6.0",
"cross-spawn": "^5.1.0",
"easy-table": "^1.1.0",
"figlet": "^1.2.0",
"hexo-fs": "^0.2.1",
Expand Down

0 comments on commit 2982f3d

Please sign in to comment.