index.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const core_1 = require("@angular-devkit/core");
  4. const schematics_1 = require("@angular-devkit/schematics");
  5. const tasks_1 = require("@angular-devkit/schematics/tasks");
  6. const utility_1 = require("@schematics/angular/utility");
  7. const workspace_1 = require("@schematics/angular/utility/workspace");
  8. const ast_1 = require("../utils/ast");
  9. const config_1 = require("./../utils/config");
  10. const package_1 = require("./../utils/package");
  11. function addIonicAngularToPackageJson() {
  12. return (host) => {
  13. (0, package_1.addPackageToPackageJson)(host, 'dependencies', '@ionic/angular', 'latest');
  14. return host;
  15. };
  16. }
  17. function addIonicAngularToolkitToPackageJson() {
  18. return (host) => {
  19. (0, package_1.addPackageToPackageJson)(host, 'devDependencies', '@ionic/angular-toolkit', 'latest');
  20. return host;
  21. };
  22. }
  23. /**
  24. * Adds the @ionic/angular-toolkit schematics and cli configuration to the project's `angular.json` file.
  25. * @param projectName The name of the project.
  26. */
  27. function addIonicAngularToolkitToAngularJson() {
  28. return (host) => {
  29. (0, config_1.addCli)(host, '@ionic/angular-toolkit');
  30. (0, config_1.addSchematics)(host, '@ionic/angular-toolkit:component', {
  31. styleext: 'scss',
  32. });
  33. (0, config_1.addSchematics)(host, '@ionic/angular-toolkit:page', {
  34. styleext: 'scss',
  35. });
  36. return host;
  37. };
  38. }
  39. /**
  40. * Adds the `IonicModule.forRoot()` usage to the project's `AppModule`.
  41. * If the project does not use modules this will operate as a noop.
  42. * @param projectSourceRoot The source root path of the project.
  43. */
  44. function addIonicAngularModuleToAppModule(projectSourceRoot) {
  45. return (host) => {
  46. const appModulePath = `${projectSourceRoot}/app/app.module.ts`;
  47. if (host.exists(appModulePath)) {
  48. (0, ast_1.addIonicModuleImportToNgModule)(host, appModulePath);
  49. }
  50. return host;
  51. };
  52. }
  53. /**
  54. * Adds the `provideIonicAngular` usage to the project's app config.
  55. * If the project does not use an app config this will operate as a noop.
  56. * @param projectName The name of the project.
  57. * @param projectSourceRoot The source root path of the project.
  58. */
  59. function addProvideIonicAngular(projectName, projectSourceRoot) {
  60. return (host) => {
  61. const appConfig = `${projectSourceRoot}/app/app.config.ts`;
  62. if (host.exists(appConfig)) {
  63. return (0, utility_1.addRootProvider)(projectName, ({ code, external }) => code `${external('provideIonicAngular', '@ionic/angular/standalone')}({})`);
  64. }
  65. return host;
  66. };
  67. }
  68. function addIonicStyles(projectName, projectSourceRoot) {
  69. return (host) => {
  70. const ionicStyles = [
  71. 'node_modules/@ionic/angular/css/core.css',
  72. 'node_modules/@ionic/angular/css/normalize.css',
  73. 'node_modules/@ionic/angular/css/structure.css',
  74. 'node_modules/@ionic/angular/css/typography.css',
  75. 'node_modules/@ionic/angular/css/display.css',
  76. 'node_modules/@ionic/angular/css/padding.css',
  77. 'node_modules/@ionic/angular/css/float-elements.css',
  78. 'node_modules/@ionic/angular/css/text-alignment.css',
  79. 'node_modules/@ionic/angular/css/text-transformation.css',
  80. 'node_modules/@ionic/angular/css/flex-utils.css',
  81. `${projectSourceRoot}/theme/variables.css`,
  82. ];
  83. ionicStyles.forEach((entry) => {
  84. (0, config_1.addStyle)(host, projectName, entry);
  85. });
  86. return host;
  87. };
  88. }
  89. function addIonicons(projectName, projectSourceRoot) {
  90. return (host) => {
  91. const hasAppModule = host.exists(`${projectSourceRoot}/app/app.module.ts`);
  92. if (hasAppModule) {
  93. /**
  94. * Add Ionicons to the `angular.json` file only if the project
  95. * is using the lazy build of `@ionic/angular` with modules.
  96. */
  97. const ioniconsGlob = {
  98. glob: '**/*.svg',
  99. input: 'node_modules/ionicons/dist/ionicons/svg',
  100. output: './svg',
  101. };
  102. (0, config_1.addAsset)(host, projectName, 'build', ioniconsGlob);
  103. (0, config_1.addAsset)(host, projectName, 'test', ioniconsGlob);
  104. }
  105. return host;
  106. };
  107. }
  108. function addIonicConfig(projectSourceRoot) {
  109. return (host) => {
  110. const ionicConfig = 'ionic.config.json';
  111. if (!host.exists(ionicConfig)) {
  112. const hasAppModule = host.exists(`${projectSourceRoot}/app/app.module.ts`);
  113. const type = hasAppModule ? 'angular' : 'angular-standalone';
  114. host.create(ionicConfig, JSON.stringify({
  115. name: 'ionic-app',
  116. app_id: '',
  117. type,
  118. integrations: {},
  119. }, null, 2));
  120. }
  121. return host;
  122. };
  123. }
  124. function addIonicBuilder(projectName) {
  125. return (host) => {
  126. (0, config_1.addArchitectBuilder)(host, projectName, 'ionic-cordova-serve', {
  127. builder: '@ionic/angular-toolkit:cordova-serve',
  128. options: {
  129. cordovaBuildTarget: `${projectName}:ionic-cordova-build`,
  130. devServerTarget: `${projectName}:serve`,
  131. },
  132. configurations: {
  133. production: {
  134. cordovaBuildTarget: `${projectName}:ionic-cordova-build:production`,
  135. devServerTarget: `${projectName}:serve:production`,
  136. },
  137. },
  138. });
  139. (0, config_1.addArchitectBuilder)(host, projectName, 'ionic-cordova-build', {
  140. builder: '@ionic/angular-toolkit:cordova-build',
  141. options: {
  142. browserTarget: `${projectName}:build`,
  143. },
  144. configurations: {
  145. production: {
  146. browserTarget: `${projectName}:build:production`,
  147. },
  148. },
  149. });
  150. return host;
  151. };
  152. }
  153. function installNodeDeps() {
  154. return (_host, context) => {
  155. context.addTask(new tasks_1.NodePackageInstallTask());
  156. };
  157. }
  158. function ngAdd(options) {
  159. return async (host) => {
  160. const workspace = await (0, workspace_1.getWorkspace)(host);
  161. if (!options.project) {
  162. options.project = (0, config_1.getDefaultAngularAppName)(workspace);
  163. }
  164. const project = workspace.projects.get(options.project);
  165. if (!project || project.extensions.projectType !== 'application') {
  166. throw new schematics_1.SchematicsException(`Ionic Add requires a project type of "application".`);
  167. }
  168. const sourcePath = (0, core_1.join)(project.sourceRoot);
  169. const rootTemplateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files/root'), [(0, schematics_1.template)(Object.assign({}, options)), (0, schematics_1.move)(sourcePath)]);
  170. return (0, schematics_1.chain)([
  171. // @ionic/angular
  172. addIonicAngularToPackageJson(),
  173. addIonicAngularToolkitToPackageJson(),
  174. addIonicAngularToolkitToAngularJson(),
  175. addIonicAngularModuleToAppModule(sourcePath),
  176. addProvideIonicAngular(options.project, sourcePath),
  177. addIonicBuilder(options.project),
  178. addIonicStyles(options.project, sourcePath),
  179. addIonicons(options.project, sourcePath),
  180. addIonicConfig(sourcePath),
  181. (0, schematics_1.mergeWith)(rootTemplateSource),
  182. // install freshly added dependencies
  183. installNodeDeps(),
  184. ]);
  185. };
  186. }
  187. exports.default = ngAdd;