-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(playground): components list (#1106)
Closes #1077
- Loading branch information
Showing
19 changed files
with
1,926 additions
and
15 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
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,173 @@ | ||
/** | ||
* @license | ||
* Copyright Akveo. All Rights Reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*/ | ||
|
||
import * as ts from 'typescript'; | ||
import { dirname, join, normalize, Path, PathFragment } from '@angular-devkit/core'; | ||
import { DirEntry, SchematicsException, Tree, Rule } from '@angular-devkit/schematics'; | ||
import { getSourceFile } from '@angular/cdk/schematics'; | ||
import { | ||
addTrailingCommas, | ||
applyReplaceChange, | ||
findDeclarationByIdentifier, | ||
findRoutesArray, | ||
getPlaygroundRootDir, | ||
getRouteChildren, | ||
getRouteComponent, | ||
getRouteLazyModule, | ||
getRoutePath, | ||
getRoutesFromArray, | ||
isComponentRoute, | ||
isLazyRoute, | ||
isRoutingModule, | ||
lazyRoutePathToFilePath, | ||
removePropsQuotes, | ||
singleQuotes, | ||
splitClassName, | ||
trimQuotes, | ||
} from '../utils'; | ||
|
||
const COMPONENTS_LIST_FILE_PATH = normalize('/src/app/playground-components.ts'); | ||
const COMPONENTS_VARIABLE_NAME = 'PLAYGROUND_COMPONENTS'; | ||
|
||
interface ComponentLink { | ||
path: string; | ||
name?: string; | ||
component?: string; | ||
link?: any[] | string; | ||
children?: ComponentLink[]; | ||
} | ||
|
||
export function playgroundComponents(): Rule { | ||
return generateComponentsList; | ||
} | ||
|
||
function generateComponentsList(tree: Tree): void { | ||
const componentsListFile = getSourceFile(tree, COMPONENTS_LIST_FILE_PATH); | ||
const componentsListArray = getComponentsListArray(componentsListFile); | ||
const routes = removeRoutesWithoutPath(findRoutesInDir(tree, getPlaygroundRootDir(tree))); | ||
updateComponentsFile(tree, componentsListFile, componentsListArray, routes); | ||
} | ||
|
||
function getComponentsListArray(fileSource: ts.SourceFile): ts.ArrayLiteralExpression { | ||
const listDeclaration = findDeclarationByIdentifier(fileSource, COMPONENTS_VARIABLE_NAME); | ||
if (!listDeclaration) { | ||
throw new SchematicsException(`Error in ${COMPONENTS_LIST_FILE_PATH}. Can't find components list variable.`); | ||
} | ||
const initializer = (listDeclaration as ts.VariableDeclaration).initializer; | ||
if (!initializer || initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) { | ||
throw new SchematicsException(`Error in ${COMPONENTS_LIST_FILE_PATH}. List should be initialized with array.`); | ||
} | ||
|
||
return initializer as ts.ArrayLiteralExpression; | ||
} | ||
|
||
function findRoutesInDir(tree: Tree, dir: DirEntry): ComponentLink[] { | ||
const routingModuleFile = dir.subfiles.find(isRoutingModule); | ||
if (routingModuleFile) { | ||
const routingModulePath = join(dir.path, routingModuleFile); | ||
const routes = getRoutesFromArray(findRoutesArray(tree, routingModulePath)); | ||
return parseRoutes(tree, dir, routes); | ||
} | ||
return []; | ||
} | ||
|
||
function parseRoutes(tree: Tree, dir: DirEntry, routeEntries: ts.ObjectLiteralExpression[]): ComponentLink[] { | ||
const foundRoutes: ComponentLink[] = []; | ||
const routesWithPath = routeEntries | ||
.filter(r => r.properties.length > 0) | ||
.filter(r => !!getRoutePath(r)) | ||
.filter(r => isLazyRoute(r) || isComponentRoute(r)); | ||
|
||
for (const route of routesWithPath) { | ||
const component = getComponentRoute(route); | ||
const routeChildren = getChildRoutes(tree, dir, route); | ||
const lazyChildren = getLazyModuleRoutes(tree, dir, route); | ||
const children = routeChildren.concat(lazyChildren); | ||
const routePath = trimQuotes((getRoutePath(route) as ts.PropertyAssignment).initializer.getText()); | ||
foundRoutes.push({ path: routePath, component, children }); | ||
} | ||
|
||
return foundRoutes; | ||
} | ||
|
||
function getComponentRoute(route: ts.ObjectLiteralExpression): string | undefined { | ||
const componentProp = getRouteComponent(route); | ||
if (componentProp) { | ||
return componentProp.initializer.getText(); | ||
} | ||
} | ||
|
||
function getChildRoutes( | ||
tree: Tree, | ||
routingModuleDir: DirEntry, | ||
route: ts.ObjectLiteralExpression, | ||
): ComponentLink[] { | ||
const childrenProp = getRouteChildren(route); | ||
if (childrenProp) { | ||
return parseRoutes(tree, routingModuleDir, getRoutesFromArray(childrenProp)); | ||
} | ||
return []; | ||
} | ||
|
||
function getLazyModuleRoutes( | ||
tree: Tree, | ||
routingModuleDir: DirEntry, | ||
route: ts.ObjectLiteralExpression, | ||
): ComponentLink[] { | ||
const lazyModule = getRouteLazyModule(route); | ||
if (lazyModule) { | ||
const lazyModulePath = lazyModule && trimQuotes(lazyModule.initializer.getText()); | ||
const moduleDirPath = dirname(lazyRoutePathToFilePath(lazyModulePath) as Path) as PathFragment; | ||
return findRoutesInDir(tree, routingModuleDir.dir(moduleDirPath)); | ||
} | ||
return []; | ||
} | ||
|
||
function removeRoutesWithoutPath(routes: ComponentLink[], startPath: string = ''): ComponentLink[] { | ||
const routesWithPath: ComponentLink[] = []; | ||
|
||
for (const { path, component, children } of routes) { | ||
const fullPath = path ? startPath + '/' + path : startPath; | ||
let routeChildren; | ||
if (children) { | ||
routeChildren = removeRoutesWithoutPath(children, fullPath); | ||
} | ||
|
||
const toAdd: ComponentLink[] = []; | ||
if (path) { | ||
const link = component ? fullPath : undefined; | ||
const name = component ? splitClassName(component) : undefined; | ||
const childRoutes = routeChildren && routeChildren.length ? routeChildren : undefined; | ||
toAdd.push({ path, link, component, name, children: childRoutes }); | ||
} else if (routeChildren) { | ||
toAdd.push(...routeChildren); | ||
} | ||
|
||
routesWithPath.push(...toAdd); | ||
} | ||
|
||
return routesWithPath; | ||
} | ||
|
||
function updateComponentsFile( | ||
tree: Tree, | ||
componentsListFile: ts.SourceFile, | ||
list: ts.ArrayLiteralExpression, | ||
routes: ComponentLink[], | ||
) { | ||
const pos = list.getFullStart(); | ||
const endPos = pos + list.getFullText().length; | ||
const oldText = componentsListFile.getFullText().slice(pos, endPos); | ||
const newText = generateRoutesString(routes); | ||
|
||
applyReplaceChange(tree, COMPONENTS_LIST_FILE_PATH, { pos, oldText, newText }); | ||
} | ||
|
||
function generateRoutesString(routes: ComponentLink[]): string { | ||
const jsonRoutes = JSON.stringify(routes, null, 2); | ||
|
||
return ` ${addTrailingCommas(removePropsQuotes(singleQuotes(jsonRoutes)))}`; | ||
} |
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,7 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "playground-components", | ||
"type": "object", | ||
"properties": { | ||
} | ||
} |
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,8 @@ | ||
import { Rule, chain, schematic } from '@angular-devkit/schematics'; | ||
|
||
export function generatePlayground(): Rule { | ||
return chain([ | ||
schematic('playground-module', {}), | ||
schematic('playground-components', {}), | ||
]); | ||
} |
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,5 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "playground", | ||
"type": "object" | ||
} |
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
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,49 @@ | ||
const QUOTES = [ `'`, `"`, '`' ]; | ||
|
||
export function trimQuotes(stringLiteral: string): string { | ||
if (stringLiteral.length === 0) { | ||
return stringLiteral; | ||
} | ||
|
||
let resultingString = stringLiteral; | ||
|
||
if (QUOTES.includes(resultingString[0])) { | ||
resultingString = resultingString.slice(1); | ||
} | ||
if (QUOTES.includes(resultingString[resultingString.length - 1])) { | ||
resultingString = resultingString.slice(0, resultingString.length - 1); | ||
} | ||
|
||
return resultingString; | ||
} | ||
|
||
const COMPONENT_SUFFIX = 'Component'; | ||
|
||
/** | ||
* Splits string in words by capital letters. Also removes 'Component' suffix. | ||
*/ | ||
export function splitClassName(className: string): string { | ||
const withoutSuffix = className.endsWith(COMPONENT_SUFFIX) | ||
? className.replace(COMPONENT_SUFFIX, '') | ||
: className; | ||
|
||
return withoutSuffix.replace(/([a-z])([A-Z])/g, '$1 $2'); | ||
} | ||
|
||
export function singleQuotes(json: string) { | ||
return json.replace(/\"/gm, `'`); | ||
} | ||
|
||
export function addTrailingCommas(json: string): string { | ||
let withCommas = json.replace(/(['}\]])$/gm, '$1,'); | ||
|
||
if (withCommas.endsWith(',')) { | ||
withCommas = withCommas.slice(0, withCommas.length - 1); | ||
} | ||
|
||
return withCommas; | ||
} | ||
|
||
export function removePropsQuotes(json: string): string { | ||
return json.replace(/^(\s*)['"](\S+)['"]:/gm, '$1$2:'); | ||
} |
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
Oops, something went wrong.