-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
350 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,97 @@ | ||
const fs = require('fs-extra'); | ||
import { getFileNameFromPath } from '../utils/fileUtils'; | ||
|
||
const os = require('os'); | ||
const chalk = require('chalk'); | ||
const Table = require('cli-table'); | ||
const _ = require('underscore'); | ||
const { fetchAllFiles, getFileContentSync } = require('./../utils/fileUtils'); | ||
const { convertPath, normalizeData } = require('./../utils/stringUtils'); | ||
// const logger = require('./logger'); | ||
|
||
const diff = (fileList1, fileList2, options) => { | ||
const summary = { TO_CREATE: [], TO_UPDATE: [], TO_DELETE: [] }; | ||
fileList1.forEach(file1 => { | ||
const file2 = convertPath(file1, options.folderNames[0], options.folderNames[1]); | ||
if (_.contains(fileList2, file2)) { | ||
const content1 = getFileContentSync(file1); | ||
const content2 = getFileContentSync(file2); | ||
|
||
if (content2) { | ||
// if (Buffer.compare(content1, content2)) { | ||
if (normalizeData(content1) != normalizeData(content2)) { | ||
summary.TO_UPDATE.push([file1, file2]); | ||
} | ||
fileList2 = _.without(fileList2, file2); | ||
} else { | ||
summary.TO_CREATE.push(file1); | ||
} | ||
} else { | ||
summary.TO_CREATE.push(file1); | ||
} | ||
}); | ||
|
||
// Add files that are left in the second list. This means that they were not present in the initial list | ||
fileList2.forEach(file2 => { | ||
summary.TO_DELETE.push(file2); | ||
}); | ||
return summary; | ||
}; | ||
|
||
const printSummary = diffResult => { | ||
var table = new Table({ | ||
head: [chalk.white('State'), chalk.white('Name')], | ||
colWidths: [10, 60] | ||
}); | ||
|
||
const addToTable = (list, state, style) => { | ||
_.each(list, file => { | ||
table.push([style(state), style(getFileNameFromPath(file))]); | ||
}); | ||
}; | ||
addToTable(diffResult.TO_CREATE, 'New', chalk.green); | ||
addToTable(diffResult.TO_DELETE, 'Deleted', chalk.red); | ||
addToTable(_.map(diffResult.TO_UPDATE, file => file[0]), 'Changed', chalk.yellow); | ||
|
||
console.log(table.toString() + os.EOL); | ||
|
||
if (diffResult.TO_CREATE.length > 0) { | ||
console.log(chalk.bold('New files: ' + chalk.green(diffResult.TO_CREATE.length))); | ||
} | ||
if (diffResult.TO_UPDATE.length > 0) { | ||
console.log(chalk.bold('Updated files: ' + chalk.green(diffResult.TO_UPDATE.length))); | ||
} | ||
if (diffResult.TO_DELETE.length > 0) { | ||
console.log(chalk.bold('Deleted files: ' + chalk.green(diffResult.TO_DELETE.length))); | ||
} | ||
if (diffResult.TO_CREATE.length + diffResult.TO_DELETE.length + diffResult.TO_UPDATE.length == 0) { | ||
console.log(chalk.bold('No changes found')); | ||
} | ||
|
||
console.log(os.EOL); | ||
}; | ||
|
||
/** | ||
* Determine if 2 files are identical | ||
* Determine if 2 dirs are identical | ||
* | ||
* @param {*} file1 path to initial file | ||
* @param {*} file2 path to second file | ||
* @returns "true" if the 2 files are identical. "false" otherwise. | ||
* @param {*} dir1 path to initial dir | ||
* @param {*} dir2 path to second dir | ||
* @param {*} [options={}] diff Options | ||
* @returns "true" if the 2 dirs are identical. "false" otherwise. | ||
*/ | ||
export const areFilesIdentical = (file1, file2) => { | ||
const data1 = fs.readFileSync(file1); | ||
const data2 = fs.readFileSync(file2); | ||
return data1 == data2; | ||
export const arePackagesIdentical = (dir1, dir2, options = {}) => { | ||
return new Promise((resolve, reject) => { | ||
Promise.all([fetchAllFiles(dir1), fetchAllFiles(dir2)]) | ||
.then(arrays => { | ||
try { | ||
const diffResult = diff(arrays[0], arrays[1], options); | ||
printSummary(diffResult); | ||
resolve(diffResult); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,19 @@ | ||
// var extract = require('extract-zip'); | ||
const path = require('path'); | ||
const extractZip = require('extract-zip'); | ||
|
||
// extract(zipPath, { dir: target }, function(err) { | ||
// // extraction is complete. make sure to handle the err | ||
// }); | ||
/** | ||
* Extracts the content of a zip file. | ||
* | ||
* @param {*} zipPath The absolute path of the zip file | ||
*/ | ||
export const extract = zipPath => { | ||
return new Promise((resolve, reject) => { | ||
// TODO: put in a variable "sfdc-org-diff" | ||
return extractZip(zipPath, { dir: path.resolve(__dirname, 'sfdc-org-diff') }, err => { | ||
if (err) { | ||
reject(err); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getFileNameFromPath } from '../utils/fileUtils'; | ||
import { spawn } from 'child_process'; | ||
|
||
const _ = require('underscore'); | ||
const inquirer = require('inquirer'); | ||
|
||
const openVsCodeDiff = filesToDiff => { | ||
_.each(filesToDiff, group => { | ||
spawn('code', ['--diff', group[0], group[1], '--new-window']).on('exit', function(error) { | ||
if (error) { | ||
console.error(error); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
export const interactive = diffResult => { | ||
const fileDict = {}; | ||
|
||
_.each(diffResult.TO_UPDATE, group => { | ||
fileDict[getFileNameFromPath(group[0])] = group; | ||
}); | ||
|
||
inquirer | ||
.prompt([ | ||
{ | ||
type: 'checkbox', | ||
message: 'Select the files to diff', | ||
|
||
name: 'filesToDiff', | ||
choices: _.keys(fileDict) | ||
} | ||
]) | ||
.then(answers => { | ||
const filesToDiff = []; | ||
_.each(answers.filesToDiff, file => { | ||
filesToDiff.push(fileDict[file]); | ||
}); | ||
openVsCodeDiff(filesToDiff); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const os = require('os'); | ||
const chalk = require('chalk'); | ||
const _ = require('underscore'); | ||
|
||
const log = (message, options, ...meta) => { | ||
let fullMessage = ''; | ||
_.each(meta, m => { | ||
if (m.toString()) { | ||
fullMessage += os.EOL + m.toString(); | ||
} | ||
options.style = options.style || (x => x); | ||
console.log(options.style(message + fullMessage)); | ||
}); | ||
}; | ||
|
||
export const error = (message, ...meta) => { | ||
// if (this.level <= LoggerSingleton.ERROR) { | ||
log(message, { style: chalk.red }, meta); | ||
// } | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const _ = require('underscore'); | ||
const fs = require('fs-extra'); | ||
const recursive = require('recursive-readdir'); | ||
|
||
export const fetchAllFiles = rootDir => { | ||
return new Promise((resolve, reject) => { | ||
const ignore = ['*-meta.xml', '.DS_Store']; | ||
recursive(rootDir, ignore) | ||
.then(data => { | ||
resolve(data); | ||
}) | ||
.catch(() => { | ||
// TODO: add logger | ||
reject(`Unable to read package directory "${rootDir}"`); | ||
}); | ||
}); | ||
}; | ||
|
||
export const getFileContentSync = filePath => { | ||
if (fs.existsSync(filePath)) { | ||
return fs.readFileSync(filePath, { encoding: 'utf8' }); | ||
} | ||
return null; | ||
}; | ||
|
||
export const getFileNameFromPath = path => { | ||
return _.last(path.split('/')); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const os = require('os'); | ||
|
||
export const convertPath = (text, initial, replacer) => { | ||
return text.replace(initial, replacer); | ||
}; | ||
|
||
export const normalizeData = data => { | ||
return data.replace(/\r\n/gm, os.EOL).replace(/\n/gm, os.EOL); | ||
}; |
Oops, something went wrong.