瀏覽代碼

/file module: corrected typescript declarations

Christian Kahlau 4 年之前
父節點
當前提交
a570207a49
共有 5 個文件被更改,包括 9 次插入8 次删除
  1. 3 3
      file/index.d.ts
  2. 1 1
      file/index.d.ts.map
  3. 1 0
      file/index.js
  4. 0 0
      file/index.js.map
  5. 4 4
      file/index.ts

+ 3 - 3
file/index.d.ts

@@ -1,8 +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 readFile(path: string, encoding?: BufferEncoding): Promise<string>;
+export declare function readJsonFile<T>(path: string, encoding?: BufferEncoding): 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 readJsonDir<T extends object>(dir: string, filenameProperty?: string): Promise<T[]>;
 export declare function getSubdirNames(dir: string): Promise<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,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"}
+{"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"}

+ 1 - 0
file/index.js

@@ -43,6 +43,7 @@ exports.getSubdirNames = exports.readJsonDir = exports.readDir = exports.readJso
 var fs_1 = __importDefault(require("fs"));
 var path_1 = __importDefault(require("path"));
 function readFile(path, encoding) {
+    if (encoding === void 0) { encoding = 'utf-8'; }
     return __awaiter(this, void 0, void 0, function () {
         return __generator(this, function (_a) {
             return [2 /*return*/, new Promise(function (resolve, reject) {

文件差異過大導致無法顯示
+ 0 - 0
file/index.js.map


+ 4 - 4
file/index.ts

@@ -1,7 +1,7 @@
 import fs from 'fs';
 import path from 'path';
 
-export async function readFile(path: string, encoding?): Promise<string> {
+export async function readFile(path: string, encoding: BufferEncoding = 'utf-8'): Promise<string> {
     return new Promise<string>((resolve, reject) => {
         fs.readFile(path, { encoding }, (err, data) => {
             if (err) return reject(err);
@@ -14,7 +14,7 @@ export async function readFile(path: string, encoding?): Promise<string> {
     });
 }
 
-export async function readJsonFile<T>(path: string, encoding = 'utf-8'): Promise<T> {
+export async function readJsonFile<T>(path: string, encoding: BufferEncoding = 'utf-8'): Promise<T> {
     return new Promise<T>(async (resolve, reject) => {
         try {
             resolve(JSON.parse(
@@ -35,13 +35,13 @@ export function readDir(dir: string): Promise<fs.Dirent[]> {
     });
 }
 
-export function readJsonDir<T>(dir: string, filenameProperty?: string): Promise<T[]> {
+export function readJsonDir<T extends object>(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));
+                const json: any = await readJsonFile<T>(path.resolve(dir, e.name));
                 if (typeof filenameProperty === 'string') {
                     json[filenameProperty] = e.name;
                 }

部分文件因文件數量過多而無法顯示