Refactoring the generator.
Refactor the generator. Refactor the code for generator phase ii. Refactor the code for generator to support more features.
This commit is contained in:
Родитель
49007c7311
Коммит
8d53cfe04b
|
@ -1,39 +1,107 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
const _ = require('lodash');
|
||||||
|
const extend = _.merge;
|
||||||
const Generator = require('yeoman-generator');
|
const Generator = require('yeoman-generator');
|
||||||
|
const askName = require('inquirer-npm-name');
|
||||||
|
|
||||||
module.exports = class extends Generator {
|
module.exports = class extends Generator {
|
||||||
constructor(args, opts) {
|
constructor(args, options) {
|
||||||
super(args, opts);
|
super(args, options);
|
||||||
|
|
||||||
this.name = '';
|
this.option('name', {
|
||||||
this.moduleType = '';
|
type: String,
|
||||||
|
required: false,
|
||||||
|
desc: 'Module project name'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
initializing() {
|
initializing() {
|
||||||
// this.composeWith(require.resolve('../js'));
|
this.props = {
|
||||||
// this.composeWith(require.resolve('../dotnet'));
|
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() {
|
prompting() {
|
||||||
return this.prompt([{
|
return this._askForProjectName()
|
||||||
type: 'list',
|
.then(this._askFor.bind(this));
|
||||||
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;
|
|
||||||
|
|
||||||
if (this.moduleType == 'node') {
|
configuring() {
|
||||||
this.composeWith(require.resolve('../node'));
|
}
|
||||||
} else if (this.moduleType == 'net') {
|
|
||||||
this.composeWith(require.resolve('../net'));
|
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() {
|
writing() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conflicts() {
|
||||||
|
}
|
||||||
|
|
||||||
|
install() {
|
||||||
|
}
|
||||||
|
|
||||||
|
end() {
|
||||||
|
this.log('Thanks for using module generator for azure iot gateway.');
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -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';
|
'use strict';
|
||||||
|
const _ = require('lodash');
|
||||||
|
const extend = _.merge;
|
||||||
const Generator = require('yeoman-generator');
|
const Generator = require('yeoman-generator');
|
||||||
|
|
||||||
module.exports = class extends Generator {
|
module.exports = class extends Generator {
|
||||||
constructor(args, opts) {
|
constructor(args, options) {
|
||||||
super(args, opts);
|
super(args, options);
|
||||||
|
|
||||||
this.name = '';
|
this.option('name', {
|
||||||
this.version = '';
|
type: String,
|
||||||
this.desc = '';
|
required: true,
|
||||||
this.author = '';
|
desc: 'Module project name'
|
||||||
this.license = '';
|
});
|
||||||
|
|
||||||
|
this.option('description', {
|
||||||
|
type: String,
|
||||||
|
required: false,
|
||||||
|
desc: 'Description of module project.'
|
||||||
|
});
|
||||||
|
|
||||||
|
this.option('authorName', {
|
||||||
|
type: String,
|
||||||
|
required: false,
|
||||||
|
desc: 'Author\'s Name'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
initializing() {
|
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() {
|
prompting() {
|
||||||
return this.prompt([{
|
const prompts = [{
|
||||||
type: 'intput',
|
type: 'input',
|
||||||
name: 'name',
|
name: 'keywords',
|
||||||
message: 'Name of your Azure IoT Gateway module project',
|
message: 'Package keywords (comma to split)',
|
||||||
default: this.appname // default to current folder name
|
when: !this.props.keywords || this.props.keywords.length == 0,
|
||||||
}, {
|
filter(words) {
|
||||||
type: 'intput',
|
return words.split(/\s*,\s*/g);
|
||||||
name: 'version',
|
}
|
||||||
message: 'Version of js module project.',
|
}];
|
||||||
default: '0.1.0' // default to current folder name
|
|
||||||
}, {
|
return this.prompt(prompts).then(props => {
|
||||||
type: 'intput',
|
this.props = extend(this.props, props);
|
||||||
name: 'desc',
|
});
|
||||||
message: 'Description of your project.',
|
}
|
||||||
default: this.appname // default to current folder name
|
|
||||||
}, {
|
configuring() {}
|
||||||
type: 'intput',
|
|
||||||
name: 'author',
|
default () {
|
||||||
message: 'Author name of your project.',
|
this.composeWith(require.resolve('generator-license/app'), {
|
||||||
default: this.appname // default to current folder name
|
name: this.props.authorName
|
||||||
}, {
|
});
|
||||||
type: 'intput',
|
|
||||||
name: 'license',
|
this.composeWith(require.resolve('../shared'));
|
||||||
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;
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writing() {
|
writing() {
|
||||||
this._copyStaticFiles();
|
this._generateDynamicFiles();
|
||||||
|
this._generateStaticFiles();
|
||||||
this._copyDynamicFiles();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private Methods
|
conflicts() {}
|
||||||
_copyStaticFiles() {
|
|
||||||
|
install() {}
|
||||||
|
|
||||||
|
end() {
|
||||||
|
this.log('Thanks for using node generator for azure iot gateway.');
|
||||||
|
}
|
||||||
|
|
||||||
|
_generateStaticFiles() {
|
||||||
this.fs.copy(
|
this.fs.copy(
|
||||||
this.templatePath('modules/printer.js'),
|
this.templatePath('modules/printer.js'),
|
||||||
this.destinationPath('modules/printer.js')
|
this.destinationPath('modules/printer.js')
|
||||||
|
@ -86,24 +107,32 @@ module.exports = class extends Generator {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_copyDynamicFiles() {
|
_generateDynamicFiles() {
|
||||||
this.log('this.author: ' + this.author);
|
// package.json
|
||||||
|
this.fs.writeJSON(this.destinationPath('package.json'), {
|
||||||
this.fs.copyTpl(
|
name: this.props.name,
|
||||||
this.templatePath('package.json'),
|
version: this.props.version,
|
||||||
this.destinationPath('package.json'), {
|
description: this.props.description,
|
||||||
name: this.name,
|
author: {
|
||||||
version: this.version,
|
name: this.props.authorName
|
||||||
desc: this.desc,
|
},
|
||||||
author: this.author,
|
license: this.props.license,
|
||||||
license: this.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.fs.copyTpl(
|
||||||
this.templatePath('README.md'),
|
this.templatePath('README.md'),
|
||||||
this.destinationPath('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) {
|
constructor(args, opts) {
|
||||||
super(args, opts);
|
super(args, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
prompting() {
|
|
||||||
}
|
|
||||||
|
|
||||||
writing() {
|
writing() {
|
||||||
this.fs.copy(
|
this.fs.copy(
|
||||||
|
|
3
index.js
3
index.js
|
@ -3,5 +3,6 @@
|
||||||
module.exports = {
|
module.exports = {
|
||||||
app: require.resolve('./generators/app'),
|
app: require.resolve('./generators/app'),
|
||||||
node: require.resolve('./generators/node'),
|
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",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"generator-license": "^5.1.0",
|
||||||
|
"inquirer-npm-name": "^2.0.0",
|
||||||
|
"lodash": "^4.17.4",
|
||||||
"yeoman-generator": "^1.0.0"
|
"yeoman-generator": "^1.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче