Skip to content

Commit

Permalink
fix(schematics): compatible with old version options (#2622)
Browse files Browse the repository at this point in the history
* fix(schematics): compatible with old version options

* refactor: add deprecated warn and format code style
  • Loading branch information
hsuanxyz authored Dec 12, 2018
1 parent 82ac7c5 commit bb1489b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 13 deletions.
14 changes: 8 additions & 6 deletions schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics';
import { NodePackageInstallTask, RunSchematicTask } from '@angular-devkit/schematics/tasks';
import { hammerjsVersion, zorroVersion } from '../utils/version-names';
import { addPackageToPackageJson } from '../utils/package-config';
import { hammerjsVersion, zorroVersion } from '../utils/version-names';
import { Schema } from './schema';

export default function (options: Schema): Rule {
return (host: Tree, context: SchematicContext) => {

addPackageToPackageJson(host, 'ng-zorro-antd', zorroVersion);
if (options.gestures) {
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
if (!options.skipPackageJson) {
addPackageToPackageJson(host, 'ng-zorro-antd', zorroVersion);
if (options.gestures) {
addPackageToPackageJson(host, 'hammerjs', hammerjsVersion);
}
}

// const installTaskId = context.addTask(new NodePackageInstallTask());
const installTaskId = context.addTask(new NodePackageInstallTask());

context.addTask(new RunSchematicTask('ng-add-setup-project', options));
context.addTask(new RunSchematicTask('ng-add-setup-project', options), [ installTaskId ]);
context.addTask(new RunSchematicTask('bootstrap', options));
};
}
6 changes: 6 additions & 0 deletions schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"description": "Whether custom theme file should be set up.",
"x-prompt": "Set up custom theme file [ Detail: https://ng.ant.design/docs/customize-theme/en ]"
},
"i18n": {
"type": "string",
"default": "en_US",
"enum": ["ar_EG","bg_BG","ca_ES","cs_CZ","de_DE","el_GR","en_GB","en_US","es_ES","et_EE","fa_IR","fi_FI","fr_BE","fr_FR","is_IS","it_IT","ja_JP","ko_KR","nb_NO","nl_BE","nl_NL","pl_PL","pt_BR","pt_PT","sk_SK","sr_RS","sv_SE","th_TH","tr_TR","ru_RU","uk_UA","vi_VN","zh_CN","zh_TW"],
"description": "add locale code to module (e.g., --locale=en_US)"
},
"locale": {
"type": "string",
"description": "Add locale code to module (e.g., --locale=en_US)",
Expand Down
9 changes: 8 additions & 1 deletion schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@

export type Locale = 'ar_EG' | 'bg_BG' | 'ca_ES' | 'cs_CZ' | 'de_DE' | 'el_GR' | 'en_GB'
| 'en_US' | 'es_ES' | 'et_EE' | 'fa_IR' | 'fi_FI' | 'fr_BE' | 'fr_FR' | 'is_IS'
| 'it_IT' | 'ja_JP' | 'ko_KR' | 'nb_NO' | 'nl_BE' | 'nl_NL' | 'pl_PL' | 'pt_BR'
| 'pt_PT' | 'sk_SK' | 'sr_RS' | 'sv_SE' | 'th_TH' | 'tr_TR' | 'ru_RU' | 'uk_UA'
| 'vi_VN' | 'zh_CN' | 'zh_TW';

export interface Schema {
/** Name of the project to target. */
project?: string;
Expand All @@ -8,5 +14,6 @@ export interface Schema {
theme?: boolean;
gestures?: boolean;
animations?: boolean;
locale?: 'ar_EG' | 'bg_BG' | 'ca_ES' | 'cs_CZ' | 'de_DE' | 'el_GR' | 'en_GB' | 'en_US' | 'es_ES' | 'et_EE' | 'fa_IR' | 'fi_FI' | 'fr_BE' | 'fr_FR' | 'is_IS' | 'it_IT' | 'ja_JP' | 'ko_KR' | 'nb_NO' | 'nl_BE' | 'nl_NL' | 'pl_PL' | 'pt_BR' | 'pt_PT' | 'sk_SK' | 'sr_RS' | 'sv_SE' | 'th_TH' | 'tr_TR' | 'ru_RU' | 'uk_UA' | 'vi_VN' | 'zh_CN' | 'zh_TW';
locale?: Locale;
i18n?: Locale;
}
2 changes: 1 addition & 1 deletion schematics/ng-add/setup-project/add-animations-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const browserAnimationsModuleName = 'BrowserAnimationsModule';
const noopAnimationsModuleName = 'NoopAnimationsModule';
const animationsModulePath = '@angular/platform-browser/animations';

export function addAnimationsModule (options: Schema): Rule {
export function addAnimationsModule(options: Schema): Rule {
return (host: Tree) => {
const workspace = getWorkspace(host);
const project = getProjectFromWorkspace(workspace, options.project);
Expand Down
2 changes: 1 addition & 1 deletion schematics/ng-add/setup-project/add-required-modules.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WorkspaceProject } from '@angular-devkit/core/src/workspace'
import { WorkspaceProject } from '@angular-devkit/core/src/workspace';
import { Rule, Tree } from '@angular-devkit/schematics';
import {
addModuleImportToRootModule,
Expand Down
2 changes: 1 addition & 1 deletion schematics/ng-add/setup-project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default function (options: Schema): Rule {
return chain([
addRequiredModules(options),
addAnimationsModule(options),
addThemeToAppStyles(options),
registerLocale(options),
addThemeToAppStyles(options),
options.dynamicIcon ? addIconToAssets(options) : noop(),
options.gestures ? hammerjsImport(options) : noop()
]);
Expand Down
24 changes: 21 additions & 3 deletions schematics/ng-add/setup-project/register-locale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function registerLocale(options: Schema): Rule {
const appModulePath = getAppModulePath(host, getProjectMainFile(project));
const moduleSource = getSourceFile(host, appModulePath);

const locale = options.locale || 'en_US';
const locale = getCompatibleLocal(options);
const localePrefix = locale.split('_')[ 0 ];

const recorder = host.beginUpdate(appModulePath);
Expand Down Expand Up @@ -52,6 +52,22 @@ export function registerLocale(options: Schema): Rule {
};
}

function getCompatibleLocal(options: Schema): string {
const defaultLocal = 'en_US';
if (options.locale === options.i18n) {
return options.locale;
} else if (options.locale === defaultLocal) {

console.log();
console.log(`${chalk.bgYellow('WARN')} ${chalk.cyan('--i18n')} option will be deprecated, ` +
`use ${chalk.cyan('--locale')} instead`);

return options.i18n;
} else {
return options.locale || defaultLocal;
}
}

function registerLocaleData(moduleSource: ts.SourceFile, modulePath: string, locale: string): Change {
const allImports = findNodes(moduleSource, ts.SyntaxKind.ImportDeclaration);
const allFun = findNodes(moduleSource, ts.SyntaxKind.ExpressionStatement);
Expand All @@ -62,7 +78,8 @@ function registerLocaleData(moduleSource: ts.SourceFile, modulePath: string, loc
});

if (registerLocaleDataFun.length === 0) {
return insertAfterLastOccurrence(allImports, `\n\nregisterLocaleData(${locale});`, modulePath, 0) as InsertChange;
return insertAfterLastOccurrence(allImports, `\n\nregisterLocaleData(${locale});`,
modulePath, 0) as InsertChange;
} else {
console.log();
console.log(chalk.yellow(`Could not add the registerLocaleData to your app.module file (${chalk.blue(modulePath)}).` +
Expand All @@ -76,7 +93,8 @@ function registerLocaleData(moduleSource: ts.SourceFile, modulePath: string, loc
function insertI18nTokenProvide(moduleSource: ts.SourceFile, modulePath: string, locale: string): Change[] {
const metadataField = 'providers';
const nodes = getDecoratorMetadata(moduleSource, 'NgModule', '@angular/core');
const addProvide = addSymbolToNgModuleMetadata(moduleSource, modulePath, 'providers', `{ provide: NZ_I18N, useValue: ${locale} }`, null);
const addProvide = addSymbolToNgModuleMetadata(moduleSource, modulePath, 'providers',
`{ provide: NZ_I18N, useValue: ${locale} }`, null);
let node: any = nodes[ 0 ]; // tslint:disable-line:no-any

if (!node) {
Expand Down

0 comments on commit bb1489b

Please sign in to comment.