105 lines
3.6 KiB
JavaScript
105 lines
3.6 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const fs_1 = __importDefault(require("fs"));
|
|
const path_1 = __importDefault(require("path"));
|
|
const crypto_1 = __importDefault(require("crypto"));
|
|
class FileChecker {
|
|
constructor(oldFileDir, newFileDir, outDir) {
|
|
this.oldFileDir = oldFileDir;
|
|
this.newFileDir = newFileDir;
|
|
this.outDir = outDir;
|
|
this.diffFiles = [];
|
|
}
|
|
pickDiff() {
|
|
outDir && this.rmdir(outDir);
|
|
this.doPickDiff();
|
|
}
|
|
doPickDiff(filePath = '') {
|
|
let newFileFullPath = path_1.default.join(this.newFileDir, filePath);
|
|
let stat = fs_1.default.statSync(newFileFullPath);
|
|
if (stat.isDirectory()) {
|
|
let childFiles = fs_1.default.readdirSync(newFileFullPath);
|
|
childFiles.map((value) => this.doPickDiff(path_1.default.join(filePath, value)));
|
|
}
|
|
else {
|
|
let oldFileFullPath = path_1.default.join(this.oldFileDir, filePath);
|
|
if (!fs_1.default.existsSync(oldFileFullPath)) {
|
|
this.copyDiffToOutDir(filePath, '+');
|
|
return;
|
|
}
|
|
let oldFileStat = fs_1.default.statSync(oldFileFullPath);
|
|
if (oldFileStat.isDirectory()) {
|
|
this.copyDiffToOutDir(filePath, 'x');
|
|
return;
|
|
}
|
|
let oldFileMD5 = this.md5(oldFileFullPath);
|
|
let newFileMD5 = this.md5(newFileFullPath);
|
|
if (oldFileMD5 !== newFileMD5) {
|
|
this.copyDiffToOutDir(filePath, 'x');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
getDiffFileList() {
|
|
return this.diffFiles;
|
|
}
|
|
makeNewOld() {
|
|
this.rmdir(this.oldFileDir);
|
|
fs_1.default.renameSync(this.newFileDir, this.oldFileDir);
|
|
}
|
|
copyDiffToOutDir(filePath, type) {
|
|
this.diffFiles.push(`[${type}]${filePath}`);
|
|
if (this.outDir) {
|
|
let srcPath = path_1.default.join(this.newFileDir, filePath);
|
|
let outPath = path_1.default.join(this.outDir, filePath);
|
|
this.mkdir(path_1.default.dirname(outPath));
|
|
fs_1.default.copyFileSync(srcPath, outPath);
|
|
}
|
|
}
|
|
md5(filePath) {
|
|
let buffer = fs_1.default.readFileSync(filePath);
|
|
let hash = crypto_1.default.createHash('md5');
|
|
hash.update(buffer);
|
|
let hex = hash.digest('hex');
|
|
return hex;
|
|
}
|
|
rmdir(dir) {
|
|
if (!fs_1.default.existsSync(dir))
|
|
return;
|
|
let files = fs_1.default.readdirSync(dir);
|
|
for (let item of files) {
|
|
let itemPath = path_1.default.join(dir, item);
|
|
let stat = fs_1.default.statSync(itemPath);
|
|
stat.isDirectory() ? this.rmdir(itemPath) : fs_1.default.unlinkSync(itemPath);
|
|
}
|
|
fs_1.default.rmdirSync(dir);
|
|
}
|
|
mkdir(dir) {
|
|
if (!fs_1.default.existsSync(dir)) {
|
|
let parentDir = path_1.default.dirname(dir);
|
|
this.mkdir(parentDir);
|
|
fs_1.default.mkdirSync(dir);
|
|
}
|
|
}
|
|
}
|
|
let cmd = process.argv[2];
|
|
let oldDir = process.argv[3];
|
|
let newDir = process.argv[4];
|
|
let outDir = process.argv[5];
|
|
if (oldDir && newDir) {
|
|
const check = new FileChecker(oldDir, newDir, outDir);
|
|
switch (cmd) {
|
|
case 'pick':
|
|
check.pickDiff();
|
|
console.log(`Diff Files:\n${check.getDiffFileList().join('\n')}`);
|
|
break;
|
|
case 'move':
|
|
check.makeNewOld();
|
|
console.log(`Move ${newDir} To ${oldDir}`);
|
|
break;
|
|
}
|
|
}
|