mili
Version:
Scaffolding with continuous control over the development of the project.
84 lines (83 loc) • 3.79 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.diff = void 0;
const show_diff_1 = require("./show-diff");
const fs = require("fs-extra");
const path = require("path");
const chalk = require("chalk");
const simple_git_1 = require("simple-git");
const logger = require("../util/logger");
const dir_compare_1 = require("dir-compare");
function dircmp(cwd, tmpDir) {
return __awaiter(this, void 0, void 0, function* () {
const git = (0, simple_git_1.default)(cwd);
const result = yield (0, dir_compare_1.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;
});
}
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((0, show_diff_1.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 (show_diff_1.showDiff) {
errors.push((0, show_diff_1.showDiff)(filepath, oldBuffer, newBuffer, options));
}
else {
errors.push(chalk.yellow(`${filepath}: Not Match Template`));
}
}
}
if (errors.length)
logger.error(errors.join('\n'));
return errors;
});
}
exports.diff = diff;