-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng-schematics): add component generate schematic
- Loading branch information
1 parent
90a22f0
commit a0e86c2
Showing
9 changed files
with
133 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
apply, chain, MergeStrategy, mergeWith, | ||
Rule, SchematicContext, SchematicsException, template, Tree, url } from "@angular-devkit/schematics"; | ||
import { NodePackageInstallTask } from "@angular-devkit/schematics/tasks"; | ||
import { IgniteUIForAngularTemplate } from "@igniteui-angular/templates"; | ||
import { NgTreeFileSystem, Util } from "@igniteui-cli/core"; | ||
import { SchematicsTemplateManager } from "../SchematicsTemplateManager"; | ||
import { ComponentOptions } from "./schema"; | ||
|
||
export function component(options: ComponentOptions): Rule { | ||
return (_tree: Tree, context: SchematicContext) => { | ||
|
||
// TODO: validate template name + nameFromPath, id, modulePath, skipRoute | ||
// TODO: jump into prompt session w/o provided id? | ||
|
||
if (!options.templateInst) { | ||
if (!options.template) { | ||
throw new SchematicsException("argument template id must be provided"); | ||
} | ||
const templateManager = new SchematicsTemplateManager(); | ||
const projLib = templateManager.getProjectLibrary("angular", "igx-ts"); | ||
options.templateInst = projLib.getTemplateById(options.template) as IgniteUIForAngularTemplate; | ||
} | ||
|
||
const config = options.templateInst.generateConfig(options.name, {}); | ||
|
||
context.logger.info(`Generating ${options.templateInst.name} with name: ${options.name}`); | ||
// TODO: reuse component schematic? | ||
return chain([ | ||
...options.templateInst.templatePaths.map(templatePath => | ||
mergeWith( | ||
apply(url(Util.relativePath(__filename, templatePath, true)), [ | ||
template(config) | ||
] | ||
), MergeStrategy.Overwrite) | ||
), | ||
(host: Tree, _context: SchematicContext) => { | ||
if (options.templateInst) { | ||
for (const packageName of options.templateInst.packages) { | ||
context.addTask(new NodePackageInstallTask({ packageName, workingDirectory: options.projectName })); | ||
} | ||
options.templateInst.virtFs = new NgTreeFileSystem(host); | ||
options.templateInst.registerInProject("", options.name, { skipRoute: options.skipRoute }); | ||
} | ||
} | ||
]); | ||
}; | ||
} |
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 @@ | ||
import { Tree } from "@angular-devkit/schematics"; | ||
import { SchematicTestRunner } from "@angular-devkit/schematics/testing"; | ||
import * as path from "path"; | ||
|
||
const collectionPath = path.join(__dirname, "../collection.json"); | ||
|
||
describe("component", () => { | ||
it("works", () => { | ||
const runner = new SchematicTestRunner("schematics", collectionPath); | ||
const tree = runner.runSchematic("component", {}, Tree.empty()); | ||
|
||
expect(tree.files).toEqual([]); | ||
}); | ||
}); |
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,29 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"id": "SchematicsAngularComponent", | ||
"title": "Angular Component Options Schema", | ||
"type": "object", | ||
"description": "Creates a new Ignite UI for Angular component in the given or default project.", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"description": "The name of the component.", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 1 | ||
}, | ||
"x-prompt": "What name would you like to use for the component?" | ||
}, | ||
"template": { | ||
"type": "string", | ||
"description": "Template ID, such as 'grid', 'combo', etc.", | ||
"$default": { | ||
"$source": "argv", | ||
"index": 0 | ||
} | ||
} | ||
}, | ||
"required": [ | ||
"name" | ||
] | ||
} |
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,9 @@ | ||
import { IgniteUIForAngularTemplate } from "@igniteui-angular/templates"; | ||
|
||
export abstract class ComponentOptions { | ||
public templateInst?: IgniteUIForAngularTemplate; | ||
public template?: string; | ||
public name: string; | ||
public skipRoute?: boolean; | ||
public projectName: string; | ||
} |
10 changes: 0 additions & 10 deletions
10
packages/ng-schematics/src/igniteui-angular-schematics/index.ts
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
packages/ng-schematics/src/igniteui-angular-schematics/index_spec.ts
This file was deleted.
Oops, something went wrong.
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