-
-
Notifications
You must be signed in to change notification settings - Fork 313
/
simple-git-api.ts
151 lines (131 loc) · 4.2 KB
/
simple-git-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { SimpleGitBase } from '../../typings';
import { taskCallback } from './task-callback';
import { changeWorkingDirectoryTask } from './tasks/change-working-directory';
import checkout from './tasks/checkout';
import commit from './tasks/commit';
import config from './tasks/config';
import grep from './tasks/grep';
import { hashObjectTask } from './tasks/hash-object';
import { initTask } from './tasks/init';
import log from './tasks/log';
import { mergeTask } from './tasks/merge';
import { pushTask } from './tasks/push';
import show from './tasks/show';
import { statusTask } from './tasks/status';
import { configurationErrorTask, straightThroughStringTask } from './tasks/task';
import version from './tasks/version';
import { outputHandler, SimpleGitExecutor, SimpleGitTask, SimpleGitTaskCallback } from './types';
import {
asArray,
filterString,
filterType,
getTrailingOptions,
trailingFunctionArgument,
} from './utils';
export class SimpleGitApi implements SimpleGitBase {
constructor(private _executor: SimpleGitExecutor) {}
protected _runTask<T>(task: SimpleGitTask<T>, then?: SimpleGitTaskCallback<T>) {
const chain = this._executor.chain();
const promise = chain.push(task);
if (then) {
taskCallback(task, promise, then);
}
return Object.create(this, {
then: { value: promise.then.bind(promise) },
catch: { value: promise.catch.bind(promise) },
_executor: { value: chain },
});
}
add(files: string | string[]) {
return this._runTask(
straightThroughStringTask(['add', ...asArray(files)]),
trailingFunctionArgument(arguments)
);
}
cwd(directory: string | { path: string; root?: boolean }) {
const next = trailingFunctionArgument(arguments);
if (typeof directory === 'string') {
return this._runTask(changeWorkingDirectoryTask(directory, this._executor), next);
}
if (typeof directory?.path === 'string') {
return this._runTask(
changeWorkingDirectoryTask(
directory.path,
(directory.root && this._executor) || undefined
),
next
);
}
return this._runTask(
configurationErrorTask('Git.cwd: workingDirectory must be supplied as a string'),
next
);
}
hashObject(path: string, write: boolean | unknown) {
return this._runTask(
hashObjectTask(path, write === true),
trailingFunctionArgument(arguments)
);
}
init(bare?: boolean | unknown) {
return this._runTask(
initTask(bare === true, this._executor.cwd, getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
}
merge() {
return this._runTask(
mergeTask(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
}
mergeFromTo(remote: string, branch: string) {
if (!(filterString(remote) && filterString(branch))) {
return this._runTask(
configurationErrorTask(
`Git.mergeFromTo requires that the 'remote' and 'branch' arguments are supplied as strings`
)
);
}
return this._runTask(
mergeTask([remote, branch, ...getTrailingOptions(arguments)]),
trailingFunctionArgument(arguments, false)
);
}
outputHandler(handler: outputHandler) {
this._executor.outputHandler = handler;
return this;
}
push() {
const task = pushTask(
{
remote: filterType(arguments[0], filterString),
branch: filterType(arguments[1], filterString),
},
getTrailingOptions(arguments)
);
return this._runTask(task, trailingFunctionArgument(arguments));
}
stash() {
return this._runTask(
straightThroughStringTask(['stash', ...getTrailingOptions(arguments)]),
trailingFunctionArgument(arguments)
);
}
status() {
return this._runTask(
statusTask(getTrailingOptions(arguments)),
trailingFunctionArgument(arguments)
);
}
}
Object.assign(
SimpleGitApi.prototype,
checkout(),
commit(),
config(),
grep(),
log(),
show(),
version()
);