-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ddc3aea
feat(schematics): add custom component schematic
mhartington 2191d08
chore(): create utils
mhartington d421b2f
chore(): fix import path
mhartington 86c9e03
feat(schematics): customize component creation
mhartington a5c13c0
chore(): spelling
mhartington File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
Empty file.
3 changes: 3 additions & 0 deletions
3
schematics/component/files/__name@dasherize@if-flat__/__name@dasherize__.component.html
This file contains hidden or 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 @@ | ||
<p> | ||
<%= dasherize(name) %> works! | ||
</p> |
27 changes: 27 additions & 0 deletions
27
schematics/component/files/__name@dasherize@if-flat__/__name@dasherize__.component.spec.ts
This file contains hidden or 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,27 @@ | ||
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; | ||
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { <%= classify(name) %>Page } from './<%= dasherize(name) %>.page'; | ||
|
||
describe('<%= classify(name) %>Page', () => { | ||
let component: <%= classify(name) %>Page; | ||
let fixture: ComponentFixture<<%= classify(name) %>Page>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ <%= classify(name) %>Page ], | ||
schemas: [CUSTOM_ELEMENTS_SCHEMA], | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(<%= classify(name) %>Page); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
schematics/component/files/__name@dasherize@if-flat__/__name@dasherize__.component.ts
This file contains hidden or 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 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: '<%= selector %>', | ||
templateUrl: './<%= dasherize(name) %>.component.html', | ||
styleUrls: ['./<%= dasherize(name) %>.component.<%= styleext %>'], | ||
}) | ||
export class <%= classify(name) %>Component implements OnInit { | ||
|
||
constructor() { } | ||
|
||
ngOnInit() {} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
schematics/component/files/__name@dasherize@if-flat__/__name@dasherize__.module.ts
This file contains hidden or 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 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { FormsModule } from '@angular/forms'; | ||
|
||
import { IonicModule } from '@ionic/angular'; | ||
|
||
import { <%= classify(name) %>Component } from './<%= dasherize(name) %>.component'; | ||
|
||
@NgModule({ | ||
imports: [ CommonModule, FormsModule,IonicModule,], | ||
declarations: [<%= classify(name) %>Component], | ||
exports: [<%= classify(name) %>Component] | ||
}) | ||
export class <%= classify(name) %>ComponentModule {} |
This file contains hidden or 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,101 @@ | ||
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 { InsertChange } from '@schematics/angular/utility/change'; | ||
import { buildRelativePath } from '@schematics/angular/utility/find-module'; | ||
import { parseName } from '@schematics/angular/utility/parse-name'; | ||
import { buildDefaultPath, getProject } from '@schematics/angular/utility/project'; | ||
import { validateHtmlSelector, validateName } from '@schematics/angular/utility/validation'; | ||
import * as ts from 'typescript'; | ||
|
||
import { Schema as ComponentOptions } from './schema'; | ||
|
||
function buildSelector(options: ComponentOptions, projectPrefix: string) { | ||
let selector = strings.dasherize(options.name); | ||
|
||
if (options.prefix) { | ||
selector = `${options.prefix}-${selector}`; | ||
} else if (options.prefix === undefined && projectPrefix) { | ||
selector = `${projectPrefix}-${selector}`; | ||
} | ||
|
||
return selector; | ||
} | ||
|
||
function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile { | ||
const text = host.read(modulePath); | ||
if (text === null) { | ||
throw new SchematicsException(`File ${modulePath} does not exist.`); | ||
} | ||
const sourceText = text.toString('utf-8'); | ||
|
||
return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); | ||
} | ||
|
||
function addImportToNgModule(options: ComponentOptions): Rule { | ||
return (host: Tree) => { | ||
if (!options.module) { | ||
return host; | ||
} | ||
|
||
const modulePath = options.module; | ||
const moduleSource = readIntoSourceFile(host, modulePath); | ||
|
||
const componentModulePath = `/${options.path}/` | ||
+ (options.flat ? '' : strings.dasherize(options.name) + '/') | ||
+ strings.dasherize(options.name) | ||
+ '.module'; | ||
|
||
const relativePath = buildRelativePath(modulePath, componentModulePath); | ||
const classifiedName = strings.classify(`${options.name}ComponentModule`); | ||
const importChanges = addSymbolToNgModuleMetadata(moduleSource, modulePath, 'imports', classifiedName, relativePath); | ||
|
||
const importRecorder = host.beginUpdate(modulePath); | ||
for (const change of importChanges) { | ||
if (change instanceof InsertChange) { | ||
importRecorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
} | ||
host.commitUpdate(importRecorder); | ||
return host; | ||
}; | ||
} | ||
|
||
export default function(options: ComponentOptions): Rule { | ||
return (host, context) => { | ||
if (!options.project) { | ||
throw new SchematicsException('Option (project) is required.'); | ||
} | ||
|
||
const project = getProject(host, options.project); | ||
|
||
if (options.path === undefined) { | ||
options.path = buildDefaultPath(project); | ||
} | ||
|
||
const parsedPath = parseName(options.path, options.name); | ||
options.name = parsedPath.name; | ||
options.path = parsedPath.path; | ||
options.selector = options.selector ? options.selector : buildSelector(options, project.prefix); | ||
|
||
validateName(options.name); | ||
validateHtmlSelector(options.selector); | ||
|
||
const templateSource = apply(url('./files'), [ | ||
options.spec ? noop() : filter(p => !p.endsWith('.spec.ts')), | ||
template({ | ||
...strings, | ||
'if-flat': (s: string) => options.flat ? '' : s, | ||
...options, | ||
}), | ||
move(parsedPath.path), | ||
]); | ||
|
||
return chain([ | ||
branchAndMerge(chain([ | ||
addImportToNgModule(options), | ||
mergeWith(templateSource), | ||
])), | ||
])(host, context); | ||
}; | ||
} |
This file contains hidden or 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,11 @@ | ||
export interface Schema { | ||
path?: string; | ||
project?: string; | ||
name: string; | ||
prefix?: string; | ||
styleext?: string; | ||
spec?: boolean; | ||
flat?: boolean; | ||
selector?: string; | ||
module?: string; | ||
} |
This file contains hidden or 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,69 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "SchematicsIonicAngularComponent", | ||
"title": "@ionic/angular Component Options Schema", | ||
"type": "object", | ||
"properties": { | ||
"path": { | ||
"type": "string", | ||
"format": "path", | ||
"description": "The path to create the page", | ||
"visible": false | ||
}, | ||
"project": { | ||
"type": "string", | ||
"description": "The name of the project", | ||
"$default": { | ||
"$source": "projectName" | ||
} | ||
}, | ||
"name": { | ||
"type": "string", | ||
"description": "The name of the page", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
} | ||
}, | ||
"prefix": { | ||
"type": "string", | ||
"description": "The prefix to apply to generated selectors", | ||
"alias": "p", | ||
"oneOf": [ | ||
{ | ||
"maxLength": 0 | ||
}, | ||
{ | ||
"minLength": 1, | ||
"format": "html-selector" | ||
} | ||
] | ||
}, | ||
"styleext": { | ||
"type": "string", | ||
"description": "The file extension of the style file for the page", | ||
"default": "css" | ||
}, | ||
"spec": { | ||
"type": "boolean", | ||
"description": "Specifies if a spec file is generated", | ||
"default": true | ||
}, | ||
"flat": { | ||
"type": "boolean", | ||
"description": "Flag to indicate if a dir is created", | ||
"default": false | ||
}, | ||
"selector": { | ||
"type": "string", | ||
"format": "html-selector", | ||
"description": "The selector to use for the page" | ||
}, | ||
"module": { | ||
"type": "string", | ||
"description": "Allows specification of the declaring module", | ||
"alias": "m" | ||
} | ||
}, | ||
"required": [] | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.