UNPKG

@o3r/schematics

Version:

Schematics module of the Otter framework

34 lines 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateSassImports = updateSassImports; const index_1 = require("../../utility/index"); const list_of_vars_1 = require("./list-of-vars"); const imports = new RegExp(/^@import\s+["']~?@(o3r|otter)\/styling.*\s*/, 'gm'); /** * Update SASS imports to use a scoped dependency * @deprecated Legacy migration: `@import "~@o3r/styling"` / `"~@otter/styling"` -> `@use`. Will be removed in v15. * @param alias The name of the otter styling package * @param dependencyName The name of the dependency to update imports on * @param exposedElements The list of exposed elements * @param fromRoot Perform on all files in project */ function updateSassImports(alias, dependencyName = '@o3r/styling', exposedElements = list_of_vars_1.listOfExposedElements, fromRoot = false) { return (tree, _context) => { const files = fromRoot ? (0, index_1.getFilesWithExtensionFromTree)(tree, 'scss') : (0, index_1.getFilesFromRootOfWorkspaceProjects)(tree, 'scss'); files.forEach((file) => { let content = tree.read(file).toString(); // eslint-disable-next-line unicorn/prefer-regexp-test -- regexp with `g` flag should not use `RegExp.test` in a loop if (content.match(imports)) { const contentWithoutImports = content.replace(imports, ''); content = `@use '${dependencyName}' as ${alias};\n${contentWithoutImports}`; exposedElements.forEach((elem) => { const elemRegex = new RegExp(`(?<![\\w\\d-]|o3r\\.)${elem.type === 'var' ? '\\' : ''}${elem.value}((?![\\w\\d-])(?!(\\s*\\:)))`, 'g'); content = content.replace(elemRegex, `${alias}.${(elem.replacement || elem.value)}`); }); tree.overwrite(file, content); } }); return tree; }; } //# sourceMappingURL=update-imports-with-scope.js.map