@baadal-sdk/dapi 0.28.4 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/types/aws/client.d.ts +13 -0
- package/dist/types/aws/client.d.ts.map +1 -0
- package/dist/types/aws/db.d.ts +261 -9
- package/dist/types/aws/db.d.ts.map +1 -1
- package/dist/types/aws/index.d.ts +10 -11
- package/dist/types/aws/index.d.ts.map +1 -1
- package/dist/types/aws/s3.d.ts +90 -0
- package/dist/types/aws/s3.d.ts.map +1 -0
- package/dist/types/common/const.d.ts +3 -0
- package/dist/types/common/const.d.ts.map +1 -0
- package/dist/types/fs/index.d.ts +29 -25
- package/dist/types/fs/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +3 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/utils/index.d.ts +6 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/package.json +5 -1
- package/src/aws/client.ts +18 -0
- package/src/aws/db.ts +534 -105
- package/src/aws/index.ts +22 -8
- package/src/aws/s3.ts +476 -0
- package/src/common/const.ts +2 -0
- package/src/fs/index.ts +60 -54
- package/src/index.ts +3 -2
- package/src/utils/index.ts +39 -0
- package/dist/types/aws/db-client.d.ts +0 -7
- package/dist/types/aws/db-client.d.ts.map +0 -1
- package/src/aws/db-client.ts +0 -6
package/src/fs/index.ts
CHANGED
|
@@ -1,24 +1,19 @@
|
|
|
1
|
-
import path from 'path';
|
|
2
1
|
import util from 'util';
|
|
3
2
|
import fs from 'fs';
|
|
4
3
|
import fsa from 'fs/promises';
|
|
5
4
|
|
|
5
|
+
import { assertPath } from '../utils';
|
|
6
6
|
import { CustomError } from '../common/error';
|
|
7
7
|
import { warn as cwarn } from '../common/logger';
|
|
8
8
|
|
|
9
|
-
const assertPath = (p: string) => {
|
|
10
|
-
if (!p || p.startsWith('/')) return p;
|
|
11
|
-
return path.resolve(process.cwd(), p);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
9
|
/**
|
|
15
10
|
* Check whether a file exists
|
|
16
11
|
* @param file file path
|
|
17
12
|
* @param loud whether to throw errors [default: false]
|
|
18
|
-
* @returns true if it exists, false
|
|
13
|
+
* @returns true if it exists, false if it doesn't, null in case of error
|
|
19
14
|
*/
|
|
20
15
|
export const existsFileSync = (file: string, loud = false) => {
|
|
21
|
-
if (!file) return
|
|
16
|
+
if (!file) return null;
|
|
22
17
|
file = assertPath(file);
|
|
23
18
|
try {
|
|
24
19
|
if (!fs.existsSync(file)) {
|
|
@@ -26,7 +21,7 @@ export const existsFileSync = (file: string, loud = false) => {
|
|
|
26
21
|
throw new CustomError(`File does not exist: ${file}`);
|
|
27
22
|
}
|
|
28
23
|
} catch (e) {
|
|
29
|
-
if (!loud) return
|
|
24
|
+
if (!loud) return null;
|
|
30
25
|
if (e instanceof CustomError) {
|
|
31
26
|
throw e;
|
|
32
27
|
} else {
|
|
@@ -40,10 +35,10 @@ export const existsFileSync = (file: string, loud = false) => {
|
|
|
40
35
|
* Check whether a directory exists
|
|
41
36
|
* @param dir directory path
|
|
42
37
|
* @param loud whether to throw errors [default: false]
|
|
43
|
-
* @returns true if it exists, false
|
|
38
|
+
* @returns true if it exists, false if it doesn't, null in case of error
|
|
44
39
|
*/
|
|
45
40
|
export const existsDirSync = (dir: string, loud = false) => {
|
|
46
|
-
if (!dir) return
|
|
41
|
+
if (!dir) return null;
|
|
47
42
|
dir = assertPath(dir);
|
|
48
43
|
try {
|
|
49
44
|
if (!fs.existsSync(dir)) {
|
|
@@ -51,7 +46,7 @@ export const existsDirSync = (dir: string, loud = false) => {
|
|
|
51
46
|
throw new CustomError(`Directory does not exist: ${dir}`);
|
|
52
47
|
}
|
|
53
48
|
} catch (e) {
|
|
54
|
-
if (!loud) return
|
|
49
|
+
if (!loud) return null;
|
|
55
50
|
if (e instanceof CustomError) {
|
|
56
51
|
throw e;
|
|
57
52
|
} else {
|
|
@@ -101,61 +96,62 @@ export const readFileSync = (file: string, warn = false) => {
|
|
|
101
96
|
* Get the list of files/directories in a directory
|
|
102
97
|
* @param dir directory path
|
|
103
98
|
* @param warn whether to show warnings [default: false]
|
|
104
|
-
* @
|
|
99
|
+
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
100
|
+
* @returns an object {dirs,files} containing list of directories & files, null in case or error
|
|
105
101
|
*/
|
|
106
|
-
export const readDir = async (dir: string, warn = false) => {
|
|
107
|
-
if (!dir) return
|
|
102
|
+
export const readDir = async (dir: string, warn = false, hiddenItems = false) => {
|
|
103
|
+
if (!dir) return null;
|
|
108
104
|
dir = assertPath(dir);
|
|
109
105
|
|
|
110
|
-
let
|
|
111
|
-
let
|
|
106
|
+
let dirs: string[] = [];
|
|
107
|
+
let files: string[] = [];
|
|
112
108
|
|
|
113
109
|
try {
|
|
114
110
|
const items = await fsa.readdir(dir, { withFileTypes: true });
|
|
115
111
|
items.forEach(item => {
|
|
116
112
|
if (item.isDirectory()) {
|
|
117
|
-
|
|
118
|
-
dirs = [item.name];
|
|
119
|
-
} else {
|
|
120
|
-
dirs.push(item.name);
|
|
121
|
-
}
|
|
113
|
+
dirs.push(item.name);
|
|
122
114
|
} else if (item.isFile()) {
|
|
123
|
-
|
|
124
|
-
files = [item.name];
|
|
125
|
-
} else {
|
|
126
|
-
files.push(item.name);
|
|
127
|
-
}
|
|
115
|
+
files.push(item.name);
|
|
128
116
|
}
|
|
129
117
|
});
|
|
130
118
|
} catch (e) {
|
|
131
119
|
if (warn) cwarn(`Cannot read dir: ${dir}`);
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (!hiddenItems) {
|
|
124
|
+
dirs = (dirs as string[]).filter(d => !d.startsWith('.'));
|
|
125
|
+
files = (files as string[]).filter(f => !f.startsWith('.'));
|
|
132
126
|
}
|
|
133
127
|
|
|
134
|
-
return { dirs, files }
|
|
128
|
+
return { dirs, files };
|
|
135
129
|
};
|
|
136
130
|
|
|
137
131
|
/**
|
|
138
132
|
* Get the list of files in a directory
|
|
139
133
|
* @param dir directory path
|
|
140
134
|
* @param warn whether to show warnings [default: false]
|
|
135
|
+
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
141
136
|
* @returns list of files, null in case of error or no items
|
|
142
137
|
*/
|
|
143
|
-
export const readDirFiles = async (dir: string, warn = false) => {
|
|
138
|
+
export const readDirFiles = async (dir: string, warn = false, hiddenItems = false) => {
|
|
144
139
|
if (!dir) return null;
|
|
145
140
|
dir = assertPath(dir);
|
|
146
|
-
return (await readDir(dir, warn))
|
|
141
|
+
return (await readDir(dir, warn, hiddenItems))?.files;
|
|
147
142
|
};
|
|
148
143
|
|
|
149
144
|
/**
|
|
150
145
|
* Get the list of directories in a directory
|
|
151
146
|
* @param dir directory path
|
|
152
147
|
* @param warn whether to show warnings [default: false]
|
|
148
|
+
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
153
149
|
* @returns list of directories, null in case of error or no items
|
|
154
150
|
*/
|
|
155
|
-
export const readDirDirs = async (dir: string, warn = false) => {
|
|
151
|
+
export const readDirDirs = async (dir: string, warn = false, hiddenItems = false) => {
|
|
156
152
|
if (!dir) return null;
|
|
157
153
|
dir = assertPath(dir);
|
|
158
|
-
return (await readDir(dir, warn))
|
|
154
|
+
return (await readDir(dir, warn, hiddenItems))?.dirs;
|
|
159
155
|
};
|
|
160
156
|
|
|
161
157
|
const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string[] | null> => {
|
|
@@ -163,7 +159,10 @@ const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string
|
|
|
163
159
|
dir = assertPath(dir);
|
|
164
160
|
|
|
165
161
|
const dirPath = basePath ? `${dir}/${basePath}` : dir;
|
|
166
|
-
const
|
|
162
|
+
const readDirObj = await readDir(dirPath);
|
|
163
|
+
if (!readDirObj) return null;
|
|
164
|
+
|
|
165
|
+
const { dirs, files } = readDirObj;
|
|
167
166
|
let allFiles: string[] = files || [];
|
|
168
167
|
allFiles = allFiles.map(file => (basePath ? `${basePath}/${file}` : file));
|
|
169
168
|
const absDirs = (dirs || []).map(d => (basePath ? `${basePath}/${d}` : d));
|
|
@@ -176,24 +175,31 @@ const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string
|
|
|
176
175
|
}
|
|
177
176
|
});
|
|
178
177
|
|
|
179
|
-
return allFiles
|
|
178
|
+
return allFiles;
|
|
180
179
|
};
|
|
181
180
|
|
|
182
181
|
/**
|
|
183
182
|
* Get the (recursive) list of files in a directory
|
|
184
183
|
* @param dir directory path
|
|
185
|
-
* @
|
|
184
|
+
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
185
|
+
* @returns complete (recursive) list of files, null in case of error
|
|
186
186
|
*/
|
|
187
|
-
export const readDirFilesRec = (dir: string) =>
|
|
187
|
+
export const readDirFilesRec = async (dir: string, hiddenItems = false) => {
|
|
188
|
+
let allFiles = await readDirFilesRecHelper(dir);
|
|
189
|
+
if (!hiddenItems) {
|
|
190
|
+
if (allFiles) allFiles = allFiles.filter(f => !f.startsWith('.'));
|
|
191
|
+
}
|
|
192
|
+
return allFiles;
|
|
193
|
+
};
|
|
188
194
|
|
|
189
195
|
/**
|
|
190
196
|
* Write contents to a file (creates the file path if it doesn't exist)
|
|
191
197
|
* @param file file path
|
|
192
198
|
* @param contents contents to write
|
|
193
|
-
* @returns true if successful,
|
|
199
|
+
* @returns true if successful, null in case of error
|
|
194
200
|
*/
|
|
195
201
|
export const writeFile = async (file: string, contents: string) => {
|
|
196
|
-
if (!file || !contents) return
|
|
202
|
+
if (!file || !contents) return null;
|
|
197
203
|
file = assertPath(file);
|
|
198
204
|
try {
|
|
199
205
|
const dir = file.substring(0, file.lastIndexOf('/'));
|
|
@@ -201,7 +207,7 @@ export const writeFile = async (file: string, contents: string) => {
|
|
|
201
207
|
await fsa.writeFile(file, contents);
|
|
202
208
|
} catch (e) {
|
|
203
209
|
console.error(`Error while writing to ${file}`, e);
|
|
204
|
-
return
|
|
210
|
+
return null;
|
|
205
211
|
}
|
|
206
212
|
return true;
|
|
207
213
|
};
|
|
@@ -210,10 +216,10 @@ export const writeFile = async (file: string, contents: string) => {
|
|
|
210
216
|
* Append contents to a file
|
|
211
217
|
* @param file file path
|
|
212
218
|
* @param contents contents to append
|
|
213
|
-
* @returns true if successful,
|
|
219
|
+
* @returns true if successful, null in case of error
|
|
214
220
|
*/
|
|
215
221
|
export const appendToFile = async (file: string, contents: string) => {
|
|
216
|
-
if (!file || !contents) return
|
|
222
|
+
if (!file || !contents) return null;
|
|
217
223
|
file = assertPath(file);
|
|
218
224
|
try {
|
|
219
225
|
const dir = file.substring(0, file.lastIndexOf('/'));
|
|
@@ -227,7 +233,7 @@ export const appendToFile = async (file: string, contents: string) => {
|
|
|
227
233
|
// stream.end();
|
|
228
234
|
} catch (e) {
|
|
229
235
|
console.error(`Error while appending to ${file}`, e);
|
|
230
|
-
return
|
|
236
|
+
return null;
|
|
231
237
|
}
|
|
232
238
|
return true;
|
|
233
239
|
};
|
|
@@ -236,17 +242,17 @@ export const appendToFile = async (file: string, contents: string) => {
|
|
|
236
242
|
* Rename a file
|
|
237
243
|
* @param oldpath old file path
|
|
238
244
|
* @param newpath new file path
|
|
239
|
-
* @returns true if successful,
|
|
245
|
+
* @returns true if successful, null in case of error
|
|
240
246
|
*/
|
|
241
247
|
export const renameFile = async (oldpath: string, newpath: string) => {
|
|
242
|
-
if (!oldpath || !newpath) return
|
|
248
|
+
if (!oldpath || !newpath) return null;
|
|
243
249
|
oldpath = assertPath(oldpath);
|
|
244
250
|
newpath = assertPath(newpath);
|
|
245
251
|
try {
|
|
246
252
|
await fsa.rename(oldpath, newpath);
|
|
247
253
|
} catch (e) {
|
|
248
254
|
console.error(`Error while renaming file ${oldpath} to ${newpath}`, e);
|
|
249
|
-
return
|
|
255
|
+
return null;
|
|
250
256
|
}
|
|
251
257
|
return true;
|
|
252
258
|
};
|
|
@@ -254,10 +260,10 @@ export const renameFile = async (oldpath: string, newpath: string) => {
|
|
|
254
260
|
/**
|
|
255
261
|
* Create a directory, if it doesn't exist
|
|
256
262
|
* @param dir directory path
|
|
257
|
-
* @returns true if successful,
|
|
263
|
+
* @returns true if successful, null in case of failure/error
|
|
258
264
|
*/
|
|
259
265
|
export const createDir = async (dir: string) => {
|
|
260
|
-
if (!dir) return
|
|
266
|
+
if (!dir) return null;
|
|
261
267
|
dir = assertPath(dir);
|
|
262
268
|
try {
|
|
263
269
|
if (!existsDirSync(dir)) {
|
|
@@ -265,7 +271,7 @@ export const createDir = async (dir: string) => {
|
|
|
265
271
|
}
|
|
266
272
|
} catch (e) {
|
|
267
273
|
console.error(`Error while creating directory: ${dir}`, e);
|
|
268
|
-
return
|
|
274
|
+
return null;
|
|
269
275
|
}
|
|
270
276
|
return true;
|
|
271
277
|
};
|
|
@@ -273,16 +279,16 @@ export const createDir = async (dir: string) => {
|
|
|
273
279
|
/**
|
|
274
280
|
* Delete a file
|
|
275
281
|
* @param file file path
|
|
276
|
-
* @returns true if successful,
|
|
282
|
+
* @returns true if successful, null in case of error
|
|
277
283
|
*/
|
|
278
284
|
export const deleteFile = async (file: string) => {
|
|
279
|
-
if (!file) return
|
|
285
|
+
if (!file) return null;
|
|
280
286
|
file = assertPath(file);
|
|
281
287
|
try {
|
|
282
288
|
await fsa.unlink(file);
|
|
283
289
|
} catch (e) {
|
|
284
290
|
console.error(`Error while deleting file ${file}`, e);
|
|
285
|
-
return
|
|
291
|
+
return null;
|
|
286
292
|
}
|
|
287
293
|
return true;
|
|
288
294
|
};
|
|
@@ -290,10 +296,10 @@ export const deleteFile = async (file: string) => {
|
|
|
290
296
|
/**
|
|
291
297
|
* Delete a directory
|
|
292
298
|
* @param dir directory path
|
|
293
|
-
* @returns true if successful,
|
|
299
|
+
* @returns true if successful, null in case of error
|
|
294
300
|
*/
|
|
295
301
|
export const deleteDir = async (dir: string) => {
|
|
296
|
-
if (!dir) return
|
|
302
|
+
if (!dir) return null;
|
|
297
303
|
dir = assertPath(dir);
|
|
298
304
|
try {
|
|
299
305
|
const rimraf = require('rimraf');
|
|
@@ -304,7 +310,7 @@ export const deleteDir = async (dir: string) => {
|
|
|
304
310
|
// await fsa.rm(dir, { recursive: true, force: true });
|
|
305
311
|
} catch (e) {
|
|
306
312
|
console.error(`Error while deleting dir ${dir}`, e);
|
|
307
|
-
return
|
|
313
|
+
return null;
|
|
308
314
|
}
|
|
309
315
|
return true;
|
|
310
316
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as fs from './fs';
|
|
2
2
|
import * as aws from './aws';
|
|
3
3
|
import * as gh from './gh';
|
|
4
|
+
import * as utils from './utils';
|
|
4
5
|
|
|
5
6
|
// Ref: https://stackoverflow.com/a/41283945
|
|
6
|
-
export { fs, aws, gh }; // named exports
|
|
7
|
-
export default { fs, aws, gh }; // default export
|
|
7
|
+
export { fs, aws, gh, utils }; // named exports
|
|
8
|
+
export default { fs, aws, gh, utils }; // default export
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import fsa from 'fs/promises';
|
|
4
|
+
|
|
5
|
+
import { error } from '../common/logger';
|
|
6
|
+
|
|
7
|
+
export const assertPath = (p: string) => {
|
|
8
|
+
if (!p || p.startsWith('/')) return p;
|
|
9
|
+
return path.resolve(process.cwd(), p);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export const sha1Hash = (data: string) => {
|
|
13
|
+
if (!data) return null;
|
|
14
|
+
const hashSum = crypto.createHash('sha1');
|
|
15
|
+
hashSum.update(data);
|
|
16
|
+
return hashSum.digest('hex');
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const sha256Hash = (data: string | Buffer) => {
|
|
20
|
+
if (!data) return null;
|
|
21
|
+
const hashSum = crypto.createHash('sha256');
|
|
22
|
+
hashSum.update(data);
|
|
23
|
+
return hashSum.digest('hex');
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const fileHash = async (file: string) => {
|
|
27
|
+
if (!file) return null;
|
|
28
|
+
let contents: Buffer | null = null;
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
// get buffer (instead of utf8 string) to support binary data
|
|
32
|
+
contents = await fsa.readFile(file);
|
|
33
|
+
} catch (e) {
|
|
34
|
+
error(e);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return sha256Hash(contents);
|
|
39
|
+
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"db-client.d.ts","sourceRoot":"","sources":["../../../src/aws/db-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;;iBAKV,sBAAsB,GAAG,IAAI;QAAM,MAAM,GAAG,IAAI;;AAArG,wBAAwG"}
|
package/src/aws/db-client.ts
DELETED