UNPKG

3.44 kBTypeScriptView Raw
1/**
2 * @license
3 * Copyright Google LLC All Rights Reserved.
4 *
5 * Use of this source code is governed by an MIT-style license that can be
6 * found in the LICENSE file at https://angular.io/license
7 */
8/**
9 * A `string` representing a specific type of path, with a particular brand `B`.
10 *
11 * A `string` is not assignable to a `BrandedPath`, but a `BrandedPath` is assignable to a `string`.
12 * Two `BrandedPath`s with different brands are not mutually assignable.
13 */
14export type BrandedPath<B extends string> = string & {
15 _brand: B;
16};
17/**
18 * A fully qualified path in the file system, in POSIX form.
19 */
20export type AbsoluteFsPath = BrandedPath<'AbsoluteFsPath'>;
21/**
22 * A path that's relative to another (unspecified) root.
23 *
24 * This does not necessarily have to refer to a physical file.
25 */
26export type PathSegment = BrandedPath<'PathSegment'>;
27/**
28 * An abstraction over the path manipulation aspects of a file-system.
29 */
30export interface PathManipulation {
31 extname(path: AbsoluteFsPath | PathSegment): string;
32 isRoot(path: AbsoluteFsPath): boolean;
33 isRooted(path: string): boolean;
34 dirname<T extends PathString>(file: T): T;
35 extname(path: AbsoluteFsPath | PathSegment): string;
36 join<T extends PathString>(basePath: T, ...paths: string[]): T;
37 /**
38 * Compute the relative path between `from` and `to`.
39 *
40 * In file-systems that can have multiple file trees the returned path may not actually be
41 * "relative" (i.e. `PathSegment`). For example, Windows can have multiple drives :
42 * `relative('c:/a/b', 'd:/a/c')` would be `d:/a/c'.
43 */
44 relative<T extends PathString>(from: T, to: T): PathSegment | AbsoluteFsPath;
45 basename(filePath: string, extension?: string): PathSegment;
46 normalize<T extends PathString>(path: T): T;
47 resolve(...paths: string[]): AbsoluteFsPath;
48 pwd(): AbsoluteFsPath;
49 chdir(path: AbsoluteFsPath): void;
50}
51/**
52 * An abstraction over the read-only aspects of a file-system.
53 */
54export interface ReadonlyFileSystem extends PathManipulation {
55 isCaseSensitive(): boolean;
56 exists(path: AbsoluteFsPath): boolean;
57 readFile(path: AbsoluteFsPath): string;
58 readFileBuffer(path: AbsoluteFsPath): Uint8Array;
59 readdir(path: AbsoluteFsPath): PathSegment[];
60 lstat(path: AbsoluteFsPath): FileStats;
61 stat(path: AbsoluteFsPath): FileStats;
62 realpath(filePath: AbsoluteFsPath): AbsoluteFsPath;
63 getDefaultLibLocation(): AbsoluteFsPath;
64}
65/**
66 * A basic interface to abstract the underlying file-system.
67 *
68 * This makes it easier to provide mock file-systems in unit tests,
69 * but also to create clever file-systems that have features such as caching.
70 */
71export interface FileSystem extends ReadonlyFileSystem {
72 writeFile(path: AbsoluteFsPath, data: string | Uint8Array, exclusive?: boolean): void;
73 removeFile(path: AbsoluteFsPath): void;
74 symlink(target: AbsoluteFsPath, path: AbsoluteFsPath): void;
75 copyFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void;
76 moveFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void;
77 ensureDir(path: AbsoluteFsPath): void;
78 removeDeep(path: AbsoluteFsPath): void;
79}
80export type PathString = string | AbsoluteFsPath | PathSegment;
81/**
82 * Information about an object in the FileSystem.
83 * This is analogous to the `fs.Stats` class in Node.js.
84 */
85export interface FileStats {
86 isFile(): boolean;
87 isDirectory(): boolean;
88 isSymbolicLink(): boolean;
89}