From 55bde8acb8f76f5fd6842f4edcd3fbb8a6edf627 Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Fri, 5 Jul 2019 12:23:43 -0400 Subject: [PATCH] Improve generic types in tools/fs/files.ts. --- tools/fs/files.ts | 38 ++++++++++++++++---------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/tools/fs/files.ts b/tools/fs/files.ts index 9337347737..99f40fd1f3 100644 --- a/tools/fs/files.ts +++ b/tools/fs/files.ts @@ -157,7 +157,7 @@ export function findGitCommitHash(path: string) { export function addToGitignore(dirPath: string, entry: string) { const filePath = pathJoin(dirPath, ".gitignore"); if (exists(filePath)) { - let data = readFile(filePath, 'utf8'); + let data = readFile(filePath, 'utf8') as string; const lines = data.split(/\n/); if (lines.some(line => line === entry)) { // already there do nothing @@ -1364,7 +1364,7 @@ export class KeyValueFile { constructor(public path: string) {} set(k: string, v: any) { - const data = this.readAll() || ''; + const data = (this.readAll() || '').toString("utf8"); const lines = data.split(/\n/); let found = false; @@ -1585,16 +1585,12 @@ type wrapFsFuncOptions = { dirty?: (...args: TArgs) => any; } -function wrapFsFunc< - TArgs extends any[], - TResult, - F extends (...args: TArgs) => TResult, ->( +function wrapFsFunc( fnName: string, - fn: F, + fn: (...args: TArgs) => TResult, pathArgIndices: number[], options?: wrapFsFuncOptions, -): F { +): typeof fn { return Profile("files." + fnName, function (...args: TArgs) { for (let j = pathArgIndices.length - 1; j >= 0; --j) { const i = pathArgIndices[j]; @@ -1637,7 +1633,7 @@ function wrapFsFunc< } return finalResult; - }) as F; + }); } const withCacheSlot = new Slot>(); @@ -1657,22 +1653,18 @@ export const dependOnPath = wrap( { disposable: true } ); -function wrapDestructiveFsFunc< - TArgs extends any[], - TResult, - F extends (...args: TArgs) => TResult, ->( +function wrapDestructiveFsFunc( fnName: string, - fn: F, + fn: (...args: TArgs) => TResult, pathArgIndices: number[] = [0], options?: wrapFsFuncOptions, -): F { - return wrapFsFunc(fnName, fn, pathArgIndices, { +): typeof fn { + return wrapFsFunc(fnName, fn, pathArgIndices, { ...options, dirty(...args: TArgs) { pathArgIndices.forEach(i => dependOnPath.dirty(args[i])); } - }) as F; + }); } export const readFile = wrapFsFunc("readFile", fs.readFileSync, [0], { @@ -1732,12 +1724,14 @@ export const rename = isWindowsLikeFilesystem() ? function (from: string, to: st } : wrappedRename; // Warning: doesn't convert slashes in the second 'cache' arg -export const realpath = wrapFsFunc("realpath", fs.realpathSync, [0], { +export const realpath = +wrapFsFunc<[string], string>("realpath", fs.realpathSync, [0], { cached: true, modifyReturnValue: convertToStandardPath, }); -export const readdir = wrapFsFunc("readdir", fs.readdirSync, [0], { +export const readdir = +wrapFsFunc<[string], string[]>("readdir", fs.readdirSync, [0], { cached: true, modifyReturnValue(entries: string[]) { return entries.map(entry => convertToStandardPath(entry)); @@ -1753,7 +1747,7 @@ export const lstat = wrapFsFunc("lstat", fs.lstatSync, [0], { cached: true }); export const mkdir = wrapDestructiveFsFunc("mkdir", fs.mkdirSync); export const open = wrapFsFunc("open", fs.openSync, [0]); export const read = wrapFsFunc("read", fs.readSync, []); -export const readlink = wrapFsFunc("readlink", fs.readlinkSync, [0]); +export const readlink = wrapFsFunc<[string], string>("readlink", fs.readlinkSync, [0]); export const rmdir = wrapDestructiveFsFunc("rmdir", fs.rmdirSync); export const stat = wrapFsFunc("stat", fs.statSync, [0], { cached: true }); export const symlink = wrapFsFunc("symlink", fs.symlinkSync, [0, 1]);