Open History to see the last commit
1import { cleanUpFileArray, cleanUpFiles } from './edge-file-helpers.js'
2import {
3 basenamePath,
4 dirnamePath,
5 joinPaths,
6 normalizePath,
7} from './edge-path.js'
8
9import type { Environment } from './types.js'
10
11export interface MemoryEnvironmentOutput {
12 files: Record<string, string>
13 deletedFiles: Array<string>
14 commands: Array<{ command: string; args: Array<string> }>
15}
16
17function hasDirectory(files: Record<string, string>, path: string) {
18 const directory = normalizePath(path)
19 const prefix = directory.endsWith('/') ? directory : `${directory}/`
20 return Object.keys(files).some((file) => file.startsWith(prefix))
21}
22
23function createMissingDirectoryError(path: string) {
24 return new Error(`Directory not found: ${path}`)
25}
26
27export function createMemoryEnvironment(returnPathsRelativeTo: string = '') {
28 const output: MemoryEnvironmentOutput = {
29 files: {},
30 commands: [],
31 deletedFiles: [],
32 }
33 const files: Record<string, string> = {}
34 let errors: Array<string> = []
35
36 const environment: Environment = {
37 startRun: () => {
38 errors = []
39 output.files = {}
40 output.commands = []
41 output.deletedFiles = []
42 },
43 finishRun: () => {
44 output.files = Object.keys(files).reduce<Record<string, string>>(
45 (acc, file) => {
46 acc[file] = files[file]
47 return acc
48 },
49 {},
50 )
51
52 if (returnPathsRelativeTo.length) {
53 output.files = cleanUpFiles(output.files, returnPathsRelativeTo)
54 output.deletedFiles = cleanUpFileArray(
55 output.deletedFiles,
56 returnPathsRelativeTo,
57 )
58 }
59 },
60 getErrors: () => errors,
61
62 appendFile: (path: string, contents: string) => {
63 const normalized = normalizePath(path)
64 files[normalized] = `${files[normalized] ?? ''}${contents}`
65 return Promise.resolve()
66 },
67 copyFile: (from: string, to: string) => {
68 const normalizedFrom = normalizePath(from)
69 const normalizedTo = normalizePath(to)
70 if (!(normalizedFrom in files)) {
71 throw new Error(`File not found: ${from}`)
72 }
73 files[normalizedTo] = files[normalizedFrom]
74 return Promise.resolve()
75 },
76 writeFile: (path: string, contents: string) => {
77 files[normalizePath(path)] = contents
78 return Promise.resolve()
79 },
80 writeFileBase64: (path: string, base64Contents: string) => {
81 files[normalizePath(path)] = base64Contents
82 return Promise.resolve()
83 },
84 execute: (command: string, args: Array<string>) => {
85 output.commands.push({
86 command,
87 args,
88 })
89 return Promise.resolve({ stdout: '' })
90 },
91 deleteFile: (path: string) => {
92 const normalized = normalizePath(path)
93 output.deletedFiles.push(normalized)
94 delete files[normalized]
95 return Promise.resolve()
96 },
97
98 exists: (path: string) => {
99 const normalized = normalizePath(path)
100 return normalized in files || hasDirectory(files, normalized)
101 },
102 isDirectory: (path: string) => hasDirectory(files, path),
103 readFile: (path: string) => {
104 const normalized = normalizePath(path)
105 if (!(normalized in files)) {
106 throw new Error(`File not found: ${path}`)
107 }
108 return Promise.resolve(files[normalized])
109 },
110 readdir: (path: string) => {
111 const normalized = normalizePath(path)
112 const directory = normalized === '.' ? '' : normalized
113 const prefix = directory ? `${directory}/` : ''
114
115 if (directory && !hasDirectory(files, directory)) {
116 throw createMissingDirectoryError(path)
117 }
118
119 const entries = new Set<string>()
120 for (const file of Object.keys(files)) {
121 if (!file.startsWith(prefix)) {
122 continue
123 }
124
125 const rest = file.slice(prefix.length)
126 const entry = rest.split('/')[0]
127 if (entry) {
128 entries.add(entry)
129 }
130 }
131
132 return Promise.resolve(Array.from(entries))
133 },
134 rimraf: (path: string) => {
135 const normalized = normalizePath(path)
136 const prefix = normalized.endsWith('/') ? normalized : `${normalized}/`
137 for (const file of Object.keys(files)) {
138 if (file === normalized || file.startsWith(prefix)) {
139 delete files[file]
140 }
141 }
142 return Promise.resolve()
143 },
144
145 appName: 'TanStack',
146
147 startStep: () => {},
148 finishStep: () => {},
149
150 intro: () => {},
151 outro: () => {},
152 info: () => {},
153 error: (_title?: string, message?: string) => {
154 if (message) {
155 errors.push(message)
156 }
157 },
158 warn: () => {},
159 confirm: () => Promise.resolve(true),
160 spinner: () => ({
161 start: () => {},
162 stop: () => {},
163 }),
164 }
165
166 return {
167 environment,
168 output,
169 paths: {
170 basename: basenamePath,
171 dirname: dirnamePath,
172 join: joinPaths,
173 },
174 }
175}