diff --git a/generators/app/index.js b/generators/app/index.js index d711dd7..b66fd31 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -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.'); + } }; diff --git a/generators/java/index.js b/generators/java/index.js new file mode 100644 index 0000000..844c846 --- /dev/null +++ b/generators/java/index.js @@ -0,0 +1,15 @@ +'use strict'; + +const Generator = require('yeoman-generator'); + +module.exports = class extends Generator { + constructor(args, opts) { + super(args, opts); + } + + prompting() { + } + + writing() { + } +}; diff --git a/generators/node/index.js b/generators/node/index.js index 4984027..67e5202 100644 --- a/generators/node/index.js +++ b/generators/node/index.js @@ -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 } ); } diff --git a/generators/node/templates/package.json b/generators/node/templates/package.json deleted file mode 100644 index a359b37..0000000 --- a/generators/node/templates/package.json +++ /dev/null @@ -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" - } -} diff --git a/generators/shared/index.js b/generators/shared/index.js index 1e348f2..44f653c 100644 --- a/generators/shared/index.js +++ b/generators/shared/index.js @@ -6,9 +6,6 @@ module.exports = class extends Generator { constructor(args, opts) { super(args, opts); } - - prompting() { - } writing() { this.fs.copy( diff --git a/index.js b/index.js index d67ca6c..100e568 100644 --- a/index.js +++ b/index.js @@ -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') }; diff --git a/package.json b/package.json index 75fb364..5bf2adc 100644 --- a/package.json +++ b/package.json @@ -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" } }