@baadal-sdk/dapi 0.30.0 → 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/db.d.ts +2 -2
- package/dist/types/aws/db.d.ts.map +1 -1
- package/dist/types/aws/s3.d.ts +2 -2
- package/dist/types/aws/s3.d.ts.map +1 -1
- package/dist/types/fs/index.d.ts +23 -23
- package/dist/types/fs/index.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/aws/db.ts +20 -17
- package/src/aws/s3.ts +12 -12
- package/src/fs/index.ts +42 -46
package/dist/types/fs/index.d.ts
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
* Check whether a file exists
|
|
3
3
|
* @param file file path
|
|
4
4
|
* @param loud whether to throw errors [default: false]
|
|
5
|
-
* @returns true if it exists, false
|
|
5
|
+
* @returns true if it exists, false if it doesn't, null in case of error
|
|
6
6
|
*/
|
|
7
|
-
export declare const existsFileSync: (file: string, loud?: boolean) => boolean;
|
|
7
|
+
export declare const existsFileSync: (file: string, loud?: boolean) => boolean | null;
|
|
8
8
|
/**
|
|
9
9
|
* Check whether a directory exists
|
|
10
10
|
* @param dir directory path
|
|
11
11
|
* @param loud whether to throw errors [default: false]
|
|
12
|
-
* @returns true if it exists, false
|
|
12
|
+
* @returns true if it exists, false if it doesn't, null in case of error
|
|
13
13
|
*/
|
|
14
|
-
export declare const existsDirSync: (dir: string, loud?: boolean) => boolean;
|
|
14
|
+
export declare const existsDirSync: (dir: string, loud?: boolean) => boolean | null;
|
|
15
15
|
/**
|
|
16
16
|
* Read contents of a file
|
|
17
17
|
* @param file file path
|
|
@@ -31,12 +31,12 @@ export declare const readFileSync: (file: string, warn?: boolean) => string | nu
|
|
|
31
31
|
* @param dir directory path
|
|
32
32
|
* @param warn whether to show warnings [default: false]
|
|
33
33
|
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
34
|
-
* @returns an object {dirs,files} containing list of directories & files
|
|
34
|
+
* @returns an object {dirs,files} containing list of directories & files, null in case or error
|
|
35
35
|
*/
|
|
36
36
|
export declare const readDir: (dir: string, warn?: boolean, hiddenItems?: boolean) => Promise<{
|
|
37
|
-
dirs: string[]
|
|
38
|
-
files: string[]
|
|
39
|
-
}>;
|
|
37
|
+
dirs: string[];
|
|
38
|
+
files: string[];
|
|
39
|
+
} | null>;
|
|
40
40
|
/**
|
|
41
41
|
* Get the list of files in a directory
|
|
42
42
|
* @param dir directory path
|
|
@@ -44,7 +44,7 @@ export declare const readDir: (dir: string, warn?: boolean, hiddenItems?: boolea
|
|
|
44
44
|
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
45
45
|
* @returns list of files, null in case of error or no items
|
|
46
46
|
*/
|
|
47
|
-
export declare const readDirFiles: (dir: string, warn?: boolean, hiddenItems?: boolean) => Promise<string[] | null>;
|
|
47
|
+
export declare const readDirFiles: (dir: string, warn?: boolean, hiddenItems?: boolean) => Promise<string[] | null | undefined>;
|
|
48
48
|
/**
|
|
49
49
|
* Get the list of directories in a directory
|
|
50
50
|
* @param dir directory path
|
|
@@ -52,51 +52,51 @@ export declare const readDirFiles: (dir: string, warn?: boolean, hiddenItems?: b
|
|
|
52
52
|
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
53
53
|
* @returns list of directories, null in case of error or no items
|
|
54
54
|
*/
|
|
55
|
-
export declare const readDirDirs: (dir: string, warn?: boolean, hiddenItems?: boolean) => Promise<string[] | null>;
|
|
55
|
+
export declare const readDirDirs: (dir: string, warn?: boolean, hiddenItems?: boolean) => Promise<string[] | null | undefined>;
|
|
56
56
|
/**
|
|
57
57
|
* Get the (recursive) list of files in a directory
|
|
58
58
|
* @param dir directory path
|
|
59
59
|
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
60
|
-
* @returns complete (recursive) list of files, null in case of error
|
|
60
|
+
* @returns complete (recursive) list of files, null in case of error
|
|
61
61
|
*/
|
|
62
62
|
export declare const readDirFilesRec: (dir: string, hiddenItems?: boolean) => Promise<string[] | null>;
|
|
63
63
|
/**
|
|
64
64
|
* Write contents to a file (creates the file path if it doesn't exist)
|
|
65
65
|
* @param file file path
|
|
66
66
|
* @param contents contents to write
|
|
67
|
-
* @returns true if successful,
|
|
67
|
+
* @returns true if successful, null in case of error
|
|
68
68
|
*/
|
|
69
|
-
export declare const writeFile: (file: string, contents: string) => Promise<
|
|
69
|
+
export declare const writeFile: (file: string, contents: string) => Promise<true | null>;
|
|
70
70
|
/**
|
|
71
71
|
* Append contents to a file
|
|
72
72
|
* @param file file path
|
|
73
73
|
* @param contents contents to append
|
|
74
|
-
* @returns true if successful,
|
|
74
|
+
* @returns true if successful, null in case of error
|
|
75
75
|
*/
|
|
76
|
-
export declare const appendToFile: (file: string, contents: string) => Promise<
|
|
76
|
+
export declare const appendToFile: (file: string, contents: string) => Promise<true | null>;
|
|
77
77
|
/**
|
|
78
78
|
* Rename a file
|
|
79
79
|
* @param oldpath old file path
|
|
80
80
|
* @param newpath new file path
|
|
81
|
-
* @returns true if successful,
|
|
81
|
+
* @returns true if successful, null in case of error
|
|
82
82
|
*/
|
|
83
|
-
export declare const renameFile: (oldpath: string, newpath: string) => Promise<
|
|
83
|
+
export declare const renameFile: (oldpath: string, newpath: string) => Promise<true | null>;
|
|
84
84
|
/**
|
|
85
85
|
* Create a directory, if it doesn't exist
|
|
86
86
|
* @param dir directory path
|
|
87
|
-
* @returns true if successful,
|
|
87
|
+
* @returns true if successful, null in case of failure/error
|
|
88
88
|
*/
|
|
89
|
-
export declare const createDir: (dir: string) => Promise<
|
|
89
|
+
export declare const createDir: (dir: string) => Promise<true | null>;
|
|
90
90
|
/**
|
|
91
91
|
* Delete a file
|
|
92
92
|
* @param file file path
|
|
93
|
-
* @returns true if successful,
|
|
93
|
+
* @returns true if successful, null in case of error
|
|
94
94
|
*/
|
|
95
|
-
export declare const deleteFile: (file: string) => Promise<
|
|
95
|
+
export declare const deleteFile: (file: string) => Promise<true | null>;
|
|
96
96
|
/**
|
|
97
97
|
* Delete a directory
|
|
98
98
|
* @param dir directory path
|
|
99
|
-
* @returns true if successful,
|
|
99
|
+
* @returns true if successful, null in case of error
|
|
100
100
|
*/
|
|
101
|
-
export declare const deleteDir: (dir: string) => Promise<
|
|
101
|
+
export declare const deleteDir: (dir: string) => Promise<true | null>;
|
|
102
102
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/fs/index.ts"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,eAAO,MAAM,cAAc,SAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/fs/index.ts"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,eAAO,MAAM,cAAc,SAAU,MAAM,mCAiB1C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,aAAa,QAAS,MAAM,mCAiBxC,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,QAAQ,SAAgB,MAAM,2CAU1C,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,SAAU,MAAM,kCAUxC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,OAAO,QAAe,MAAM;;;SA2BxC,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,QAAe,MAAM,gFAI7C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,QAAe,MAAM,gFAI5C,CAAC;AA0BF;;;;;GAKG;AACH,eAAO,MAAM,eAAe,QAAe,MAAM,oDAMhD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,SAAS,SAAgB,MAAM,YAAY,MAAM,yBAY7D,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,YAAY,SAAgB,MAAM,YAAY,MAAM,yBAkBhE,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,UAAU,YAAmB,MAAM,WAAW,MAAM,yBAWhE,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,SAAS,QAAe,MAAM,yBAY1C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,UAAU,SAAgB,MAAM,yBAU5C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,SAAS,QAAe,MAAM,yBAe1C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@baadal-sdk/dapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "Dead-simple API wrappers",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"@aws-sdk/client-dynamodb": "^3.34.0",
|
|
78
78
|
"@aws-sdk/client-s3": "^3.36.0",
|
|
79
79
|
"@aws-sdk/lib-dynamodb": "^3.34.0",
|
|
80
|
-
"@baadal-sdk/utils": "0.
|
|
80
|
+
"@baadal-sdk/utils": "0.12.0",
|
|
81
81
|
"@octokit/core": "^3.5.1",
|
|
82
82
|
"chalk": "^2.4.2",
|
|
83
83
|
"core-js": "^3.18.0",
|
package/src/aws/db.ts
CHANGED
|
@@ -176,7 +176,8 @@ export const writeItem = async (input: WriteItemInput) => {
|
|
|
176
176
|
const batchWriteItems = async (table: string, items: StringIndexable[]) => {
|
|
177
177
|
if (!dbClient.client) tryInit();
|
|
178
178
|
if (!dbClient.client) return null;
|
|
179
|
-
if (!table || !items || !Array.isArray(items)
|
|
179
|
+
if (!table || !items || !Array.isArray(items)) return null;
|
|
180
|
+
if (!items.length) return false;
|
|
180
181
|
|
|
181
182
|
const reqList = items.map(item => ({ PutRequest: { Item: item } }));
|
|
182
183
|
const cmdParams: BatchWriteCommandInput = {
|
|
@@ -224,7 +225,8 @@ export interface WriteItemsAllInput {
|
|
|
224
225
|
export const writeItemsAll = async (input: WriteItemsAllInput) => {
|
|
225
226
|
if (!dbClient.client) tryInit();
|
|
226
227
|
if (!dbClient.client) return null;
|
|
227
|
-
if (!input.table || !input.items || !Array.isArray(input.items)
|
|
228
|
+
if (!input.table || !input.items || !Array.isArray(input.items)) return null;
|
|
229
|
+
if (!input.items.length) return false;
|
|
228
230
|
|
|
229
231
|
let errFlag = false;
|
|
230
232
|
|
|
@@ -368,9 +370,10 @@ const batchReadItems = async <T = any>(
|
|
|
368
370
|
) => {
|
|
369
371
|
if (!dbClient.client) tryInit();
|
|
370
372
|
if (!dbClient.client) return null;
|
|
371
|
-
if (!table || !keys || !Array.isArray(keys)
|
|
373
|
+
if (!table || !keys || !Array.isArray(keys)) return null;
|
|
374
|
+
if (!keys.length) return [];
|
|
372
375
|
|
|
373
|
-
let contents: StringIndexable<T>[]
|
|
376
|
+
let contents: StringIndexable<T>[] = [];
|
|
374
377
|
|
|
375
378
|
let reqParams: any = { Keys: keys };
|
|
376
379
|
if (projection) reqParams = { ...reqParams, ProjectionExpression: projection };
|
|
@@ -432,9 +435,10 @@ export interface ReadItemsAllInput {
|
|
|
432
435
|
export const readItemsAll = async <T = any>(input: ReadItemsAllInput) => {
|
|
433
436
|
if (!dbClient.client) tryInit();
|
|
434
437
|
if (!dbClient.client) return null;
|
|
435
|
-
if (!input.table || !input.keys || !Array.isArray(input.keys)
|
|
438
|
+
if (!input.table || !input.keys || !Array.isArray(input.keys)) return null;
|
|
439
|
+
if (!input.keys.length) return [];
|
|
436
440
|
|
|
437
|
-
let contents: StringIndexable<T>[]
|
|
441
|
+
let contents: StringIndexable<T>[] = [];
|
|
438
442
|
let errFlag = false;
|
|
439
443
|
|
|
440
444
|
const batchedKeys = chunkifyArray(input.keys, BATCH_SIZE);
|
|
@@ -450,13 +454,10 @@ export const readItemsAll = async <T = any>(input: ReadItemsAllInput) => {
|
|
|
450
454
|
const isError = icontents.find(e => e === null) === null;
|
|
451
455
|
if (isError) {
|
|
452
456
|
errFlag = true;
|
|
453
|
-
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
} else {
|
|
458
|
-
contents = icontents as StringIndexable[];
|
|
459
|
-
}
|
|
457
|
+
return null;
|
|
458
|
+
}
|
|
459
|
+
if (!errFlag) {
|
|
460
|
+
contents = contents.concat(icontents as StringIndexable[]);
|
|
460
461
|
}
|
|
461
462
|
}
|
|
462
463
|
|
|
@@ -504,7 +505,7 @@ export const queryItems = async (input: QueryItemsInput) => {
|
|
|
504
505
|
if (!dbClient.client) return null;
|
|
505
506
|
if (!input.table || !input.cond || !input.attr) return null;
|
|
506
507
|
|
|
507
|
-
let contents: StringIndexable[]
|
|
508
|
+
let contents: StringIndexable[] = [];
|
|
508
509
|
const desc = input.desc || false;
|
|
509
510
|
|
|
510
511
|
// Ref: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html
|
|
@@ -570,7 +571,7 @@ export const scanItems = async (input: ScanItemsInput) => {
|
|
|
570
571
|
if (!dbClient.client) return null;
|
|
571
572
|
if (!input.table) return null;
|
|
572
573
|
|
|
573
|
-
let contents: StringIndexable[]
|
|
574
|
+
let contents: StringIndexable[] = [];
|
|
574
575
|
|
|
575
576
|
// Ref: https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/dynamodb-example-query-scan.html
|
|
576
577
|
let cmdParams: ScanCommandInput = {
|
|
@@ -650,7 +651,8 @@ export const deleteItem = async (input: DeleteItemInput) => {
|
|
|
650
651
|
const batchDeleteItems = async (table: string, keys: StringIndexable[]) => {
|
|
651
652
|
if (!dbClient.client) tryInit();
|
|
652
653
|
if (!dbClient.client) return null;
|
|
653
|
-
if (!table || !keys || !Array.isArray(keys)
|
|
654
|
+
if (!table || !keys || !Array.isArray(keys)) return null;
|
|
655
|
+
if (!keys.length) return false;
|
|
654
656
|
|
|
655
657
|
const reqList = keys.map(key => ({ DeleteRequest: { Key: key } }));
|
|
656
658
|
const cmdParams: BatchWriteCommandInput = {
|
|
@@ -698,7 +700,8 @@ export interface DeleteItemsAllInput {
|
|
|
698
700
|
export const deleteItemsAll = async (input: DeleteItemsAllInput) => {
|
|
699
701
|
if (!dbClient.client) tryInit();
|
|
700
702
|
if (!dbClient.client) return null;
|
|
701
|
-
if (!input.table || !input.keys || !Array.isArray(input.keys)
|
|
703
|
+
if (!input.table || !input.keys || !Array.isArray(input.keys)) return null;
|
|
704
|
+
if (!input.keys.length) return false;
|
|
702
705
|
|
|
703
706
|
let errFlag = false;
|
|
704
707
|
|
package/src/aws/s3.ts
CHANGED
|
@@ -175,7 +175,8 @@ export const uploadFile = async (bucket: string, file: string, s3path?: string)
|
|
|
175
175
|
export const uploadFilesAll = async (bucket: string, files: string[], s3paths?: string[]) => {
|
|
176
176
|
if (!s3Client.client) tryInit();
|
|
177
177
|
if (!s3Client.client) return null;
|
|
178
|
-
if (!bucket || !files || !Array.isArray(files)
|
|
178
|
+
if (!bucket || !files || !Array.isArray(files)) return null;
|
|
179
|
+
if (!files.length) return false;
|
|
179
180
|
if (s3paths && (!Array.isArray(s3paths) || !s3paths.length || files.length !== s3paths.length)) return null;
|
|
180
181
|
|
|
181
182
|
let errFlag = false;
|
|
@@ -292,7 +293,7 @@ export const listObjects = async (bucket: string, prefix?: string) => {
|
|
|
292
293
|
if (!s3Client.client) return null;
|
|
293
294
|
if (!bucket) return null;
|
|
294
295
|
|
|
295
|
-
let filesList: string[]
|
|
296
|
+
let filesList: string[] = [];
|
|
296
297
|
|
|
297
298
|
let cmdParams: ListObjectsV2CommandInput = { Bucket: bucket };
|
|
298
299
|
if (prefix) cmdParams = { ...cmdParams, Prefix: prefix };
|
|
@@ -363,9 +364,10 @@ export const getObjectHead = async (bucket: string, s3path: string) => {
|
|
|
363
364
|
export const getObjectHeadsAll = async (bucket: string, s3paths: string[]) => {
|
|
364
365
|
if (!s3Client.client) tryInit();
|
|
365
366
|
if (!s3Client.client) return null;
|
|
366
|
-
if (!bucket || !s3paths || !Array.isArray(s3paths)
|
|
367
|
+
if (!bucket || !s3paths || !Array.isArray(s3paths)) return null;
|
|
368
|
+
if (!s3paths.length) return [];
|
|
367
369
|
|
|
368
|
-
let contents: (HeadObject | null)[]
|
|
370
|
+
let contents: (HeadObject | null)[] = [];
|
|
369
371
|
|
|
370
372
|
const chunkedItems = chunkifyArray(s3paths, CHUNK_SIZE);
|
|
371
373
|
|
|
@@ -374,14 +376,10 @@ export const getObjectHeadsAll = async (bucket: string, s3paths: string[]) => {
|
|
|
374
376
|
const pList = chunk.map(item => getObjectHead(bucket, item));
|
|
375
377
|
const rList = await Promise.all(pList); // eslint-disable-line no-await-in-loop
|
|
376
378
|
|
|
377
|
-
|
|
378
|
-
contents = contents.concat(rList);
|
|
379
|
-
} else {
|
|
380
|
-
contents = rList;
|
|
381
|
-
}
|
|
379
|
+
contents = contents.concat(rList);
|
|
382
380
|
}
|
|
383
381
|
|
|
384
|
-
if (contents
|
|
382
|
+
if (contents.length) {
|
|
385
383
|
contents = contents.filter(e => !!e);
|
|
386
384
|
}
|
|
387
385
|
|
|
@@ -417,7 +415,8 @@ export const deleteObject = async (bucket: string, s3path: string) => {
|
|
|
417
415
|
const batchDeleteObjects = async (bucket: string, s3paths: string[]) => {
|
|
418
416
|
if (!s3Client.client) tryInit();
|
|
419
417
|
if (!s3Client.client) return null;
|
|
420
|
-
if (!bucket || !s3paths || !Array.isArray(s3paths)
|
|
418
|
+
if (!bucket || !s3paths || !Array.isArray(s3paths)) return null;
|
|
419
|
+
if (!s3paths.length) return false;
|
|
421
420
|
|
|
422
421
|
const keys = s3paths.map(key => ({ Key: key }));
|
|
423
422
|
const cmdParams: DeleteObjectsCommandInput = { Bucket: bucket, Delete: { Objects: keys } };
|
|
@@ -444,7 +443,8 @@ const batchDeleteObjects = async (bucket: string, s3paths: string[]) => {
|
|
|
444
443
|
export const deleteObjectsAll = async (bucket: string, s3paths: string[]) => {
|
|
445
444
|
if (!s3Client.client) tryInit();
|
|
446
445
|
if (!s3Client.client) return null;
|
|
447
|
-
if (!bucket || !s3paths || !Array.isArray(s3paths)
|
|
446
|
+
if (!bucket || !s3paths || !Array.isArray(s3paths)) return null;
|
|
447
|
+
if (!s3paths.length) return false;
|
|
448
448
|
|
|
449
449
|
let errFlag = false;
|
|
450
450
|
|
package/src/fs/index.ts
CHANGED
|
@@ -10,10 +10,10 @@ import { warn as cwarn } from '../common/logger';
|
|
|
10
10
|
* Check whether a file exists
|
|
11
11
|
* @param file file path
|
|
12
12
|
* @param loud whether to throw errors [default: false]
|
|
13
|
-
* @returns true if it exists, false
|
|
13
|
+
* @returns true if it exists, false if it doesn't, null in case of error
|
|
14
14
|
*/
|
|
15
15
|
export const existsFileSync = (file: string, loud = false) => {
|
|
16
|
-
if (!file) return
|
|
16
|
+
if (!file) return null;
|
|
17
17
|
file = assertPath(file);
|
|
18
18
|
try {
|
|
19
19
|
if (!fs.existsSync(file)) {
|
|
@@ -21,7 +21,7 @@ export const existsFileSync = (file: string, loud = false) => {
|
|
|
21
21
|
throw new CustomError(`File does not exist: ${file}`);
|
|
22
22
|
}
|
|
23
23
|
} catch (e) {
|
|
24
|
-
if (!loud) return
|
|
24
|
+
if (!loud) return null;
|
|
25
25
|
if (e instanceof CustomError) {
|
|
26
26
|
throw e;
|
|
27
27
|
} else {
|
|
@@ -35,10 +35,10 @@ export const existsFileSync = (file: string, loud = false) => {
|
|
|
35
35
|
* Check whether a directory exists
|
|
36
36
|
* @param dir directory path
|
|
37
37
|
* @param loud whether to throw errors [default: false]
|
|
38
|
-
* @returns true if it exists, false
|
|
38
|
+
* @returns true if it exists, false if it doesn't, null in case of error
|
|
39
39
|
*/
|
|
40
40
|
export const existsDirSync = (dir: string, loud = false) => {
|
|
41
|
-
if (!dir) return
|
|
41
|
+
if (!dir) return null;
|
|
42
42
|
dir = assertPath(dir);
|
|
43
43
|
try {
|
|
44
44
|
if (!fs.existsSync(dir)) {
|
|
@@ -46,7 +46,7 @@ export const existsDirSync = (dir: string, loud = false) => {
|
|
|
46
46
|
throw new CustomError(`Directory does not exist: ${dir}`);
|
|
47
47
|
}
|
|
48
48
|
} catch (e) {
|
|
49
|
-
if (!loud) return
|
|
49
|
+
if (!loud) return null;
|
|
50
50
|
if (e instanceof CustomError) {
|
|
51
51
|
throw e;
|
|
52
52
|
} else {
|
|
@@ -97,42 +97,35 @@ export const readFileSync = (file: string, warn = false) => {
|
|
|
97
97
|
* @param dir directory path
|
|
98
98
|
* @param warn whether to show warnings [default: false]
|
|
99
99
|
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
100
|
-
* @returns an object {dirs,files} containing list of directories & files
|
|
100
|
+
* @returns an object {dirs,files} containing list of directories & files, null in case or error
|
|
101
101
|
*/
|
|
102
102
|
export const readDir = async (dir: string, warn = false, hiddenItems = false) => {
|
|
103
|
-
if (!dir) return
|
|
103
|
+
if (!dir) return null;
|
|
104
104
|
dir = assertPath(dir);
|
|
105
105
|
|
|
106
|
-
let dirs: string[]
|
|
107
|
-
let files: string[]
|
|
106
|
+
let dirs: string[] = [];
|
|
107
|
+
let files: string[] = [];
|
|
108
108
|
|
|
109
109
|
try {
|
|
110
110
|
const items = await fsa.readdir(dir, { withFileTypes: true });
|
|
111
111
|
items.forEach(item => {
|
|
112
112
|
if (item.isDirectory()) {
|
|
113
|
-
|
|
114
|
-
dirs = [item.name];
|
|
115
|
-
} else {
|
|
116
|
-
dirs.push(item.name);
|
|
117
|
-
}
|
|
113
|
+
dirs.push(item.name);
|
|
118
114
|
} else if (item.isFile()) {
|
|
119
|
-
|
|
120
|
-
files = [item.name];
|
|
121
|
-
} else {
|
|
122
|
-
files.push(item.name);
|
|
123
|
-
}
|
|
115
|
+
files.push(item.name);
|
|
124
116
|
}
|
|
125
117
|
});
|
|
126
118
|
} catch (e) {
|
|
127
119
|
if (warn) cwarn(`Cannot read dir: ${dir}`);
|
|
120
|
+
return null;
|
|
128
121
|
}
|
|
129
122
|
|
|
130
123
|
if (!hiddenItems) {
|
|
131
|
-
|
|
132
|
-
|
|
124
|
+
dirs = (dirs as string[]).filter(d => !d.startsWith('.'));
|
|
125
|
+
files = (files as string[]).filter(f => !f.startsWith('.'));
|
|
133
126
|
}
|
|
134
127
|
|
|
135
|
-
return { dirs, files }
|
|
128
|
+
return { dirs, files };
|
|
136
129
|
};
|
|
137
130
|
|
|
138
131
|
/**
|
|
@@ -145,7 +138,7 @@ export const readDir = async (dir: string, warn = false, hiddenItems = false) =>
|
|
|
145
138
|
export const readDirFiles = async (dir: string, warn = false, hiddenItems = false) => {
|
|
146
139
|
if (!dir) return null;
|
|
147
140
|
dir = assertPath(dir);
|
|
148
|
-
return (await readDir(dir, warn, hiddenItems))
|
|
141
|
+
return (await readDir(dir, warn, hiddenItems))?.files;
|
|
149
142
|
};
|
|
150
143
|
|
|
151
144
|
/**
|
|
@@ -158,7 +151,7 @@ export const readDirFiles = async (dir: string, warn = false, hiddenItems = fals
|
|
|
158
151
|
export const readDirDirs = async (dir: string, warn = false, hiddenItems = false) => {
|
|
159
152
|
if (!dir) return null;
|
|
160
153
|
dir = assertPath(dir);
|
|
161
|
-
return (await readDir(dir, warn, hiddenItems))
|
|
154
|
+
return (await readDir(dir, warn, hiddenItems))?.dirs;
|
|
162
155
|
};
|
|
163
156
|
|
|
164
157
|
const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string[] | null> => {
|
|
@@ -166,7 +159,10 @@ const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string
|
|
|
166
159
|
dir = assertPath(dir);
|
|
167
160
|
|
|
168
161
|
const dirPath = basePath ? `${dir}/${basePath}` : dir;
|
|
169
|
-
const
|
|
162
|
+
const readDirObj = await readDir(dirPath);
|
|
163
|
+
if (!readDirObj) return null;
|
|
164
|
+
|
|
165
|
+
const { dirs, files } = readDirObj;
|
|
170
166
|
let allFiles: string[] = files || [];
|
|
171
167
|
allFiles = allFiles.map(file => (basePath ? `${basePath}/${file}` : file));
|
|
172
168
|
const absDirs = (dirs || []).map(d => (basePath ? `${basePath}/${d}` : d));
|
|
@@ -179,14 +175,14 @@ const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string
|
|
|
179
175
|
}
|
|
180
176
|
});
|
|
181
177
|
|
|
182
|
-
return allFiles
|
|
178
|
+
return allFiles;
|
|
183
179
|
};
|
|
184
180
|
|
|
185
181
|
/**
|
|
186
182
|
* Get the (recursive) list of files in a directory
|
|
187
183
|
* @param dir directory path
|
|
188
184
|
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
189
|
-
* @returns complete (recursive) list of files, null in case of error
|
|
185
|
+
* @returns complete (recursive) list of files, null in case of error
|
|
190
186
|
*/
|
|
191
187
|
export const readDirFilesRec = async (dir: string, hiddenItems = false) => {
|
|
192
188
|
let allFiles = await readDirFilesRecHelper(dir);
|
|
@@ -200,10 +196,10 @@ export const readDirFilesRec = async (dir: string, hiddenItems = false) => {
|
|
|
200
196
|
* Write contents to a file (creates the file path if it doesn't exist)
|
|
201
197
|
* @param file file path
|
|
202
198
|
* @param contents contents to write
|
|
203
|
-
* @returns true if successful,
|
|
199
|
+
* @returns true if successful, null in case of error
|
|
204
200
|
*/
|
|
205
201
|
export const writeFile = async (file: string, contents: string) => {
|
|
206
|
-
if (!file || !contents) return
|
|
202
|
+
if (!file || !contents) return null;
|
|
207
203
|
file = assertPath(file);
|
|
208
204
|
try {
|
|
209
205
|
const dir = file.substring(0, file.lastIndexOf('/'));
|
|
@@ -211,7 +207,7 @@ export const writeFile = async (file: string, contents: string) => {
|
|
|
211
207
|
await fsa.writeFile(file, contents);
|
|
212
208
|
} catch (e) {
|
|
213
209
|
console.error(`Error while writing to ${file}`, e);
|
|
214
|
-
return
|
|
210
|
+
return null;
|
|
215
211
|
}
|
|
216
212
|
return true;
|
|
217
213
|
};
|
|
@@ -220,10 +216,10 @@ export const writeFile = async (file: string, contents: string) => {
|
|
|
220
216
|
* Append contents to a file
|
|
221
217
|
* @param file file path
|
|
222
218
|
* @param contents contents to append
|
|
223
|
-
* @returns true if successful,
|
|
219
|
+
* @returns true if successful, null in case of error
|
|
224
220
|
*/
|
|
225
221
|
export const appendToFile = async (file: string, contents: string) => {
|
|
226
|
-
if (!file || !contents) return
|
|
222
|
+
if (!file || !contents) return null;
|
|
227
223
|
file = assertPath(file);
|
|
228
224
|
try {
|
|
229
225
|
const dir = file.substring(0, file.lastIndexOf('/'));
|
|
@@ -237,7 +233,7 @@ export const appendToFile = async (file: string, contents: string) => {
|
|
|
237
233
|
// stream.end();
|
|
238
234
|
} catch (e) {
|
|
239
235
|
console.error(`Error while appending to ${file}`, e);
|
|
240
|
-
return
|
|
236
|
+
return null;
|
|
241
237
|
}
|
|
242
238
|
return true;
|
|
243
239
|
};
|
|
@@ -246,17 +242,17 @@ export const appendToFile = async (file: string, contents: string) => {
|
|
|
246
242
|
* Rename a file
|
|
247
243
|
* @param oldpath old file path
|
|
248
244
|
* @param newpath new file path
|
|
249
|
-
* @returns true if successful,
|
|
245
|
+
* @returns true if successful, null in case of error
|
|
250
246
|
*/
|
|
251
247
|
export const renameFile = async (oldpath: string, newpath: string) => {
|
|
252
|
-
if (!oldpath || !newpath) return
|
|
248
|
+
if (!oldpath || !newpath) return null;
|
|
253
249
|
oldpath = assertPath(oldpath);
|
|
254
250
|
newpath = assertPath(newpath);
|
|
255
251
|
try {
|
|
256
252
|
await fsa.rename(oldpath, newpath);
|
|
257
253
|
} catch (e) {
|
|
258
254
|
console.error(`Error while renaming file ${oldpath} to ${newpath}`, e);
|
|
259
|
-
return
|
|
255
|
+
return null;
|
|
260
256
|
}
|
|
261
257
|
return true;
|
|
262
258
|
};
|
|
@@ -264,10 +260,10 @@ export const renameFile = async (oldpath: string, newpath: string) => {
|
|
|
264
260
|
/**
|
|
265
261
|
* Create a directory, if it doesn't exist
|
|
266
262
|
* @param dir directory path
|
|
267
|
-
* @returns true if successful,
|
|
263
|
+
* @returns true if successful, null in case of failure/error
|
|
268
264
|
*/
|
|
269
265
|
export const createDir = async (dir: string) => {
|
|
270
|
-
if (!dir) return
|
|
266
|
+
if (!dir) return null;
|
|
271
267
|
dir = assertPath(dir);
|
|
272
268
|
try {
|
|
273
269
|
if (!existsDirSync(dir)) {
|
|
@@ -275,7 +271,7 @@ export const createDir = async (dir: string) => {
|
|
|
275
271
|
}
|
|
276
272
|
} catch (e) {
|
|
277
273
|
console.error(`Error while creating directory: ${dir}`, e);
|
|
278
|
-
return
|
|
274
|
+
return null;
|
|
279
275
|
}
|
|
280
276
|
return true;
|
|
281
277
|
};
|
|
@@ -283,16 +279,16 @@ export const createDir = async (dir: string) => {
|
|
|
283
279
|
/**
|
|
284
280
|
* Delete a file
|
|
285
281
|
* @param file file path
|
|
286
|
-
* @returns true if successful,
|
|
282
|
+
* @returns true if successful, null in case of error
|
|
287
283
|
*/
|
|
288
284
|
export const deleteFile = async (file: string) => {
|
|
289
|
-
if (!file) return
|
|
285
|
+
if (!file) return null;
|
|
290
286
|
file = assertPath(file);
|
|
291
287
|
try {
|
|
292
288
|
await fsa.unlink(file);
|
|
293
289
|
} catch (e) {
|
|
294
290
|
console.error(`Error while deleting file ${file}`, e);
|
|
295
|
-
return
|
|
291
|
+
return null;
|
|
296
292
|
}
|
|
297
293
|
return true;
|
|
298
294
|
};
|
|
@@ -300,10 +296,10 @@ export const deleteFile = async (file: string) => {
|
|
|
300
296
|
/**
|
|
301
297
|
* Delete a directory
|
|
302
298
|
* @param dir directory path
|
|
303
|
-
* @returns true if successful,
|
|
299
|
+
* @returns true if successful, null in case of error
|
|
304
300
|
*/
|
|
305
301
|
export const deleteDir = async (dir: string) => {
|
|
306
|
-
if (!dir) return
|
|
302
|
+
if (!dir) return null;
|
|
307
303
|
dir = assertPath(dir);
|
|
308
304
|
try {
|
|
309
305
|
const rimraf = require('rimraf');
|
|
@@ -314,7 +310,7 @@ export const deleteDir = async (dir: string) => {
|
|
|
314
310
|
// await fsa.rm(dir, { recursive: true, force: true });
|
|
315
311
|
} catch (e) {
|
|
316
312
|
console.error(`Error while deleting dir ${dir}`, e);
|
|
317
|
-
return
|
|
313
|
+
return null;
|
|
318
314
|
}
|
|
319
315
|
return true;
|
|
320
316
|
};
|