-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(add-plugin): add schematics for create a new custom plugin (#196)
* feat(add-plugin): add schematics for create a new custom plugin * docs(add-plugin): add more comments for support devs
- Loading branch information
1 parent
72d3ae4
commit bdc0053
Showing
6 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {Rule, Tree, url, applyTemplates, move, chain, SchematicContext} from '@angular-devkit/schematics'; | ||
import {strings, normalize} from '@angular-devkit/core'; | ||
import {Schema} from './schema'; | ||
import {applyWithOverwrite, getRoot} from '../utils/utils'; | ||
|
||
export default (options: Schema): Rule => { | ||
return chain([addPlugin(options), registerPlugin(options)]); | ||
}; | ||
|
||
const addPlugin = (options: Schema) => (tree: Tree, context: SchematicContext) => { | ||
const sourceRoot = getRoot(tree); | ||
const pathName = strings.dasherize(`${sourceRoot}/scullyPlugins/${options.name}.js`); | ||
return applyWithOverwrite(url('../files/add-plugin'), [ | ||
applyTemplates({ | ||
classify: strings.classify, | ||
dasherize: strings.dasherize, | ||
camelize: strings.camelize, | ||
name: options.name, | ||
}), | ||
move(normalize(pathName)), | ||
]); | ||
}; | ||
|
||
const registerPlugin = (options: Schema) => (tree: Tree, context: SchematicContext) => { | ||
let scullyConfig = tree.read(`${getRoot(tree)}/scully.config.js`).toString(); | ||
scullyConfig = `require('./scullyPlugins/extra-plugin.js');\n${scullyConfig}`; | ||
tree.overwrite(`${getRoot(tree)}/scully.config.js`, scullyConfig); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "@scullyio/init:plugin", | ||
"title": "Scully: Create a plugin for scully", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "add the name for the plugin", | ||
"x-prompt": "What name do you want to use for the plugin?" | ||
} | ||
}, | ||
"required": ["name"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Schema { | ||
/** | ||
* add the name for the plugin | ||
*/ | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
schematics/scully/src/files/plugin/__name@dasherize__.plugin.js.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** import from scully the register plugin function */ | ||
const {registerPlugin} = require('@scullyio/scully'); | ||
|
||
/** import from scully the register plugin function */ | ||
const <%= camelize(name) %> = async (route, options) => { | ||
const handleRoute = []; | ||
/** | ||
this is a router plugin and this need return a handle route | ||
the HandleRoute is a type available for you. | ||
If you will create another type of plugin, you need return HTML. | ||
* */ | ||
return handleRoute; | ||
}; | ||
/** | ||
You can add extra validator for your custom plugin | ||
*/ | ||
const validator = async conf => []; | ||
/** | ||
registerPlugin(TypeOfPlugin, name of the plugin, plugin function, validator) | ||
*/ | ||
registerPlugin('router', '<%= camelize(name) %>', <%= camelize(name) %>, validator); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters