lfs-auto-track
Version:
git lfs auto track module
116 lines (115 loc) • 5.13 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseConditions = parseConditions;
exports.getLFSTrackFiles = getLFSTrackFiles;
exports.run = run;
const child_process_1 = require("child_process");
const fs_1 = __importDefault(require("fs"));
const mime_1 = __importDefault(require("mime"));
const path_1 = __importDefault(require("path"));
function parseConditions(conditions) {
return conditions
.split(';')
.map((item) => {
const [ext, sizeStr] = item.split(':');
if (!ext || !sizeStr) {
return { exts: [], size: 0 };
}
// Parse extensions
const exts = ext
.split(',')
.map((item) => item.trim())
.filter(Boolean);
// Parse size with unit conversion to bytes
let size = 0;
const sizeMatch = sizeStr.trim().match(/^(\d+(?:\.\d+)?)\s*(kb|mb|gb|b)?$/i);
if (sizeMatch) {
const value = parseFloat(sizeMatch[1]);
const unit = (sizeMatch[2] || 'kb').toLowerCase();
switch (unit) {
case 'b':
size = value;
break;
case 'kb':
size = value * 1024;
break;
case 'mb':
size = value * 1024 * 1024;
break;
case 'gb':
size = value * 1024 * 1024 * 1024;
break;
default:
size = value * 1024; // Default to KB if unit is unrecognized
}
}
return { exts, size };
})
.filter((item) => item.exts.length > 0);
}
function getLFSTrackFiles(conditions, files, gitattributesFiles, cwd, mockFileInfo) {
const result = [];
const cs = parseConditions(conditions);
if ((files === null || files === void 0 ? void 0 : files.length) > 0 && cs.length > 0) {
for (const filepath of files) {
const ext = path_1.default.extname(filepath).toLowerCase().replace('.', '');
if (ext) {
const mimetype = mime_1.default.getType(ext);
const extInfo = cs.find((item) => item.exts.find((_ext) => {
if (_ext === '*')
return true;
if (mimetype && ['video', 'audio', 'image'].includes(_ext)) {
return mimetype.startsWith(_ext);
}
// Fixed the typo here: swapped variables in the comparison
return ext === _ext || '.' + ext === _ext;
}));
if (extInfo) {
const fileinfo = (mockFileInfo === null || mockFileInfo === void 0 ? void 0 : mockFileInfo[filepath]) || fs_1.default.statSync(filepath);
const filesize = fileinfo.size;
if (process.env.DEBUG)
console.log('filepath, extInfo.size, filesize', filepath, extInfo.size, filesize);
if (filesize >= extInfo.size) {
const relativePath = path_1.default.relative(cwd, filepath);
const alreadyTrack = gitattributesFiles.find((item) => item.startsWith(relativePath));
if (!alreadyTrack) {
result.push(relativePath);
}
}
}
}
}
}
return result;
}
function run(conditions, files) {
return __awaiter(this, void 0, void 0, function* () {
const cwd = process.cwd();
const gitattributesFiles = fs_1.default.existsSync('./.gitattributes')
? fs_1.default
.readFileSync('./.gitattributes', 'utf-8')
.split('\n')
.filter((item) => item.trim())
: [];
const trackFiles = getLFSTrackFiles(conditions, files, gitattributesFiles, cwd);
if (trackFiles.length > 0) {
(0, child_process_1.execSync)(`git lfs track ${trackFiles.map((item) => `'${item}'`).join(' ')}`, {
stdio: 'inherit',
});
(0, child_process_1.execSync)(`git add .gitattributes`, { stdio: 'inherit' });
}
return trackFiles;
});
}