1 | import Api from './modules/Api'
|
2 | import { Config, Find, File, Create, Update, Delete } from '../index.d'
|
3 |
|
4 | export default class Gitorm {
|
5 | private _token: string
|
6 | private _repository: string
|
7 | private _status: any
|
8 | private _owner: string
|
9 |
|
10 | constructor({ token, repository, owner, log = false }: Config) {
|
11 | this._token = token
|
12 | this._repository = repository
|
13 | this._owner = owner
|
14 |
|
15 | if (!log) {
|
16 | console.error = () => {
|
17 | return
|
18 | }
|
19 | }
|
20 | }
|
21 |
|
22 | async connect(): Promise<void | boolean> {
|
23 | try {
|
24 | const status = await Api.get(
|
25 | `/repos/${this._owner}/${this._repository}`,
|
26 | {
|
27 | headers: {
|
28 | Authorization: 'token ' + this._token
|
29 | }
|
30 | }
|
31 | )
|
32 | this._status =
|
33 | status && status.data && status.data.git_url
|
34 | ? status.data.git_url
|
35 | : false
|
36 | } catch (error) {
|
37 | this._status = false
|
38 | console.error(error)
|
39 | }
|
40 | }
|
41 |
|
42 | async find({ path }: Find): Promise<File | boolean> {
|
43 | if (!this._status) return false
|
44 |
|
45 | try {
|
46 | const response = await Api.get(
|
47 | `/repos/${this._owner}/${this._repository}/contents/${path}`,
|
48 | {
|
49 | headers: {
|
50 | Authorization: 'token ' + this._token
|
51 | }
|
52 | }
|
53 | )
|
54 | if (response.status !== 200) return false
|
55 |
|
56 | const file: File = response.data
|
57 |
|
58 | if (!file) return false
|
59 |
|
60 | return {
|
61 | name: file.name,
|
62 | path: file.path,
|
63 | sha: file.sha,
|
64 | size: file.size,
|
65 | url: file.url,
|
66 | html_url: file.html_url,
|
67 | git_url: file.git_url,
|
68 | download_url: file.download_url,
|
69 | type: file.type
|
70 | }
|
71 | } catch (error) {
|
72 | console.error(error)
|
73 | return false
|
74 | }
|
75 | }
|
76 |
|
77 | async findAll({ path }: Find): Promise<File[] | boolean> {
|
78 | if (!this._status) return false
|
79 |
|
80 | try {
|
81 | const response = await Api.get(
|
82 | `/repos/${this._owner}/${this._repository}/contents/${path}`,
|
83 | {
|
84 | headers: {
|
85 | Authorization: 'token ' + this._token
|
86 | }
|
87 | }
|
88 | )
|
89 | if (response.status !== 200) return false
|
90 |
|
91 | const files: File[] = response.data
|
92 |
|
93 | if (!files || files.length === 0) return false
|
94 |
|
95 | return files
|
96 | } catch (error) {
|
97 | console.error(error)
|
98 | return false
|
99 | }
|
100 | }
|
101 |
|
102 | async create({
|
103 | data,
|
104 | path,
|
105 | message = 'Create',
|
106 | branch = 'master'
|
107 | }: Create): Promise<File | boolean> {
|
108 | if (!this._status) return false
|
109 |
|
110 | try {
|
111 | const fileExists: boolean | File = await this.find({ path })
|
112 |
|
113 | if (fileExists) return false
|
114 |
|
115 | const response = await Api.put(
|
116 | `/repos/${this._owner}/${this._repository}/contents/${path}`,
|
117 | {
|
118 | message,
|
119 | content: Buffer.from(data).toString('base64'),
|
120 | branch
|
121 | },
|
122 | {
|
123 | headers: {
|
124 | Authorization: 'token ' + this._token
|
125 | }
|
126 | }
|
127 | )
|
128 |
|
129 | if (
|
130 | response.status !== 200 &&
|
131 | response.status !== 201 &&
|
132 | response.status !== 422
|
133 | )
|
134 | return false
|
135 |
|
136 | const file: File = response.data.content
|
137 |
|
138 | return {
|
139 | name: file.name,
|
140 | path: file.path,
|
141 | sha: file.sha,
|
142 | size: file.size,
|
143 | url: file.url,
|
144 | html_url: file.html_url,
|
145 | git_url: file.git_url,
|
146 | download_url: file.download_url,
|
147 | type: file.type
|
148 | }
|
149 | } catch (error) {
|
150 | console.error(error)
|
151 | return false
|
152 | }
|
153 | }
|
154 |
|
155 | async update({
|
156 | data,
|
157 | path,
|
158 | message = 'Update'
|
159 | }: Update): Promise<File | boolean> {
|
160 | if (!this._status) return false
|
161 |
|
162 | try {
|
163 | const fileExists: any = await this.find({ path })
|
164 |
|
165 | if (!fileExists) return false
|
166 |
|
167 | const response = await Api.put(
|
168 | `/repos/${this._owner}/${this._repository}/contents/${fileExists.path}`,
|
169 | {
|
170 | message,
|
171 | content: Buffer.from(data).toString('base64'),
|
172 | sha: fileExists.sha
|
173 | },
|
174 | {
|
175 | headers: {
|
176 | Authorization: 'token ' + this._token
|
177 | }
|
178 | }
|
179 | )
|
180 |
|
181 | if (response.status !== 200) return false
|
182 |
|
183 | const file: File = response.data.content
|
184 |
|
185 | return {
|
186 | name: file.name,
|
187 | path: file.path,
|
188 | sha: file.sha,
|
189 | size: file.size,
|
190 | url: file.url,
|
191 | html_url: file.html_url,
|
192 | git_url: file.git_url,
|
193 | download_url: file.download_url,
|
194 | type: file.type
|
195 | }
|
196 | } catch (error) {
|
197 | console.error(error)
|
198 | return false
|
199 | }
|
200 | }
|
201 |
|
202 | async delete({ path, message = 'Delete' }: Delete): Promise<boolean> {
|
203 | if (!this._status) return false
|
204 |
|
205 | try {
|
206 | const fileExists: any = await this.find({ path })
|
207 |
|
208 | if (!fileExists) return false
|
209 |
|
210 | const response = await Api.delete(
|
211 | `/repos/${this._owner}/${this._repository}/contents/${fileExists.path}`,
|
212 | {
|
213 | data: {
|
214 | message,
|
215 | sha: fileExists.sha
|
216 | },
|
217 | headers: {
|
218 | Authorization: 'token ' + this._token
|
219 | }
|
220 | }
|
221 | )
|
222 |
|
223 | if (response.status !== 200) return false
|
224 |
|
225 | return true
|
226 | } catch (error) {
|
227 | console.error(error)
|
228 | return false
|
229 | }
|
230 | }
|
231 |
|
232 | get status(): boolean {
|
233 | return this._status
|
234 | }
|
235 | }
|
236 |
|
237 | module.exports = Gitorm
|
238 | module.exports.default = Gitorm
|