Add basic plugin generator

This commit is contained in:
Pierre Burgy 2017-02-14 01:18:07 +01:00
parent 17135196a5
commit 353252d68c
12 changed files with 453 additions and 0 deletions

View File

@ -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

102
packages/strapi-generate-plugin/.gitignore vendored Executable file
View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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)

View File

@ -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();
};

View File

@ -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();
};

View File

@ -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
}
}
};

View File

@ -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"
}

View File

@ -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'
});
}
};

View File

@ -0,0 +1,11 @@
'use strict';
/**
* <%= filename %> service
*
* @description: A set of functions similar to controller's actions to avoid code duplication.
*/
module.exports = {
};

View File

@ -108,6 +108,13 @@ program
.description('generate a service for an API')
.action(require('./strapi-generate'));
// `$ strapi generate:plugin`
program
.command('generate:plugin <id>')
.option('-p, --plugin <plugin>', 'plugin name')
.description('generate a basic plugin')
.action(require('./strapi-generate'));
// `$ strapi generate:hook`
program
.command('generate:hook <id>')