xref: /expo/tools/src/Transforms.types.ts (revision ea0d7378)
1/**
2 * Basic string transform that describes what should be replaced in the input string.
3 */
4export type ReplaceTransform = {
5  /**
6   * A substring or RegExp matching a part of the input that you want to replace.
7   */
8  find: RegExp | string;
9
10  /**
11   * A string that replaces matched substrings in the input.
12   */
13  replaceWith: string | ((substring: string, ...args: any[]) => string);
14};
15
16export type RawTransform = {
17  /**
18   * Function that takes string as an argument and returns result of the transformations.
19   */
20  transform: (text: string) => string;
21};
22
23export type StringTransform = RawTransform | ReplaceTransform;
24
25/**
26 * A string transform extended by paths glob filter.
27 */
28export type FileTransform = StringTransform & {
29  /**
30   * An array of glob patterns matching files to which the transform should be applied.
31   * Patterns without slashes will be matched against the basename of the path.
32   */
33  paths?: string | string[];
34
35  /**
36   * When truthy every transform that changes anything will print a diff
37   * and wait for confirm to continue. If value is a string it will be used to identify
38   * the transformation in the output
39   */
40  debug?: boolean | string;
41};
42
43/**
44 * An object containing both file content transforms and path transforms.
45 */
46export type FileTransforms = {
47  /**
48   * An array of transforms to apply on each file's content.
49   */
50  content?: FileTransform[];
51
52  /**
53   * An array of transforms to apply on the relative file path.
54   */
55  path?: StringTransform[];
56};
57
58/**
59 * An object with options passed to `copyFilesWithTransformsAsync` function.
60 */
61export type CopyFileOptions = {
62  /**
63   * Path of the file to copy, relative to `sourceDirectory`.
64   */
65  sourceFile: string;
66
67  /**
68   * A directory from which the files will be copied.
69   */
70  sourceDirectory: string;
71
72  /**
73   * A directory to which the transformed files will be copied.
74   */
75  targetDirectory: string;
76
77  /**
78   * An object with transform rules for file paths and contents.
79   */
80  transforms: FileTransforms;
81};
82
83/**
84 * The result of copying the file.
85 */
86export type CopyFileResult = {
87  /**
88   * The final file content after transformations.
89   */
90  content: string;
91
92  /**
93   * The final target path after transformations. Relative to provided `targetDirectory`.
94   */
95  targetFile: string;
96};
97