Refactor the generator.

Refactor the code for generator phase ii.

Refactor the code for generator to support more features.
This commit is contained in:
Su Shi 2017-03-24 18:19:02 -07:00
Родитель 49007c7311
Коммит 8d53cfe04b
7 изменённых файлов: 200 добавлений и 103 удалений

Просмотреть файл

@ -1,39 +1,107 @@
'use strict';
const _ = require('lodash');
const extend = _.merge;
const Generator = require('yeoman-generator');
const askName = require('inquirer-npm-name');
module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);
constructor(args, options) {
super(args, options);
this.name = '';
this.moduleType = '';
this.option('name', {
type: String,
required: false,
desc: 'Module project name'
});
}
initializing() {
// this.composeWith(require.resolve('../js'));
// this.composeWith(require.resolve('../dotnet'));
this.props = {
name: this.options.name || this.appname
};
}
_askForProjectName() {
return askName({
type: 'input',
name: 'name',
message: 'Module project Name',
filter: _.kebabCase,
validate(str) {
return str.length > 0;
}
}, this).then(answer => {
this.props.name = answer.name || this.props.name;
});
}
_askFor() {
const prompts = [{
type: 'input',
name: 'description',
message: 'Description of module project.',
when: !this.props.description
}, {
type: 'input',
name: 'authorName',
message: 'Author\'s Name',
when: !this.props.authorName,
store: true
}, {
type: 'list',
name: 'moduleType',
message: 'Choose the module type(language) for your project.',
choices: ['node', 'net', 'java'],
default: 'node',
}];
return this.prompt(prompts).then(props => {
this.props = extend(this.props, props);
})
}
prompting() {
return this.prompt([{
type: 'list',
name: 'moduleType',
message: 'Choose the language for your project',
choices: ['node', 'net', 'java'],
default: 'node' // default to current folder name
}]).then((answers) => {
this.name = answers.name;
this.moduleType = answers.moduleType;
return this._askForProjectName()
.then(this._askFor.bind(this));
}
if (this.moduleType == 'node') {
this.composeWith(require.resolve('../node'));
} else if (this.moduleType == 'net') {
this.composeWith(require.resolve('../net'));
}
});
configuring() {
}
default() {
switch(this.props.moduleType) {
case 'node':
this.composeWith(require.resolve('../node'), {
name: this.props.name,
description: this.props.description,
license: this.props.license,
authorName: this.props.authorName
});
break;
case 'net':
throw '.Net module type is not supported yet.'
break;
case 'java':
throw 'Java module type is not supported yet.'
break;
default:
throw 'Invalid module type.';
}
}
writing() {
}
conflicts() {
}
install() {
}
end() {
this.log('Thanks for using module generator for azure iot gateway.');
}
};

15
generators/java/index.js Normal file
Просмотреть файл

@ -0,0 +1,15 @@
'use strict';
const Generator = require('yeoman-generator');
module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);
}
prompting() {
}
writing() {
}
};

Просмотреть файл

@ -1,65 +1,86 @@
'use strict';
const _ = require('lodash');
const extend = _.merge;
const Generator = require('yeoman-generator');
module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);
constructor(args, options) {
super(args, options);
this.name = '';
this.version = '';
this.desc = '';
this.author = '';
this.license = '';
this.option('name', {
type: String,
required: true,
desc: 'Module project name'
});
this.option('description', {
type: String,
required: false,
desc: 'Description of module project.'
});
this.option('authorName', {
type: String,
required: false,
desc: 'Author\'s Name'
});
}
initializing() {
this.composeWith(require.resolve('../shared'));
// Refresh the current package content to latest one.
const currentPkg = this.fs.readJSON(this.destinationPath('package.json'), {});
this.props = {
name: currentPkg.name || _.kebabCase(this.options.name),
description: currentPkg.descrption || this.options.description,
version: currentPkg.version || '0.0.0',
authorName: this.options.authorName,
keywords: currentPkg.keywords || [],
license: currentPkg.license,
devDependencies: currentPkg.devDependencies || {}
};
}
prompting() {
return this.prompt([{
type: 'intput',
name: 'name',
message: 'Name of your Azure IoT Gateway module project',
default: this.appname // default to current folder name
}, {
type: 'intput',
name: 'version',
message: 'Version of js module project.',
default: '0.1.0' // default to current folder name
}, {
type: 'intput',
name: 'desc',
message: 'Description of your project.',
default: this.appname // default to current folder name
}, {
type: 'intput',
name: 'author',
message: 'Author name of your project.',
default: this.appname // default to current folder name
}, {
type: 'intput',
name: 'license',
message: 'License type(MIT/BSD) of your project.',
default: ''
}]).then((answers) => {
this.name = answers.name;
this.version = answers.version;
this.desc = answers.desc;
this.author = answers.author;
this.license = answers.license;
})
const prompts = [{
type: 'input',
name: 'keywords',
message: 'Package keywords (comma to split)',
when: !this.props.keywords || this.props.keywords.length == 0,
filter(words) {
return words.split(/\s*,\s*/g);
}
}];
return this.prompt(prompts).then(props => {
this.props = extend(this.props, props);
});
}
configuring() {}
default () {
this.composeWith(require.resolve('generator-license/app'), {
name: this.props.authorName
});
this.composeWith(require.resolve('../shared'));
}
writing() {
this._copyStaticFiles();
this._copyDynamicFiles();
this._generateDynamicFiles();
this._generateStaticFiles();
}
// Private Methods
_copyStaticFiles() {
conflicts() {}
install() {}
end() {
this.log('Thanks for using node generator for azure iot gateway.');
}
_generateStaticFiles() {
this.fs.copy(
this.templatePath('modules/printer.js'),
this.destinationPath('modules/printer.js')
@ -86,24 +107,32 @@ module.exports = class extends Generator {
);
}
_copyDynamicFiles() {
this.log('this.author: ' + this.author);
this.fs.copyTpl(
this.templatePath('package.json'),
this.destinationPath('package.json'), {
name: this.name,
version: this.version,
desc: this.desc,
author: this.author,
license: this.license
}
);
_generateDynamicFiles() {
// package.json
this.fs.writeJSON(this.destinationPath('package.json'), {
name: this.props.name,
version: this.props.version,
description: this.props.description,
author: {
name: this.props.authorName
},
license: this.props.license,
main: 'app.js',
scripts: {
start: 'node app.js'
},
keywords: _.uniq(this.props.keywords || []),
devDependencies: extend(
this.props.devDependencies, {
'azure-iot-gateway': '~1.0.0'
})
});
// README.md
this.fs.copyTpl(
this.templatePath('README.md'),
this.destinationPath('README.md'), {
name: this.name
name: this.props.name
}
);
}

Просмотреть файл

@ -1,16 +0,0 @@
{
"name": "<%= name %>",
"version": "<%= version %>",
"description": "<%= desc %>",
"main": "app.js",
"scripts": {
"start": "node app.js"
},
"author": "<%= author %>",
"license": "<%= license %>",
"dependencies": {
},
"devDependencies": {
"azure-iot-gateway": "~1.0.0"
}
}

Просмотреть файл

@ -6,9 +6,6 @@ module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts);
}
prompting() {
}
writing() {
this.fs.copy(

Просмотреть файл

@ -3,5 +3,6 @@
module.exports = {
app: require.resolve('./generators/app'),
node: require.resolve('./generators/node'),
net: require.resolve('./generators/net')
net: require.resolve('./generators/net'),
java: require.resolve('./generators/java')
};

Просмотреть файл

@ -20,6 +20,9 @@
},
"license": "MIT",
"dependencies": {
"generator-license": "^5.1.0",
"inquirer-npm-name": "^2.0.0",
"lodash": "^4.17.4",
"yeoman-generator": "^1.0.0"
}
}