|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; |
| 8 | + |
| 9 | +const bootstrapImportsToReplace = [ |
| 10 | + { |
| 11 | + find: 'bootstrap/scss/alert', |
| 12 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/alert', |
| 13 | + }, |
| 14 | + { |
| 15 | + find: 'bootstrap/scss/badge', |
| 16 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/badge', |
| 17 | + }, |
| 18 | + { |
| 19 | + find: 'bootstrap/scss/breadcrumb', |
| 20 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/breadcrumb', |
| 21 | + }, |
| 22 | + { |
| 23 | + find: 'bootstrap/scss/button-group', |
| 24 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/button-group', |
| 25 | + }, |
| 26 | + { |
| 27 | + find: 'bootstrap/scss/buttons', |
| 28 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/buttons', |
| 29 | + }, |
| 30 | + { |
| 31 | + find: 'bootstrap/scss/card', |
| 32 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/card', |
| 33 | + }, |
| 34 | + { |
| 35 | + find: 'bootstrap/scss/carousel', |
| 36 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/carousel', |
| 37 | + }, |
| 38 | + { |
| 39 | + find: 'bootstrap/scss/close', |
| 40 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/close', |
| 41 | + }, |
| 42 | + { |
| 43 | + find: 'bootstrap/scss/code', |
| 44 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/code', |
| 45 | + }, |
| 46 | + { |
| 47 | + find: 'bootstrap/scss/custom-forms', |
| 48 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/custom-forms', |
| 49 | + }, |
| 50 | + { |
| 51 | + find: 'bootstrap/scss/dropdown', |
| 52 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/dropdown', |
| 53 | + }, |
| 54 | + { |
| 55 | + find: 'bootstrap/scss/forms', |
| 56 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/forms', |
| 57 | + }, |
| 58 | + { |
| 59 | + find: 'bootstrap/scss/functions', |
| 60 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/functions', |
| 61 | + }, |
| 62 | + { |
| 63 | + find: 'bootstrap/scss/grid', |
| 64 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/grid', |
| 65 | + }, |
| 66 | + { |
| 67 | + find: 'bootstrap/scss/images', |
| 68 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/images', |
| 69 | + }, |
| 70 | + { |
| 71 | + find: 'bootstrap/scss/input-group', |
| 72 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/input-group', |
| 73 | + }, |
| 74 | + { |
| 75 | + find: 'bootstrap/scss/jumbotron', |
| 76 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/jumbotron', |
| 77 | + }, |
| 78 | + { |
| 79 | + find: 'bootstrap/scss/list-group', |
| 80 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/list-group', |
| 81 | + }, |
| 82 | + { |
| 83 | + find: 'bootstrap/scss/media', |
| 84 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/media', |
| 85 | + }, |
| 86 | + { |
| 87 | + find: 'bootstrap/scss/mixins', |
| 88 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/mixins', |
| 89 | + }, |
| 90 | + { |
| 91 | + find: 'bootstrap/scss/modal', |
| 92 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/modal', |
| 93 | + }, |
| 94 | + { |
| 95 | + find: 'bootstrap/scss/nav', |
| 96 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/nav', |
| 97 | + }, |
| 98 | + { |
| 99 | + find: 'bootstrap/scss/navbar', |
| 100 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/navbar', |
| 101 | + }, |
| 102 | + { |
| 103 | + find: 'bootstrap/scss/pagination', |
| 104 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/pagination', |
| 105 | + }, |
| 106 | + { |
| 107 | + find: 'bootstrap/scss/popover', |
| 108 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/popover', |
| 109 | + }, |
| 110 | + { |
| 111 | + find: 'bootstrap/scss/print', |
| 112 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/print', |
| 113 | + }, |
| 114 | + { |
| 115 | + find: 'bootstrap/scss/progress', |
| 116 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/progress', |
| 117 | + }, |
| 118 | + { |
| 119 | + find: 'bootstrap/scss/reboot', |
| 120 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/reboot', |
| 121 | + }, |
| 122 | + { |
| 123 | + find: 'bootstrap/scss/root', |
| 124 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/root', |
| 125 | + }, |
| 126 | + { |
| 127 | + find: 'bootstrap/scss/tables', |
| 128 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/tables', |
| 129 | + }, |
| 130 | + { |
| 131 | + find: 'bootstrap/scss/toasts', |
| 132 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/toasts', |
| 133 | + }, |
| 134 | + { |
| 135 | + find: 'bootstrap/scss/tooltip', |
| 136 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/tooltip', |
| 137 | + }, |
| 138 | + { |
| 139 | + find: 'bootstrap/scss/transitions', |
| 140 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/transitions', |
| 141 | + }, |
| 142 | + { |
| 143 | + find: 'bootstrap/scss/type', |
| 144 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/type', |
| 145 | + }, |
| 146 | + { |
| 147 | + find: 'bootstrap/scss/utilities', |
| 148 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/utilities', |
| 149 | + }, |
| 150 | + { |
| 151 | + find: 'bootstrap/scss/variables', |
| 152 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/variables', |
| 153 | + }, |
| 154 | + { |
| 155 | + find: 'bootstrap/scss/spinners', |
| 156 | + replaceWith: '@spartacus/styles/vendor/bootstrap/scss/spinners', |
| 157 | + }, |
| 158 | +]; |
| 159 | + |
| 160 | +export function replaceBootstrapImports(): Rule { |
| 161 | + return (tree: Tree, context: SchematicContext) => { |
| 162 | + context.logger.info( |
| 163 | + 'Scanning files for Bootstrap imports. ' + |
| 164 | + 'Imports will be updated to use `@spartacus/styles/vendor/bootstrap/scss/`.' |
| 165 | + ); |
| 166 | + |
| 167 | + tree.visit((filePath) => { |
| 168 | + if (filePath.endsWith('.scss')) { |
| 169 | + const fileContent = tree.read(filePath)?.toString('utf-8'); |
| 170 | + if (fileContent) { |
| 171 | + let updatedContent = fileContent; |
| 172 | + let hasChanges = false; |
| 173 | + |
| 174 | + bootstrapImportsToReplace.forEach(({ find, replaceWith }) => { |
| 175 | + const regex = new RegExp(`@import\\s+['"]${find}['"];`, 'g'); |
| 176 | + if (regex.test(updatedContent)) { |
| 177 | + updatedContent = updatedContent.replace( |
| 178 | + regex, |
| 179 | + `@import '${replaceWith}';` |
| 180 | + ); |
| 181 | + hasChanges = true; |
| 182 | + } |
| 183 | + }); |
| 184 | + |
| 185 | + if (hasChanges) { |
| 186 | + tree.overwrite(filePath, updatedContent); |
| 187 | + context.logger.info( |
| 188 | + `Updated imports of Bootstrap in file '${filePath}'` |
| 189 | + ); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + }); |
| 194 | + |
| 195 | + context.logger.info('Bootstrap import replacement process completed.'); |
| 196 | + return tree; |
| 197 | + }; |
| 198 | +} |
0 commit comments