UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

46 lines (44 loc) 2.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentSelectorUpdater = void 0; const tslib_1 = require("tslib"); const fs_extra_1 = tslib_1.__importDefault(require("fs-extra")); const common_1 = require("../../common"); const get_files_1 = require("../get-files"); class ComponentSelectorUpdater { /** * Resolves component-selector linter error * @param {string} fileData String contents of file containing linter error * @param {ErrorResult} error Object representing linter error * @returns {Promise<string>} Updated file content after resolving linter error or the original contents if unable to resolve */ async update(fileData, error) { const selectorNameRegex = /(?<=selector:\s*').+(?=')/; const selectorName = fileData.match(selectorNameRegex); if (selectorName === null) { return fileData; } const newSelectorName = 'sme-' + selectorName; const result = common_1.Common.replaceInString(fileData, selectorNameRegex, newSelectorName); const files = await get_files_1.CodeFilePaths.initialize(); for (const file of files) { this.findAndReplaceInFile(file, selectorName[0], newSelectorName); } error.resolved = common_1.ResolveState.Resolved; return new Promise(resolve => resolve(result)); } findAndReplaceInFile(filePath, selectorName, newSelectorName) { let fileData = common_1.Common.readFileData(filePath); if (!fileData) { return; } const selectorUsageRegex = new RegExp(`(?<=(?<!selector:\\s*)'|<|<\\/)${selectorName}`, 'g'); if (fileData.match(selectorUsageRegex) === null) { return; } fileData = common_1.Common.replaceInString(fileData, selectorUsageRegex, newSelectorName); fs_extra_1.default.writeFileSync(filePath, fileData); } } exports.ComponentSelectorUpdater = ComponentSelectorUpdater; //# sourceMappingURL=component-selector-updater.js.map