@baadal-sdk/dapi 0.31.5 → 1.0.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/README.md +62 -2
- package/dist/index.d.ts +674 -0
- package/dist/index.js +1118 -0
- package/dist/index.js.map +1 -0
- package/package.json +44 -106
- package/LICENSE.txt +0 -21
- package/dist/cjs/index.js +0 -3
- package/dist/cjs/index.js.LICENSE.txt +0 -1
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/package.json +0 -3
- package/dist/esm/index.js +0 -3
- package/dist/esm/index.js.LICENSE.txt +0 -1
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/package.json +0 -3
- package/dist/types/aws/client.d.ts +0 -13
- package/dist/types/aws/client.d.ts.map +0 -1
- package/dist/types/aws/db.d.ts +0 -291
- package/dist/types/aws/db.d.ts.map +0 -1
- package/dist/types/aws/index.d.ts +0 -12
- package/dist/types/aws/index.d.ts.map +0 -1
- package/dist/types/aws/s3.d.ts +0 -90
- package/dist/types/aws/s3.d.ts.map +0 -1
- package/dist/types/common/common.model.d.ts +0 -4
- package/dist/types/common/common.model.d.ts.map +0 -1
- package/dist/types/common/const.d.ts +0 -4
- package/dist/types/common/const.d.ts.map +0 -1
- package/dist/types/common/error.d.ts +0 -4
- package/dist/types/common/error.d.ts.map +0 -1
- package/dist/types/common/logger.d.ts +0 -29
- package/dist/types/common/logger.d.ts.map +0 -1
- package/dist/types/fs/index.d.ts +0 -102
- package/dist/types/fs/index.d.ts.map +0 -1
- package/dist/types/gh/index.d.ts +0 -22
- package/dist/types/gh/index.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -13
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/utils/index.d.ts +0 -6
- package/dist/types/utils/index.d.ts.map +0 -1
- package/src/aws/client.ts +0 -18
- package/src/aws/db.ts +0 -764
- package/src/aws/index.ts +0 -33
- package/src/aws/s3.ts +0 -476
- package/src/common/common.model.ts +0 -3
- package/src/common/const.ts +0 -3
- package/src/common/error.ts +0 -12
- package/src/common/logger.ts +0 -18
- package/src/fs/index.ts +0 -316
- package/src/gh/index.ts +0 -60
- package/src/index.ts +0 -8
- package/src/typings/index.d.ts +0 -0
- package/src/utils/index.ts +0 -39
package/src/fs/index.ts
DELETED
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
import util from 'util';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import fsa from 'fs/promises';
|
|
4
|
-
|
|
5
|
-
import { assertPath } from '../utils';
|
|
6
|
-
import { CustomError } from '../common/error';
|
|
7
|
-
import { warn as cwarn } from '../common/logger';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Check whether a file exists
|
|
11
|
-
* @param file file path
|
|
12
|
-
* @param loud whether to throw errors [default: false]
|
|
13
|
-
* @returns true if it exists, false if it doesn't, null in case of error
|
|
14
|
-
*/
|
|
15
|
-
export const existsFileSync = (file: string, loud = false) => {
|
|
16
|
-
if (!file) return null;
|
|
17
|
-
file = assertPath(file);
|
|
18
|
-
try {
|
|
19
|
-
if (!fs.existsSync(file)) {
|
|
20
|
-
if (!loud) return false;
|
|
21
|
-
throw new CustomError(`File does not exist: ${file}`);
|
|
22
|
-
}
|
|
23
|
-
} catch (e) {
|
|
24
|
-
if (!loud) return null;
|
|
25
|
-
if (e instanceof CustomError) {
|
|
26
|
-
throw e;
|
|
27
|
-
} else {
|
|
28
|
-
throw new CustomError(`Error while accessing file: ${file}`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return true;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Check whether a directory exists
|
|
36
|
-
* @param dir directory path
|
|
37
|
-
* @param loud whether to throw errors [default: false]
|
|
38
|
-
* @returns true if it exists, false if it doesn't, null in case of error
|
|
39
|
-
*/
|
|
40
|
-
export const existsDirSync = (dir: string, loud = false) => {
|
|
41
|
-
if (!dir) return null;
|
|
42
|
-
dir = assertPath(dir);
|
|
43
|
-
try {
|
|
44
|
-
if (!fs.existsSync(dir)) {
|
|
45
|
-
if (!loud) return false;
|
|
46
|
-
throw new CustomError(`Directory does not exist: ${dir}`);
|
|
47
|
-
}
|
|
48
|
-
} catch (e) {
|
|
49
|
-
if (!loud) return null;
|
|
50
|
-
if (e instanceof CustomError) {
|
|
51
|
-
throw e;
|
|
52
|
-
} else {
|
|
53
|
-
throw new CustomError(`Error while accessing directory: ${dir}`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return true;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Read contents of a file
|
|
61
|
-
* @param file file path
|
|
62
|
-
* @param warn whether to show warnings [default: false]
|
|
63
|
-
* @returns contents of the file, null in case of error
|
|
64
|
-
*/
|
|
65
|
-
export const readFile = async (file: string, warn = false) => {
|
|
66
|
-
if (!file) return null;
|
|
67
|
-
file = assertPath(file);
|
|
68
|
-
let contents = null;
|
|
69
|
-
try {
|
|
70
|
-
contents = await fsa.readFile(file, 'utf8');
|
|
71
|
-
} catch (e) {
|
|
72
|
-
if (warn) cwarn(`Cannot read file: ${file}`);
|
|
73
|
-
}
|
|
74
|
-
return contents;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Read contents of a file
|
|
79
|
-
* @param file file path
|
|
80
|
-
* @param warn whether to show warnings [default: false]
|
|
81
|
-
* @returns contents of the file, null in case of error
|
|
82
|
-
*/
|
|
83
|
-
export const readFileSync = (file: string, warn = false) => {
|
|
84
|
-
if (!file) return null;
|
|
85
|
-
file = assertPath(file);
|
|
86
|
-
let contents = null;
|
|
87
|
-
try {
|
|
88
|
-
contents = fs.readFileSync(file, 'utf8');
|
|
89
|
-
} catch (e) {
|
|
90
|
-
if (warn) cwarn(`Cannot read file: ${file}`);
|
|
91
|
-
}
|
|
92
|
-
return contents;
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Get the list of files/directories in a directory
|
|
97
|
-
* @param dir directory path
|
|
98
|
-
* @param warn whether to show warnings [default: false]
|
|
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
|
|
101
|
-
*/
|
|
102
|
-
export const readDir = async (dir: string, warn = false, hiddenItems = false) => {
|
|
103
|
-
if (!dir) return null;
|
|
104
|
-
dir = assertPath(dir);
|
|
105
|
-
|
|
106
|
-
let dirs: string[] = [];
|
|
107
|
-
let files: string[] = [];
|
|
108
|
-
|
|
109
|
-
try {
|
|
110
|
-
const items = await fsa.readdir(dir, { withFileTypes: true });
|
|
111
|
-
items.forEach(item => {
|
|
112
|
-
if (item.isDirectory()) {
|
|
113
|
-
dirs.push(item.name);
|
|
114
|
-
} else if (item.isFile()) {
|
|
115
|
-
files.push(item.name);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
} catch (e) {
|
|
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('.'));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return { dirs, files };
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Get the list of files in a directory
|
|
133
|
-
* @param dir directory path
|
|
134
|
-
* @param warn whether to show warnings [default: false]
|
|
135
|
-
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
136
|
-
* @returns list of files, null in case of error
|
|
137
|
-
*/
|
|
138
|
-
export const readDirFiles = async (dir: string, warn = false, hiddenItems = false) => {
|
|
139
|
-
if (!dir) return null;
|
|
140
|
-
dir = assertPath(dir);
|
|
141
|
-
return (await readDir(dir, warn, hiddenItems))?.files || null;
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Get the list of directories in a directory
|
|
146
|
-
* @param dir directory path
|
|
147
|
-
* @param warn whether to show warnings [default: false]
|
|
148
|
-
* @param hiddenItems whether to include items starting with dot(.) [default: false]
|
|
149
|
-
* @returns list of directories, null in case of error
|
|
150
|
-
*/
|
|
151
|
-
export const readDirDirs = async (dir: string, warn = false, hiddenItems = false) => {
|
|
152
|
-
if (!dir) return null;
|
|
153
|
-
dir = assertPath(dir);
|
|
154
|
-
return (await readDir(dir, warn, hiddenItems))?.dirs || null;
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string[] | null> => {
|
|
158
|
-
if (!dir) return null;
|
|
159
|
-
dir = assertPath(dir);
|
|
160
|
-
|
|
161
|
-
const dirPath = basePath ? `${dir}/${basePath}` : dir;
|
|
162
|
-
const readDirObj = await readDir(dirPath);
|
|
163
|
-
if (!readDirObj) return null;
|
|
164
|
-
|
|
165
|
-
const { dirs, files } = readDirObj;
|
|
166
|
-
let allFiles: string[] = files || [];
|
|
167
|
-
allFiles = allFiles.map(file => (basePath ? `${basePath}/${file}` : file));
|
|
168
|
-
const absDirs = (dirs || []).map(d => (basePath ? `${basePath}/${d}` : d));
|
|
169
|
-
|
|
170
|
-
const pList = absDirs.map(dirx => readDirFilesRecHelper(dir, dirx));
|
|
171
|
-
const filesxList = await Promise.all(pList);
|
|
172
|
-
filesxList.forEach(filesx => {
|
|
173
|
-
if (filesx) {
|
|
174
|
-
allFiles = [...allFiles, ...filesx];
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
return allFiles;
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Get the (recursive) list of files in a directory
|
|
183
|
-
* @param dir directory path
|
|
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
|
-
*/
|
|
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
|
-
};
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Write contents to a file (creates the file path if it doesn't exist)
|
|
197
|
-
* @param file file path
|
|
198
|
-
* @param contents contents to write
|
|
199
|
-
* @returns true if successful, null in case of error
|
|
200
|
-
*/
|
|
201
|
-
export const writeFile = async (file: string, contents: string) => {
|
|
202
|
-
if (!file || !contents) return null;
|
|
203
|
-
file = assertPath(file);
|
|
204
|
-
try {
|
|
205
|
-
const dir = file.substring(0, file.lastIndexOf('/'));
|
|
206
|
-
await fsa.mkdir(dir, { recursive: true });
|
|
207
|
-
await fsa.writeFile(file, contents);
|
|
208
|
-
} catch (e) {
|
|
209
|
-
console.error(`Error while writing to ${file}`, e);
|
|
210
|
-
return null;
|
|
211
|
-
}
|
|
212
|
-
return true;
|
|
213
|
-
};
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Append contents to a file
|
|
217
|
-
* @param file file path
|
|
218
|
-
* @param contents contents to append
|
|
219
|
-
* @returns true if successful, null in case of error
|
|
220
|
-
*/
|
|
221
|
-
export const appendToFile = async (file: string, contents: string) => {
|
|
222
|
-
if (!file || !contents) return null;
|
|
223
|
-
file = assertPath(file);
|
|
224
|
-
try {
|
|
225
|
-
const dir = file.substring(0, file.lastIndexOf('/'));
|
|
226
|
-
await fsa.mkdir(dir, { recursive: true });
|
|
227
|
-
|
|
228
|
-
await fsa.appendFile(file, contents + '\n');
|
|
229
|
-
|
|
230
|
-
// Ref: https://stackoverflow.com/a/43370201
|
|
231
|
-
// const stream = fs.createWriteStream(file, { flags: 'a' });
|
|
232
|
-
// stream.write(contents + '\n');
|
|
233
|
-
// stream.end();
|
|
234
|
-
} catch (e) {
|
|
235
|
-
console.error(`Error while appending to ${file}`, e);
|
|
236
|
-
return null;
|
|
237
|
-
}
|
|
238
|
-
return true;
|
|
239
|
-
};
|
|
240
|
-
|
|
241
|
-
/**
|
|
242
|
-
* Rename a file
|
|
243
|
-
* @param oldpath old file path
|
|
244
|
-
* @param newpath new file path
|
|
245
|
-
* @returns true if successful, null in case of error
|
|
246
|
-
*/
|
|
247
|
-
export const renameFile = async (oldpath: string, newpath: string) => {
|
|
248
|
-
if (!oldpath || !newpath) return null;
|
|
249
|
-
oldpath = assertPath(oldpath);
|
|
250
|
-
newpath = assertPath(newpath);
|
|
251
|
-
try {
|
|
252
|
-
await fsa.rename(oldpath, newpath);
|
|
253
|
-
} catch (e) {
|
|
254
|
-
console.error(`Error while renaming file ${oldpath} to ${newpath}`, e);
|
|
255
|
-
return null;
|
|
256
|
-
}
|
|
257
|
-
return true;
|
|
258
|
-
};
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Create a directory, if it doesn't exist
|
|
262
|
-
* @param dir directory path
|
|
263
|
-
* @returns true if successful, null in case of failure/error
|
|
264
|
-
*/
|
|
265
|
-
export const createDir = async (dir: string) => {
|
|
266
|
-
if (!dir) return null;
|
|
267
|
-
dir = assertPath(dir);
|
|
268
|
-
try {
|
|
269
|
-
if (!existsDirSync(dir)) {
|
|
270
|
-
await fsa.mkdir(dir, { recursive: true });
|
|
271
|
-
}
|
|
272
|
-
} catch (e) {
|
|
273
|
-
console.error(`Error while creating directory: ${dir}`, e);
|
|
274
|
-
return null;
|
|
275
|
-
}
|
|
276
|
-
return true;
|
|
277
|
-
};
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Delete a file
|
|
281
|
-
* @param file file path
|
|
282
|
-
* @returns true if successful, null in case of error
|
|
283
|
-
*/
|
|
284
|
-
export const deleteFile = async (file: string) => {
|
|
285
|
-
if (!file) return null;
|
|
286
|
-
file = assertPath(file);
|
|
287
|
-
try {
|
|
288
|
-
await fsa.unlink(file);
|
|
289
|
-
} catch (e) {
|
|
290
|
-
console.error(`Error while deleting file ${file}`, e);
|
|
291
|
-
return null;
|
|
292
|
-
}
|
|
293
|
-
return true;
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Delete a directory
|
|
298
|
-
* @param dir directory path
|
|
299
|
-
* @returns true if successful, null in case of error
|
|
300
|
-
*/
|
|
301
|
-
export const deleteDir = async (dir: string) => {
|
|
302
|
-
if (!dir) return null;
|
|
303
|
-
dir = assertPath(dir);
|
|
304
|
-
try {
|
|
305
|
-
const rimraf = require('rimraf');
|
|
306
|
-
const rimrafPr = util.promisify(rimraf);
|
|
307
|
-
await rimrafPr(dir);
|
|
308
|
-
|
|
309
|
-
// Added in: v14.14.0
|
|
310
|
-
// await fsa.rm(dir, { recursive: true, force: true });
|
|
311
|
-
} catch (e) {
|
|
312
|
-
console.error(`Error while deleting dir ${dir}`, e);
|
|
313
|
-
return null;
|
|
314
|
-
}
|
|
315
|
-
return true;
|
|
316
|
-
};
|
package/src/gh/index.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { Octokit } from '@octokit/core';
|
|
2
|
-
|
|
3
|
-
import { CustomError } from '../common/error';
|
|
4
|
-
import { error } from '../common/logger';
|
|
5
|
-
|
|
6
|
-
let github: Octokit | null = null;
|
|
7
|
-
let owner: string | null = null;
|
|
8
|
-
|
|
9
|
-
const GitHubError = (msg: string) => new CustomError(msg, { name: 'GitHubError' });
|
|
10
|
-
|
|
11
|
-
const initializationError = () => {
|
|
12
|
-
throw GitHubError('GitHub SDK is possibly uninitialized!');
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Initialize GitHub SDK
|
|
17
|
-
* @param authToken GitHub auth token for the user account [GITHUB_TOKEN]
|
|
18
|
-
* @param account account id for the GitHub user account [GITHUB_ACCOUNT]
|
|
19
|
-
*/
|
|
20
|
-
export const init = (authToken: string, account: string) => {
|
|
21
|
-
if (!authToken || !account) {
|
|
22
|
-
error(`GitHub initialization error! authToken: ${!!authToken}, account: ${!!account}`);
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
github = new Octokit({ auth: authToken });
|
|
26
|
-
owner = account;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Get contents of a particular file in a repo
|
|
31
|
-
* @param repo name of the repo
|
|
32
|
-
* @param path path of a file in the repo
|
|
33
|
-
* @returns an object {response,headers} containing response and response headers, null in case of error
|
|
34
|
-
*/
|
|
35
|
-
export const getContent = async (repo: string, path: string) => {
|
|
36
|
-
if (!github || !owner) return initializationError();
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
const ghResp = await github.request('GET /repos/{owner}/{repo}/contents/{path}', {
|
|
40
|
-
owner,
|
|
41
|
-
repo,
|
|
42
|
-
path,
|
|
43
|
-
});
|
|
44
|
-
const response = ghResp.data as GitHubContent;
|
|
45
|
-
const { headers } = ghResp;
|
|
46
|
-
return { response, headers };
|
|
47
|
-
} catch (e) {
|
|
48
|
-
error(`[ERROR:gh.getContent]`, e);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
return null;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// --------------
|
|
55
|
-
|
|
56
|
-
export interface GitHubContent {
|
|
57
|
-
path: string;
|
|
58
|
-
content: string;
|
|
59
|
-
sha: string;
|
|
60
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import * as fs from './fs';
|
|
2
|
-
import * as aws from './aws';
|
|
3
|
-
import * as gh from './gh';
|
|
4
|
-
import * as utils from './utils';
|
|
5
|
-
|
|
6
|
-
// Ref: https://stackoverflow.com/a/41283945
|
|
7
|
-
export { fs, aws, gh, utils }; // named exports
|
|
8
|
-
export default { fs, aws, gh, utils }; // default export
|
package/src/typings/index.d.ts
DELETED
|
File without changes
|
package/src/utils/index.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
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
|
-
};
|