forked from angular/angular
/
node_js_file_system.ts
135 lines (131 loc) · 4.25 KB
/
node_js_file_system.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/// <reference types="node" />
import * as fs from 'fs';
import * as fsExtra from 'fs-extra';
import * as p from 'path';
import {absoluteFrom} from './helpers';
import {AbsoluteFsPath, FileStats, FileSystem, PathSegment, PathString} from './types';
/**
* A wrapper around the Node.js file-system (i.e the `fs` package).
*/
export class NodeJSFileSystem implements FileSystem {
private _caseSensitive: boolean|undefined = undefined;
exists(path: AbsoluteFsPath): boolean {
return fs.existsSync(path);
}
readFile(path: AbsoluteFsPath): string {
return fs.readFileSync(path, 'utf8');
}
readFileBuffer(path: AbsoluteFsPath): Uint8Array {
return fs.readFileSync(path);
}
writeFile(path: AbsoluteFsPath, data: string|Uint8Array, exclusive: boolean = false): void {
fs.writeFileSync(path, data, exclusive ? {flag: 'wx'} : undefined);
}
removeFile(path: AbsoluteFsPath): void {
fs.unlinkSync(path);
}
symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void {
fs.symlinkSync(target, path);
}
readdir(path: AbsoluteFsPath): PathSegment[] {
return fs.readdirSync(path) as PathSegment[];
}
lstat(path: AbsoluteFsPath): FileStats {
return fs.lstatSync(path);
}
stat(path: AbsoluteFsPath): FileStats {
return fs.statSync(path);
}
pwd(): AbsoluteFsPath {
return this.normalize(process.cwd()) as AbsoluteFsPath;
}
chdir(dir: AbsoluteFsPath): void {
process.chdir(dir);
}
copyFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void {
fs.copyFileSync(from, to);
}
moveFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void {
fs.renameSync(from, to);
}
ensureDir(path: AbsoluteFsPath): void {
const parents: AbsoluteFsPath[] = [];
while (!this.isRoot(path) && !this.exists(path)) {
parents.push(path);
path = this.dirname(path);
}
while (parents.length) {
this.safeMkdir(parents.pop()!);
}
}
removeDeep(path: AbsoluteFsPath): void {
fsExtra.removeSync(path);
}
isCaseSensitive(): boolean {
if (this._caseSensitive === undefined) {
// Note the use of the real file-system is intentional:
// `this.exists()` relies upon `isCaseSensitive()` so that would cause an infinite recursion.
this._caseSensitive = !fs.existsSync(togglePathCase(__filename));
}
return this._caseSensitive;
}
resolve(...paths: string[]): AbsoluteFsPath {
return this.normalize(p.resolve(...paths)) as AbsoluteFsPath;
}
dirname<T extends string>(file: T): T {
return this.normalize(p.dirname(file)) as T;
}
join<T extends string>(basePath: T, ...paths: string[]): T {
return this.normalize(p.join(basePath, ...paths)) as T;
}
isRoot(path: AbsoluteFsPath): boolean {
return this.dirname(path) === this.normalize(path);
}
isRooted(path: string): boolean {
return p.isAbsolute(path);
}
relative<T extends PathString>(from: T, to: T): PathSegment|AbsoluteFsPath {
return this.normalize(p.relative(from, to)) as PathSegment | AbsoluteFsPath;
}
basename(filePath: string, extension?: string): PathSegment {
return p.basename(filePath, extension) as PathSegment;
}
extname(path: AbsoluteFsPath|PathSegment): string {
return p.extname(path);
}
realpath(path: AbsoluteFsPath): AbsoluteFsPath {
return this.resolve(fs.realpathSync(path));
}
getDefaultLibLocation(): AbsoluteFsPath {
return this.resolve(require.resolve('typescript'), '..');
}
normalize<T extends string>(path: T): T {
// Convert backslashes to forward slashes
return path.replace(/\\/g, '/') as T;
}
private safeMkdir(path: AbsoluteFsPath): void {
try {
fs.mkdirSync(path);
} catch (err) {
// Ignore the error, if the path already exists and points to a directory.
// Re-throw otherwise.
if (!this.exists(path) || !this.stat(path).isDirectory()) {
throw err;
}
}
}
}
/**
* Toggle the case of each character in a file path.
*/
function togglePathCase(str: string): AbsoluteFsPath {
return absoluteFrom(
str.replace(/\w/g, ch => ch.toUpperCase() === ch ? ch.toLowerCase() : ch.toUpperCase()));
}