UNPKG

3.23 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 */
8import { JsonValue, Path, PathFragment } from '@angular-devkit/core';
9import { Action } from './action';
10export declare enum MergeStrategy {
11 AllowOverwriteConflict = 2,
12 AllowCreationConflict = 4,
13 AllowDeleteConflict = 8,
14 Default = 0,
15 Error = 1,
16 ContentOnly = 2,
17 Overwrite = 14
18}
19export declare const FileVisitorCancelToken: symbol;
20export type FileVisitor = FilePredicate<void>;
21export interface FileEntry {
22 readonly path: Path;
23 readonly content: Buffer;
24}
25export interface DirEntry {
26 readonly parent: DirEntry | null;
27 readonly path: Path;
28 readonly subdirs: PathFragment[];
29 readonly subfiles: PathFragment[];
30 dir(name: PathFragment): DirEntry;
31 file(name: PathFragment): FileEntry | null;
32 visit(visitor: FileVisitor): void;
33}
34export interface FilePredicate<T> {
35 (path: Path, entry?: Readonly<FileEntry> | null): T;
36}
37export declare const TreeSymbol: symbol;
38export interface Tree {
39 branch(): Tree;
40 merge(other: Tree, strategy?: MergeStrategy): void;
41 readonly root: DirEntry;
42 read(path: string): Buffer | null;
43 /**
44 * Reads a file from the Tree as a UTF-8 encoded text file.
45 *
46 * @param path The path of the file to read.
47 * @returns A string containing the contents of the file.
48 * @throws {@link FileDoesNotExistException} if the file is not found.
49 * @throws An error if the file contains invalid UTF-8 characters.
50 */
51 readText(path: string): string;
52 /**
53 * Reads and parses a file from the Tree as a UTF-8 encoded JSON file.
54 * Supports parsing JSON (RFC 8259) with the following extensions:
55 * * Single-line and multi-line JavaScript comments
56 * * Trailing commas within objects and arrays
57 *
58 * @param path The path of the file to read.
59 * @returns A JsonValue containing the parsed contents of the file.
60 * @throws {@link FileDoesNotExistException} if the file is not found.
61 * @throws An error if the file contains invalid UTF-8 characters.
62 * @throws An error if the file contains malformed JSON.
63 */
64 readJson(path: string): JsonValue;
65 exists(path: string): boolean;
66 get(path: string): FileEntry | null;
67 getDir(path: string): DirEntry;
68 visit(visitor: FileVisitor): void;
69 overwrite(path: string, content: Buffer | string): void;
70 beginUpdate(path: string): UpdateRecorder;
71 commitUpdate(record: UpdateRecorder): void;
72 create(path: string, content: Buffer | string): void;
73 delete(path: string): void;
74 rename(from: string, to: string): void;
75 apply(action: Action, strategy?: MergeStrategy): void;
76 readonly actions: Action[];
77}
78export interface TreeConstructor {
79 isTree(maybeTree: object): maybeTree is Tree;
80}
81export declare const Tree: TreeConstructor;
82export interface UpdateRecorder {
83 insertLeft(index: number, content: Buffer | string): UpdateRecorder;
84 insertRight(index: number, content: Buffer | string): UpdateRecorder;
85 remove(index: number, length: number): UpdateRecorder;
86}