refactor(compiler-cli): remove unused method FileSystem#mkdir() (#33237)

Previously, the `FileSystem` abstraction featured a `mkdir()` method. In
`NodeJSFileSystem` (the default `FileSystem` implementation used in
actual code), the method behaved similar to Node.js' `fs.mkdirSync()`
(i.e. failing if any parent directory is missing or the directory exists
already). In contrast, `MockFileSystem` (which is the basis or mock
`FileSystem` implementations used in tests) implemented `mkdir()` as an
alias to `ensureDir()`, which behaved more like Node.js'
`fs.mkdirSync()` with the `recursive` option set to `true` (i.e.
creating any missing parent directories and succeeding if the directory
exists already).

This commit fixes this inconsistency by removing the `mkdir()` method,
which was not used anyway and only keeping `ensureDir()` (which is
consistent across our different `FileSystem` implementations).

PR Close #33237
This commit is contained in:
George Kalpakas
2019-10-18 16:15:57 +03:00
committed by Matias Niemelä
parent 8017229292
commit d7dc6cbc04
7 changed files with 15 additions and 51 deletions

View File

@ -96,10 +96,21 @@ export abstract class MockFileSystem implements FileSystem {
delete folder[name];
}
mkdir(path: AbsoluteFsPath): void { this.ensureFolders(this._fileTree, this.splitPath(path)); }
ensureDir(path: AbsoluteFsPath): void {
this.ensureFolders(this._fileTree, this.splitPath(path));
const segments = this.splitPath(path);
let current: Folder = this._fileTree;
// Convert the root folder to a canonical empty string `''` (on Windows it would be `'C:'`).
segments[0] = '';
for (const segment of segments) {
if (isFile(current[segment])) {
throw new Error(`Folder already exists as a file.`);
}
if (!current[segment]) {
current[segment] = {};
}
current = current[segment] as Folder;
}
}
isRoot(path: AbsoluteFsPath): boolean { return this.dirname(path) === path; }
@ -173,21 +184,6 @@ export abstract class MockFileSystem implements FileSystem {
const file = segments.pop() !;
return [path.substring(0, path.length - file.length - 1) as AbsoluteFsPath, file];
}
protected ensureFolders(current: Folder, segments: string[]): Folder {
// Convert the root folder to a canonical empty string `""` (on Windows it would be `C:`).
segments[0] = '';
for (const segment of segments) {
if (isFile(current[segment])) {
throw new Error(`Folder already exists as a file.`);
}
if (!current[segment]) {
current[segment] = {};
}
current = current[segment] as Folder;
}
return current;
}
}
export interface FindResult {
path: AbsoluteFsPath;