project-style-file.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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.getProjectStyleFile = getProjectStyleFile;
  11. const core_1 = require("@angular-devkit/core");
  12. const project_targets_1 = require("./project-targets");
  13. /** Regular expression that matches all possible Angular CLI default style files. */
  14. const defaultStyleFileRegex = /styles\.(c|le|sc)ss/;
  15. /** Regular expression that matches all files that have a proper stylesheet extension. */
  16. const validStyleFileRegex = /\.(c|le|sc)ss/;
  17. /**
  18. * Gets a style file with the given extension in a project and returns its path. If no
  19. * extension is specified, any style file with a valid extension will be returned.
  20. */
  21. function getProjectStyleFile(project, extension) {
  22. const buildOptions = (0, project_targets_1.getProjectTargetOptions)(project, 'build');
  23. const buildStyles = buildOptions['styles'];
  24. if (buildStyles && (0, core_1.isJsonArray)(buildStyles) && buildStyles.length) {
  25. const styles = buildStyles.map(s => (typeof s === 'string' ? s : s.input));
  26. // Look for the default style file that is generated for new projects by the Angular CLI. This
  27. // default style file is usually called `styles.ext` unless it has been changed explicitly.
  28. const defaultMainStylePath = styles.find(file => extension ? file === `styles.${extension}` : defaultStyleFileRegex.test(file));
  29. if (defaultMainStylePath) {
  30. return (0, core_1.normalize)(defaultMainStylePath);
  31. }
  32. // If no default style file could be found, use the first style file that matches the given
  33. // extension. If no extension specified explicitly, we look for any file with a valid style
  34. // file extension.
  35. const fallbackStylePath = styles.find(file => extension ? file.endsWith(`.${extension}`) : validStyleFileRegex.test(file));
  36. if (fallbackStylePath) {
  37. return (0, core_1.normalize)(fallbackStylePath);
  38. }
  39. }
  40. return null;
  41. }
  42. //# sourceMappingURL=project-style-file.js.map