Skip to content

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

Merged
merged 5 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 75 additions & 3 deletions schematics/component/index.ts
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';
Expand All @@ -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.
Copy link
Contributor

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?

Copy link
Contributor Author

@mhartington mhartington Feb 6, 2019

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.

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);

Expand All @@ -47,8 +119,7 @@ function addImportToNgModule(options: ComponentOptions): Rule {
}
}
host.commitUpdate(importRecorder);
return host;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume it wasn't necessary to return host... my bad 😛

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions schematics/component/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ export interface Schema {
spec?: boolean;
flat?: boolean;
selector?: string;
createModule?: boolean;
module?: string;
export?: boolean;
entryComponent?: boolean;
}
20 changes: 17 additions & 3 deletions schematics/component/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,24 @@
"format": "html-selector",
"description": "The selector to use for the page"
},
"module": {
"createModule": {
"type": "boolean",
"description": "Allows creating a module for the component",
"default": false
},
"module": {
"type": "string",
"description": "Allows specification of the declaring module",
"alias": "m"
"description": "Allows adding to a modules imports or declarations"
},
"export": {
"type": "boolean",
"default": false,
"description": "When true, the declaring NgModule exports this component."
},
"entryComponent": {
"type": "boolean",
"default": false,
"description": "When true, the new component is the entry component of the declaring NgModule."
}
},
"required": []
Expand Down