UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

67 lines (65 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileComparison = void 0; const logger_1 = require("./logger"); const fs = require('fs'); /** * the normalize line endings function. * @param text the text. * @returns the normalized line endings. */ function normalizeLineEndings(text) { return text.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); } /** * the file comparison function. * @param file1Path the file 1 path. * @param file2Path the file 2 path. * @returns the file comparison result. */ function fileComparison(file1Path, file2Path) { const prefix = 'fileComparison'; try { logger_1.Logger.log(`Reading ${file1Path} and ${file2Path}`, prefix); const content1 = fs.readFileSync(file1Path, 'utf-8'); const content2 = fs.readFileSync(file2Path, 'utf-8'); const normalizedContent1 = normalizeLineEndings(content1); const normalizedContent2 = normalizeLineEndings(content2); const lines1 = normalizedContent1.split('\n'); const lines2 = normalizedContent2.split('\n'); let differencesFound = false; for (let i = 0; i < Math.min(lines1.length, lines2.length); i++) { if (lines1[i] !== lines2[i]) { logger_1.Logger.log(`Difference at line ${i + 1}:`, prefix); logger_1.Logger.log(`File 1: ${lines1[i]}`, prefix); logger_1.Logger.log(`File 2: ${lines2[i]}`, prefix); differencesFound = true; } } // Check for additional lines in either file if (lines1.length > lines2.length) { for (let i = lines2.length; i < lines1.length; i++) { logger_1.Logger.log(`Additional line in File 1 at line ${i + 1}:`, prefix); logger_1.Logger.log(`File 1: ${lines1[i]}`, prefix); differencesFound = true; } } else if (lines2.length > lines1.length) { for (let i = lines1.length; i < lines2.length; i++) { logger_1.Logger.log(`Additional line in File 2 at line ${i + 1}:`, prefix); logger_1.Logger.log(`File 2: ${lines2[i]}`, prefix); differencesFound = true; } } if (!differencesFound) { logger_1.Logger.log('No differences found.', prefix); return true; } } catch (error) { logger_1.Logger.error(`Error reading files: ${error.message}`); } return false; } exports.fileComparison = fileComparison; //# sourceMappingURL=file-comparison.js.map