Skip to content

Commit

Permalink
feat(schematics): Import ChartsModule in module
Browse files Browse the repository at this point in the history
When generating a charts schematics of any kind, make sure that
`ChartsModule` is added to the `imports` section of the selected
module.
  • Loading branch information
paviad committed Mar 13, 2019
1 parent a02ed16 commit 611f081
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ng2-charts-schematics/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng2-charts-schematics",
"version": "0.1.6",
"version": "0.1.7",
"description": "ng2-charts schematics collection for generating chart components",
"scripts": {
"build": "tsc -p tsconfig.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ export function ng2ChartsBar(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,6 @@ export function ng2ChartsBubble(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export function ng2ChartsDoughnut(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ const newImports: [string, string][] = [
// You don't have to export the function as default. You can also have more than one rule factory
// per file.
export function ng2ChartsLine(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export function ng2ChartsPie(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export function ng2ChartsPolarArea(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ export function ng2ChartsRadar(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export function ng2ChartsScatter(_options: any): Rule {
// console.log('options', _options);
return chain([
externalSchematic('@schematics/angular', 'component', _options),
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, newCode, newMarkup, newImports)
(tree: Tree, _context: SchematicContext) => ng2ProcessTree(tree, _context, _options, newCode, newMarkup, newImports)
]);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { SchematicContext, Tree } from '@angular-devkit/schematics';
import { getSourceNodes, findNodes, insertImport } from '@schematics/angular/utility/ast-utils';
import { SchematicContext, Tree, SchematicsException } from '@angular-devkit/schematics';
import { getSourceNodes, findNodes, insertImport, addImportToModule } from '@schematics/angular/utility/ast-utils';
import { findModuleFromOptions } from '@schematics/angular/utility/find-module';
import { readIntoSourceFile } from './read-into-source-file';
import * as ts from 'typescript';
import { InsertChange } from '@schematics/angular/utility/change';
import { buildDefaultPath, getProject } from '@schematics/angular/utility/project';

export function ng2ProcessTree(
tree: Tree,
_context: SchematicContext,
_options: any,
newCode: string,
newMarkup: string,
newImports: [string, string][] = []) {
if (!_options.project) {
throw new SchematicsException('Option (project) is required.');
}
const project = getProject(tree, _options.project);

if (_options.path === undefined) {
_options.path = buildDefaultPath(project);
}

_options.module = findModuleFromOptions(tree, _options);

const codeAction = tree.actions.filter(r => r.path.endsWith('.component.ts'))[0];
const markupActions = tree.actions.filter(r => r.path.endsWith('.component.html'));
const codeSource = readIntoSourceFile(tree, codeAction.path);
Expand Down Expand Up @@ -48,6 +62,16 @@ export function ng2ProcessTree(
recorder.insertLeft(change.pos, change.toAdd);
}
}
if (_options.module) {
const moduleSource = readIntoSourceFile(tree, _options.module);
const addImport = addImportToModule(moduleSource, _options.module, 'ChartsModule', 'ng2-charts');
console.log('import', addImport);
for (const change of addImport) {
if (change instanceof InsertChange) {
recorder.insertLeft(change.pos, change.toAdd);
}
}
}
tree.commitUpdate(recorder);
return tree;
}
18 changes: 18 additions & 0 deletions ng2-charts-schematics/src/ng2-charts-schematics/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@
"type": "object",
"description": "description 1",
"properties": {
"path": {
"type": "string",
"format": "path",
"description": "The path at which to create the component file, relative to the current workspace. Default is a folder with the same name as the component in the project root.",
"visible": false
},
"project": {
"type": "string",
"description": "The name of the project.",
"$default": {
"$source": "projectName"
}
},
"name": {
"type": "string",
"description": "The name of the component.",
Expand All @@ -19,6 +32,11 @@
"type": "boolean",
"default": false,
"alias": "t"
},
"module": {
"type": "string",
"description": "The declaring NgModule.",
"alias": "m"
}
},
"required": [
Expand Down

0 comments on commit 611f081

Please sign in to comment.