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