runok
Version:
NPM scripts on steroids! Replace your scripts with pure JS
128 lines (127 loc) • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GitConfig = void 0;
const task_1 = require("../task");
const exec_1 = require("./exec");
const result_1 = require("../result");
/**
* Provides flexible interface to running multiple git commands:
*
* ```js
* await git(cmd => {
* cmd.pull();
* cmd.commit('-m updated');
* cmd.push();
* })
* ```
*
* ### Commands API
*
* * `cmd.init`
* * `cmd.tag`
* * `cmd.branch`
* * `cmd.commit`
* * `cmd.pull`
* * `cmd.push`
* * `cmd.add`
* * `cmd.clone`
* * `cmd.cloneShallow`
*
* @param configFn
*/
async function git(configFn) {
const cfg = new GitConfig();
cfg.apply(configFn);
const result = result_1.Result.start(cfg.TASK, cfg.commands.map(c => `git ${c}`).join(' && '));
try {
for (const command of cfg.commands) {
await exec_1.default(`git ${command}`);
}
}
catch (error) {
return result.fail(error);
}
return result.success();
}
exports.default = git;
/**
* Git Config Class
*/
class GitConfig extends task_1.TaskConfig {
constructor() {
super(...arguments);
this.commands = [];
this.TASK = 'git';
}
/**
* @param tag
*/
tag(tag) {
this.commands.push(`tag ${tag}`);
return this;
}
/**
* Commit params
* @param message
*/
commit(message = '') {
this.commands.push(`commit ${message}`);
return this;
}
/**
* @param branch
*/
pull(branch = '') {
this.commands.push(`pull ${branch}`);
return this;
}
push(branch = '') {
this.commands.push(`push ${branch}`);
return this;
}
/**
* Initialize git repository
*/
init() {
this.commands.push(`init`);
return this;
}
/**
*
* @param params
*/
add(params = '') {
this.commands.push(`add ${params}`);
return this;
}
/**
*
* @param url
* @param path
*/
clone(url, path) {
this.commands.push(`clone ${url} ${path}`);
return this;
}
/**
*
* @param command
*/
branch(command) {
this.commands.push(`clone ${command}`);
return this;
}
cloneShallow(url, path) {
this.commands.push(`clone ${url} ${path} --depth=1`);
return this;
}
/**
*
* @param params
*/
checkout(params) {
this.commands.push(`checkout ${params}`);
return this;
}
}
exports.GitConfig = GitConfig;