-
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(route-discovery): add a route discovery (#233)
- Loading branch information
1 parent
530d323
commit 96c890e
Showing
8 changed files
with
83 additions
and
8 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
Binary file not shown.
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
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,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.
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:discovery-routes", | ||
"title": "Scully: Create a discovery routes", | ||
"type": "object", | ||
"properties": { | ||
"project": { | ||
"type": "string", | ||
"description": "add the project", | ||
"default": "defaultProject" | ||
} | ||
}, | ||
"required": [] | ||
} |
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,3 @@ | ||
export interface Schema { | ||
project: 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