git-csv-diff
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "git-csv-diff", | ||
"author": "Valor-Software", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"license": "GPL-3.0", | ||
@@ -28,4 +28,5 @@ "description": "Library generate difference between csv-files based on Git commit hash", | ||
"daff": "1.3.18", | ||
"lodash": "4.16.6", | ||
"simple-git": "1.57.0" | ||
} | ||
} |
@@ -23,3 +23,6 @@ # Git CSV Diff | ||
hashTo: '177cbf088612423289c7666b9fb29e6607eb54eb', | ||
sourceFolder: '*path-to-repo-folder*' | ||
sourceFolder: './repos/', | ||
// optional | ||
translations: true, | ||
resultToFile: true | ||
}; | ||
@@ -26,0 +29,0 @@ |
'use strict'; | ||
const daff = require('daff'); | ||
const _ = require('lodash'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const async = require("async"); | ||
const gitFlow = require('./git-flow'); | ||
const DIFF_STRUCTURE = { | ||
header: { | ||
create: [], | ||
remove: [], | ||
update: [] | ||
}, | ||
body: { | ||
create: [], | ||
remove: [], | ||
update: [], | ||
change: [], | ||
function gitCsvDiff () {}; | ||
translate: { | ||
create: [], | ||
remove: [], | ||
update: [], | ||
change: [] | ||
} | ||
} | ||
}; | ||
function gitCsvDiff() {}; | ||
gitCsvDiff.prototype.process = function (data, callback) { | ||
let sourceFolderPath = data.sourceFolder; | ||
const sourceFolderPath = path.resolve(data.sourceFolder) + "/"; | ||
const resultToFile = data.resultToFile ? true : false; | ||
const translations = data.translations ? true : false; | ||
@@ -18,18 +42,17 @@ let dataRequest = {}; | ||
gitFlow.setSourceFolder(data.sourceFolder); | ||
gitFlow.getFileDiffByHashes(data, gitDiffFileStatus, function(error, gitDiffFileList) { | ||
gitFlow.setSourceFolder(sourceFolderPath); | ||
gitFlow.getFileDiffByHashes(data, gitDiffFileStatus, function (error, gitDiffFileList) { | ||
if(!!error) { | ||
if (!!error) { | ||
return callback(error); | ||
} | ||
async.mapSeries ( | ||
async.mapSeries( | ||
gitDiffFileList, | ||
// iteration | ||
function(fileName, doneMapLimit){ | ||
function (fileName, doneMapLimit) { | ||
//console.log("generate diff for file: ", fileName); | ||
gitFlow.showFileStateByHash(data, fileName, function(error, result) { | ||
gitFlow.showFileStateByHash(data, fileName, function (error, result) { | ||
@@ -43,4 +66,8 @@ getDiffByFile(fileName, result); | ||
// callback | ||
function(error){ | ||
function (error) { | ||
if (!!error) { | ||
return callback(error); | ||
} | ||
let result = { | ||
@@ -51,5 +78,11 @@ 'files': gitDiffFileStatus, | ||
let resultFileName = sourceFolderPath + "diff-operation-result.json"; | ||
//fs.writeFileSync(resultFileName, JSON.stringify(result)); | ||
if (translations) { | ||
generateTranslations(result); | ||
} | ||
if (resultToFile) { | ||
const resultFileName = sourceFolderPath + "diff-operation-result.json"; | ||
fs.writeFileSync(resultFileName, JSON.stringify(result)); | ||
} | ||
//console.log("* Diff generation completed!"); | ||
@@ -62,3 +95,3 @@ return callback(false, result); | ||
function getDiffByFile (fileName, dataDiff) { | ||
function getDiffByFile(fileName, dataDiff) { | ||
@@ -70,3 +103,3 @@ let diffResult = []; | ||
let filesDiff = daff.compareTables(tableFrom,tableTo).align(); | ||
let filesDiff = daff.compareTables(tableFrom, tableTo).align(); | ||
@@ -85,22 +118,4 @@ let flags = new daff.CompareFlags(); | ||
/* Prepare Data Structure */ | ||
let fileDiffData = _.cloneDeep(DIFF_STRUCTURE); | ||
let fileDiffData = { | ||
"header": { | ||
"create": [], | ||
"remove": [], | ||
"update": [] | ||
}, | ||
"body": { | ||
"create": [], | ||
"remove": [], | ||
"update": [], | ||
"change": [] | ||
} | ||
}; | ||
/* Slice Groupd of Changes */ | ||
@@ -112,3 +127,3 @@ | ||
if(firsDiffRow[0] == '!') { | ||
if (firsDiffRow[0] == '!') { | ||
@@ -120,8 +135,8 @@ // [ '!', '', '(old_column)', '+++', '---' ], | ||
if(diffResultHeader[0] == "!") { | ||
if (diffResultHeader[0] == "!") { | ||
diffResultHeader.shift(); | ||
diffResultHeader.forEach(function(value, index){ | ||
diffResultHeader.forEach(function (value, index) { | ||
if(value != '') { | ||
if (value != '') { | ||
@@ -153,3 +168,3 @@ if (value == '+++') { | ||
let diffResultGidField; | ||
if(diffResultColumns[0] == "@@") { | ||
if (diffResultColumns[0] == "@@") { | ||
diffResultColumns.shift(); | ||
@@ -159,7 +174,7 @@ diffResultGidField = diffResultColumns[0]; | ||
let isDataPointsFile = fileName.indexOf("--datapoints--") != -1 ? true : false; | ||
let isDataPointsFile = isDatapointFile(fileName); | ||
if(diffResult.length) { | ||
if (diffResult.length) { | ||
diffResult.forEach(function(value, index){ | ||
diffResult.forEach(function (value, index) { | ||
@@ -170,3 +185,3 @@ // simple-way, collect all data (mean full row) for update | ||
if(modificationType != '') { | ||
if (modificationType != '') { | ||
@@ -177,4 +192,4 @@ if (modificationType == '+++') { | ||
let dataRow = {}; | ||
diffResultColumns.forEach(function(columnValue, columnIndex){ | ||
if(fileDiffData.header.remove.indexOf(columnValue) == -1) { | ||
diffResultColumns.forEach(function (columnValue, columnIndex) { | ||
if (fileDiffData.header.remove.indexOf(columnValue) == -1) { | ||
// ready columns | ||
@@ -185,3 +200,3 @@ dataRow[columnValue] = value[columnIndex]; | ||
if(dataRow) { | ||
if (dataRow) { | ||
fileDiffData.body.create.push(dataRow); | ||
@@ -196,5 +211,5 @@ } | ||
// check that file with datapoints | ||
if(isDataPointsFile) { | ||
diffResultColumns.forEach(function(columnValue, columnIndex){ | ||
if( | ||
if (isDataPointsFile) { | ||
diffResultColumns.forEach(function (columnValue, columnIndex) { | ||
if ( | ||
// disable changes for removed files | ||
@@ -220,3 +235,3 @@ // fileDiffData.header.remove.indexOf(columnValue) == -1 && | ||
let dataRowOrigin = {}; | ||
diffResultHeader.forEach(function(columnValue, columnIndex) { | ||
diffResultHeader.forEach(function (columnValue, columnIndex) { | ||
let columnKey = diffResultColumns[columnIndex]; | ||
@@ -235,3 +250,3 @@ if (fileDiffData.header.create.indexOf(columnKey) != -1) { | ||
if(isDataPointsFile) { | ||
if (isDataPointsFile) { | ||
dataRowUpdated["data-origin"] = dataRowOrigin; | ||
@@ -248,3 +263,3 @@ } | ||
value.forEach(function(valueCell, indexCell){ | ||
value.forEach(function (valueCell, indexCell) { | ||
let modificationSeparatorPosition = valueCell.indexOf('->'); | ||
@@ -254,3 +269,3 @@ let columnKey = diffResultColumns[indexCell]; | ||
// cell modified | ||
if(modificationSeparatorPosition != -1) { | ||
if (modificationSeparatorPosition != -1) { | ||
@@ -265,3 +280,3 @@ let readyValueCell = valueCell.substring(modificationSeparatorPosition + 2); | ||
dataRow[columnKey] = valueCell; | ||
if(fileDiffData.header.create.indexOf(columnKey) == -1) { | ||
if (fileDiffData.header.create.indexOf(columnKey) == -1) { | ||
dataRowOrigin[columnKey] = valueCell; | ||
@@ -280,3 +295,3 @@ } | ||
if(conceptValueTypeIndex != -1) { | ||
if (conceptValueTypeIndex != -1) { | ||
conceptValueSearchFor = value[0].substring(0, conceptValueTypeIndex) | ||
@@ -290,3 +305,3 @@ } | ||
if(isDataPointsFile) { | ||
if (isDataPointsFile) { | ||
dataRowUpdated["data-origin"] = dataRowOrigin; | ||
@@ -379,3 +394,3 @@ } | ||
// clear remove header section for removed files | ||
if(gitDiffFileStatus[fileName] == "D") { | ||
if (gitDiffFileStatus[fileName] == "D") { | ||
fileDiffData.header.remove = []; | ||
@@ -392,4 +407,229 @@ } | ||
function generateTranslations(resultDiff) { | ||
if (!resultDiff.files) { | ||
return; | ||
} | ||
for (let file in resultDiff.files) { | ||
if (!resultDiff.files.hasOwnProperty(file)) { | ||
continue; | ||
} | ||
const langFileMeta = getTranslationLanguage(file); | ||
if (!!langFileMeta) { | ||
// 1. check that base file exists in file-structure | ||
if (!resultDiff.files[langFileMeta.base]) { | ||
// Type of Modified File | ||
resultDiff.files[langFileMeta.base] = 'M'; | ||
// Adding default structure | ||
resultDiff.changes[langFileMeta.base] = _.cloneDeep(DIFF_STRUCTURE); | ||
} | ||
// 2. merge into base file diff | ||
mergeWithTranslations( | ||
langFileMeta.base, | ||
resultDiff.changes[langFileMeta.base].body, | ||
resultDiff.changes[file].body | ||
); | ||
} | ||
} | ||
} | ||
function getTranslationLanguage(fileName) { | ||
const regexpRule = /lang\/(.*)\/(.*)/; | ||
const regexpMatch = regexpRule.exec(fileName); | ||
return !!regexpMatch ? { | ||
lang: regexpMatch[1], | ||
base: regexpMatch[2] | ||
} : false; | ||
} | ||
function isDatapointFile(filename) { | ||
return filename.indexOf("--datapoints--") != -1 ? true : false; | ||
} | ||
function getUniqueKeyForRemove(filename, item, isDatapoint) { | ||
const keyArray = []; | ||
if(isDatapoint) { | ||
const fileParts = /ddf--datapoints--(.*)--by--(.*).csv/.exec(filename); | ||
//const fileIndicator = fileParts[1]; | ||
const fileDemensions = fileParts[2].split("--"); | ||
_.forEach(fileDemensions, function(itemDemension){ | ||
keyArray.push(item[itemDemension]); | ||
}); | ||
} else { | ||
/* { | ||
"gid": "company_size", | ||
"company_size": "small" | ||
} */ | ||
const mainKey = _.head(_.keys(item)); | ||
keyArray.push(item[mainKey]); | ||
keyArray.push(item[item[mainKey]]); | ||
// 'company_size*small' | ||
} | ||
return _.join(keyArray, '*'); | ||
} | ||
function getUniqueKeyForCreate(filename, item, isDatapoint) { | ||
if(isDatapoint) { | ||
return getUniqueKeyForRemove(filename, item, isDatapoint); | ||
} | ||
/* { | ||
"company_scale": "small", | ||
"is--company_scale": "TRUE" | ||
} */ | ||
const keyArray = []; | ||
const mainKey = _.head(_.keys(item)); | ||
keyArray.push(mainKey); | ||
keyArray.push(item[mainKey]); | ||
// 'company_scale*small' | ||
return _.join(keyArray, '*'); | ||
} | ||
function getUniqueKeyForChange(filename, item, isDatapoint) { | ||
if(isDatapoint) { | ||
return getUniqueKeyForRemove(filename, item["data-origin"], isDatapoint); | ||
} else { | ||
return getUniqueKeyForRemove(filename, item, isDatapoint); | ||
} | ||
} | ||
function mergeWithTranslations(filename, diffTarget, diffBase) { | ||
const uniqueKeys = _getUniqueKeys(filename, diffTarget); | ||
_mergeStructureRemove(uniqueKeys, filename, diffTarget, diffBase); | ||
_mergeStructureCreate(uniqueKeys, filename, diffTarget, diffBase); | ||
_mergeStructureChange(uniqueKeys, filename, diffTarget, diffBase); | ||
_mergeStructureUpdate(uniqueKeys, filename, diffTarget, diffBase); | ||
} | ||
function _getUniqueKeys(filename, diffTarget) { | ||
const isDatapoint = isDatapointFile(filename); | ||
const uniqueKeys = new Set(); | ||
_.forEach(diffTarget.remove, function (item) { | ||
const key = getUniqueKeyForRemove(filename, item, isDatapoint); | ||
uniqueKeys.add(key); | ||
}); | ||
_.forEach(diffTarget.create, function (item) { | ||
const key = getUniqueKeyForCreate(filename, item, isDatapoint); | ||
uniqueKeys.add(key); | ||
}); | ||
_.forEach(diffTarget.change, function (item) { | ||
const key = getUniqueKeyForChange(filename, item, isDatapoint); | ||
uniqueKeys.add(key); | ||
}); | ||
_.forEach(diffTarget.update, function (item) { | ||
const key = getUniqueKeyForChange(filename, item, isDatapoint); | ||
uniqueKeys.add(key); | ||
}); | ||
return uniqueKeys; | ||
} | ||
function _mergeStructureRemove(uniqueKeys, filename, diffTarget, diffBase) { | ||
if (diffBase.remove.length) { | ||
const isDatapoint = isDatapointFile(filename); | ||
// merge unique keys into base file | ||
_.forEach(diffBase.remove, function (item) { | ||
const key = getUniqueKeyForRemove(filename, item, isDatapoint); | ||
if(uniqueKeys.has(key)) { | ||
return; | ||
} | ||
// add to target | ||
diffTarget.translate.remove.push(item); | ||
uniqueKeys.add(key); | ||
}); | ||
} | ||
} | ||
function _mergeStructureCreate(uniqueKeys, filename, diffTarget, diffBase) { | ||
if (diffBase.create.length) { | ||
const isDatapoint = isDatapointFile(filename); | ||
// merge unique keys into base file | ||
_.forEach(diffBase.create, function (item) { | ||
const key = getUniqueKeyForCreate(filename, item, isDatapoint); | ||
if(uniqueKeys.has(key)) { | ||
return; | ||
} | ||
// add to target | ||
diffTarget.translate.create.push(item); | ||
uniqueKeys.add(key); | ||
}); | ||
} | ||
} | ||
function _mergeStructureChange(uniqueKeys, filename, diffTarget, diffBase) { | ||
if (diffBase.change.length) { | ||
const isDatapoint = isDatapointFile(filename); | ||
// merge unique keys into base file | ||
_.forEach(diffBase.change, function (item) { | ||
const key = getUniqueKeyForChange(filename, item, isDatapoint); | ||
if(uniqueKeys.has(key)) { | ||
return; | ||
} | ||
// add to target | ||
diffTarget.translate.change.push(item); | ||
uniqueKeys.add(key); | ||
}); | ||
} | ||
} | ||
function _mergeStructureUpdate(uniqueKeys, filename, diffTarget, diffBase) { | ||
if (diffBase.update.length) { | ||
const isDatapoint = isDatapointFile(filename); | ||
// merge unique keys into base file | ||
_.forEach(diffBase.update, function (item) { | ||
const key = getUniqueKeyForChange(filename, item, isDatapoint); | ||
if(uniqueKeys.has(key)) { | ||
return; | ||
} | ||
// add to target | ||
diffTarget.translate.update.push(item); | ||
uniqueKeys.add(key); | ||
}); | ||
} | ||
} | ||
}; | ||
module.exports = new gitCsvDiff(); |
@@ -68,2 +68,3 @@ 'use strict'; | ||
}); | ||
}); | ||
@@ -94,3 +95,4 @@ }); | ||
gitDiffFileList.forEach(function(item, index, arr){ | ||
arr[index] = path.parse(item).base; | ||
//arr[index] = path.parse(item).base; | ||
arr[index] = item; | ||
}); | ||
@@ -97,0 +99,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58919
8
594
36
4
+ Addedlodash@4.16.6
+ Addedlodash@4.16.6(transitive)
- Removedlodash@4.17.21(transitive)