From 353252d68c8589a904f72b88ff033ca545ed3b51 Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Tue, 14 Feb 2017 01:18:07 +0100 Subject: [PATCH] Add basic plugin generator --- packages/strapi-generate-plugin/.editorconfig | 16 +++ packages/strapi-generate-plugin/.gitignore | 102 +++++++++++++++++ packages/strapi-generate-plugin/.npmignore | 103 ++++++++++++++++++ packages/strapi-generate-plugin/LICENSE.md | 7 ++ packages/strapi-generate-plugin/README.md | 25 +++++ .../json/routes.json.js | 22 ++++ packages/strapi-generate-plugin/lib/before.js | 49 +++++++++ packages/strapi-generate-plugin/lib/index.js | 40 +++++++ packages/strapi-generate-plugin/package.json | 46 ++++++++ .../templates/controller.template | 25 +++++ .../templates/service.template | 11 ++ packages/strapi/bin/strapi.js | 7 ++ 12 files changed, 453 insertions(+) create mode 100755 packages/strapi-generate-plugin/.editorconfig create mode 100755 packages/strapi-generate-plugin/.gitignore create mode 100755 packages/strapi-generate-plugin/.npmignore create mode 100755 packages/strapi-generate-plugin/LICENSE.md create mode 100755 packages/strapi-generate-plugin/README.md create mode 100755 packages/strapi-generate-plugin/json/routes.json.js create mode 100755 packages/strapi-generate-plugin/lib/before.js create mode 100755 packages/strapi-generate-plugin/lib/index.js create mode 100644 packages/strapi-generate-plugin/package.json create mode 100755 packages/strapi-generate-plugin/templates/controller.template create mode 100755 packages/strapi-generate-plugin/templates/service.template diff --git a/packages/strapi-generate-plugin/.editorconfig b/packages/strapi-generate-plugin/.editorconfig new file mode 100755 index 0000000000..473e45184b --- /dev/null +++ b/packages/strapi-generate-plugin/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +indent_style = space +indent_size = 2 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[{package.json,*.yml}] +indent_style = space +indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/packages/strapi-generate-plugin/.gitignore b/packages/strapi-generate-plugin/.gitignore new file mode 100755 index 0000000000..1f78ea7b73 --- /dev/null +++ b/packages/strapi-generate-plugin/.gitignore @@ -0,0 +1,102 @@ +############################ +# OS X +############################ + +.DS_Store +.AppleDouble +.LSOverride +Icon +.Spotlight-V100 +.Trashes +._* + + +############################ +# Linux +############################ + +*~ + + +############################ +# Windows +############################ + +Thumbs.db +ehthumbs.db +Desktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msm +*.msp + + +############################ +# Packages +############################ + +*.7z +*.csv +*.dat +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip +*.com +*.class +*.dll +*.exe +*.o +*.seed +*.so +*.swo +*.swp +*.swn +*.swm +*.out +*.pid + + +############################ +# Logs and databases +############################ + +*.log +*.sql +*.sqlite + + +############################ +# Misc. +############################ + +*# +ssl +.idea +nbproject + + +############################ +# Node.js +############################ + +lib-cov +lcov.info +pids +logs +results +build +node_modules +.node_history + + +############################ +# Tests +############################ + +testApp +coverage diff --git a/packages/strapi-generate-plugin/.npmignore b/packages/strapi-generate-plugin/.npmignore new file mode 100755 index 0000000000..0a140df4e0 --- /dev/null +++ b/packages/strapi-generate-plugin/.npmignore @@ -0,0 +1,103 @@ +############################ +# OS X +############################ + +.DS_Store +.AppleDouble +.LSOverride +Icon +.Spotlight-V100 +.Trashes +._* + + +############################ +# Linux +############################ + +*~ + + +############################ +# Windows +############################ + +Thumbs.db +ehthumbs.db +Desktop.ini +$RECYCLE.BIN/ +*.cab +*.msi +*.msm +*.msp + + +############################ +# Packages +############################ + +*.7z +*.csv +*.dat +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip +*.com +*.class +*.dll +*.exe +*.o +*.seed +*.so +*.swo +*.swp +*.swn +*.swm +*.out +*.pid + + +############################ +# Logs and databases +############################ + +*.log +*.sql +*.sqlite + + +############################ +# Misc. +############################ + +*# +ssl +.idea +nbproject + + +############################ +# Node.js +############################ + +lib-cov +lcov.info +pids +logs +results +build +node_modules +.node_history + + +############################ +# Tests +############################ + +test +testApp +coverage diff --git a/packages/strapi-generate-plugin/LICENSE.md b/packages/strapi-generate-plugin/LICENSE.md new file mode 100755 index 0000000000..e27c5b5d59 --- /dev/null +++ b/packages/strapi-generate-plugin/LICENSE.md @@ -0,0 +1,7 @@ +Copyright (c) 2015-2017 Strapi Solutions. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/strapi-generate-plugin/README.md b/packages/strapi-generate-plugin/README.md new file mode 100755 index 0000000000..b8a0ec74e6 --- /dev/null +++ b/packages/strapi-generate-plugin/README.md @@ -0,0 +1,25 @@ +# strapi-generate-plugin + +[![npm version](https://img.shields.io/npm/v/strapi-generate-plugin.svg)](https://www.npmjs.org/package/strapi-generate-plugin) +[![npm downloads](https://img.shields.io/npm/dm/strapi-generate-plugin.svg)](https://www.npmjs.org/package/strapi-generate-plugin) +[![npm dependencies](https://david-dm.org/strapi/strapi-generate-plugin.svg)](https://david-dm.org/strapi/strapi-generate-plugin) +[![Build status](https://travis-ci.org/strapi/strapi-generate-plugin.svg?branch=master)](https://travis-ci.org/strapi/strapi-generate-plugin) +[![Slack status](http://strapi-slack.herokuapp.com/badge.svg)](http://slack.strapi.io) + +This Strapi generator contains all the default files for a new plugin. + +This generator can be called with: + +```bash +$ strapi generate:plugin pluginName +``` + +## Resources + +- [MIT License](LICENSE.md) + +## Links + +- [Strapi website](http://strapi.io/) +- [Strapi community on Slack](http://slack.strapi.io) +- [Strapi news on Twitter](https://twitter.com/strapijs) diff --git a/packages/strapi-generate-plugin/json/routes.json.js b/packages/strapi-generate-plugin/json/routes.json.js new file mode 100755 index 0000000000..fda8c8c691 --- /dev/null +++ b/packages/strapi-generate-plugin/json/routes.json.js @@ -0,0 +1,22 @@ +'use strict'; + +/** + * Expose main routes of the generated plugin + */ + +module.exports = scope => { + function generateRoutes() { + return { + routes: [{ + method: 'GET', + path: '/', + handler: scope.globalID + '.index', + config: { + policies: [] + } + }] + }; + } + + return generateRoutes(); +}; diff --git a/packages/strapi-generate-plugin/lib/before.js b/packages/strapi-generate-plugin/lib/before.js new file mode 100755 index 0000000000..220fa00c20 --- /dev/null +++ b/packages/strapi-generate-plugin/lib/before.js @@ -0,0 +1,49 @@ +'use strict'; + +/** + * Module dependencies + */ + +// Public node modules. +const _ = require('lodash'); + +/** + * This `before` function is run before generating targets. + * Validate, configure defaults, get extra dependencies, etc. + * + * @param {Object} scope + * @param {Function} cb + */ + +module.exports = (scope, cb) => { + if (!scope.rootPath || !scope.id) { + return cb.invalid('Usage: `$ strapi generate:plugin pluginName`'); + } + + // `scope.args` are the raw command line arguments. + _.defaults(scope, { + id: _.trim(_.deburr(scope.id)) + }); + + // Determine default values based on the available scope. + _.defaults(scope, { + globalID: _.upperFirst(_.camelCase(scope.id)), + ext: '.js' + }); + + + // Take another pass to take advantage of the defaults absorbed in previous passes. + _.defaults(scope, { + filename: `${scope.globalID}${scope.ext}` + }); + + + // Humanize output. + _.defaults(scope, { + humanizeId: _.camelCase(scope.id).toLowerCase(), + humanizedPath: '`./plugins`' + }); + + // Trigger callback with no error to proceed. + return cb.success(); +}; diff --git a/packages/strapi-generate-plugin/lib/index.js b/packages/strapi-generate-plugin/lib/index.js new file mode 100755 index 0000000000..a3979845c0 --- /dev/null +++ b/packages/strapi-generate-plugin/lib/index.js @@ -0,0 +1,40 @@ +'use strict'; + +/** + * Module dependencies + */ + +// Node.js core. +const path = require('path'); + +// Local dependencies. +const routesJSON = require('../json/routes.json.js'); + +/** + * Generate a core API + */ + +module.exports = { + templatesDirectory: () => { + return path.resolve(__dirname, '..', 'templates'); + }, + before: require('./before'), + targets: { + + // Use the default `controller` file as a template for + // every generated controller. + 'plugins/:humanizeId/controllers/:filename': { + template: 'controller.template' + }, + + // every generated controller. + 'plugins/:humanizeId/services/:filename': { + template: 'service.template' + }, + + // Generate routes. + 'plugins/:humanizeId/config/routes.json': { + jsonfile: routesJSON + } + } +}; diff --git a/packages/strapi-generate-plugin/package.json b/packages/strapi-generate-plugin/package.json new file mode 100644 index 0000000000..6ec7a7148a --- /dev/null +++ b/packages/strapi-generate-plugin/package.json @@ -0,0 +1,46 @@ +{ + "name": "strapi-generate-plugin", + "version": "3.0.0-alpha.3", + "description": "Generate an plugin for a Strapi application.", + "homepage": "http://strapi.io", + "keywords": [ + "generate", + "generator", + "strapi" + ], + "main": "./lib/index.js", + "directories": { + "lib": "./lib" + }, + "dependencies": { + "lodash": "^4.16.5", + "pluralize": "~3.1.0" + }, + "scripts": { + "prepublish": "npm prune" + }, + "author": { + "email": "hi@strapi.io", + "name": "Strapi team", + "url": "http://strapi.io" + }, + "maintainers": [ + { + "name": "Strapi team", + "email": "hi@strapi.io", + "url": "http://strapi.io" + } + ], + "repository": { + "type": "git", + "url": "git://github.com/strapi/strapi.git" + }, + "bugs": { + "url": "https://github.com/strapi/strapi/issues" + }, + "engines": { + "node": ">= 7.0.0", + "npm": ">= 3.0.0" + }, + "license": "MIT" +} diff --git a/packages/strapi-generate-plugin/templates/controller.template b/packages/strapi-generate-plugin/templates/controller.template new file mode 100755 index 0000000000..5db3e2d6e8 --- /dev/null +++ b/packages/strapi-generate-plugin/templates/controller.template @@ -0,0 +1,25 @@ +'use strict'; + +/** + * <%= filename %> controller + * + * @description: A set of functions called "actions" of the `<%= humanizeId %>` plugin. + */ + +module.exports = { + + /** + * Default action. + * + * @return {Object} + */ + + index: async (ctx) => { + // Add your own logic here. + + // Send 200 `ok` + ctx.send({ + message: 'ok' + }); + } +}; diff --git a/packages/strapi-generate-plugin/templates/service.template b/packages/strapi-generate-plugin/templates/service.template new file mode 100755 index 0000000000..5891b61a5a --- /dev/null +++ b/packages/strapi-generate-plugin/templates/service.template @@ -0,0 +1,11 @@ +'use strict'; + +/** + * <%= filename %> service + * + * @description: A set of functions similar to controller's actions to avoid code duplication. + */ + +module.exports = { + +}; diff --git a/packages/strapi/bin/strapi.js b/packages/strapi/bin/strapi.js index 893c084106..ca182c07ee 100755 --- a/packages/strapi/bin/strapi.js +++ b/packages/strapi/bin/strapi.js @@ -108,6 +108,13 @@ program .description('generate a service for an API') .action(require('./strapi-generate')); +// `$ strapi generate:plugin` +program + .command('generate:plugin ') + .option('-p, --plugin ', 'plugin name') + .description('generate a basic plugin') + .action(require('./strapi-generate')); + // `$ strapi generate:hook` program .command('generate:hook ')