Skip to content

Commit

Permalink
feat: support active subgenerator (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikewu authored and cpselvis committed May 22, 2019
1 parent 49777ab commit 234e6bc
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ class Feflow {
require('../internal/eject')(this);
require('../internal/deploy')(this);
require('../internal/clean')(this);

require('../internal/create')(this);

// Init client and load external plugins
return Promise.each([
'initClient',
Expand Down
70 changes: 70 additions & 0 deletions lib/internal/create/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const pathFn = require('path');
const fs = require('hexo-fs');
const yeoman = require('yeoman-environment');
const yeomanEnv = yeoman.createEnv();

class Creator {

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

init() {
const {
args,
pluginDir,
log
} = this.ctx;
const generator = args.generator;
const subgenerator = args[''][1];
if (!subgenerator || !generator) {
log.warn(
'请输入子脚手架及脚手架名称 eg:feflow create <subgenerator> --generator <generator>'
);
return;
}
const dirPath = pathFn.join(pluginDir, generator, 'generators');
if (fs.existsSync(dirPath)) {
const files = fs.readdirSync(dirPath);
let selectedSub = "";
files.forEach((filename) => {
if (filename == subgenerator) {
selectedSub = subgenerator
}
});
if (selectedSub) {
this.run(generator, subgenerator)
} else {
log.warn(
'请确认输入了正确的子脚手架名称'
);
}
} else {
log.warn(
'请确认输入了正确的脚手架名称'
);
}
}


run(generator, subgenerator) {
const ctx = this.ctx;
const pluginDir = ctx.pluginDir;
let path = pathFn.join(pluginDir, generator, `${subgenerator}/index.js`);
if (!fs.existsSync(path)) {
path = pathFn.join(pluginDir, generator, 'generators', `${subgenerator}/index.js`);
}
yeomanEnv.register(require.resolve(path), generator);
yeomanEnv.run(generator, this.args, err => {});
}

}

module.exports = function (args) {
const creator = new Creator(this, args);

return creator.init();
};
8 changes: 8 additions & 0 deletions lib/internal/create/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = function (ctx) {

const cmd = ctx.cmd;

cmd.register('create', 'active a subgenerator.', {}, require('./create'));
};

0 comments on commit 234e6bc

Please sign in to comment.