setup-project.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.default = default_1;
  11. const schematics_1 = require("@angular-devkit/schematics");
  12. const schematics_2 = require("@angular/cdk/schematics");
  13. const workspace_1 = require("@schematics/angular/utility/workspace");
  14. const workspace_models_1 = require("@schematics/angular/utility/workspace-models");
  15. const material_fonts_1 = require("./fonts/material-fonts");
  16. const theming_1 = require("./theming/theming");
  17. /**
  18. * Scaffolds the basics of a Angular Material application, this includes:
  19. * - Add Packages to package.json
  20. * - Adds pre-built themes to styles.ext
  21. */
  22. function default_1(options) {
  23. return async (host, context) => {
  24. const workspace = await (0, workspace_1.getWorkspace)(host);
  25. const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
  26. if (project.extensions['projectType'] === workspace_models_1.ProjectType.Application) {
  27. return (0, schematics_1.chain)([
  28. (0, theming_1.addThemeToAppStyles)(options),
  29. (0, material_fonts_1.addFontsToIndex)(options),
  30. addMaterialAppStyles(options),
  31. (0, theming_1.addTypographyClass)(options),
  32. ]);
  33. }
  34. context.logger.warn('Angular Material has been set up in your workspace. There is no additional setup ' +
  35. 'required for consuming Angular Material in your library project.\n\n' +
  36. 'If you intended to run the schematic on a different project, pass the `--project` ' +
  37. 'option.');
  38. return;
  39. };
  40. }
  41. /**
  42. * Adds custom Material styles to the project style file. The custom CSS sets up the Roboto font
  43. * and reset the default browser body margin.
  44. */
  45. function addMaterialAppStyles(options) {
  46. return async (host, context) => {
  47. const workspace = await (0, workspace_1.getWorkspace)(host);
  48. const project = (0, schematics_2.getProjectFromWorkspace)(workspace, options.project);
  49. const styleFilePath = (0, schematics_2.getProjectStyleFile)(project);
  50. const logger = context.logger;
  51. if (!styleFilePath) {
  52. logger.error(`Could not find the default style file for this project.`);
  53. logger.info(`Consider manually adding the Roboto font to your CSS.`);
  54. logger.info(`More information at https://fonts.google.com/specimen/Roboto`);
  55. return;
  56. }
  57. const buffer = host.read(styleFilePath);
  58. if (!buffer) {
  59. logger.error(`Could not read the default style file within the project ` + `(${styleFilePath})`);
  60. logger.info(`Please consider manually setting up the Roboto font.`);
  61. return;
  62. }
  63. const htmlContent = buffer.toString();
  64. const insertion = '\n' +
  65. `html, body { height: 100%; }\n` +
  66. `body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }\n`;
  67. if (htmlContent.includes(insertion)) {
  68. return;
  69. }
  70. const recorder = host.beginUpdate(styleFilePath);
  71. recorder.insertLeft(htmlContent.length, insertion);
  72. host.commitUpdate(recorder);
  73. };
  74. }
  75. //# sourceMappingURL=setup-project.js.map