Skip to content

Commit

Permalink
feat(add-plugin): add schematics for create a new custom plugin (#196)
Browse files Browse the repository at this point in the history
* feat(add-plugin): add schematics for create a new custom plugin

* docs(add-plugin): add more comments for support devs
  • Loading branch information
jorgeucano authored Jan 20, 2020
1 parent 72d3ae4 commit bdc0053
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
28 changes: 28 additions & 0 deletions schematics/scully/src/add-plugin/index.ts
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);
};
14 changes: 14 additions & 0 deletions schematics/scully/src/add-plugin/schema.json
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"]
}
6 changes: 6 additions & 0 deletions schematics/scully/src/add-plugin/schema.ts
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;
}
6 changes: 6 additions & 0 deletions schematics/scully/src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
"factory": "./create-markdown/index",
"schema": "./create-markdown/schema.json",
"aliases": ["c-markdown", "markdown", "md"]
},
"add-plugin": {
"description": "Add a custom plugin for scully",
"factory": "./add-plugin/index",
"schema": "./add-plugin/schema.json",
"aliases": ["plugin"]
}
}
}
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);
10 changes: 10 additions & 0 deletions schematics/scully/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import {
} from '@angular-devkit/schematics';
import {normalize, strings} from '@angular-devkit/core';
import {join} from 'path';
// @ts-ignore
import fs = require('fs');
// @ts-ignore
import yaml = require('js-yaml');

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

export function getRoot(host: Tree) {
const angularConfig = JSON.parse(host.read('./angular.json').toString());
// TODO: make scully handle other projects as just the default one.
const defaultProject = angularConfig.defaultProject;
return angularConfig.projects[defaultProject].root;
}

class FileNotFoundException extends Error {
constructor(fileName: string) {
const message = `File ${fileName} not found!`;
Expand Down

0 comments on commit bdc0053

Please sign in to comment.