setup-project.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.io/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. const schematics_1 = require("@angular-devkit/schematics");
  11. const schematics_2 = require("@angular/cdk/schematics");
  12. const chalk_1 = require("chalk");
  13. const config_1 = require("@schematics/angular/utility/config");
  14. const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
  15. const material_fonts_1 = require("./fonts/material-fonts");
  16. const hammerjs_import_1 = require("./gestures/hammerjs-import");
  17. const theming_1 = require("./theming/theming");
  18. /** Name of the Angular module that enables Angular browser animations. */
  19. const browserAnimationsModuleName = 'BrowserAnimationsModule';
  20. /** Name of the module that switches Angular animations to a noop implementation. */
  21. const noopAnimationsModuleName = 'NoopAnimationsModule';
  22. /**
  23. * Scaffolds the basics of a Angular Material application, this includes:
  24. * - Add Packages to package.json
  25. * - Adds pre-built themes to styles.ext
  26. * - Adds Browser Animation to app.module
  27. */
  28. function default_1(options) {
  29. return schematics_1.chain([
  30. options && options.gestures ? hammerjs_import_1.addHammerJsToMain(options) : schematics_1.noop(),
  31. addAnimationsModule(options),
  32. theming_1.addThemeToAppStyles(options),
  33. material_fonts_1.addFontsToIndex(options),
  34. addMaterialAppStyles(options),
  35. ]);
  36. }
  37. exports.default = default_1;
  38. /**
  39. * Adds an animation module to the root module of the specified project. In case the "animations"
  40. * option is set to false, we still add the `NoopAnimationsModule` because otherwise various
  41. * components of Angular Material will throw an exception.
  42. */
  43. function addAnimationsModule(options) {
  44. return (host) => {
  45. const workspace = config_1.getWorkspace(host);
  46. const project = schematics_2.getProjectFromWorkspace(workspace, options.project);
  47. const appModulePath = ng_ast_utils_1.getAppModulePath(host, schematics_2.getProjectMainFile(project));
  48. if (options.animations) {
  49. // In case the project explicitly uses the NoopAnimationsModule, we should print a warning
  50. // message that makes the user aware of the fact that we won't automatically set up
  51. // animations. If we would add the BrowserAnimationsModule while the NoopAnimationsModule
  52. // is already configured, we would cause unexpected behavior and runtime exceptions.
  53. if (schematics_2.hasNgModuleImport(host, appModulePath, noopAnimationsModuleName)) {
  54. return console.warn(chalk_1.red(`Could not set up "${chalk_1.bold(browserAnimationsModuleName)}" ` +
  55. `because "${chalk_1.bold(noopAnimationsModuleName)}" is already imported. Please manually ` +
  56. `set up browser animations.`));
  57. }
  58. schematics_2.addModuleImportToRootModule(host, browserAnimationsModuleName, '@angular/platform-browser/animations', project);
  59. }
  60. else if (!schematics_2.hasNgModuleImport(host, appModulePath, browserAnimationsModuleName)) {
  61. // Do not add the NoopAnimationsModule module if the project already explicitly uses
  62. // the BrowserAnimationsModule.
  63. schematics_2.addModuleImportToRootModule(host, noopAnimationsModuleName, '@angular/platform-browser/animations', project);
  64. }
  65. return host;
  66. };
  67. }
  68. /**
  69. * Adds custom Material styles to the project style file. The custom CSS sets up the Roboto font
  70. * and reset the default browser body margin.
  71. */
  72. function addMaterialAppStyles(options) {
  73. return (host) => {
  74. const workspace = config_1.getWorkspace(host);
  75. const project = schematics_2.getProjectFromWorkspace(workspace, options.project);
  76. const styleFilePath = schematics_2.getProjectStyleFile(project);
  77. if (!styleFilePath) {
  78. console.warn(chalk_1.red(`Could not find the default style file for this project.`));
  79. console.warn(chalk_1.red(`Please consider manually setting up the Roboto font in your CSS.`));
  80. return;
  81. }
  82. const buffer = host.read(styleFilePath);
  83. if (!buffer) {
  84. console.warn(chalk_1.red(`Could not read the default style file within the project ` +
  85. `(${chalk_1.italic(styleFilePath)})`));
  86. console.warn(chalk_1.red(`Please consider manually setting up the Robot font.`));
  87. return;
  88. }
  89. const htmlContent = buffer.toString();
  90. const insertion = '\n' +
  91. `html, body { height: 100%; }\n` +
  92. `body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }\n`;
  93. if (htmlContent.includes(insertion)) {
  94. return;
  95. }
  96. const recorder = host.beginUpdate(styleFilePath);
  97. recorder.insertLeft(htmlContent.length, insertion);
  98. host.commitUpdate(recorder);
  99. };
  100. }
  101. //# sourceMappingURL=setup-project.js.map