simple-git
Version:
Simple GIT interface for node.js
131 lines • 3.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const git_response_error_1 = require("../errors/git-response-error");
const functionNamesBuilderApi = [
'customBinary', 'env', 'outputHandler', 'silent',
];
const functionNamesPromiseApi = [
'add',
'addAnnotatedTag',
'addConfig',
'addRemote',
'addTag',
'binaryCatFile',
'branch',
'branchLocal',
'catFile',
'checkIgnore',
'checkIsRepo',
'checkout',
'checkoutBranch',
'checkoutLatestTag',
'checkoutLocalBranch',
'clean',
'clone',
'commit',
'cwd',
'deleteLocalBranch',
'deleteLocalBranches',
'diff',
'diffSummary',
'exec',
'fetch',
'getRemotes',
'init',
'listConfig',
'listRemote',
'log',
'merge',
'mergeFromTo',
'mirror',
'mv',
'pull',
'push',
'pushTags',
'raw',
'rebase',
'remote',
'removeRemote',
'reset',
'revert',
'revparse',
'rm',
'rmKeepLocal',
'show',
'stash',
'stashList',
'status',
'subModule',
'submoduleAdd',
'submoduleInit',
'submoduleUpdate',
'tag',
'tags',
'updateServerInfo'
];
const { gitInstanceFactory } = require('../../git-factory');
function gitP(...args) {
let git;
let chain = Promise.resolve();
try {
git = gitInstanceFactory(...args);
}
catch (e) {
chain = Promise.reject(e);
}
function builderReturn() {
return promiseApi;
}
function chainReturn() {
return chain;
}
const promiseApi = [...functionNamesBuilderApi, ...functionNamesPromiseApi].reduce((api, name) => {
const isAsync = functionNamesPromiseApi.includes(name);
const valid = isAsync ? asyncWrapper(name, git) : syncWrapper(name, git, api);
const alternative = isAsync ? chainReturn : builderReturn;
Object.defineProperty(api, name, {
enumerable: false,
configurable: false,
value: git ? valid : alternative,
});
return api;
}, {});
return promiseApi;
function asyncWrapper(fn, git) {
return function (...args) {
if (typeof args[args.length] === 'function') {
throw new TypeError('Promise interface requires that handlers are not supplied inline, ' +
'trailing function not allowed in call to ' + fn);
}
return chain.then(function () {
return new Promise(function (resolve, reject) {
const callback = (err, result) => {
if (err) {
return reject(toError(err));
}
resolve(result);
};
args.push(callback);
git[fn].apply(git, args);
});
});
};
}
function syncWrapper(fn, git, api) {
return (...args) => {
git[fn](...args);
return api;
};
}
}
exports.gitP = gitP;
function toError(error) {
if (error instanceof Error) {
return error;
}
if (typeof error === 'string') {
return new Error(error);
}
return new git_response_error_1.GitResponseError(error);
}
//# sourceMappingURL=promise-wrapped.js.map