1
- const fs = require ( 'fs' ) ;
2
- const Datastore = require ( '@seald-io/nedb' ) ;
1
+ import fs from 'fs' ;
2
+ import Datastore from '@seald-io/nedb'
3
+ import { Action } from '../../proxy/actions/Action' ;
4
+ import { Repo } from '../types' ;
3
5
4
6
if ( ! fs . existsSync ( './.data' ) ) fs . mkdirSync ( './.data' ) ;
5
7
if ( ! fs . existsSync ( './.data/db' ) ) fs . mkdirSync ( './.data/db' ) ;
6
8
7
9
const db = new Datastore ( { filename : './.data/db/repos.db' , autoload : true } ) ;
8
10
9
- exports . getRepos = async ( query = { } ) => {
10
- return new Promise ( ( resolve , reject ) => {
11
- db . find ( { } , ( err , docs ) => {
11
+ export const getRepos = async ( query = { } ) => {
12
+ return new Promise < Repo [ ] > ( ( resolve , reject ) => {
13
+ db . find ( { } , ( err : Error , docs : Repo [ ] ) => {
12
14
if ( err ) {
13
15
reject ( err ) ;
14
16
} else {
@@ -18,29 +20,26 @@ exports.getRepos = async (query = {}) => {
18
20
} ) ;
19
21
} ;
20
22
21
- exports . getRepo = async ( name ) => {
22
- return new Promise ( ( resolve , reject ) => {
23
- db . findOne ( { name : name } , ( err , doc ) => {
23
+ export const getRepo = async ( name : string ) => {
24
+ return new Promise < Repo | null > ( ( resolve , reject ) => {
25
+ db . findOne ( { name } , ( err : Error | null , doc : Repo ) => {
24
26
if ( err ) {
25
27
reject ( err ) ;
26
28
} else {
27
- if ( ! doc ) {
28
- resolve ( null ) ;
29
- } else {
30
- resolve ( doc ) ;
31
- }
29
+ resolve ( doc ) ;
32
30
}
33
31
} ) ;
34
32
} ) ;
35
33
} ;
36
34
37
- exports . createRepo = async ( repo ) => {
35
+
36
+ export const createRepo = async ( repo : Repo ) => {
38
37
repo . users = {
39
38
canPush : [ ] ,
40
39
canAuthorise : [ ] ,
41
40
} ;
42
41
43
- return new Promise ( ( resolve , reject ) => {
42
+ return new Promise < Repo > ( ( resolve , reject ) => {
44
43
db . insert ( repo , ( err , doc ) => {
45
44
if ( err ) {
46
45
reject ( err ) ;
@@ -51,9 +50,13 @@ exports.createRepo = async (repo) => {
51
50
} ) ;
52
51
} ;
53
52
54
- exports . addUserCanPush = async ( name , user ) => {
53
+ export const addUserCanPush = async ( name : string , user : string ) => {
55
54
return new Promise ( async ( resolve , reject ) => {
56
- const repo = await exports . getRepo ( name ) ;
55
+ const repo = await getRepo ( name ) ;
56
+ if ( ! repo ) {
57
+ reject ( new Error ( 'Repo not found' ) ) ;
58
+ return ;
59
+ }
57
60
58
61
if ( repo . users . canPush . includes ( user ) ) {
59
62
resolve ( null ) ;
@@ -72,9 +75,13 @@ exports.addUserCanPush = async (name, user) => {
72
75
} ) ;
73
76
} ;
74
77
75
- exports . addUserCanAuthorise = async ( name , user ) => {
78
+ export const addUserCanAuthorise = async ( name : string , user : string ) => {
76
79
return new Promise ( async ( resolve , reject ) => {
77
- const repo = await exports . getRepo ( name ) ;
80
+ const repo = await getRepo ( name ) ;
81
+ if ( ! repo ) {
82
+ reject ( new Error ( 'Repo not found' ) ) ;
83
+ return ;
84
+ }
78
85
79
86
if ( repo . users . canAuthorise . includes ( user ) ) {
80
87
resolve ( null ) ;
@@ -94,11 +101,15 @@ exports.addUserCanAuthorise = async (name, user) => {
94
101
} ) ;
95
102
} ;
96
103
97
- exports . removeUserCanAuthorise = async ( name , user ) => {
104
+ export const removeUserCanAuthorise = async ( name : string , user : string ) => {
98
105
return new Promise ( async ( resolve , reject ) => {
99
- const repo = await exports . getRepo ( name ) ;
106
+ const repo = await getRepo ( name ) ;
107
+ if ( ! repo ) {
108
+ reject ( new Error ( 'Repo not found' ) ) ;
109
+ return ;
110
+ }
100
111
101
- repo . users . canAuthorise = repo . users . canAuthorise . filter ( ( x ) => x != user ) ;
112
+ repo . users . canAuthorise = repo . users . canAuthorise . filter ( ( x : string ) => x != user ) ;
102
113
103
114
const options = { multi : false , upsert : false } ;
104
115
db . update ( { name : name } , repo , options , ( err ) => {
@@ -111,9 +122,13 @@ exports.removeUserCanAuthorise = async (name, user) => {
111
122
} ) ;
112
123
} ;
113
124
114
- exports . removeUserCanPush = async ( name , user ) => {
125
+ export const removeUserCanPush = async ( name : string , user : string ) => {
115
126
return new Promise ( async ( resolve , reject ) => {
116
- const repo = await exports . getRepo ( name ) ;
127
+ const repo = await getRepo ( name ) ;
128
+ if ( ! repo ) {
129
+ reject ( new Error ( 'Repo not found' ) ) ;
130
+ return ;
131
+ }
117
132
118
133
repo . users . canPush = repo . users . canPush . filter ( ( x ) => x != user ) ;
119
134
@@ -128,8 +143,8 @@ exports.removeUserCanPush = async (name, user) => {
128
143
} ) ;
129
144
} ;
130
145
131
- exports . deleteRepo = async ( name ) => {
132
- return new Promise ( ( resolve , reject ) => {
146
+ export const deleteRepo = async ( name : string ) => {
147
+ return new Promise < void > ( ( resolve , reject ) => {
133
148
db . remove ( { name : name } , ( err ) => {
134
149
if ( err ) {
135
150
reject ( err ) ;
@@ -140,9 +155,9 @@ exports.deleteRepo = async (name) => {
140
155
} ) ;
141
156
} ;
142
157
143
- exports . isUserPushAllowed = async ( name , user ) => {
158
+ export const isUserPushAllowed = async ( name : string , user : string ) => {
144
159
name = name . toLowerCase ( ) ;
145
- return new Promise ( async ( resolve , reject ) => {
160
+ return new Promise < boolean > ( async ( resolve , reject ) => {
146
161
const repo = await exports . getRepo ( name ) ;
147
162
console . log ( repo . users . canPush ) ;
148
163
console . log ( repo . users . canAuthorise ) ;
@@ -155,10 +170,10 @@ exports.isUserPushAllowed = async (name, user) => {
155
170
} ) ;
156
171
} ;
157
172
158
- exports . canUserApproveRejectPushRepo = async ( name , user ) => {
173
+ export const canUserApproveRejectPushRepo = async ( name : string , user : string ) => {
159
174
name = name . toLowerCase ( ) ;
160
175
console . log ( `checking if user ${ user } can approve/reject for ${ name } ` ) ;
161
- return new Promise ( async ( resolve , reject ) => {
176
+ return new Promise < boolean > ( async ( resolve , reject ) => {
162
177
const repo = await exports . getRepo ( name ) ;
163
178
if ( repo . users . canAuthorise . includes ( user ) ) {
164
179
console . log ( `user ${ user } can approve/reject to repo ${ name } ` ) ;
0 commit comments