UNPKG

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