no-migration
Version:
CLI tool to manage migration changes and user changes separately for React Native miniapp projects
243 lines • 7.47 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitHelper = void 0;
const simple_git_1 = __importDefault(require("simple-git"));
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
const util_1 = require("util");
const execAsync = (0, util_1.promisify)(child_process_1.exec);
class GitHelper {
constructor(workingDir = process.cwd()) {
this.workingDir = workingDir;
this.git = (0, simple_git_1.default)(workingDir);
}
/**
* Get current git status
*/
async getStatus() {
const status = await this.git.status();
return {
staged: status.staged,
unstaged: [...status.modified, ...status.deleted, ...status.not_added],
untracked: status.not_added,
};
}
/**
* Get diff for a specific file
*/
async getFileDiff(filePath, cached = false) {
try {
const options = cached ? ['--cached', filePath] : [filePath];
const diff = await this.git.diff(options);
return diff;
}
catch (error) {
console.error(`Error getting diff for ${filePath}:`, error);
return '';
}
}
/**
* Get current content of a file
*/
getFileContent(filePath) {
try {
const fullPath = path.join(this.workingDir, filePath);
if (fs.existsSync(fullPath)) {
return fs.readFileSync(fullPath, 'utf-8');
}
return null;
}
catch (error) {
return null;
}
}
/**
* Get file content from git at a specific state
*/
async getFileContentFromGit(filePath, ref = 'HEAD') {
try {
const { stdout } = await execAsync(`git show ${ref}:${filePath}`, { cwd: this.workingDir });
return stdout;
}
catch (error) {
// File might not exist in HEAD (new file)
return null;
}
}
/**
* Stage a file
*/
async stageFile(filePath) {
try {
await this.git.add(filePath);
}
catch (error) {
console.error(`Error staging ${filePath}:`, error);
throw error;
}
}
/**
* Stage files
*/
async stageFiles(filePaths) {
if (filePaths.length === 0)
return;
try {
await this.git.add(filePaths);
}
catch (error) {
console.error(`Error staging files:`, error);
throw error;
}
}
/**
* Unstage a file
*/
async unstageFile(filePath) {
try {
await this.git.reset(['HEAD', '--', filePath]);
}
catch (error) {
console.error(`Error unstaging ${filePath}:`, error);
throw error;
}
}
/**
* Unstage files
*/
async unstageFiles(filePaths) {
if (filePaths.length === 0)
return;
for (const file of filePaths) {
await this.unstageFile(file);
}
}
/**
* Get all changed files (staged and unstaged)
*/
async getAllChangedFiles() {
const status = await this.git.status();
const files = [];
status.modified.forEach(f => files.push({ file: f, status: 'modified' }));
status.deleted.forEach(f => files.push({ file: f, status: 'deleted' }));
status.created.forEach(f => files.push({ file: f, status: 'added' }));
status.staged.forEach(f => {
if (!files.find(x => x.file === f)) {
files.push({ file: f, status: 'modified' });
}
});
return files;
}
/**
* Create a snapshot of current changes
*/
async createChangesSnapshot(filePaths) {
const snapshot = {};
for (const filePath of filePaths) {
const content = this.getFileContent(filePath);
const diff = await this.getFileDiff(filePath, false);
let status = 'modified';
if (content === null) {
status = 'deleted';
}
else {
const headContent = await this.getFileContentFromGit(filePath);
if (headContent === null) {
status = 'added';
}
}
snapshot[filePath] = {
path: filePath,
status,
content: content || undefined,
diff,
};
}
return snapshot;
}
/**
* Write file content
*/
writeFileContent(filePath, content) {
const fullPath = path.join(this.workingDir, filePath);
const dirPath = path.dirname(fullPath);
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}
fs.writeFileSync(fullPath, content, 'utf-8');
}
/**
* Restore file to a specific content
*/
async restoreFileContent(filePath, content) {
this.writeFileContent(filePath, content);
}
/**
* Commit with message
*/
async commit(message, options = {}) {
const commitOptions = [];
if (options.amend) {
commitOptions.push('--amend');
}
if (options.noEdit) {
commitOptions.push('--no-edit');
}
else if (message) {
commitOptions.push('-m', message);
}
try {
await this.git.commit(message, commitOptions);
}
catch (error) {
console.error('Error committing:', error);
throw error;
}
}
/**
* Check if working directory is clean
*/
async isClean() {
const status = await this.git.status();
return status.isClean();
}
}
exports.GitHelper = GitHelper;
//# sourceMappingURL=gitHelper.js.map