Sfoglia il codice sorgente

added /file module with filesystem functions

Christian Kahlau 4 anni fa
parent
commit
8fff4e504e
9 ha cambiato i file con 244 aggiunte e 2 eliminazioni
  1. 8 0
      file/index.d.ts
  2. 1 0
      file/index.d.ts.map
  3. 153 0
      file/index.js
  4. 0 0
      file/index.js.map
  5. 63 0
      file/index.ts
  6. 8 0
      index.d.ts
  7. 1 1
      index.d.ts.map
  8. 9 0
      index.ts
  9. 1 1
      package.json

+ 8 - 0
file/index.d.ts

@@ -0,0 +1,8 @@
+/// <reference types="node" />
+import fs from 'fs';
+export declare function readFile(path: string, encoding?: any): Promise<string>;
+export declare function readJsonFile<T>(path: string, encoding?: string): Promise<T>;
+export declare function readDir(dir: string): Promise<fs.Dirent[]>;
+export declare function readJsonDir<T>(dir: string, filenameProperty?: string): Promise<T[]>;
+export declare function getSubdirNames(dir: string): Promise<string[]>;
+//# sourceMappingURL=index.d.ts.map

+ 1 - 0
file/index.d.ts.map

@@ -0,0 +1 @@
+{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AAGpB,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,CAAC,KAAA,GAAG,OAAO,CAAC,MAAM,CAAC,CAWvE;AAED,wBAAsB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,SAAU,GAAG,OAAO,CAAC,CAAC,CAAC,CAUlF;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAOzD;AAED,wBAAgB,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBnF;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAO7D"}

+ 153 - 0
file/index.js

@@ -0,0 +1,153 @@
+"use strict";
+var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
+    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
+    return new (P || (P = Promise))(function (resolve, reject) {
+        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
+        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
+        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
+        step((generator = generator.apply(thisArg, _arguments || [])).next());
+    });
+};
+var __generator = (this && this.__generator) || function (thisArg, body) {
+    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
+    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
+    function verb(n) { return function (v) { return step([n, v]); }; }
+    function step(op) {
+        if (f) throw new TypeError("Generator is already executing.");
+        while (_) try {
+            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
+            if (y = 0, t) op = [op[0] & 2, t.value];
+            switch (op[0]) {
+                case 0: case 1: t = op; break;
+                case 4: _.label++; return { value: op[1], done: false };
+                case 5: _.label++; y = op[1]; op = [0]; continue;
+                case 7: op = _.ops.pop(); _.trys.pop(); continue;
+                default:
+                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
+                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
+                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
+                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
+                    if (t[2]) _.ops.pop();
+                    _.trys.pop(); continue;
+            }
+            op = body.call(thisArg, _);
+        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
+        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
+    }
+};
+var __importDefault = (this && this.__importDefault) || function (mod) {
+    return (mod && mod.__esModule) ? mod : { "default": mod };
+};
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.getSubdirNames = exports.readJsonDir = exports.readDir = exports.readJsonFile = exports.readFile = void 0;
+var fs_1 = __importDefault(require("fs"));
+var path_1 = __importDefault(require("path"));
+function readFile(path, encoding) {
+    return __awaiter(this, void 0, void 0, function () {
+        return __generator(this, function (_a) {
+            return [2 /*return*/, new Promise(function (resolve, reject) {
+                    fs_1.default.readFile(path, { encoding: encoding }, function (err, data) {
+                        if (err)
+                            return reject(err);
+                        try {
+                            resolve(data.toString());
+                        }
+                        catch (e) {
+                            reject(e);
+                        }
+                    });
+                })];
+        });
+    });
+}
+exports.readFile = readFile;
+function readJsonFile(path, encoding) {
+    if (encoding === void 0) { encoding = 'utf-8'; }
+    return __awaiter(this, void 0, void 0, function () {
+        var _this = this;
+        return __generator(this, function (_a) {
+            return [2 /*return*/, new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
+                    var _a, _b, _c, e_1;
+                    return __generator(this, function (_d) {
+                        switch (_d.label) {
+                            case 0:
+                                _d.trys.push([0, 2, , 3]);
+                                _a = resolve;
+                                _c = (_b = JSON).parse;
+                                return [4 /*yield*/, readFile(path, encoding)];
+                            case 1:
+                                _a.apply(void 0, [_c.apply(_b, [_d.sent()])]);
+                                return [3 /*break*/, 3];
+                            case 2:
+                                e_1 = _d.sent();
+                                reject(e_1);
+                                return [3 /*break*/, 3];
+                            case 3: return [2 /*return*/];
+                        }
+                    });
+                }); })];
+        });
+    });
+}
+exports.readJsonFile = readJsonFile;
+function readDir(dir) {
+    return new Promise(function (resolve, reject) {
+        fs_1.default.readdir(dir, { withFileTypes: true }, function (err, list) {
+            if (err)
+                return reject(err);
+            resolve(list);
+        });
+    });
+}
+exports.readDir = readDir;
+function readJsonDir(dir, filenameProperty) {
+    var _this = this;
+    return new Promise(function (resolve, reject) {
+        fs_1.default.readdir(dir, { withFileTypes: true }, function (err, list) { return __awaiter(_this, void 0, void 0, function () {
+            var arr, _i, _a, e, json;
+            return __generator(this, function (_b) {
+                switch (_b.label) {
+                    case 0:
+                        if (err)
+                            return [2 /*return*/, reject(err)];
+                        arr = [];
+                        _i = 0, _a = list.filter(function (e) { return e.isFile() && e.name.match(/\.json$/); });
+                        _b.label = 1;
+                    case 1:
+                        if (!(_i < _a.length)) return [3 /*break*/, 4];
+                        e = _a[_i];
+                        return [4 /*yield*/, readJsonFile(path_1.default.resolve(dir, e.name))];
+                    case 2:
+                        json = _b.sent();
+                        if (typeof filenameProperty === 'string') {
+                            json[filenameProperty] = e.name;
+                        }
+                        arr.push(json);
+                        _b.label = 3;
+                    case 3:
+                        _i++;
+                        return [3 /*break*/, 1];
+                    case 4:
+                        resolve(arr);
+                        return [2 /*return*/];
+                }
+            });
+        }); });
+    });
+}
+exports.readJsonDir = readJsonDir;
+function getSubdirNames(dir) {
+    var _this = this;
+    return new Promise(function (resolve, reject) {
+        fs_1.default.readdir(dir, { withFileTypes: true }, function (err, list) { return __awaiter(_this, void 0, void 0, function () {
+            return __generator(this, function (_a) {
+                if (err)
+                    return [2 /*return*/, reject(err)];
+                resolve(list.filter(function (e) { return e.isDirectory(); }).map(function (e) { return e.name; }));
+                return [2 /*return*/];
+            });
+        }); });
+    });
+}
+exports.getSubdirNames = getSubdirNames;
+//# sourceMappingURL=index.js.map

File diff suppressed because it is too large
+ 0 - 0
file/index.js.map


+ 63 - 0
file/index.ts

@@ -0,0 +1,63 @@
+import fs from 'fs';
+import path from 'path';
+
+export async function readFile(path: string, encoding?): Promise<string> {
+    return new Promise<string>((resolve, reject) => {
+        fs.readFile(path, { encoding }, (err, data) => {
+            if (err) return reject(err);
+            try {
+                resolve(data.toString());
+            } catch (e) {
+                reject(e);
+            }
+        });
+    });
+}
+
+export async function readJsonFile<T>(path: string, encoding = 'utf-8'): Promise<T> {
+    return new Promise<T>(async (resolve, reject) => {
+        try {
+            resolve(JSON.parse(
+                await readFile(path, encoding)
+            ) as T);
+        } catch (e) {
+            reject(e);
+        }
+    });
+}
+
+export function readDir(dir: string): Promise<fs.Dirent[]> {
+    return new Promise<fs.Dirent[]>((resolve, reject) => {
+        fs.readdir(dir, { withFileTypes: true }, (err, list) => {
+            if (err) return reject(err);
+            resolve(list);
+        });
+    });
+}
+
+export function readJsonDir<T>(dir: string, filenameProperty?: string): Promise<T[]> {
+    return new Promise<T[]>((resolve, reject) => {
+        fs.readdir(dir, { withFileTypes: true }, async (err, list) => {
+            if (err) return reject(err);
+            const arr: T[] = [];
+            for (const e of list.filter(e => e.isFile() && e.name.match(/\.json$/))) {
+                const json: T = await readJsonFile<T>(path.resolve(dir, e.name));
+                if (typeof filenameProperty === 'string') {
+                    json[filenameProperty] = e.name;
+                }
+                arr.push(json);
+            }
+
+            resolve(arr);
+        });
+    });
+}
+
+export function getSubdirNames(dir: string): Promise<string[]> {
+    return new Promise<string[]>((resolve, reject) => {
+        fs.readdir(dir, { withFileTypes: true }, async (err, list) => {
+            if (err) return reject(err);
+            resolve(list.filter(e => e.isDirectory()).map(e => e.name));
+        });
+    });
+}

+ 8 - 0
index.d.ts

@@ -1,8 +1,16 @@
 import { exec, spawn } from './shell';
+import { readFile, readJsonFile, readDir, readJsonDir, getSubdirNames } from './file';
 export declare namespace NodeUtils {
     type Shell = {
         exec: typeof exec;
         spawn: typeof spawn;
     };
+    type FileSystem = {
+        readFile: typeof readFile;
+        readJsonFile: typeof readJsonFile;
+        readDir: typeof readDir;
+        readJsonDir: typeof readJsonDir;
+        getSubdirNames: typeof getSubdirNames;
+    };
 }
 //# sourceMappingURL=index.d.ts.map

+ 1 - 1
index.d.ts.map

@@ -1 +1 @@
-{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEtC,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IAC/B,KAAK,KAAK,GAAG;QACT,IAAI,EAAE,OAAO,IAAI,CAAC;QAClB,KAAK,EAAE,OAAO,KAAK,CAAC;KACvB,CAAA;CACJ"}
+{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,QAAQ,CAAC;AAEtF,MAAM,CAAC,OAAO,WAAW,SAAS,CAAC;IAC/B,KAAK,KAAK,GAAG;QACT,IAAI,EAAE,OAAO,IAAI,CAAC;QAClB,KAAK,EAAE,OAAO,KAAK,CAAC;KACvB,CAAA;IAED,KAAK,UAAU,GAAG;QACd,QAAQ,EAAE,OAAO,QAAQ,CAAC;QAC1B,YAAY,EAAE,OAAO,YAAY,CAAC;QAClC,OAAO,EAAE,OAAO,OAAO,CAAC;QACxB,WAAW,EAAE,OAAO,WAAW,CAAC;QAChC,cAAc,EAAE,OAAO,cAAc,CAAC;KACzC,CAAA;CACJ"}

+ 9 - 0
index.ts

@@ -1,8 +1,17 @@
 import { exec, spawn } from './shell';
+import { readFile, readJsonFile, readDir, readJsonDir, getSubdirNames } from './file';
 
 export declare namespace NodeUtils {
     type Shell = {
         exec: typeof exec;
         spawn: typeof spawn;
     }
+
+    type FileSystem = {
+        readFile: typeof readFile;
+        readJsonFile: typeof readJsonFile;
+        readDir: typeof readDir;
+        readJsonDir: typeof readJsonDir;
+        getSubdirNames: typeof getSubdirNames;
+    }
 }

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "node-utils",
-  "version": "1.0.1",
+  "version": "1.0.2",
   "description": "Shared common library for JavaScript in NodeJS",
   "repository": {
     "type": "git",

Some files were not shown because too many files changed in this diff