@baadal-sdk/dapi 0.14.0 → 0.18.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.
@@ -0,0 +1,4 @@
1
+ export declare class CustomError extends Error {
2
+ constructor(message: string, options?: any);
3
+ }
4
+ //# sourceMappingURL=error.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../../../src/common/error.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,KAAK;gBACxB,OAAO,EAAE,MAAM,EAAE,OAAO,GAAE,GAAQ;CAU/C"}
@@ -1,5 +1,17 @@
1
- declare const _default: {
2
- log: () => void;
3
- };
4
- export default _default;
1
+ export declare const existsFileSync: (file: string, loud?: boolean) => boolean;
2
+ export declare const existsDirSync: (dir: string, loud?: boolean) => boolean;
3
+ export declare const createDir: (dir: string) => Promise<boolean>;
4
+ export declare const readFile: (file: string, warn?: boolean) => Promise<string | null>;
5
+ export declare const readDir: (dir: string, warn?: boolean) => Promise<{
6
+ dirs: string[] | null;
7
+ files: string[] | null;
8
+ }>;
9
+ export declare const readDirFiles: (dir: string, warn?: boolean) => Promise<string[] | null>;
10
+ export declare const readDirDirs: (dir: string, warn?: boolean) => Promise<string[] | null>;
11
+ export declare const readDirFilesRec: (dir: string) => Promise<string[] | null>;
12
+ export declare const writeFile: (file: string, contents: string) => Promise<boolean>;
13
+ export declare const appendToFile: (file: string, contents: string) => Promise<boolean>;
14
+ export declare const renameFile: (oldpath: string, newpath: string) => Promise<boolean>;
15
+ export declare const deleteFile: (file: string) => Promise<boolean>;
16
+ export declare const deleteDir: (dir: string) => Promise<boolean>;
5
17
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/fs/index.ts"],"names":[],"mappings":";;;AAIA,wBAAuB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/fs/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,cAAc,SAAU,MAAM,4BAiB1C,CAAC;AAQF,eAAO,MAAM,aAAa,QAAS,MAAM,4BAiBxC,CAAC;AAOF,eAAO,MAAM,SAAS,QAAe,MAAM,qBAY1C,CAAC;AAQF,eAAO,MAAM,QAAQ,SAAgB,MAAM,2CAU1C,CAAC;AAQF,eAAO,MAAM,OAAO,QAAe,MAAM;UA4BL,MAAM,EAAE,GAAG,IAAI;WAAS,MAAM,EAAE,GAAG,IAAI;EAC1E,CAAC;AAQF,eAAO,MAAM,YAAY,QAAe,MAAM,6CAI7C,CAAC;AAQF,eAAO,MAAM,WAAW,QAAe,MAAM,6CAI5C,CAAC;AA4BF,eAAO,MAAM,eAAe,QAAS,MAAM,6BAA+B,CAAC;AAQ3E,eAAO,MAAM,SAAS,SAAgB,MAAM,YAAY,MAAM,qBAY7D,CAAC;AAQF,eAAO,MAAM,YAAY,SAAgB,MAAM,YAAY,MAAM,qBAkBhE,CAAC;AAQF,eAAO,MAAM,UAAU,YAAmB,MAAM,WAAW,MAAM,qBAWhE,CAAC;AAOF,eAAO,MAAM,UAAU,SAAgB,MAAM,qBAU5C,CAAC;AAOF,eAAO,MAAM,SAAS,QAAe,MAAM,qBAc1C,CAAC"}
@@ -1,6 +1,3 @@
1
- import fs from './fs';
2
- declare const foo: () => string;
3
- export { fs, foo };
4
- export declare const num = 5;
5
- export default fs;
1
+ import * as fs from './fs';
2
+ export { fs };
6
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,MAAM,CAAC;AAEtB,QAAA,MAAM,GAAG,cAER,CAAC;AAEF,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC;AACnB,eAAO,MAAM,GAAG,IAAI,CAAC;AACrB,eAAe,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,MAAM,CAAC;AAE3B,OAAO,EAAE,EAAE,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baadal-sdk/dapi",
3
- "version": "0.14.0",
3
+ "version": "0.18.0",
4
4
  "description": "Dead-simple API wrappers",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -18,31 +18,84 @@
18
18
  ],
19
19
  "author": "Abhishek Raj <abhi@raj.me>",
20
20
  "license": "MIT",
21
+ "engines": {
22
+ "node": ">=12.5.0"
23
+ },
21
24
  "scripts": {
22
- "build": "bash tasks/build.sh",
25
+ "build": "npm run verify && bash tasks/build.sh",
23
26
  "clean": "shx rm -rf dist",
24
- "prepublishOnly": "run-s --silent clean build",
25
- "pub": "npm publish",
26
- "pub:test": "npm publish --dry-run"
27
+ "lint": "eslint '**/*.{js,jsx,ts,tsx}'",
28
+ "lint:fix": "npm run lint -- --fix",
29
+ "prepub": "run-s --silent clean build",
30
+ "pub": "npm run prepub && npm publish",
31
+ "pub:test": "npm run prepub && npm publish --dry-run",
32
+ "test": "ts-node test/index.ts",
33
+ "verify": "jsonlint -q tsconfig.json",
34
+ "blame": "git log -S",
35
+ "hist": "git log --oneline | grep -i",
36
+ "follow": "git log --follow --oneline",
37
+ "ncu": "ncu",
38
+ "ncuu": "ncu -u"
27
39
  },
28
40
  "devDependencies": {
29
41
  "@babel/core": "7.15.5",
30
42
  "@babel/preset-env": "7.15.6",
43
+ "@babel/preset-react": "^7.14.5",
31
44
  "@babel/preset-typescript": "7.15.0",
32
45
  "@types/node": "16.9.6",
46
+ "@types/react": "^16.0.0",
47
+ "@types/react-dom": "^16.0.0",
48
+ "@types/rimraf": "3.0.2",
49
+ "@typescript-eslint/eslint-plugin": "4.31.2",
50
+ "@typescript-eslint/parser": "4.31.2",
33
51
  "babel-loader": "8.2.2",
52
+ "eslint": "7.32.0",
53
+ "eslint-config-airbnb": "18.2.1",
54
+ "eslint-config-airbnb-typescript": "14.0.0",
55
+ "eslint-config-prettier": "8.3.0",
56
+ "eslint-plugin-import": "2.24.2",
57
+ "eslint-plugin-jsx-a11y": "6.4.1",
58
+ "eslint-plugin-prettier": "4.0.0",
59
+ "eslint-plugin-react": "7.26.0",
60
+ "eslint-plugin-react-hooks": "4.2.0",
61
+ "husky": "4.3.8",
62
+ "jsonlint": "1.6.3",
63
+ "lint-staged": "11.1.2",
34
64
  "npm-run-all": "4.1.5",
65
+ "prettier": "2.4.1",
35
66
  "shx": "0.3.3",
67
+ "ts-node": "10.2.1",
36
68
  "typescript": "4.4.3",
37
69
  "webpack": "5.53.0",
38
70
  "webpack-cli": "4.8.0"
39
71
  },
40
72
  "dependencies": {
41
- "core-js": "3.18.0"
73
+ "core-js": "3.18.0",
74
+ "rimraf": "3.0.2"
75
+ },
76
+ "peerDependencies": {
77
+ "react": ">=16.8.6",
78
+ "react-dom": ">=16.8.6"
79
+ },
80
+ "peerDependenciesMeta": {
81
+ "react": {
82
+ "optional": true
83
+ },
84
+ "react-dom": {
85
+ "optional": true
86
+ }
42
87
  },
43
88
  "browserslist": [
44
89
  "chrome >= 23"
45
90
  ],
91
+ "lint-staged": {
92
+ "*.{js,jsx,ts,tsx}": "eslint --fix"
93
+ },
94
+ "husky": {
95
+ "hooks": {
96
+ "pre-commit": "lint-staged"
97
+ }
98
+ },
46
99
  "repository": {
47
100
  "type": "git",
48
101
  "url": "git+https://github.com/baadal/dapi.git"
@@ -0,0 +1,12 @@
1
+ export class CustomError extends Error {
2
+ constructor(message: string, options: any = {}) {
3
+ super(message);
4
+
5
+ // Maintains proper stack trace for where our error was thrown (only available on V8)
6
+ if (Error.captureStackTrace) {
7
+ Error.captureStackTrace(this, CustomError);
8
+ }
9
+
10
+ this.name = options.name || 'CustomError';
11
+ }
12
+ }
package/src/fs/index.ts CHANGED
@@ -1,5 +1,291 @@
1
- const log = () => {
2
- console.log('fs/log');
1
+ import path from 'path';
2
+ import util from 'util';
3
+ import fs from 'fs';
4
+ import fsa from 'fs/promises';
5
+ import rimraf from 'rimraf';
6
+
7
+ import { CustomError } from '../common/error';
8
+
9
+ const assertPath = (p: string) => {
10
+ if (!p || p.startsWith('/')) return p;
11
+ return path.resolve(process.cwd(), p);
12
+ };
13
+
14
+ /**
15
+ * Check whether a file exists
16
+ * @param file file path
17
+ * @param loud whether to throw errors [default: false]
18
+ * @returns true if it exists, false otherwise
19
+ */
20
+ export const existsFileSync = (file: string, loud = false) => {
21
+ if (!file) return false;
22
+ file = assertPath(file);
23
+ try {
24
+ if (!fs.existsSync(file)) {
25
+ if (!loud) return false;
26
+ throw new CustomError(`File does not exist: ${file}`);
27
+ }
28
+ } catch (e) {
29
+ if (!loud) return false;
30
+ if (e instanceof CustomError) {
31
+ throw e;
32
+ } else {
33
+ throw new CustomError(`Error while accessing file: ${file}`);
34
+ }
35
+ }
36
+ return true;
37
+ };
38
+
39
+ /**
40
+ * Check whether a directory exists
41
+ * @param dir directory path
42
+ * @param loud whether to throw errors [default: false]
43
+ * @returns true if it exists, false otherwise
44
+ */
45
+ export const existsDirSync = (dir: string, loud = false) => {
46
+ if (!dir) return false;
47
+ dir = assertPath(dir);
48
+ try {
49
+ if (!fs.existsSync(dir)) {
50
+ if (!loud) return false;
51
+ throw new CustomError(`Directory does not exist: ${dir}`);
52
+ }
53
+ } catch (e) {
54
+ if (!loud) return false;
55
+ if (e instanceof CustomError) {
56
+ throw e;
57
+ } else {
58
+ throw new CustomError(`Error while accessing directory: ${dir}`);
59
+ }
60
+ }
61
+ return true;
62
+ };
63
+
64
+ /**
65
+ * Create a directory, if it doesn't exist
66
+ * @param dir directory path
67
+ * @returns true if successful, false in case of failure
68
+ */
69
+ export const createDir = async (dir: string) => {
70
+ if (!dir) return false;
71
+ dir = assertPath(dir);
72
+ try {
73
+ if (!existsDirSync(dir)) {
74
+ await fsa.mkdir(dir, { recursive: true });
75
+ }
76
+ } catch (e) {
77
+ console.error(`Error while creating directory: ${dir}`, e);
78
+ return false;
79
+ }
80
+ return true;
81
+ };
82
+
83
+ /**
84
+ * Read contents of a file
85
+ * @param file file path
86
+ * @param warn whether to show warnings [default: false]
87
+ * @returns contents of the file, null in case of error
88
+ */
89
+ export const readFile = async (file: string, warn = false) => {
90
+ if (!file) return null;
91
+ file = assertPath(file);
92
+ let contents = null;
93
+ try {
94
+ contents = await fsa.readFile(file, 'utf8');
95
+ } catch (e) {
96
+ if (warn) console.warn(`Cannot read file: ${file}`);
97
+ }
98
+ return contents;
99
+ };
100
+
101
+ /**
102
+ * Get the list of files/directories in a directory
103
+ * @param dir directory path
104
+ * @param warn whether to show warnings [default: false]
105
+ * @returns an object {dirs,files} containing list of directories & files
106
+ */
107
+ export const readDir = async (dir: string, warn = false) => {
108
+ if (!dir) return { dirs: null, files: null };
109
+ dir = assertPath(dir);
110
+
111
+ let files: string[] | null = null;
112
+ let dirs: string[] | null = null;
113
+
114
+ try {
115
+ const items = await fsa.readdir(dir, { withFileTypes: true });
116
+ items.forEach(item => {
117
+ if (item.isDirectory()) {
118
+ if (!dirs) {
119
+ dirs = [item.name];
120
+ } else {
121
+ dirs.push(item.name);
122
+ }
123
+ } else if (item.isFile()) {
124
+ if (!files) {
125
+ files = [item.name];
126
+ } else {
127
+ files.push(item.name);
128
+ }
129
+ }
130
+ });
131
+ } catch (e) {
132
+ if (warn) console.warn(`Cannot read dir: ${dir}`);
133
+ }
134
+
135
+ return { dirs, files } as { dirs: string[] | null; files: string[] | null };
136
+ };
137
+
138
+ /**
139
+ * Get the list of files in a directory
140
+ * @param dir directory path
141
+ * @param warn whether to show warnings [default: false]
142
+ * @returns list of files, null in case of error or no items
143
+ */
144
+ export const readDirFiles = async (dir: string, warn = false) => {
145
+ if (!dir) return null;
146
+ dir = assertPath(dir);
147
+ return (await readDir(dir, warn)).files;
148
+ };
149
+
150
+ /**
151
+ * Get the list of directories in a directory
152
+ * @param dir directory path
153
+ * @param warn whether to show warnings [default: false]
154
+ * @returns list of directories, null in case of error or no items
155
+ */
156
+ export const readDirDirs = async (dir: string, warn = false) => {
157
+ if (!dir) return null;
158
+ dir = assertPath(dir);
159
+ return (await readDir(dir, warn)).dirs;
3
160
  };
4
161
 
5
- export default { log };
162
+ const readDirFilesRecHelper = async (dir: string, basePath = ''): Promise<string[] | null> => {
163
+ if (!dir) return null;
164
+ dir = assertPath(dir);
165
+
166
+ const dirPath = basePath ? `${dir}/${basePath}` : dir;
167
+ const { dirs, files } = await readDir(dirPath);
168
+ let allFiles: string[] = files || [];
169
+ allFiles = allFiles.map(file => (basePath ? `${basePath}/${file}` : file));
170
+ const absDirs = (dirs || []).map(d => (basePath ? `${basePath}/${d}` : d));
171
+
172
+ const pList = absDirs.map(dirx => readDirFilesRecHelper(dir, dirx));
173
+ const filesxList = await Promise.all(pList);
174
+ filesxList.forEach(filesx => {
175
+ if (filesx) {
176
+ allFiles = [...allFiles, ...filesx];
177
+ }
178
+ });
179
+
180
+ return allFiles.length ? allFiles : null;
181
+ };
182
+
183
+ /**
184
+ * Get the (recursive) list of files in a directory
185
+ * @param dir directory path
186
+ * @returns complete (recursive) list of files, null in case of error or no items
187
+ */
188
+ export const readDirFilesRec = (dir: string) => readDirFilesRecHelper(dir);
189
+
190
+ /**
191
+ * Write contents to a file (creates the file path if it doesn't exist)
192
+ * @param file file path
193
+ * @param contents contents to write
194
+ * @returns true if successful, false on error
195
+ */
196
+ export const writeFile = async (file: string, contents: string) => {
197
+ if (!file || !contents) return false;
198
+ file = assertPath(file);
199
+ try {
200
+ const dir = file.substring(0, file.lastIndexOf('/'));
201
+ await fsa.mkdir(dir, { recursive: true });
202
+ await fsa.writeFile(file, contents);
203
+ } catch (e) {
204
+ console.error(`Error while writing to ${file}`, e);
205
+ return false;
206
+ }
207
+ return true;
208
+ };
209
+
210
+ /**
211
+ * Append contents to a file
212
+ * @param file file path
213
+ * @param contents contents to append
214
+ * @returns true if successful, false on error
215
+ */
216
+ export const appendToFile = async (file: string, contents: string) => {
217
+ if (!file || !contents) return false;
218
+ file = assertPath(file);
219
+ try {
220
+ const dir = file.substring(0, file.lastIndexOf('/'));
221
+ await fsa.mkdir(dir, { recursive: true });
222
+
223
+ await fsa.appendFile(file, contents + '\n');
224
+
225
+ // Ref: https://stackoverflow.com/a/43370201
226
+ // const stream = fs.createWriteStream(file, { flags: 'a' });
227
+ // stream.write(contents + '\n');
228
+ // stream.end();
229
+ } catch (e) {
230
+ console.error(`Error while appending to ${file}`, e);
231
+ return false;
232
+ }
233
+ return true;
234
+ };
235
+
236
+ /**
237
+ * Rename a file
238
+ * @param oldpath old file path
239
+ * @param newpath new file path
240
+ * @returns true if successful, false on error
241
+ */
242
+ export const renameFile = async (oldpath: string, newpath: string) => {
243
+ if (!oldpath || !newpath) return false;
244
+ oldpath = assertPath(oldpath);
245
+ newpath = assertPath(newpath);
246
+ try {
247
+ await fsa.rename(oldpath, newpath);
248
+ } catch (e) {
249
+ console.error(`Error while renaming file ${oldpath} to ${newpath}`, e);
250
+ return false;
251
+ }
252
+ return true;
253
+ };
254
+
255
+ /**
256
+ * Delete a file
257
+ * @param file file path
258
+ * @returns true if successful, false on error
259
+ */
260
+ export const deleteFile = async (file: string) => {
261
+ if (!file) return false;
262
+ file = assertPath(file);
263
+ try {
264
+ await fsa.unlink(file);
265
+ } catch (e) {
266
+ console.error(`Error while deleting file ${file}`, e);
267
+ return false;
268
+ }
269
+ return true;
270
+ };
271
+
272
+ /**
273
+ * Delete a directory
274
+ * @param dir directory path
275
+ * @returns true if successful, false on error
276
+ */
277
+ export const deleteDir = async (dir: string) => {
278
+ if (!dir) return false;
279
+ dir = assertPath(dir);
280
+ try {
281
+ const rimrafPr = util.promisify(rimraf);
282
+ await rimrafPr(dir);
283
+
284
+ // Added in: v14.14.0
285
+ // await fsa.rm(dir, { recursive: true, force: true });
286
+ } catch (e) {
287
+ console.error(`Error while deleting dir ${dir}`, e);
288
+ return false;
289
+ }
290
+ return true;
291
+ };
package/src/index.ts CHANGED
@@ -1,16 +1,3 @@
1
- import fs from './fs';
1
+ import * as fs from './fs';
2
2
 
3
- const foo = () => {
4
- return 'bar';
5
- };
6
-
7
- export { fs, foo };
8
- export const num = 5;
9
- export default fs;
10
-
11
- // ----------------
12
- // TODO:
13
- // npm pack, tarball, gzip [why?]
14
- // eslint, prettier
15
- // husky/lint-staged
16
- // ----------------
3
+ export { fs };