github.com/Racer159/jackal@v0.32.7-0.20240401174413-0bd2339e4f2e/src/internal/packager/sbom/viewer/compare.js (about)

     1  const leftJsonPicker = document.getElementById('leftJson');
     2  const rightJsonPicker = document.getElementById('rightJson');
     3  
     4  function initSelector() {
     5  	sbomSelector.add(new Option('-', '-', true, true));
     6  
     7  	JACKAL_SBOM_LIST.sort().forEach((item) => {
     8  		sbomSelector.add(new Option(item, item, false, false));
     9  	});
    10  }
    11  
    12  function compare() {
    13  	if (
    14  		document.getElementById('leftJson').files.length == 0 ||
    15  		document.getElementById('rightJson').files.length == 0
    16  	) {
    17  		showModal('Unable to Compare', 'You must select 2 files from the file browsers');
    18  		return;
    19  	}
    20  
    21  	let leftJson = document.getElementById('leftJson').files[0];
    22  	let rightJson = document.getElementById('rightJson').files[0];
    23  
    24  	let leftReader = new FileReader();
    25  	leftReader.readAsText(leftJson);
    26  
    27  	leftReader.onload = function () {
    28  		try {
    29  			let leftData = JSON.parse(leftReader.result);
    30  			const leftMap = {};
    31  			leftData.artifacts.map((artifact) => {
    32  				if (!leftMap[artifact.name]) {
    33  					leftMap[artifact.name] = {};
    34  				}
    35  				leftMap[artifact.name][artifact.version] = artifact;
    36  			});
    37  
    38  			let rightReader = new FileReader();
    39  			rightReader.readAsText(rightJson);
    40  
    41  			rightReader.onload = function () {
    42  				try {
    43  					let rightData = JSON.parse(rightReader.result);
    44  					const rightMap = {};
    45  					rightData.artifacts.map((artifact) => {
    46  						if (!rightMap[artifact.name]) {
    47  							rightMap[artifact.name] = {};
    48  						}
    49  						rightMap[artifact.name][artifact.version] = artifact;
    50  					});
    51  
    52  					let differences = [];
    53  					rightData.artifacts.map((artifact) => {
    54  						if (!leftMap[artifact.name]) {
    55  							artifact.jackalDiff = 'Added';
    56  							differences.push(artifact);
    57  						} else if (!leftMap[artifact.name][artifact.version]) {
    58  							artifact.jackalDiff = 'Changed';
    59  							oldVersion = Object.keys(leftMap[artifact.name])[0];
    60  							artifact.version = oldVersion + ' -> ' + artifact.version;
    61  							differences.push(artifact);
    62  						}
    63  					});
    64  
    65  					leftData.artifacts.map((artifact) => {
    66  						if (!rightMap[artifact.name]) {
    67  							artifact.jackalDiff = 'Removed';
    68  							differences.push(artifact);
    69  						}
    70  					});
    71  
    72  					loadDataTable(differences, artifactsTable);
    73  				} catch (e) {
    74  					showModal('Unable to Compare', 'You must select 2 Syft JSON files');
    75  				}
    76  			};
    77  		} catch (e) {
    78  			showModal('Unable to Compare', 'You must select 2 Syft JSON files');
    79  		}
    80  	};
    81  }
    82  
    83  function loadDataTable(artifacts, dataTable) {
    84  	const transformedData = artifacts.map((artifact) => {
    85  		return [
    86  			diff(artifact.jackalDiff),
    87  			artifact.type,
    88  			artifact.name,
    89  			artifact.version,
    90  			fileList(artifact.locations, artifact.name),
    91  			(artifact.metadata && fileList(artifact.metadata.files, artifact.name)) || '-',
    92  			(artifact.metadata && artifact.metadata.description) || '-',
    93  			((artifact.metadata && artifact.metadata.maintainer) || '-').replace(
    94  				/\u003c(.*)\u003e/,
    95  				mailtoMaintainerReplace
    96  			),
    97  			(artifact.metadata && artifact.metadata.installedSize) || '-'
    98  		];
    99  	});
   100  
   101  	const data = {
   102  		headings: ['Difference', 'Type', 'Name', 'Version', 'Sources', 'Package Files', 'Notes', 'Maintainer', 'Size'],
   103  		data: transformedData
   104  	};
   105  
   106  	if (window.dt) {
   107  		window.dt.destroy();
   108  	}
   109  
   110  	window.dt = new simpleDatatables.DataTable(dataTable, {
   111  		data,
   112  		perPage: 20
   113  	});
   114  }
   115  
   116  function diff(diffTag) {
   117  	return `<span class="${diffTag.toLowerCase()}">${diffTag}</span>`;
   118  }
   119  
   120  function getCompareName() {
   121  	leftFilename = leftJsonPicker.value.split('/').pop().split('\\').pop();
   122  	rightFilename = rightJsonPicker.value.split('/').pop().split('\\').pop();
   123      return leftFilename.replace(/\.json$/, '') + '-' + rightFilename.replace(/\.json$/, '');
   124  }
   125  
   126  initSelector();