-
Notifications
You must be signed in to change notification settings - Fork 32
feat(schematics): add custom component schematic #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
ddc3aea
2191d08
d421b2f
86c9e03
a5c13c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { strings } from '@angular-devkit/core'; | ||
import { Rule, SchematicsException, Tree, apply, branchAndMerge, chain, filter, mergeWith, move, noop, template, url } from '@angular-devkit/schematics'; | ||
import { addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils'; | ||
import { addDeclarationToModule, addEntryComponentToModule, addExportToModule, addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils'; | ||
import { InsertChange } from '@schematics/angular/utility/change'; | ||
import { buildRelativePath } from '@schematics/angular/utility/find-module'; | ||
import { parseName } from '@schematics/angular/utility/parse-name'; | ||
|
@@ -27,7 +27,79 @@ function addImportToNgModule(options: ComponentOptions): Rule { | |
if (!options.module) { | ||
return host; | ||
} | ||
if (!options.createModule && options.module) { | ||
addImportToDeclarations(host, options); | ||
} | ||
if (options.createModule && options.module) { | ||
addImportToImports(host, options); | ||
} | ||
return host; | ||
}; | ||
} | ||
|
||
function addImportToDeclarations(host: Tree, options: ComponentOptions): void { | ||
if (options.module) { | ||
const modulePath = options.module; | ||
let source = readIntoSourceFile(host, modulePath); | ||
|
||
const componentPath = `/${options.path}/` | ||
+ (options.flat ? '' : strings.dasherize(options.name) + '/') | ||
+ strings.dasherize(options.name) | ||
+ '.component'; | ||
const relativePath = buildRelativePath(modulePath, componentPath); | ||
const classifiedName = strings.classify(`${options.name}Component`); | ||
const declarationChanges = addDeclarationToModule(source, | ||
modulePath, | ||
classifiedName, | ||
relativePath); | ||
|
||
const declarationRecorder = host.beginUpdate(modulePath); | ||
for (const change of declarationChanges) { | ||
if (change instanceof InsertChange) { | ||
declarationRecorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
} | ||
host.commitUpdate(declarationRecorder); | ||
|
||
if (options.export) { | ||
// Need to refresh the AST because we overwrote the file in the host. | ||
source = readIntoSourceFile(host, modulePath); | ||
|
||
const exportRecorder = host.beginUpdate(modulePath); | ||
const exportChanges = addExportToModule(source, modulePath, | ||
strings.classify(`${options.name}Component`), | ||
relativePath); | ||
|
||
for (const change of exportChanges) { | ||
if (change instanceof InsertChange) { | ||
exportRecorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
} | ||
host.commitUpdate(exportRecorder); | ||
} | ||
|
||
if (options.entryComponent) { | ||
// Need to refresh the AST because we overwrote the file in the host. | ||
source = readIntoSourceFile(host, modulePath); | ||
|
||
const entryComponentRecorder = host.beginUpdate(modulePath); | ||
const entryComponentChanges = addEntryComponentToModule( | ||
source, modulePath, | ||
strings.classify(`${options.name}Component`), | ||
relativePath); | ||
|
||
for (const change of entryComponentChanges) { | ||
if (change instanceof InsertChange) { | ||
entryComponentRecorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
} | ||
host.commitUpdate(entryComponentRecorder); | ||
} | ||
} | ||
} | ||
|
||
function addImportToImports(host: Tree, options: ComponentOptions): void { | ||
if (options.module) { | ||
const modulePath = options.module; | ||
const moduleSource = readIntoSourceFile(host, modulePath); | ||
|
||
|
@@ -47,8 +119,7 @@ function addImportToNgModule(options: ComponentOptions): Rule { | |
} | ||
} | ||
host.commitUpdate(importRecorder); | ||
return host; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I presume it wasn't necessary to return host... my bad 😛 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Host should not to return here. We return the host though further up on line 36, after the modifying the fs. |
||
}; | ||
} | ||
} | ||
|
||
export default function(options: ComponentOptions): Rule { | ||
|
@@ -73,6 +144,7 @@ export default function(options: ComponentOptions): Rule { | |
|
||
const templateSource = apply(url('./files'), [ | ||
options.spec ? noop() : filter(p => !p.endsWith('.spec.ts')), | ||
options.createModule ? noop() : filter(p => !p.endsWith('.module.ts')), | ||
template({ | ||
...strings, | ||
'if-flat': (s: string) => options.flat ? '' : s, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you just commit the update later? Or does this get a bit messy?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was copied over from the angular schematic, so Im assuming it's needed. Probably to make sure that the file is properly updated and not overwriting it.