1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.addStyles = void 0;
|
4 | const getVersions_1 = require("./getVersions");
|
5 | const index_1 = require("./index");
|
6 | const path = require("path");
|
7 | const DEFAULT_STYLE_EXTENSION = 'css';
|
8 | function addStyles(project, targetName, host, availableAssetPaths, projectName, extension) {
|
9 | var _a;
|
10 | let targetOptions = index_1.getProjectTargetOptions(project, targetName);
|
11 | const styles = targetOptions.styles;
|
12 | if (!styles || (styles instanceof Array && !styles.length)) {
|
13 | targetOptions = addEmptyStyles(targetOptions, extension, availableAssetPaths);
|
14 | return setUpdatedTargetOptions(host, project, targetOptions, targetName, projectName);
|
15 | }
|
16 | const existingStyles = styles.map((s) => typeof s === 'string' ? s : s['input']);
|
17 | const styleFilePath = getVersions_1.getProjectStyleFile(existingStyles, extension) || '';
|
18 | const styleFileExtension = normalizeExtension(path.extname(styleFilePath), extension, DEFAULT_STYLE_EXTENSION);
|
19 | const styleFilePatch = (_a = availableAssetPaths[styleFileExtension]) === null || _a === void 0 ? void 0 : _a[0];
|
20 | if (!styleFilePath && styleFileExtension !== 'css' && styleFileExtension !== 'scss') {
|
21 | return host;
|
22 | }
|
23 | if (styleFileExtension === 'scss') {
|
24 | return addImportToStylesFile(host, styleFilePath, styleFilePatch);
|
25 | }
|
26 | if (styleFileExtension === 'css') {
|
27 | targetOptions = addStylesPathsToTargetOptions(targetOptions, existingStyles, styleFilePatch);
|
28 | return setUpdatedTargetOptions(host, project, targetOptions, targetName, projectName);
|
29 | }
|
30 | return host;
|
31 | }
|
32 | exports.addStyles = addStyles;
|
33 | function addStylesPathsToTargetOptions(targetOptions, existingStyles, stylePatch) {
|
34 | var _a, _b;
|
35 | if (!existingStyles.some(path => path === stylePatch)) {
|
36 | (_b = (_a = targetOptions.styles) === null || _a === void 0 ? void 0 : _a.unshift) === null || _b === void 0 ? void 0 : _b.call(_a, stylePatch);
|
37 | }
|
38 | return targetOptions;
|
39 | }
|
40 | function addEmptyStyles(targetOptions, extension, availableAssetPaths) {
|
41 | targetOptions.styles = availableAssetPaths[DEFAULT_STYLE_EXTENSION];
|
42 | return targetOptions;
|
43 | }
|
44 | function addImportToStylesFile(host, styleFilePath, styleFilePatch) {
|
45 | const styleContent = host.read(styleFilePath).toString('utf-8');
|
46 | if (!styleContent.includes(styleFilePatch)) {
|
47 | const recorder = host.beginUpdate(styleFilePath);
|
48 | recorder.insertRight(styleContent.length, styleFilePatch);
|
49 | host.commitUpdate(recorder);
|
50 | }
|
51 | return host;
|
52 | }
|
53 | function setUpdatedTargetOptions(host, project, targetOptions, targetName, projectName) {
|
54 | if (host.exists('angular.json')) {
|
55 | const currentAngular = JSON.parse(host.read('angular.json').toString('utf-8'));
|
56 | if (currentAngular['projects'][projectName].targets) {
|
57 | currentAngular['projects'][projectName].targets[targetName]['options'] = targetOptions;
|
58 | }
|
59 | if (currentAngular['projects'][projectName].architect) {
|
60 | currentAngular['projects'][projectName].architect[targetName]['options'] = targetOptions;
|
61 | }
|
62 | host.overwrite('angular.json', JSON.stringify(currentAngular, null, 2));
|
63 | }
|
64 | return host;
|
65 | }
|
66 |
|
67 | function normalizeExtension(pathExtension, extension, defaultValue) {
|
68 | if (!pathExtension) {
|
69 | return defaultValue;
|
70 | }
|
71 | if (extension) {
|
72 | return extension;
|
73 | }
|
74 | const res = pathExtension.split('.');
|
75 | if ((res === null || res === void 0 ? void 0 : res.length) > 1) {
|
76 | pathExtension = pathExtension.replace('.', '');
|
77 | }
|
78 | return pathExtension;
|
79 | }
|
80 |
|
\ | No newline at end of file |