@notes-sync/service
Version:
Background service for AI-powered note synchronization
74 lines • 2.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isGitRepo = isGitRepo;
exports.hasOriginRemote = hasOriginRemote;
exports.createGit = createGit;
exports.safeSync = safeSync;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const simple_git_1 = __importDefault(require("simple-git"));
const logger_1 = require("./logger");
function nowIso() {
return new Date().toISOString();
}
function isGitRepo(directoryPath) {
return fs_1.default.existsSync(path_1.default.join(directoryPath, '.git'));
}
async function hasOriginRemote(git) {
const remotes = await git.getRemotes(true);
const origin = remotes.find(r => r.name === 'origin');
return Boolean(origin && origin.refs && (origin.refs.push || origin.refs.fetch));
}
function createGit(baseDir) {
return (0, simple_git_1.default)({ baseDir });
}
/**
* Sync the repo with the remote.
*
* 1) Stage and commit local changes first to ensure clean working tree
* 2) Fetch and rebase onto remote, with autostash as a safety net
* 3) Push if we are ahead or after creating commits
*/
async function safeSync(git, reason) {
try {
logger_1.Logger.log(`- [GIT] sync start (${reason})`);
// 1) Stage and commit local changes first to ensure clean working tree
let status = await git.status();
if (status.files.length > 0) {
await git.add(['-A']);
status = await git.status();
if (status.files.length > 0) {
const msg = `notesync: ${nowIso()} (${status.files.length} changes)`;
await git.commit(msg);
}
}
// 2) Fetch and rebase onto remote, with autostash as a safety net
await git.fetch();
try {
await git.pull(['--rebase', '--autostash']);
}
catch (pullErr) {
logger_1.Logger.error(`- [GIT] - pull --rebase --autostash failed: ${pullErr?.message || pullErr}`);
// Best effort fallback: try a normal pull
try {
await git.pull();
}
catch (_) { }
}
// 3) Push if we are ahead or after creating commits
try {
await git.push();
logger_1.Logger.log(`- [GIT] push complete`);
}
catch (pushErr) {
logger_1.Logger.error(`- [GIT] push failed: ${pushErr?.message || pushErr}`);
}
}
catch (err) {
logger_1.Logger.error(`- [GIT] sync error: ${err?.message || err}`);
}
}
//# sourceMappingURL=git.js.map