structure-validation
Version:
A Node.js CLI tool for validating codebase folder and file structure using a clean declarative configuration. Part of the guardz ecosystem for comprehensive TypeScript development.
145 lines • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitService = void 0;
const simple_git_1 = require("simple-git");
/**
* Application service for git operations
*/
class GitService {
constructor(repoPath = process.cwd()) {
this.git = (0, simple_git_1.simpleGit)(repoPath);
}
/**
* Get changed files compared to a reference branch
*/
async getChangedFiles(referenceBranch = 'origin/main') {
try {
// Check if we're in a git repository
const isRepo = await this.git.checkIsRepo();
if (!isRepo) {
return {
changedFiles: [],
hasChanges: false
};
}
// Get diff between current branch and reference branch
const diff = await this.git.diff([
'--name-only',
`${referenceBranch}...HEAD`
]);
if (!diff) {
return {
changedFiles: [],
hasChanges: false
};
}
const changedFiles = diff
.split('\n')
.filter(line => line.trim().length > 0)
.map(file => file.trim());
return {
changedFiles,
hasChanges: changedFiles.length > 0
};
}
catch (error) {
// If there's an error (e.g., reference branch doesn't exist), return empty result
return {
changedFiles: [],
hasChanges: false
};
}
}
/**
* Get staged files (files that are staged for commit)
*/
async getStagedFiles() {
try {
// Check if we're in a git repository
const isRepo = await this.git.checkIsRepo();
if (!isRepo) {
return {
changedFiles: [],
hasChanges: false
};
}
// Get staged files
const staged = await this.git.diff([
'--name-only',
'--cached'
]);
if (!staged) {
return {
changedFiles: [],
hasChanges: false
};
}
const stagedFiles = staged
.split('\n')
.filter(line => line.trim().length > 0)
.map(file => file.trim());
return {
changedFiles: stagedFiles,
hasChanges: stagedFiles.length > 0
};
}
catch (error) {
return {
changedFiles: [],
hasChanges: false
};
}
}
/**
* Get all changed files (staged + unstaged)
*/
async getAllChangedFiles() {
try {
// Check if we're in a git repository
const isRepo = await this.git.checkIsRepo();
if (!isRepo) {
return {
changedFiles: [],
hasChanges: false
};
}
// Get all changed files (staged + unstaged)
const allChanged = await this.git.diff([
'--name-only'
]);
if (!allChanged) {
return {
changedFiles: [],
hasChanges: false
};
}
const changedFiles = allChanged
.split('\n')
.filter(line => line.trim().length > 0)
.map(file => file.trim());
return {
changedFiles,
hasChanges: changedFiles.length > 0
};
}
catch (error) {
return {
changedFiles: [],
hasChanges: false
};
}
}
/**
* Check if the current directory is a git repository
*/
async isGitRepository() {
try {
return await this.git.checkIsRepo();
}
catch {
return false;
}
}
}
exports.GitService = GitService;
//# sourceMappingURL=GitService.js.map