Skip to content

Commit

Permalink
GitOps commit
Browse files Browse the repository at this point in the history
* repo dir prepare
* git add
* naive commit
  • Loading branch information
Eccenux committed Jul 13, 2023
1 parent 4fa4471 commit 35efeb1
Showing 1 changed file with 54 additions and 5 deletions.
59 changes: 54 additions & 5 deletions src/GitOps.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,44 @@
import HistoryEntry from './HistoryEntry.js';
import util from 'util';
import { exec as execClassic } from 'child_process';
import { exec as execClassic, execFile as execFileC } from 'child_process';
const exec = util.promisify(execClassic);
const execFile = util.promisify(execFileC);

/**
* Git operations helper.
*/
export default class GitOps {

constructor(baseDir, repoName) {
/** Base, output directory. */
this.baseDir = baseDir;
/** Repository directory. */
this.repoName = repoName;
}

set baseDir(dir) {
// only allows strings
if (typeof dir === 'string') {
// remove trailing slash
if (dir.search(/\/$/) > 0) {
dir = dir.replace(/\/+$/, '');
}
this._baseDir = dir;
}
}
/** @returns {String} Base, output directory. */
get baseDir() {
return this._baseDir;
}

/** @returns {String} Repo directory. */
get dir() {
return `${this.baseDir}/${this.repoName}`;
}

/** Create repo. */
async create() {
const result = await this.exec(`git init ${this.baseDir}/${this.repoName}`);
const result = await this.exec(`git init ${this.repoName}`, this.baseDir);
if (!result) {
throw 'Unable to create repo!';
}
Expand All @@ -25,18 +47,39 @@ export default class GitOps {

/** Add all files (stage changes). */
async addAll() {
const result = await this.exec(`git add -A`, this.dir);
if (!result) {
console.warn('Some problems when staging...');
}
return result;
}

/**
* Commit staged changes.
* @param {HistoryEntry} history
*/
async commit(history) {
// git commit -m "Description from history" --author="Matma Rex <[email protected]>"
// --date=<date>
const args = ['commit'];
args.push('-m'); args.push(history.message);
args.push('--author='); args.push(history.author);
args.push('--date='); args.push(history.dt);
const result = await this.execFile('git', args, this.dir);
if (!result) {
throw 'Unable to commit changes!';
}
return result;
}

/** @private Execute and report problems. */
async exec(cmd) {
const { stdout, stderr } = await exec(cmd);
/** @private Execute and report problems (unsafe!). */
async exec(cmd, dir) {
const { stdout, stderr } = await exec(cmd, {cwd: dir});
return this.execInfo(stdout, stderr);
}

/** @private Report exec problems. */
async execInfo(stdout, stderr) {
if (stdout && stdout.length) {
console.log(stdout);
}
Expand All @@ -46,4 +89,10 @@ export default class GitOps {
}
return true;
}

/** @private Execute and report problems. */
async execFile(cmd, args, dir) {
const { stdout, stderr } = await execFile(cmd, args, {cwd: dir});
return this.execInfo(stdout, stderr);
}
}

0 comments on commit 35efeb1

Please sign in to comment.