refactor(schematics): use schematic context to format the generated code
This commit is contained in:
parent
acdeb1b71c
commit
e7481a790f
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,5 +1,6 @@
|
||||
node_modules
|
||||
.idea
|
||||
.vscode
|
||||
dist
|
||||
build
|
||||
.DS_Store
|
||||
|
||||
@ -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,6 +156,7 @@ function updateComponentTemplate(options: NormalizedSchema): Rule {
|
||||
}
|
||||
|
||||
export default function(schema: Schema): Rule {
|
||||
return wrapIntoFormat(() => {
|
||||
let npmScope = schema.npmScope;
|
||||
if (!npmScope) {
|
||||
npmScope = readCliConfigFile().project.npmScope;
|
||||
@ -172,6 +175,7 @@ export default function(schema: Schema): Rule {
|
||||
]);
|
||||
|
||||
const selector = `${options.prefix}-root`;
|
||||
|
||||
return chain([
|
||||
branchAndMerge(chain([mergeWith(templateSource)])),
|
||||
externalSchematic('@schematics/angular', 'module', {
|
||||
@ -200,6 +204,7 @@ export default function(schema: Schema): Rule {
|
||||
addAppToAngularCliJson(options),
|
||||
options.routing ? addRouterRootConfiguration(options.fullPath) : noop()
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeOptions(options: Schema): NormalizedSchema {
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
import { apply, branchAndMerge, chain, mergeWith, move, Rule, template, Tree, url } from '@angular-devkit/schematics';
|
||||
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 {wrapIntoFormat} from '../utility/tasks';
|
||||
|
||||
export default function(options: Schema): Rule {
|
||||
return wrapIntoFormat(() => {
|
||||
const npmScope = options.npmScope ? options.npmScope : options.name;
|
||||
const templateSource = apply(url('./files'), [
|
||||
template({
|
||||
@ -14,6 +16,6 @@ export default function(options: Schema): Rule {
|
||||
npmScope
|
||||
})
|
||||
]);
|
||||
|
||||
return chain([branchAndMerge(chain([mergeWith(templateSource)]))]);
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
import { chain, noop, Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
|
||||
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,6 +72,7 @@ function addEntryComponentsToModule(options: Schema): Rule {
|
||||
}
|
||||
|
||||
export default function(options: Schema): Rule {
|
||||
return wrapIntoFormat(() => {
|
||||
const angularJsImport = options.angularJsImport ? options.angularJsImport : options.name;
|
||||
|
||||
return chain([
|
||||
@ -79,4 +81,5 @@ export default function(options: Schema): Rule {
|
||||
rewriteBootstrapLogic(options),
|
||||
options.skipPackageJson ? noop() : addUpgradeToPackageJson()
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,24 +1,13 @@
|
||||
import {
|
||||
apply,
|
||||
branchAndMerge,
|
||||
chain,
|
||||
externalSchematic,
|
||||
mergeWith,
|
||||
move,
|
||||
noop,
|
||||
Rule,
|
||||
template,
|
||||
Tree,
|
||||
url
|
||||
} from '@angular-devkit/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 {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 {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,6 +158,7 @@ function updateTsLint(schema: NormalizedSchema): Rule {
|
||||
}
|
||||
|
||||
export default function(schema: Schema): Rule {
|
||||
return wrapIntoFormat(() => {
|
||||
const options = normalizeOptions(schema);
|
||||
const moduleFileName = `${toFileName(options.name)}.module`;
|
||||
const modulePath = `${options.fullPath}/${moduleFileName}.ts`;
|
||||
@ -201,6 +191,7 @@ export default function(schema: Schema): Rule {
|
||||
options.routing && !options.lazy ? addRouterConfiguration(options, indexFile, moduleFileName, modulePath) : noop(),
|
||||
options.routing && !options.lazy && options.parentModule ? addChildren(options) : noop()
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
function normalizeOptions(options: Schema): NormalizedSchema {
|
||||
|
||||
@ -14,12 +14,12 @@ import {
|
||||
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 {addImportToModule, addProviderToModule, insert} from '../utility/ast-utils';
|
||||
import {insertImport} from '@schematics/angular/utility/route-utils';
|
||||
import {Schema} from './schema';
|
||||
import { InsertChange } from '@schematics/angular/utility/change';
|
||||
import {ngrxVersion, routerStoreVersion} from '../utility/lib-versions';
|
||||
import {serializeJson} from '../utility/fileutils';
|
||||
import {wrapIntoFormat} from '../utility/tasks';
|
||||
|
||||
function addImportsToModule(name: string, options: Schema): Rule {
|
||||
return (host: Tree) => {
|
||||
@ -129,6 +129,7 @@ function addNgRxToPackageJson() {
|
||||
}
|
||||
|
||||
export default function(options: Schema): Rule {
|
||||
return wrapIntoFormat(() => {
|
||||
const name = options.name;
|
||||
const moduleDir = path.dirname(options.module);
|
||||
|
||||
@ -142,4 +143,5 @@ export default function(options: Schema): Rule {
|
||||
options.skipPackageJson ? noop() : addNgRxToPackageJson()
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -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,6 +109,7 @@ function createFiles(angularJsImport: string, options: Schema): Rule {
|
||||
}
|
||||
|
||||
export default function(options: Schema): Rule {
|
||||
return wrapIntoFormat(() => {
|
||||
const angularJsImport = options.angularJsImport ? options.angularJsImport : options.name;
|
||||
|
||||
return chain([
|
||||
@ -116,4 +118,5 @@ export default function(options: Schema): Rule {
|
||||
addNgDoBootstrapToModule(options),
|
||||
options.skipPackageJson ? noop() : addUpgradeToPackageJson()
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
20
packages/schematics/src/collection/utility/tasks.ts
Normal file
20
packages/schematics/src/collection/utility/tasks.ts
Normal 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);
|
||||
};
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user