register-locale.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. "use strict";
  2. /**
  3. * Use of this source code is governed by an MIT-style license that can be
  4. * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
  5. */
  6. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  7. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  8. return new (P || (P = Promise))(function (resolve, reject) {
  9. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  10. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  11. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  12. step((generator = generator.apply(thisArg, _arguments || [])).next());
  13. });
  14. };
  15. Object.defineProperty(exports, "__esModule", { value: true });
  16. exports.registerLocale = registerLocale;
  17. const schematics_1 = require("@angular/cdk/schematics");
  18. const schematics_2 = require("@angular-devkit/schematics");
  19. const utility_1 = require("@schematics/angular/utility");
  20. const change_1 = require("@schematics/angular/utility/change");
  21. const app_config_1 = require("@schematics/angular/utility/standalone/app_config");
  22. const util_1 = require("@schematics/angular/utility/standalone/util");
  23. const workspace_1 = require("@schematics/angular/utility/workspace");
  24. const chalk_1 = require("chalk");
  25. const ts = require("typescript");
  26. const apply_changes_1 = require("../../utils/apply-changes");
  27. function registerLocale(options) {
  28. return (host) => __awaiter(this, void 0, void 0, function* () {
  29. const workspace = yield (0, workspace_1.getWorkspace)(host);
  30. const project = (0, schematics_1.getProjectFromWorkspace)(workspace, options.project);
  31. const mainFile = (0, schematics_1.getProjectMainFile)(project);
  32. if ((0, schematics_1.isStandaloneApp)(host, mainFile)) {
  33. return registerLocaleInStandaloneApp(mainFile, options);
  34. }
  35. else {
  36. return registerLocaleInAppModule(mainFile, options);
  37. }
  38. });
  39. }
  40. function registerLocaleInAppModule(mainFile, options) {
  41. return (host) => __awaiter(this, void 0, void 0, function* () {
  42. const appModulePath = (0, schematics_1.getAppModulePath)(host, mainFile);
  43. const moduleSource = (0, schematics_1.parseSourceFile)(host, appModulePath);
  44. const locale = options.locale || 'en_US';
  45. const localePrefix = locale.split('_')[0];
  46. (0, apply_changes_1.applyChangesToFile)(host, appModulePath, [
  47. (0, schematics_1.insertImport)(moduleSource, appModulePath, 'provideNzI18n', 'ng-zorro-antd/i18n'),
  48. (0, schematics_1.insertImport)(moduleSource, appModulePath, locale, 'ng-zorro-antd/i18n'),
  49. (0, schematics_1.insertImport)(moduleSource, appModulePath, 'registerLocaleData', '@angular/common'),
  50. (0, schematics_1.insertImport)(moduleSource, appModulePath, localePrefix, `@angular/common/locales/${localePrefix}`, true),
  51. registerLocaleData(moduleSource, appModulePath, localePrefix),
  52. ...insertI18nTokenProvide(moduleSource, appModulePath, locale)
  53. ]);
  54. });
  55. }
  56. function registerLocaleInStandaloneApp(mainFile, options) {
  57. const locale = options.locale || 'en_US';
  58. return (0, schematics_2.chain)([
  59. (host) => __awaiter(this, void 0, void 0, function* () {
  60. const bootstrapCall = (0, util_1.findBootstrapApplicationCall)(host, mainFile);
  61. const appConfig = (0, app_config_1.findAppConfig)(bootstrapCall, host, mainFile);
  62. const appConfigFile = appConfig.filePath;
  63. const appConfigSource = (0, schematics_1.parseSourceFile)(host, appConfig.filePath);
  64. const localePrefix = locale.split('_')[0];
  65. (0, apply_changes_1.applyChangesToFile)(host, appConfigFile, [
  66. (0, schematics_1.insertImport)(appConfigSource, appConfigFile, locale, 'ng-zorro-antd/i18n'),
  67. (0, schematics_1.insertImport)(appConfigSource, appConfigFile, 'registerLocaleData', '@angular/common'),
  68. (0, schematics_1.insertImport)(appConfigSource, appConfigFile, localePrefix, `@angular/common/locales/${localePrefix}`, true),
  69. registerLocaleData(appConfigSource, appConfigFile, localePrefix)
  70. ]);
  71. }),
  72. (0, utility_1.addRootProvider)(options.project, ({ code, external }) => {
  73. return code `${external('provideNzI18n', 'ng-zorro-antd/i18n')}(${locale})`;
  74. })
  75. ]);
  76. }
  77. function registerLocaleData(moduleSource, modulePath, locale) {
  78. const allImports = (0, schematics_1.findNodes)(moduleSource, ts.SyntaxKind.ImportDeclaration);
  79. const allFun = (0, schematics_1.findNodes)(moduleSource, ts.SyntaxKind.ExpressionStatement);
  80. const registerLocaleDataFun = allFun.filter(node => {
  81. var _a;
  82. const fun = node.getChildren();
  83. return ((_a = fun[0].getChildren()[0]) === null || _a === void 0 ? void 0 : _a.getText()) === 'registerLocaleData';
  84. });
  85. if (registerLocaleDataFun.length === 0) {
  86. return (0, schematics_1.insertAfterLastOccurrence)(allImports, `\n\nregisterLocaleData(${locale});`, modulePath, 0);
  87. }
  88. else {
  89. console.log();
  90. console.log((0, chalk_1.yellow)(`Could not add the registerLocaleData to file (${(0, chalk_1.blue)(modulePath)}).` +
  91. `because there is already a registerLocaleData function.`));
  92. console.log((0, chalk_1.yellow)(`Please manually add the following code:`));
  93. console.log((0, chalk_1.cyan)(`registerLocaleData(${locale});`));
  94. return new change_1.NoopChange();
  95. }
  96. }
  97. function insertI18nTokenProvide(moduleSource, modulePath, locale) {
  98. const metadataField = 'providers';
  99. const nodes = (0, schematics_1.getDecoratorMetadata)(moduleSource, 'NgModule', '@angular/core');
  100. const addProvide = (0, schematics_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'providers', `provideNzI18n(${locale})`, null);
  101. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  102. const node = nodes[0];
  103. if (!node) {
  104. return [];
  105. }
  106. const matchingProperties = node.properties
  107. .filter(prop => prop.kind === ts.SyntaxKind.PropertyAssignment)
  108. .filter((prop) => {
  109. const name = prop.name;
  110. switch (name.kind) {
  111. case ts.SyntaxKind.Identifier:
  112. return name.getText(moduleSource) === metadataField;
  113. case ts.SyntaxKind.StringLiteral:
  114. return name.text === metadataField;
  115. }
  116. return false;
  117. });
  118. if (!matchingProperties) {
  119. return [];
  120. }
  121. if (matchingProperties.length) {
  122. const assignment = matchingProperties[0];
  123. if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
  124. return [];
  125. }
  126. const arrLiteral = assignment.initializer;
  127. if (arrLiteral.elements.length === 0) {
  128. return addProvide;
  129. }
  130. else {
  131. const provideWithToken = arrLiteral.elements.some(e => { var _a; return (_a = e.getText) === null || _a === void 0 ? void 0 : _a.call(e).includes('NZ_I18N'); });
  132. const provideWithFunc = arrLiteral.elements.some(e => { var _a; return (_a = e.getText) === null || _a === void 0 ? void 0 : _a.call(e).includes('provideNzI18n'); });
  133. if (!provideWithFunc && !provideWithToken) {
  134. return addProvide;
  135. }
  136. console.log();
  137. console.log((0, chalk_1.yellow)(`Could not provide the locale token to file (${(0, chalk_1.blue)(modulePath)}), because there is already a locale token in providers.`));
  138. if (provideWithToken) {
  139. console.log((0, chalk_1.yellow)(`Please manually add the following code to your providers:`));
  140. console.log((0, chalk_1.cyan)(`provideNzI18n(${locale})`));
  141. }
  142. return [];
  143. }
  144. }
  145. else {
  146. return addProvide;
  147. }
  148. }
  149. //# sourceMappingURL=register-locale.js.map