Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: included symlinked directories when reading a directory #1290

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 15 additions & 28 deletions deno/common/DenoRuntime.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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