refactor(schematics): use schematic context to format the generated code

This commit is contained in:
vsavkin 2018-02-25 18:48:23 -05:00
parent acdeb1b71c
commit e7481a790f
8 changed files with 169 additions and 142 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
node_modules
.idea
.vscode
dist
build
.DS_Store

View File

@ -11,7 +11,8 @@ import {
Rule,
template,
Tree,
url
url,
SchematicContext
} from '@angular-devkit/schematics';
import { Schema } from './schema';
import {strings} from '@angular-devkit/core';
@ -22,6 +23,7 @@ import { insertImport } from '@schematics/angular/utility/route-utils';
import { addApp, serializeJson, cliConfig, readCliConfigFile } from '../utility/fileutils';
import { addImportToTestBed } from '../utility/ast-utils';
import { offsetFromRoot } from '../utility/common';
import {FormatFiles, wrapIntoFormat} from '../utility/tasks';
interface NormalizedSchema extends Schema {
fullName: string;
@ -154,52 +156,55 @@ function updateComponentTemplate(options: NormalizedSchema): Rule {
}
export default function(schema: Schema): Rule {
let npmScope = schema.npmScope;
if (!npmScope) {
npmScope = readCliConfigFile().project.npmScope;
}
return wrapIntoFormat(() => {
let npmScope = schema.npmScope;
if (!npmScope) {
npmScope = readCliConfigFile().project.npmScope;
}
const options = normalizeOptions(schema);
const templateSource = apply(url('./files'), [
template({
utils: strings,
dot: '.',
tmpl: '',
offsetFromRoot: offsetFromRoot(options.fullPath),
...(options as object),
npmScope
})
]);
const options = normalizeOptions(schema);
const templateSource = apply(url('./files'), [
template({
utils: strings,
dot: '.',
tmpl: '',
offsetFromRoot: offsetFromRoot(options.fullPath),
...(options as object),
npmScope
})
]);
const selector = `${options.prefix}-root`;
return chain([
branchAndMerge(chain([mergeWith(templateSource)])),
externalSchematic('@schematics/angular', 'module', {
name: 'app',
commonModule: false,
flat: true,
routing: false,
sourceDir: options.fullPath,
spec: false
}),
externalSchematic('@schematics/angular', 'component', {
name: 'app',
selector: selector,
sourceDir: options.fullPath,
flat: true,
inlineStyle: options.inlineStyle,
inlineTemplate: options.inlineTemplate,
spec: !options.skipTests,
styleext: options.style,
viewEncapsulation: options.viewEncapsulation,
changeDetection: options.changeDetection
}),
updateComponentTemplate(options),
addBootstrap(options.fullPath),
addNxModule(options.fullPath),
addAppToAngularCliJson(options),
options.routing ? addRouterRootConfiguration(options.fullPath) : noop()
]);
const selector = `${options.prefix}-root`;
return chain([
branchAndMerge(chain([mergeWith(templateSource)])),
externalSchematic('@schematics/angular', 'module', {
name: 'app',
commonModule: false,
flat: true,
routing: false,
sourceDir: options.fullPath,
spec: false
}),
externalSchematic('@schematics/angular', 'component', {
name: 'app',
selector: selector,
sourceDir: options.fullPath,
flat: true,
inlineStyle: options.inlineStyle,
inlineTemplate: options.inlineTemplate,
spec: !options.skipTests,
styleext: options.style,
viewEncapsulation: options.viewEncapsulation,
changeDetection: options.changeDetection
}),
updateComponentTemplate(options),
addBootstrap(options.fullPath),
addNxModule(options.fullPath),
addAppToAngularCliJson(options),
options.routing ? addRouterRootConfiguration(options.fullPath) : noop()
]);
});
}
function normalizeOptions(options: Schema): NormalizedSchema {

View File

@ -1,19 +1,21 @@
import { apply, branchAndMerge, chain, mergeWith, move, Rule, template, Tree, url } from '@angular-devkit/schematics';
import { Schema } from './schema';
import {apply, branchAndMerge, chain, mergeWith, Rule, template, url} from '@angular-devkit/schematics';
import {Schema} from './schema';
import {strings} from '@angular-devkit/core';
import { libVersions } from '../utility/lib-versions';
import {libVersions} from '../utility/lib-versions';
import {wrapIntoFormat} from '../utility/tasks';
export default function(options: Schema): Rule {
const npmScope = options.npmScope ? options.npmScope : options.name;
const templateSource = apply(url('./files'), [
template({
utils: strings,
dot: '.',
...libVersions,
...(options as object),
npmScope
})
]);
return chain([branchAndMerge(chain([mergeWith(templateSource)]))]);
return wrapIntoFormat(() => {
const npmScope = options.npmScope ? options.npmScope : options.name;
const templateSource = apply(url('./files'), [
template({
utils: strings,
dot: '.',
...libVersions,
...(options as object),
npmScope
})
]);
return chain([branchAndMerge(chain([mergeWith(templateSource)]))]);
});
}

View File

@ -1,7 +1,8 @@
import { chain, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { addEntryComponents, addMethod, insert, readBootstrapInfo, removeFromNgModule } from '../utility/ast-utils';
import { Schema } from './schema';
import { addUpgradeToPackageJson } from '../utility/common';
import {chain, noop, Rule, Tree} from '@angular-devkit/schematics';
import {addEntryComponents, addMethod, insert, readBootstrapInfo, removeFromNgModule} from '../utility/ast-utils';
import {Schema} from './schema';
import {addUpgradeToPackageJson} from '../utility/common';
import {wrapIntoFormat} from '../utility/tasks';
function updateMain(angularJsImport: string, options: Schema): Rule {
return (host: Tree) => {
@ -71,12 +72,14 @@ function addEntryComponentsToModule(options: Schema): Rule {
}
export default function(options: Schema): Rule {
const angularJsImport = options.angularJsImport ? options.angularJsImport : options.name;
return wrapIntoFormat(() => {
const angularJsImport = options.angularJsImport ? options.angularJsImport : options.name;
return chain([
updateMain(angularJsImport, options),
addEntryComponentsToModule(options),
rewriteBootstrapLogic(options),
options.skipPackageJson ? noop() : addUpgradeToPackageJson()
]);
return chain([
updateMain(angularJsImport, options),
addEntryComponentsToModule(options),
rewriteBootstrapLogic(options),
options.skipPackageJson ? noop() : addUpgradeToPackageJson()
]);
});
}

View File

@ -1,24 +1,13 @@
import {
apply,
branchAndMerge,
chain,
externalSchematic,
mergeWith,
move,
noop,
Rule,
template,
Tree,
url
} from '@angular-devkit/schematics';
import { Schema } from './schema';
import { addImportToModule, insert, names, toClassName, toFileName, toPropertyName } from '@nrwl/schematics';
import {apply, branchAndMerge, chain, mergeWith, noop, Rule, template, Tree, url} from '@angular-devkit/schematics';
import {Schema} from './schema';
import {addImportToModule, insert, names, toClassName, toFileName, toPropertyName} from '@nrwl/schematics';
import * as path from 'path';
import { serializeJson, addApp, cliConfig, updateJsonFile } from '../utility/fileutils';
import { insertImport } from '@schematics/angular/utility/route-utils';
import {addApp, cliConfig, serializeJson} from '../utility/fileutils';
import {insertImport} from '@schematics/angular/utility/route-utils';
import * as ts from 'typescript';
import { addGlobal, addIncludeToTsConfig, addReexport, addRoute, offset } from '../utility/ast-utils';
import { offsetFromRoot } from '../utility/common';
import {addGlobal, addIncludeToTsConfig, addReexport, addRoute} from '../utility/ast-utils';
import {offsetFromRoot} from '../utility/common';
import {wrapIntoFormat} from '../utility/tasks';
interface NormalizedSchema extends Schema {
name: string;
@ -169,38 +158,40 @@ function updateTsLint(schema: NormalizedSchema): Rule {
}
export default function(schema: Schema): Rule {
const options = normalizeOptions(schema);
const moduleFileName = `${toFileName(options.name)}.module`;
const modulePath = `${options.fullPath}/${moduleFileName}.ts`;
const indexFile = `libs/${toFileName(options.fullName)}/index.ts`;
return wrapIntoFormat(() => {
const options = normalizeOptions(schema);
const moduleFileName = `${toFileName(options.name)}.module`;
const modulePath = `${options.fullPath}/${moduleFileName}.ts`;
const indexFile = `libs/${toFileName(options.fullName)}/index.ts`;
if (options.routing && options.nomodule) {
throw new Error(`nomodule and routing cannot be used together`);
}
if (options.routing && options.nomodule) {
throw new Error(`nomodule and routing cannot be used together`);
}
if (!options.routing && options.lazy) {
throw new Error(`routing must be set`);
}
if (!options.routing && options.lazy) {
throw new Error(`routing must be set`);
}
const templateSource = apply(url(options.nomodule ? './files' : './ngfiles'), [
template({
...names(options.name),
dot: '.',
tmpl: '',
...(options as object)
})
]);
const templateSource = apply(url(options.nomodule ? './files' : './ngfiles'), [
template({
...names(options.name),
dot: '.',
tmpl: '',
...(options as object)
})
]);
return chain([
branchAndMerge(chain([mergeWith(templateSource)])),
addLibToAngularCliJson(options),
options.routing && options.lazy ? addLazyLoadedRouterConfiguration(modulePath) : noop(),
options.routing && options.lazy ? updateTsLint(options) : noop(),
options.routing && options.lazy && options.parentModule ? addLoadChildren(options) : noop(),
return chain([
branchAndMerge(chain([mergeWith(templateSource)])),
addLibToAngularCliJson(options),
options.routing && options.lazy ? addLazyLoadedRouterConfiguration(modulePath) : noop(),
options.routing && options.lazy ? updateTsLint(options) : noop(),
options.routing && options.lazy && options.parentModule ? addLoadChildren(options) : noop(),
options.routing && !options.lazy ? addRouterConfiguration(options, indexFile, moduleFileName, modulePath) : noop(),
options.routing && !options.lazy && options.parentModule ? addChildren(options) : noop()
]);
options.routing && !options.lazy ? addRouterConfiguration(options, indexFile, moduleFileName, modulePath) : noop(),
options.routing && !options.lazy && options.parentModule ? addChildren(options) : noop()
]);
});
}
function normalizeOptions(options: Schema): NormalizedSchema {

View File

@ -11,15 +11,15 @@ import {
url
} from '@angular-devkit/schematics';
import { names, toClassName, toFileName, toPropertyName } from '../utility/name-utils';
import {names, toClassName, toFileName, toPropertyName} from '../utility/name-utils';
import * as path from 'path';
import * as ts from 'typescript';
import { addImportToModule, addProviderToModule, insert, offset } from '../utility/ast-utils';
import { insertImport } from '@schematics/angular/utility/route-utils';
import { Schema } from './schema';
import { InsertChange } from '@schematics/angular/utility/change';
import {addImportToModule, addProviderToModule, insert} from '../utility/ast-utils';
import {insertImport} from '@schematics/angular/utility/route-utils';
import {Schema} from './schema';
import {ngrxVersion, routerStoreVersion} from '../utility/lib-versions';
import { serializeJson } from '../utility/fileutils';
import {serializeJson} from '../utility/fileutils';
import {wrapIntoFormat} from '../utility/tasks';
function addImportsToModule(name: string, options: Schema): Rule {
return (host: Tree) => {
@ -129,17 +129,19 @@ function addNgRxToPackageJson() {
}
export default function(options: Schema): Rule {
const name = options.name;
const moduleDir = path.dirname(options.module);
return wrapIntoFormat(() => {
const name = options.name;
const moduleDir = path.dirname(options.module);
if (options.onlyEmptyRoot) {
return chain([addImportsToModule(name, options), options.skipPackageJson ? noop() : addNgRxToPackageJson()]);
} else {
const templateSource = apply(url('./files'), [template({ ...options, tmpl: '', ...names(name) }), move(moduleDir)]);
return chain([
branchAndMerge(chain([mergeWith(templateSource)])),
addImportsToModule(name, options),
options.skipPackageJson ? noop() : addNgRxToPackageJson()
]);
}
if (options.onlyEmptyRoot) {
return chain([addImportsToModule(name, options), options.skipPackageJson ? noop() : addNgRxToPackageJson()]);
} else {
const templateSource = apply(url('./files'), [template({...options, tmpl: '', ...names(name)}), move(moduleDir)]);
return chain([
branchAndMerge(chain([mergeWith(templateSource)])),
addImportsToModule(name, options),
options.skipPackageJson ? noop() : addNgRxToPackageJson()
]);
}
});
}

View File

@ -31,6 +31,7 @@ import { insertImport } from '@schematics/angular/utility/route-utils';
import { Schema } from './schema';
import { angularJsVersion } from '../utility/lib-versions';
import { addUpgradeToPackageJson } from '../utility/common';
import {wrapIntoFormat} from '../utility/tasks';
function addImportsToModule(options: Schema): Rule {
return (host: Tree) => {
@ -108,12 +109,14 @@ function createFiles(angularJsImport: string, options: Schema): Rule {
}
export default function(options: Schema): Rule {
const angularJsImport = options.angularJsImport ? options.angularJsImport : options.name;
return wrapIntoFormat(() => {
const angularJsImport = options.angularJsImport ? options.angularJsImport : options.name;
return chain([
createFiles(angularJsImport, options),
addImportsToModule(options),
addNgDoBootstrapToModule(options),
options.skipPackageJson ? noop() : addUpgradeToPackageJson()
]);
return chain([
createFiles(angularJsImport, options),
addImportsToModule(options),
addNgDoBootstrapToModule(options),
options.skipPackageJson ? noop() : addUpgradeToPackageJson()
]);
});
}

View File

@ -0,0 +1,20 @@
import {TaskConfigurationGenerator, TaskConfiguration, Tree, SchematicContext} from "@angular-devkit/schematics";
export class FormatFiles implements TaskConfigurationGenerator<any> {
toConfiguration(): TaskConfiguration<any> {
return {
name: 'node-package',
options: {
command: 'run format',
quiet: true
},
};
}
}
export function wrapIntoFormat(fn: Function): any {
return (host: Tree, context: SchematicContext) => {
context.addTask(new FormatFiles());
return fn()(host, context);
};
}