UNPKG

sourcecontrol

Version:

A modern TypeScript CLI application for source control

111 lines 4.24 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.IndexFileAdder = void 0; const repo_1 = require("../../../core/repo"); const utils_1 = require("../../../utils"); const fs_extra_1 = __importDefault(require("fs-extra")); const path_1 = __importDefault(require("path")); const index_entry_1 = require("../index-entry"); const objects_1 = require("../../../core/objects"); class IndexFileAdder { constructor(repository, repoRoot) { this.repository = repository; this.repoRoot = repoRoot; } async addFiles(filePaths, index) { const result = { added: [], modified: [], failed: [], ignored: [], }; for (const filePath of filePaths) { try { await this.addSingleFile(filePath, index, result); } catch (error) { result.failed.push({ path: filePath, reason: error.message, }); } } return result; } async addSingleFile(filePath, index, result) { const { absolutePath, relativePath } = this.resolvePaths(filePath); if (!(await utils_1.FileUtils.exists(absolutePath))) { throw new Error('File does not exist'); } const stats = await fs_extra_1.default.stat(absolutePath); if (stats.isDirectory()) { await this.addDirectory(absolutePath, index, result); return; } if (!stats.isFile()) throw new Error('Not a regular file'); await this.addRegularFile(absolutePath, relativePath, index, result); } async addDirectory(absolutePath, index, result) { const files = await this.getFilesInDirectory(absolutePath); for (const file of files) { try { const { relativePath } = this.resolvePaths(file); await this.addRegularFile(file, relativePath, index, result); } catch (error) { result.failed.push({ path: file, reason: error.message, }); } } } async addRegularFile(absolutePath, relativePath, index, result) { const content = await utils_1.FileUtils.readFile(absolutePath); const blob = new objects_1.BlobObject(new Uint8Array(content)); const sha = await this.repository.writeObject(blob); const stats = await fs_extra_1.default.stat(absolutePath); const entry = index_entry_1.IndexEntry.fromFileStats(relativePath, { ...stats, }, sha); const existingEntry = index.getEntry(entry.filePath); const isModified = existingEntry !== undefined; index.add(entry); if (isModified) { result.modified.push(relativePath); } else { result.added.push(relativePath); } } resolvePaths(filePath) { const absolutePath = path_1.default.isAbsolute(filePath) ? path_1.default.normalize(filePath) : path_1.default.join(this.repoRoot, filePath); const relativePath = path_1.default.relative(this.repoRoot, absolutePath).replace(/\\/g, '/'); return { absolutePath, relativePath }; } async getFilesInDirectory(dirPath) { const files = []; const entries = await fs_extra_1.default.readdir(dirPath, { withFileTypes: true }); for (const entry of entries) { const fullPath = path_1.default.join(dirPath, entry.name); if (entry.name === repo_1.SourceRepository.DEFAULT_GIT_DIR) continue; if (entry.isDirectory()) { const subFiles = await this.getFilesInDirectory(fullPath); files.push(...subFiles); continue; } if (entry.isFile()) files.push(fullPath); } return files; } } exports.IndexFileAdder = IndexFileAdder; //# sourceMappingURL=index-file-adder.js.map