Open History to see the last commit
1import {
2 appendFile,
3 copyFile,
4 mkdir,
5 readFile,
6 readdir,
7 unlink,
8 writeFile,
9} from 'node:fs/promises'
10import { existsSync, statSync } from 'node:fs'
11import { dirname } from 'node:path'
12import { execa } from 'execa'
13import { memfs } from 'memfs'
14import { rimraf } from 'rimraf'
15
16import {
17 cleanUpFileArray,
18 cleanUpFiles,
19 getBinaryFile,
20} from './file-helpers.js'
21
22import type { Environment } from './types.js'
23
24export interface MemoryEnvironmentOutput {
25 files: Record<string, string>
26 deletedFiles: Array<string>
27 commands: Array<{ command: string; args: Array<string> }>
28}
29
30export function createDefaultEnvironment(): Environment {
31 let errors: Array<string> = []
32 return {
33 startRun: () => {
34 errors = []
35 },
36 finishRun: () => {},
37 getErrors: () => errors,
38
39 appendFile: async (path: string, contents: string) => {
40 await mkdir(dirname(path), { recursive: true })
41 return appendFile(path, contents)
42 },
43 copyFile: async (from: string, to: string) => {
44 await mkdir(dirname(to), { recursive: true })
45 return copyFile(from, to)
46 },
47 writeFile: async (path: string, contents: string) => {
48 await mkdir(dirname(path), { recursive: true })
49 return writeFile(path, contents)
50 },
51 writeFileBase64: async (path: string, base64Contents: string) => {
52 await mkdir(dirname(path), { recursive: true })
53 return writeFile(path, getBinaryFile(base64Contents) as string)
54 },
55 execute: async (
56 command: string,
57 args: Array<string>,
58 cwd: string,
59 options?: { inherit?: boolean },
60 ) => {
61 try {
62 if (options?.inherit) {
63 // For commands that should show output directly to the user
64 await execa(command, args, {
65 cwd,
66 stdio: 'inherit',
67 })
68 return { stdout: '' }
69 } else {
70 // For commands where we need to capture output
71 const result = await execa(command, args, {
72 cwd,
73 })
74 return { stdout: result.stdout }
75 }
76 } catch {
77 errors.push(
78 `Command "${command} ${args.join(' ')}" did not run successfully. Please run this manually in your project.`,
79 )
80 return { stdout: '' }
81 }
82 },
83 deleteFile: async (path: string) => {
84 if (existsSync(path)) {
85 await unlink(path)
86 }
87 },
88
89 readFile: async (path: string) => {
90 return (await readFile(path)).toString()
91 },
92 exists: (path: string) => existsSync(path),
93 isDirectory: (path: string) => statSync(path).isDirectory(),
94 readdir: async (path: string) => readdir(path),
95 rimraf: async (path: string) => {
96 await rimraf(path)
97 },
98
99 appName: 'TanStack',
100
101 startStep: () => {},
102 finishStep: () => {},
103
104 intro: () => {},
105 outro: () => {},
106 info: () => {},
107 error: () => {},
108 warn: () => {},
109 confirm: () => Promise.resolve(true),
110 spinner: () => ({
111 start: () => {},
112 stop: () => {},
113 }),
114 }
115}
116
117export function createMemoryEnvironment(returnPathsRelativeTo: string = '') {
118 const environment = createDefaultEnvironment()
119
120 const output: MemoryEnvironmentOutput = {
121 files: {},
122 commands: [],
123 deletedFiles: [],
124 }
125
126 const { fs, vol } = memfs({})
127
128 environment.appendFile = async (path: string, contents: string) => {
129 fs.mkdirSync(dirname(path), { recursive: true })
130 await fs.appendFileSync(path, contents)
131 }
132 environment.copyFile = async (from: string, to: string) => {
133 fs.mkdirSync(dirname(to), { recursive: true })
134 fs.copyFileSync(from, to)
135 return Promise.resolve()
136 }
137 environment.execute = async (command: string, args: Array<string>) => {
138 output.commands.push({
139 command,
140 args,
141 })
142 return Promise.resolve({ stdout: '' })
143 }
144 environment.readFile = async (path: string) => {
145 return Promise.resolve(fs.readFileSync(path, 'utf-8').toString())
146 }
147 environment.writeFile = async (path: string, contents: string) => {
148 fs.mkdirSync(dirname(path), { recursive: true })
149 await fs.writeFileSync(path, contents)
150 }
151 environment.writeFileBase64 = async (path: string, contents: string) => {
152 // For the in-memory file system, we are not converting the base64 to binary
153 // because it's not needed.
154 fs.mkdirSync(dirname(path), { recursive: true })
155 await fs.writeFileSync(path, contents)
156 }
157 environment.deleteFile = async (path: string) => {
158 output.deletedFiles.push(path)
159 if (fs.existsSync(path)) {
160 await fs.unlinkSync(path)
161 }
162 }
163 environment.finishRun = () => {
164 output.files = vol.toJSON() as Record<string, string>
165 for (const file of Object.keys(output.files)) {
166 if (fs.statSync(file).isDirectory()) {
167 delete output.files[file]
168 }
169 }
170 if (returnPathsRelativeTo.length) {
171 output.files = cleanUpFiles(output.files, returnPathsRelativeTo)
172 output.deletedFiles = cleanUpFileArray(
173 output.deletedFiles,
174 returnPathsRelativeTo,
175 )
176 }
177 }
178 environment.exists = (path: string) => {
179 return fs.existsSync(path)
180 }
181 environment.isDirectory = (path: string) => {
182 return fs.statSync(path).isDirectory()
183 }
184 environment.readdir = async (path: string) => {
185 return Promise.resolve(fs.readdirSync(path).map((d) => d.toString()))
186 }
187 environment.rimraf = async () => {}
188
189 return {
190 environment,
191 output,
192 }
193}