in packages/blueprints/blueprint/src/differences/differences.ts [6:39]
export function generateDifferencePatch(intendedOldFile: string, intendedNewFile: string, destination: string): string {
let oldFile = intendedOldFile;
if (!fs.existsSync(oldFile)) {
oldFile = '/dev/null';
}
let newFile = intendedNewFile;
if (!fs.existsSync(newFile)) {
newFile = '/dev/null';
}
// todo clean up in the future to use a stream
const args = ['diff', '--binary', '--no-index', oldFile, newFile];
const diffCommand = cp.spawnSync('git', args, { maxBuffer: 999_990_999_999 });
if (diffCommand.error) {
throw new Error(`git diff failed: ${diffCommand.error}`);
}
// git diff returns 0 if there is no diff and 1 if there is a diff. Otherwise, the status indicates an
// error:
if (diffCommand.status !== 0 && diffCommand.status !== 1) {
throw new Error(`git diff failed: ${diffCommand.stderr.toString()}`);
}
let rawDiff = diffCommand.stdout.toString();
if (rawDiff.length) {
rawDiff = rawDiff.replace(/^(.*)$/m, `diff --git a/${destination} b/${destination}`);
rawDiff = rawDiff.replace(`--- a${oldFile}`, `--- a/${destination}`);
rawDiff = rawDiff.replace(`--- a/${oldFile}`, `--- a/${destination}`);
rawDiff = rawDiff.replace(`+++ b${newFile}`, `+++ b/${destination}`);
rawDiff = rawDiff.replace(`+++ b/${newFile}`, `+++ b/${destination}`);
}
return rawDiff;
}