Skip to content

Commit bdc0053

Browse files
authored
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
1 parent 72d3ae4 commit bdc0053

6 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {Rule, Tree, url, applyTemplates, move, chain, SchematicContext} from '@angular-devkit/schematics';
2+
import {strings, normalize} from '@angular-devkit/core';
3+
import {Schema} from './schema';
4+
import {applyWithOverwrite, getRoot} from '../utils/utils';
5+
6+
export default (options: Schema): Rule => {
7+
return chain([addPlugin(options), registerPlugin(options)]);
8+
};
9+
10+
const addPlugin = (options: Schema) => (tree: Tree, context: SchematicContext) => {
11+
const sourceRoot = getRoot(tree);
12+
const pathName = strings.dasherize(`${sourceRoot}/scullyPlugins/${options.name}.js`);
13+
return applyWithOverwrite(url('../files/add-plugin'), [
14+
applyTemplates({
15+
classify: strings.classify,
16+
dasherize: strings.dasherize,
17+
camelize: strings.camelize,
18+
name: options.name,
19+
}),
20+
move(normalize(pathName)),
21+
]);
22+
};
23+
24+
const registerPlugin = (options: Schema) => (tree: Tree, context: SchematicContext) => {
25+
let scullyConfig = tree.read(`${getRoot(tree)}/scully.config.js`).toString();
26+
scullyConfig = `require('./scullyPlugins/extra-plugin.js');\n${scullyConfig}`;
27+
tree.overwrite(`${getRoot(tree)}/scully.config.js`, scullyConfig);
28+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"$schema": "http://json-schema.org/schema",
3+
"id": "@scullyio/init:plugin",
4+
"title": "Scully: Create a plugin for scully",
5+
"type": "object",
6+
"properties": {
7+
"name": {
8+
"type": "string",
9+
"description": "add the name for the plugin",
10+
"x-prompt": "What name do you want to use for the plugin?"
11+
}
12+
},
13+
"required": ["name"]
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export interface Schema {
2+
/**
3+
* add the name for the plugin
4+
*/
5+
name: string;
6+
}

schematics/scully/src/collection.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
"factory": "./create-markdown/index",
3232
"schema": "./create-markdown/schema.json",
3333
"aliases": ["c-markdown", "markdown", "md"]
34+
},
35+
"add-plugin": {
36+
"description": "Add a custom plugin for scully",
37+
"factory": "./add-plugin/index",
38+
"schema": "./add-plugin/schema.json",
39+
"aliases": ["plugin"]
3440
}
3541
}
3642
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** import from scully the register plugin function */
2+
const {registerPlugin} = require('@scullyio/scully');
3+
4+
/** import from scully the register plugin function */
5+
const <%= camelize(name) %> = async (route, options) => {
6+
const handleRoute = [];
7+
/**
8+
this is a router plugin and this need return a handle route
9+
the HandleRoute is a type available for you.
10+
If you will create another type of plugin, you need return HTML.
11+
* */
12+
return handleRoute;
13+
};
14+
/**
15+
You can add extra validator for your custom plugin
16+
*/
17+
const validator = async conf => [];
18+
/**
19+
registerPlugin(TypeOfPlugin, name of the plugin, plugin function, validator)
20+
*/
21+
registerPlugin('router', '<%= camelize(name) %>', <%= camelize(name) %>, validator);

schematics/scully/src/utils/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import {
1010
} from '@angular-devkit/schematics';
1111
import {normalize, strings} from '@angular-devkit/core';
1212
import {join} from 'path';
13+
// @ts-ignore
1314
import fs = require('fs');
15+
// @ts-ignore
1416
import yaml = require('js-yaml');
1517

1618
import {buildRelativePath} from '@schematics/angular/utility/find-module';
1719
import {addRouteDeclarationToModule} from '@schematics/angular/utility/ast-utils';
20+
// @ts-ignore
1821
import ts = require('@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript');
1922
import {InsertChange} from '@schematics/angular/utility/change';
2023
import {ModuleOptions} from '@schematics/angular/utility/find-module';
@@ -158,6 +161,13 @@ export function getSrc(host: Tree) {
158161
return angularConfig.projects[defaultProject].sourceRoot;
159162
}
160163

164+
export function getRoot(host: Tree) {
165+
const angularConfig = JSON.parse(host.read('./angular.json').toString());
166+
// TODO: make scully handle other projects as just the default one.
167+
const defaultProject = angularConfig.defaultProject;
168+
return angularConfig.projects[defaultProject].root;
169+
}
170+
161171
class FileNotFoundException extends Error {
162172
constructor(fileName: string) {
163173
const message = `File ${fileName} not found!`;

0 commit comments

Comments
 (0)