143 lines
8.8 KiB
JavaScript
143 lines
8.8 KiB
JavaScript
|
"use strict";
|
||
|
const q = require("q");
|
||
|
const plugins = require("./smartgit.plugins");
|
||
|
/**
|
||
|
* class GitRepo allows access to git directories from node
|
||
|
*/
|
||
|
class GitRepo {
|
||
|
constructor(repoBaseArg) {
|
||
|
this.repoBase = repoBaseArg;
|
||
|
}
|
||
|
/**
|
||
|
* checks if the Repo is valid
|
||
|
*/
|
||
|
check() {
|
||
|
try {
|
||
|
return plugins.smartfile.fs.isDirectory(plugins.path.join(this.repoBase, '.git'));
|
||
|
}
|
||
|
catch (err) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
/**
|
||
|
* stage all files in working directory
|
||
|
*/
|
||
|
addAll(dirPathArg) {
|
||
|
let done = q.defer();
|
||
|
plugins.shelljs.exec(`(cd ${dirPathArg} && git add -A && git status)`);
|
||
|
done.resolve(dirPathArg);
|
||
|
return done.promise;
|
||
|
}
|
||
|
;
|
||
|
/**
|
||
|
* add a remote to the GitRepo
|
||
|
*/
|
||
|
remoteAdd(remoteNameArg, remoteLinkArg) {
|
||
|
let done = q.defer();
|
||
|
if (!remoteNameArg) {
|
||
|
let err = new Error('smartgit.remote.add expects a valid remote name');
|
||
|
plugins.beautylog.error(err.message);
|
||
|
done.reject(err);
|
||
|
return done.promise;
|
||
|
}
|
||
|
;
|
||
|
if (!remoteLinkArg) {
|
||
|
let err = new Error();
|
||
|
plugins.beautylog.error(err.message);
|
||
|
done.reject(err);
|
||
|
return done.promise;
|
||
|
}
|
||
|
;
|
||
|
// if everything is all right proceed
|
||
|
plugins.shelljs.exec(`cd ${this.repoBase} && git remote add ${remoteNameArg} ${remoteLinkArg}`);
|
||
|
done.resolve();
|
||
|
return done.promise;
|
||
|
}
|
||
|
/**
|
||
|
* list remotes for a Gip
|
||
|
*/
|
||
|
remoteList(dirPathArg) {
|
||
|
let done = q.defer();
|
||
|
let remotes = {};
|
||
|
plugins.shelljs.exec(`cd ${dirPathArg} && git remote -v`);
|
||
|
done.resolve(remotes);
|
||
|
return done.promise;
|
||
|
}
|
||
|
;
|
||
|
/**
|
||
|
* remove remote
|
||
|
*/
|
||
|
remoteRemove(dirPathArg) {
|
||
|
let done = q.defer();
|
||
|
return done.promise;
|
||
|
}
|
||
|
/**
|
||
|
* commit all files that are currently staged
|
||
|
*/
|
||
|
commit(commitMessage) {
|
||
|
let done = q.defer();
|
||
|
plugins.shelljs.exec(`(cd ${this.repoBase} && git commit -m "${commitMessage}")`);
|
||
|
done.resolve();
|
||
|
return done.promise;
|
||
|
}
|
||
|
/**
|
||
|
* pull latest changes from remote
|
||
|
*/
|
||
|
pull(sourceArg = '', branchArg = '') {
|
||
|
let done = q.defer();
|
||
|
// if everything is allright proceed
|
||
|
plugins.shelljs.exec(`(cd ${this.repoBase} && git pull ${sourceArg} ${branchArg})`);
|
||
|
done.resolve();
|
||
|
return done.promise;
|
||
|
}
|
||
|
/**
|
||
|
* push new commits to remote
|
||
|
*/
|
||
|
push(remoteNameArg = '', remoteBranchArg = '') {
|
||
|
let done = q.defer();
|
||
|
// if everything seems allright proceed
|
||
|
plugins.shelljs.exec(`(cd ${this.repoBase} && git push ${remoteNameArg} ${remoteBranchArg})`);
|
||
|
done.resolve();
|
||
|
return done.promise;
|
||
|
}
|
||
|
/**
|
||
|
* sync
|
||
|
*/
|
||
|
sync() {
|
||
|
this.pull().then(() => {
|
||
|
this.push();
|
||
|
});
|
||
|
}
|
||
|
/**
|
||
|
* get the current status
|
||
|
*/
|
||
|
status() {
|
||
|
let done = q.defer();
|
||
|
plugins.shelljs.exec(`(cd ${this.repoBase} && git status)`);
|
||
|
done.resolve();
|
||
|
return done.promise;
|
||
|
}
|
||
|
}
|
||
|
exports.GitRepo = GitRepo;
|
||
|
/**
|
||
|
* creates a new GitRepo Instance after cloning a project
|
||
|
*/
|
||
|
exports.createRepoFromClone = (fromArg, toArg) => {
|
||
|
let done = q.defer();
|
||
|
plugins.smartfile.fs.ensureDir(toArg);
|
||
|
plugins.shelljs.exec(`git clone ${fromArg} ${toArg}`);
|
||
|
let newRepo = new GitRepo(toArg);
|
||
|
done.resolve(newRepo);
|
||
|
return done.promise;
|
||
|
};
|
||
|
/**
|
||
|
* creates a new GitRepo instance after initializing a new Git Repository
|
||
|
*/
|
||
|
exports.createRepoFromInit = (destinationDirArg) => {
|
||
|
let done = q.defer();
|
||
|
plugins.smartfile.fs.ensureDir(destinationDirArg);
|
||
|
plugins.shelljs.exec(`cd destinationDirArg && git init`);
|
||
|
let newRepo = new GitRepo(destinationDirArg);
|
||
|
done.resolve(newRepo);
|
||
|
};
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRnaXQuY2xhc3Nlcy5naXRyZXBvLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRnaXQuY2xhc3Nlcy5naXRyZXBvLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSx1QkFBc0I7QUFDdEIsOENBQTZDO0FBRTdDOztHQUVHO0FBQ0g7SUFFSSxZQUFZLFdBQW1CO1FBQzNCLElBQUksQ0FBQyxRQUFRLEdBQUcsV0FBVyxDQUFBO0lBQy9CLENBQUM7SUFFRDs7T0FFRztJQUNILEtBQUs7UUFDRCxJQUFJLENBQUM7WUFDRCxNQUFNLENBQUMsT0FBTyxDQUFDLFNBQVMsQ0FBQyxFQUFFLENBQUMsV0FBVyxDQUFDLE9BQU8sQ0FBQyxJQUFJLENBQUMsSUFBSSxDQUFDLElBQUksQ0FBQyxRQUFRLEVBQUUsTUFBTSxDQUFDLENBQUMsQ0FBQTtRQUNyRixDQUFFO1FBQUEsS0FBSyxDQUFDLENBQUMsR0FBRyxDQUFDLENBQUMsQ0FBQztZQUNYLE1BQU0sQ0FBQyxLQUFLLENBQUE7UUFDaEIsQ0FBQztJQUNMLENBQUM7SUFFRDs7T0FFRztJQUNILE1BQU0sQ0FBQyxVQUFrQ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