project-style-file.js 2.1 KB

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