UNPKG

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