diff --git a/generators/app/index.js b/generators/app/index.js index e0150d6..b89e235 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -56,7 +56,7 @@ module.exports = class extends Generator { this.log('Hi! I am your DatoCMS plugin generator!'); this.log('I will generate for you the structure of a Webpack project to start developing your shiny new DatoCMS plugin right away! Let\'s start, shall we? :)'); - this.answers = await this.prompt([ + const { packageName } = await this.prompt([ { type : 'input', name : 'packageName', @@ -69,11 +69,15 @@ module.exports = class extends Generator { return "You need to add a datocms-plugin- prefix!"; } } - }, + } + ]); + + this.answers = await this.prompt([ { type : 'input', name : 'pluginTitle', message : 'Please insert a human name for this plugin', + default: changeCase.titleCase(packageName.replace('datocms-plugin-', '')), validate: required, }, { @@ -93,42 +97,6 @@ module.exports = class extends Generator { ], validate: required, }, - { - type : 'input', - name : 'keywords', - message : 'Please add some tags to make the plugin more discoverable (comma separated)', - validate: required, - }, - { - type : 'input', - name : 'authorName', - message : 'What\'s your name?', - validate: required, - }, - { - type : 'input', - name : 'authorEmail', - message : 'What\'s your email?', - validate: required, - }, - { - type : 'checkbox', - name : 'fieldTypes', - message : 'Which kind of fields is this plugin compatible with?', - choices: - [ - { name: 'Boolean', value: 'boolean' }, - { name: 'Date', value: 'date' }, - { name: 'Date-time', value: 'date_time' }, - { name: 'Float', value: 'float' }, - { name: 'Integer', value: 'integer' }, - { name: 'String', value: 'string' }, - { name: 'Text', value: 'text' }, - { name: 'JSON', value: 'json' }, - { name: 'Color', value: 'color' }, - ], - validate: atLeastOne, - }, { type : 'checkbox', name : 'fieldTypes', @@ -148,12 +116,62 @@ module.exports = class extends Generator { validate: atLeastOne, }, { - type : 'confirm', - name : 'addToProject', - message : 'Would you like to add this plugin in development mode to one of your projects and start developing it right away?', + type : 'editor', + name : 'parameterDefinitions', + message : 'Please insert any configuration parameters this plugin requires (see https://www.datocms.com/docs/plugins/creating-a-new-plugin/#configuration-parameters)', + default : JSON.stringify({ global: [], instance: [] }, null, 2), + validate: (value) => { + try { + const defs = JSON.parse(value); + if (defs.instance && Array.isArray(defs.instance) && defs.global && Array.isArray(defs.global)) { + return true; + } + return 'Please insert a valid JSON object'; + } catch(e) { + return 'Not valid JSON'; + } + }, }, ]); + this.answers = Object.assign( + { packageName }, + this.answers, + await this.prompt([ + { + type : 'input', + name : 'keywords', + message : 'Please add some tags to make the plugin more discoverable (comma separated)', + validate: required, + default: [changeCase.paramCase(this.answers.pluginType)].concat(this.answers.fieldTypes.map(ft => `${changeCase.paramCase(ft)}-field` )).join(', '), + }, + { + type : 'input', + name : 'homepage', + message : 'Please add the homepage for this plugin', + validate: required, + default: `https://github.com/YOUR-USER/${packageName}` + }, + { + type : 'input', + name : 'authorName', + message : 'What\'s your name?', + validate: required, + }, + { + type : 'input', + name : 'authorEmail', + message : 'What\'s your email?', + validate: required, + }, + { + type : 'confirm', + name : 'addToProject', + message : 'Would you like to add this plugin in development mode to one of your projects and start developing it right away?', + }, + ]) + ); + if (this.answers.addToProject) { await this.prompt([ { @@ -179,8 +197,6 @@ module.exports = class extends Generator { ]); this.answers.site = answers2.site; - - console.log(this.answers.site); } } @@ -191,9 +207,11 @@ module.exports = class extends Generator { pluginType, description, authorName, + parameterDefinitions, authorEmail, keywords, - fieldTypes + fieldTypes, + homepage, } = this.answers; this.fs.copy( @@ -212,12 +230,13 @@ module.exports = class extends Generator { authorName, authorEmail, fieldTypes, - keywords: keywords.split(',').concat( - [ - "datocms", - "datocms-plugin" - ] - ) + homepage, + parameterDefinitions: JSON.parse(parameterDefinitions), + keywords: ['datocms', 'datocms-plugin'].concat( + [changeCase.paramCase(this.answers.pluginType)], + this.answers.fieldTypes.map(ft => `${changeCase.paramCase(ft)}-field`), + keywords.split(/\s*,\s*/) + ).filter((value, index, self) => self.indexOf(value) === index), } ); @@ -238,10 +257,7 @@ module.exports = class extends Generator { description: description, fieldTypes, pluginType, - parameterDefinitions: { - global: [], - instance: [], - }, + parameterDefinitions: JSON.parse(parameterDefinitions), url: `https://${packageName}.localtunnel.me/`, }); } diff --git a/generators/app/templates/README.md b/generators/app/templates/README.md index 4115baa..acbb799 100644 --- a/generators/app/templates/README.md +++ b/generators/app/templates/README.md @@ -19,7 +19,8 @@ Start the local development server with: ``` yarn start ``` -The plugin will be served from https://<%= packageName %>.localtunnel.me. Insert this URL as the plugin [Entry point URL](https://www.datocms.com/docs/plugins/creating-a-new-plugin/). + +The plugin will be served from [https://<%= packageName %>.localtunnel.me/](https://<%= packageName %>.localtunnel.me/). Insert this URL as the plugin [Entry point URL](https://www.datocms.com/docs/plugins/creating-a-new-plugin/). ## Publishing diff --git a/generators/app/templates/package.json b/generators/app/templates/package.json index 2221471..11ba98e 100644 --- a/generators/app/templates/package.json +++ b/generators/app/templates/package.json @@ -1,6 +1,6 @@ { "name": "<%= packageName %>", - "homepage": "https://github.com/YOUR-USER/YOUR-PLUGIN-REPO", + "homepage": "<%= homepage %>", "version": "0.0.1", "description": "<%= description %>", "main": "index.js", @@ -13,7 +13,7 @@ "test": "echo 1" }, "keywords": <%- JSON.stringify(keywords) %>, - "author": "<%= authorName %> <%= authorEmail %>", + "author": "<%= authorName %> <<%= authorEmail %>>", "license": "ISC", "datoCmsPlugin": { "title": "<%= pluginTitle %>", @@ -22,10 +22,7 @@ "entryPoint": "dist/index.html", "pluginType": "field_addon", "fieldTypes": <%- JSON.stringify(fieldTypes) %>, - "parameters": { - "global": [], - "instance": [] - } + "parameters": <%- JSON.stringify(parameterDefinitions) %> }, "devDependencies": { "@babel/cli": "^7.0.0-beta.54",