Przeglądaj źródła

/file module: +dotEnv()

Christian Kahlau 4 lat temu
rodzic
commit
8cac3cf29f
9 zmienionych plików z 56 dodań i 6 usunięć
  1. 3 0
      file/index.d.ts
  2. 1 1
      file/index.d.ts.map
  3. 24 1
      file/index.js
  4. 0 0
      file/index.js.map
  5. 22 0
      file/index.ts
  6. 2 1
      index.d.ts
  7. 1 1
      index.d.ts.map
  8. 2 1
      index.ts
  9. 1 1
      package.json

+ 3 - 0
file/index.d.ts

@@ -5,4 +5,7 @@ export declare function readJsonFile<T>(path: string, encoding?: BufferEncoding)
 export declare function readDir(dir: string): Promise<fs.Dirent[]>;
 export declare function readJsonDir<T extends object>(dir: string, filenameProperty?: string): Promise<T[]>;
 export declare function getSubdirNames(dir: string): Promise<string[]>;
+export declare function dotEnv(path: string): Promise<{
+    [key: string]: string;
+}>;
 //# sourceMappingURL=index.d.ts.map

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

@@ -1 +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,GAAE,cAAwB,GAAG,OAAO,CAAC,MAAM,CAAC,CAWhG;AAED,wBAAsB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,cAAwB,GAAG,OAAO,CAAC,CAAC,CAAC,CAUlG;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAOzD;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBlG;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAO7D"}
+{"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,GAAE,cAAwB,GAAG,OAAO,CAAC,MAAM,CAAC,CAWhG;AAED,wBAAsB,YAAY,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,GAAE,cAAwB,GAAG,OAAO,CAAC,CAAC,CAAC,CAUlG;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAOzD;AAED,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,gBAAgB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,EAAE,CAAC,CAgBlG;AAED,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAO7D;AAED,wBAAgB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAAC,CAoBvE"}

+ 24 - 1
file/index.js

@@ -39,7 +39,7 @@ 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;
+exports.dotEnv = 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) {
@@ -151,4 +151,27 @@ function getSubdirNames(dir) {
     });
 }
 exports.getSubdirNames = getSubdirNames;
+function dotEnv(path) {
+    return new Promise(function (resolve, reject) {
+        fs_1.default.readFile(path, function (err, buf) {
+            if (err)
+                return reject(err);
+            try {
+                resolve(buf.toString()
+                    .split(/\r?\n/)
+                    .filter(function (v) { return v.trim().length !== 0; })
+                    .reduce(function (result, current) {
+                    var match = current.match(/^(export\s+)?([^=]+)=(.*)$/);
+                    if (match)
+                        result[match[2]] = match[3];
+                    return result;
+                }, {}));
+            }
+            catch (e) {
+                reject(e);
+            }
+        });
+    });
+}
+exports.dotEnv = dotEnv;
 //# sourceMappingURL=index.js.map

Plik diff jest za duży
+ 0 - 0
file/index.js.map


+ 22 - 0
file/index.ts

@@ -61,3 +61,25 @@ export function getSubdirNames(dir: string): Promise<string[]> {
         });
     });
 }
+
+export function dotEnv(path: string): Promise<{ [key: string]: string }> {
+    return new Promise((resolve, reject) => {
+        fs.readFile(path, (err, buf) => {
+            if (err) return reject(err);
+            try {
+                resolve(
+                    buf.toString()
+                        .split(/\r?\n/)
+                        .filter((v) => v.trim().length !== 0)
+                        .reduce((result, current) => {
+                            const match = current.match(/^(export\s+)?([^=]+)=(.*)$/);
+                            if (match) result[match[2]] = match[3];
+                            return result;
+                        }, {} as { [key: string]: string })
+                );
+            } catch (e) {
+                reject(e);
+            }
+        });
+    });
+}

+ 2 - 1
index.d.ts

@@ -1,5 +1,5 @@
 import { exec, spawn } from './shell';
-import { readFile, readJsonFile, readDir, readJsonDir, getSubdirNames } from './file';
+import { readFile, readJsonFile, readDir, readJsonDir, getSubdirNames, dotEnv } from './file';
 export declare namespace NodeUtils {
     type Shell = {
         exec: typeof exec;
@@ -11,6 +11,7 @@ export declare namespace NodeUtils {
         readDir: typeof readDir;
         readJsonDir: typeof readJsonDir;
         getSubdirNames: typeof getSubdirNames;
+        dotEnv: typeof dotEnv;
     };
 }
 //# 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;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"}
+{"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,EAAE,MAAM,QAAQ,CAAC;AAE9F,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;QACtC,MAAM,EAAE,OAAO,MAAM,CAAC;KACzB,CAAA;CACJ"}

+ 2 - 1
index.ts

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

+ 1 - 1
package.json

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

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików