mili
Version:
Scaffolding with continuous control over the development of the project.
80 lines (79 loc) • 3.58 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { showDiff, showRemoved } from './show-diff';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as chalk from 'chalk';
import simpleGit from 'simple-git';
import * as logger from "../util/logger";
import { compare } from 'dir-compare';
function dircmp(cwd, tmpDir) {
return __awaiter(this, void 0, void 0, function* () {
const git = simpleGit(cwd);
const result = yield compare(cwd, tmpDir, {
compareContent: true,
skipSymlinks: true,
skipSubdirs: true,
excludeFilter: '.git',
});
const diff = [];
for (const item of result.diffSet || []) {
if (item.state === 'equal' && item.type1 !== 'directory' && item.type2 !== 'directory')
continue;
if (item.state !== 'equal' && item.path1 && item.name1) {
const list = yield git.checkIgnore(path.join(item.path1, item.name1));
if (list.length)
continue;
}
if (item.type1 === 'directory' && item.type2 === 'directory') {
if (item.path1 && item.name1 && item.path2 && item.name2) {
const subdiff = yield dircmp(path.join(item.path1, item.name1), path.join(item.path2, item.name2));
diff.push(...subdiff);
}
continue;
}
diff.push(item);
}
return diff;
});
}
export function diff(cwd, tmpDir, options) {
return __awaiter(this, void 0, void 0, function* () {
const errors = [];
const diff = yield dircmp(cwd, tmpDir);
for (const item of diff) {
if (item.type2 === 'missing') {
if (options.showDiff && item.name1) {
errors.push(showRemoved(item.name1));
}
else {
const filepath = path.relative(cwd, path.join(item.path1 || '', item.name1 || ''));
errors.push(chalk.red(`${filepath}: Should be remove`));
}
}
else {
const oldFilepath = path.join(item.path1 || '', item.name1 || '');
const newFilepath = path.join(item.path2 || '', item.name2 || '');
const oldBuffer = item.type1 === 'missing' ? Buffer.from('') : yield fs.readFile(oldFilepath);
const newBuffer = yield fs.readFile(newFilepath);
const filepath = path.relative(tmpDir, newFilepath);
if (showDiff) {
errors.push(showDiff(filepath, oldBuffer, newBuffer, options));
}
else {
errors.push(chalk.yellow(`${filepath}: Not Match Template`));
}
}
}
if (errors.length)
logger.error(errors.join('\n'));
return errors;
});
}