Skip to content

Commit

Permalink
feat(route-discovery): add a route discovery (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgeucano authored Jan 24, 2020
1 parent 530d323 commit 96c890e
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
5 changes: 4 additions & 1 deletion schematics/scully/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"schematics-pack": "tsc -p tsconfig.json && npm pack",
"schematics": "npm run build && schematics .:install --blog",
"test": "npm run build && jasmine src/**/*_spec.js",
"publish:path": "tsc -p ./tsconfig.json && npm version patch && npm publish --access public",
"publish:patch": "tsc -p ./tsconfig.json && npm version patch && npm publish --access public",
"publish:minor": "tsc -p ./tsconfig.json && npm version minor && npm publish --access public",
"publish:major": "tsc -p ./tsconfig.json && npm version major && npm publish --access public"
},
Expand All @@ -40,5 +40,8 @@
"@types/node": "^8.0.31",
"jasmine": "^3.3.1",
"typescript": "~3.5.3"
},
"peerDependencies": {
"guess-parser": "^0.4.13"
}
}
Binary file modified schematics/scully/scullyio-init-0.0.17.tgz
Binary file not shown.
6 changes: 6 additions & 0 deletions schematics/scully/src/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
"factory": "./add-plugin/index",
"schema": "./add-plugin/schema.json",
"aliases": ["plugin"]
},
"route-discovery": {
"description": "Show the routes",
"factory": "./route-discovery/index",
"schema": "./route-discovery/schema.json",
"aliases": ["route", "discovery"]
}
}
}
53 changes: 53 additions & 0 deletions schematics/scully/src/route-discovery/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {Rule, Tree, chain, SchematicContext} from '@angular-devkit/schematics';
import {Schema} from './schema';
import {parseAngularRoutes} from 'guess-parser';
import {getFileContents, getProject, getScullyConfig} from '../utils/utils';

export default (options: Schema): Rule => {
return chain([routeDiscovery(options)]);
};

const routeDiscovery = (options: Schema) => async (tree: Tree, context: SchematicContext) => {
let routes: any[] | string[] = [];
const projectName = getProject(tree, options.project);
try {
routes = parseAngularRoutes(projectName).map(r => r.path);
} catch (e) {
const allRoutes = routes;
if (allRoutes.findIndex(r => r === '') === -1) {
console.log('error');
}
}
console.log(routes);
/**
* read the scully.{{nameProject}}.config.js for check if
* not exist the route, add into the file with the `type: 'ignored'`
*/

// @ts-ignore
const scullyConfigFile = getScullyConfig(tree, options.project);
const scullyJs = getFileContents(tree, scullyConfigFile);
if (!scullyJs) {
context.logger.error(`No scully configuration file found ${scullyConfigFile}`);
}

console.log(scullyJs);
let newScullyJs = '';
routes.forEach(route => {
if (+scullyJs.search(route) < 0) {
const addRoute = `\n '${route}': {\n type: 'ignored'\n },`;
if (+scullyJs.search(/routes: \{/g) > 0) {
const position = +scullyJs.search(/routes: \{/g) + 'routes: {'.length;
newScullyJs = [scullyJs.slice(0, position), addRoute, scullyJs.slice(position)].join('');
} else if (+scullyJs.search(/routes:\{/g) > 0) {
const position = +scullyJs.search(/routes:\{/g) + 'routes:{'.length;
newScullyJs = [scullyJs.slice(0, position), addRoute, scullyJs.slice(position)].join('');
}
} else {
console.log(`the ${route} exist.`);
}
});
newScullyJs = newScullyJs === '' ? scullyJs : newScullyJs;
console.log(newScullyJs);
tree.overwrite(scullyConfigFile, newScullyJs);
};
Empty file.
14 changes: 14 additions & 0 deletions schematics/scully/src/route-discovery/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://json-schema.org/schema",
"id": "@scullyio/init:discovery-routes",
"title": "Scully: Create a discovery routes",
"type": "object",
"properties": {
"project": {
"type": "string",
"description": "add the project",
"default": "defaultProject"
}
},
"required": []
}
3 changes: 3 additions & 0 deletions schematics/scully/src/route-discovery/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Schema {
project: string;
}
10 changes: 3 additions & 7 deletions schematics/scully/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ export function getSrc(host: Tree, project: string) {

export function getRoot(host: Tree, project: string) {
const angularConfig = JSON.parse(host.read('./angular.json').toString());
const defaultProject = angularConfig[getProject(host, project)];
return angularConfig.projects[defaultProject].root;
return angularConfig.projects[getProject(host, project)].root;
}

class FileNotFoundException extends Error {
Expand Down Expand Up @@ -238,16 +237,13 @@ export const getProject = (host: Tree, project: string) => {
};

export const getScullyConfig = (host: Tree, project: string) => {
let scullyConfigFile = './scully.config.js';
if (project !== 'defaultProject') {
scullyConfigFile = `scully.${getProject(host, project)}.config.js`;
}
const scullyConfigFile = `scully.${getProject(host, project)}.config.js`;
return scullyConfigFile;
};

export const checkProjectExist = (host: Tree, projectName: string) => {
const angularJson = JSON.parse(host.read('/angular.json').toString());
if (angularJson[projectName] !== undefined) {
if (angularJson.projects[projectName] !== undefined) {
return true;
}
return false;
Expand Down

0 comments on commit 96c890e

Please sign in to comment.