Files
2025-08-04 10:46:00 +08:00

155 lines
5.7 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Utils = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const archiver_1 = __importDefault(require("archiver"));
const crypto_1 = __importDefault(require("crypto"));
const BASE64_KEYS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
const HEXCHAR = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
var BASE64_VALUES = new Array(123);
for (let i = 0; i < 123; ++i)
BASE64_VALUES[i] = 64;
for (let i = 0; i < 64; ++i)
BASE64_VALUES[BASE64_KEYS.charCodeAt(i)] = i;
var _t = ['', '', '', ''];
var uuidTemplate = _t.concat(_t, '-', _t, '-', _t, '-', _t, '-', _t, _t, _t);
var indices = uuidTemplate.map(function (value, index) { return value === '-' ? NaN : index; }).filter(isFinite);
class Utils {
static mkdir(dir) {
if (!fs_1.default.existsSync(dir)) {
let parentDir = path_1.default.dirname(dir);
this.mkdir(parentDir);
fs_1.default.mkdirSync(dir);
}
}
static 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);
}
static zipdir(srcDir, outputFile) {
Utils.mkdir(path_1.default.dirname(outputFile));
var promise = new Promise((resolve, reject) => {
let writeStream = fs_1.default.createWriteStream(outputFile);
let archive = archiver_1.default('zip', {
zlib: { level: 9 }
});
writeStream.on('close', () => {
resolve();
});
archive.on('error', function (error) {
reject(error);
});
archive.pipe(writeStream);
archive.directory(srcDir, false);
archive.finalize();
});
return promise;
}
static md5dir(dir, md5) {
md5 = md5 || [];
let files = fs_1.default.readdirSync(dir);
for (let file of files) {
let filePath = path_1.default.join(dir, file);
let stat = fs_1.default.statSync(filePath);
if (stat.isDirectory()) {
this.md5dir(filePath, md5);
}
else {
let buffer = fs_1.default.readFileSync(filePath);
let hash = crypto_1.default.createHash('md5');
hash.update(buffer);
let hex = hash.digest('hex');
md5.push({ path: filePath, md5: hex });
}
}
return md5;
}
static fileMD5Compare(src, dst) {
let result = { added: [], deleted: [], changed: [], same: [] };
let srcCopy = src.concat([]);
let dstCopy = dst.concat([]);
let dstItem;
while (dstItem = dstCopy.shift()) {
let foundItem = undefined;
for (let j = 0, srcLenght = srcCopy.length; j < srcLenght; j++) {
let srcItem = srcCopy[j];
if (srcItem.path === dstItem.path) {
foundItem = srcItem;
srcCopy.splice(j, 1);
break;
}
}
if (!foundItem) {
result.added.push(dstItem);
}
else {
foundItem.md5 === dstItem.md5 ? result.same.push(dstItem) : result.changed.push(dstItem);
}
}
result.deleted.push(...srcCopy);
return result;
}
static setLogLevel(level) {
Utils.logLevel = level;
}
static log(text, level = Utils.LogLevel.Simple) {
if (Utils.logLevel === Utils.LogLevel.Disable)
return;
if (level <= Utils.logLevel) {
console.log(text);
}
}
static toBase64(uuid) {
let j = 2;
let base64 = uuid.substring(0, j);
for (let i = 2; i < 22; i += 2) {
let hex1 = HEXCHAR.indexOf(uuid.charAt(indices[j++]));
let hex2 = HEXCHAR.indexOf(uuid.charAt(indices[j++]));
let hex3 = HEXCHAR.indexOf(uuid.charAt(indices[j++]));
let lhs = (hex1 << 2) | (hex2 >> 2);
let rhs = ((hex2 & 3) << 4) | hex3;
base64 += BASE64_KEYS.charAt(lhs);
base64 += BASE64_KEYS.charAt(rhs);
}
return base64;
}
static toUUID(base64) {
let baselen = base64.length;
if (baselen < 22) {
return base64;
}
uuidTemplate[0] = base64[0];
uuidTemplate[1] = base64[1];
for (let i = 2, j = 2; i < 22; i += 2) {
let lhs = BASE64_VALUES[base64.charCodeAt(i)];
let rhs = BASE64_VALUES[base64.charCodeAt(i + 1)];
uuidTemplate[indices[j++]] = HEXCHAR[lhs >> 2];
uuidTemplate[indices[j++]] = HEXCHAR[((lhs & 3) << 2) | rhs >> 4];
uuidTemplate[indices[j++]] = HEXCHAR[rhs & 0xF];
}
return uuidTemplate.join('');
}
}
exports.Utils = Utils;
Utils.logLevel = 1;
(function (Utils) {
let LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["Disable"] = 0] = "Disable";
LogLevel[LogLevel["Simple"] = 1] = "Simple";
LogLevel[LogLevel["Particular"] = 2] = "Particular";
})(LogLevel = Utils.LogLevel || (Utils.LogLevel = {}));
;
})(Utils = exports.Utils || (exports.Utils = {}));