Skip to content

Commit

Permalink
fix: included symlinked directories when reading a directory (#1290)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 3, 2022
1 parent 284a7fd commit f1b4ea2
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 142 deletions.
43 changes: 15 additions & 28 deletions deno/common/DenoRuntime.ts
Expand Up @@ -90,38 +90,25 @@ class DenoRuntimeFileSystem {
return Deno.copyFileSync(srcPath, destPath);
}

async fileExists(filePath: string) {
try {
const stat = await Deno.stat(filePath);
return stat.isFile;
} catch {
return false;
}
}

fileExistsSync(filePath: string) {
try {
return Deno.statSync(filePath).isFile;
} catch {
return false;
}
async stat(filePath: string) {
const stat = await Deno.stat(filePath);
return this._toStat(stat);
}

async directoryExists(dirPath: string) {
try {
const stat = await Deno.stat(dirPath);
return stat.isDirectory;
} catch {
return false;
}
statSync(path: string) {
const stat = Deno.statSync(path);
return this._toStat(stat);
}

directoryExistsSync(dirPath: string) {
try {
return Deno.statSync(dirPath).isDirectory;
} catch (err) {
return false;
}
private _toStat(stat: Deno.FileInfo) {
return {
isFile() {
return stat.isFile;
},
isDirectory() {
return stat.isDirectory;
},
};
}

realpathSync(path: string) {
Expand Down
17 changes: 9 additions & 8 deletions deno/common/ts_morph_common.d.ts
Expand Up @@ -914,6 +914,11 @@ export interface RuntimeDirEntry {
isSymlink: boolean;
}

export interface RuntimeFileInfo {
isFile(): boolean;
isDirectory(): boolean;
}

export interface RuntimeFileSystem {
/** Gets if this file system is case sensitive. */
isCaseSensitive(): boolean;
Expand Down Expand Up @@ -943,14 +948,10 @@ export interface RuntimeFileSystem {
copy(srcPath: string, destPath: string): Promise<void>;
/** Synchronously copies a file or directory. */
copySync(srcPath: string, destPath: string): void;
/** Asynchronously checks if a file exists. */
fileExists(filePath: string): Promise<boolean>;
/** Synchronously checks if a file exists. */
fileExistsSync(filePath: string): boolean;
/** Asynchronously checks if a directory exists. */
directoryExists(dirPath: string): Promise<boolean>;
/** Synchronously checks if a directory exists. */
directoryExistsSync(dirPath: string): boolean;
/** Asynchronously gets the path's stat information. */
stat(path: string): Promise<RuntimeFileInfo>;
/** Synchronously gets the path's stat information. */
statSync(path: string): RuntimeFileInfo;
/** See https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options */
realpathSync(path: string): string;
/** Gets the current directory of the environment. */
Expand Down
44 changes: 37 additions & 7 deletions deno/common/ts_morph_common.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions deno/ts_morph.d.ts
Expand Up @@ -9214,6 +9214,8 @@ export declare class Symbol {
getExportSymbol(): Symbol;
/** Gets if the symbol is an alias. */
isAlias(): boolean;
/** Gets if the symbol is optional. */
isOptional(): boolean;
/** Gets the symbol flags. */
getFlags(): SymbolFlags;
/**
Expand Down Expand Up @@ -10124,6 +10126,8 @@ export declare class Type<TType extends ts.Type = ts.Type> {
isAny(): boolean;
/** Gets if this is an array type. */
isArray(): boolean;
/** Gets if this is a template literal type. */
isTemplateLiteral(): boolean;
/** Gets if this is a boolean type. */
isBoolean(): boolean;
/** Gets if this is a string type. */
Expand Down
6 changes: 6 additions & 0 deletions deno/ts_morph.js
Expand Up @@ -16840,6 +16840,9 @@ class Symbol {
isAlias() {
return (this.getFlags() & SymbolFlags.Alias) === SymbolFlags.Alias;
}
isOptional() {
return (this.getFlags() & SymbolFlags.Optional) === SymbolFlags.Optional;
}
getFlags() {
return this.compilerSymbol.getFlags();
}
Expand Down Expand Up @@ -17971,6 +17974,9 @@ class Type {
return false;
return symbol.getName() === "Array" && this.getTypeArguments().length === 1;
}
isTemplateLiteral() {
return this._hasTypeFlag(TypeFlags.TemplateLiteral);
}
isBoolean() {
return this._hasTypeFlag(TypeFlags.Boolean);
}
Expand Down
17 changes: 9 additions & 8 deletions packages/common/lib/ts-morph-common.d.ts
Expand Up @@ -913,6 +913,11 @@ export interface RuntimeDirEntry {
isSymlink: boolean;
}

export interface RuntimeFileInfo {
isFile(): boolean;
isDirectory(): boolean;
}

export interface RuntimeFileSystem {
/** Gets if this file system is case sensitive. */
isCaseSensitive(): boolean;
Expand Down Expand Up @@ -942,14 +947,10 @@ export interface RuntimeFileSystem {
copy(srcPath: string, destPath: string): Promise<void>;
/** Synchronously copies a file or directory. */
copySync(srcPath: string, destPath: string): void;
/** Asynchronously checks if a file exists. */
fileExists(filePath: string): Promise<boolean>;
/** Synchronously checks if a file exists. */
fileExistsSync(filePath: string): boolean;
/** Asynchronously checks if a directory exists. */
directoryExists(dirPath: string): Promise<boolean>;
/** Synchronously checks if a directory exists. */
directoryExistsSync(dirPath: string): boolean;
/** Asynchronously gets the path's stat information. */
stat(path: string): Promise<RuntimeFileInfo>;
/** Synchronously gets the path's stat information. */
statSync(path: string): RuntimeFileInfo;
/** See https://nodejs.org/api/fs.html#fs_fs_realpathsync_path_options */
realpathSync(path: string): string;
/** Gets the current directory of the environment. */
Expand Down
40 changes: 33 additions & 7 deletions packages/common/src/fileSystem/RealFileSystemHost.ts
Expand Up @@ -29,8 +29,18 @@ export class RealFileSystemHost implements FileSystemHost {
readDirSync(dirPath: string): RuntimeDirEntry[] {
try {
const entries = fs.readDirSync(dirPath);
for (const entry of entries)
for (const entry of entries) {
entry.name = FileUtils.pathJoin(dirPath, entry.name);
if (entry.isSymlink) {
try {
const info = fs.statSync(entry.name);
entry.isDirectory = info.isDirectory();
entry.isFile = info.isFile();
} catch {
// ignore
}
}
}
return entries;
} catch (err) {
throw this.getDirectoryNotFoundErrorIfNecessary(err, dirPath);
Expand Down Expand Up @@ -96,23 +106,39 @@ export class RealFileSystemHost implements FileSystemHost {
}

/** @inheritdoc */
fileExists(filePath: string) {
return fs.fileExists(filePath);
async fileExists(filePath: string) {
try {
return (await fs.stat(filePath)).isFile();
} catch {
return false;
}
}

/** @inheritdoc */
fileExistsSync(filePath: string) {
return fs.fileExistsSync(filePath);
try {
return fs.statSync(filePath).isFile();
} catch {
return false;
}
}

/** @inheritdoc */
directoryExists(dirPath: string) {
return fs.directoryExists(dirPath);
async directoryExists(dirPath: string) {
try {
return (await fs.stat(dirPath)).isDirectory();
} catch {
return false;
}
}

/** @inheritdoc */
directoryExistsSync(dirPath: string) {
return fs.directoryExistsSync(dirPath);
try {
return fs.statSync(dirPath).isDirectory();
} catch {
return false;
}
}

/** @inheritdoc */
Expand Down
14 changes: 3 additions & 11 deletions packages/common/src/runtimes/BrowserRuntime.ts
@@ -1,5 +1,5 @@
import minimatch from "minimatch";
import { Runtime, RuntimeDirEntry, RuntimeFileSystem, RuntimePath } from "./Runtime";
import { Runtime, RuntimeDirEntry, RuntimeFileInfo, RuntimeFileSystem, RuntimePath } from "./Runtime";

const path = require("path-browserify");

Expand Down Expand Up @@ -90,19 +90,11 @@ class BrowserRuntimeFileSystem implements RuntimeFileSystem {
throw new Error(this._errorMessage);
}

fileExists(_filePath: string): Promise<boolean> {
stat(_path: string): Promise<RuntimeFileInfo> {
return Promise.reject(new Error(this._errorMessage));
}

fileExistsSync(_filePath: string): boolean {
throw new Error(this._errorMessage);
}

directoryExists(_dirPath: string): Promise<boolean> {
return Promise.reject(new Error(this._errorMessage));
}

directoryExistsSync(_dirPath: string): boolean {
statSync(_path: string): RuntimeFileInfo {
throw new Error(this._errorMessage);
}

Expand Down
43 changes: 15 additions & 28 deletions packages/common/src/runtimes/DenoRuntime.ts
Expand Up @@ -95,38 +95,25 @@ class DenoRuntimeFileSystem {
return Deno.copyFileSync(srcPath, destPath);
}

async fileExists(filePath: string) {
try {
const stat = await Deno.stat(filePath);
return stat.isFile;
} catch {
return false;
}
}

fileExistsSync(filePath: string) {
try {
return Deno.statSync(filePath).isFile;
} catch {
return false;
}
async stat(filePath: string) {
const stat = await Deno.stat(filePath);
return this._toStat(stat);
}

async directoryExists(dirPath: string) {
try {
const stat = await Deno.stat(dirPath);
return stat.isDirectory;
} catch {
return false;
}
statSync(path: string) {
const stat = Deno.statSync(path);
return this._toStat(stat);
}

directoryExistsSync(dirPath: string) {
try {
return Deno.statSync(dirPath).isDirectory;
} catch (err) {
return false;
}
private _toStat(stat: Deno.FileInfo) {
return {
isFile() {
return stat.isFile;
},
isDirectory() {
return stat.isDirectory;
},
};
}

realpathSync(path: string) {
Expand Down

0 comments on commit f1b4ea2

Please sign in to comment.