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, resolve } from 'node:path'
12import { execa } from 'execa'
13import { rimraf } from 'rimraf'
14
15import { createMemoryEnvironment as createEdgeMemoryEnvironment } from './edge-environment.js'
16import { getBinaryFile } from './file-helpers.js'
17
18import type { Environment } from './types.js'
19
20export type { MemoryEnvironmentOutput } from './edge-environment.js'
21
22export function createDefaultEnvironment(): Environment {
23 let errors: Array<string> = []
24 return {
25 startRun: () => {
26 errors = []
27 },
28 finishRun: () => {},
29 getErrors: () => errors,
30
31 appendFile: async (path: string, contents: string) => {
32 await mkdir(dirname(path), { recursive: true })
33 return appendFile(path, contents)
34 },
35 copyFile: async (from: string, to: string) => {
36 await mkdir(dirname(to), { recursive: true })
37 return copyFile(from, to)
38 },
39 writeFile: async (path: string, contents: string) => {
40 await mkdir(dirname(path), { recursive: true })
41 return writeFile(path, contents)
42 },
43 writeFileBase64: async (path: string, base64Contents: string) => {
44 await mkdir(dirname(path), { recursive: true })
45 return writeFile(path, getBinaryFile(base64Contents) as string)
46 },
47 execute: async (
48 command: string,
49 args: Array<string>,
50 cwd: string,
51 options?: { inherit?: boolean },
52 ) => {
53 try {
54 if (options?.inherit) {
55 // For commands that should show output directly to the user
56 await execa(command, args, {
57 cwd,
58 stdio: 'inherit',
59 })
60 return { stdout: '' }
61 } else {
62 // For commands where we need to capture output
63 const result = await execa(command, args, {
64 cwd,
65 })
66 return { stdout: result.stdout }
67 }
68 } catch {
69 errors.push(
70 `Command "${command} ${args.join(' ')}" did not run successfully. Please run this manually in your project.`,
71 )
72 return { stdout: '' }
73 }
74 },
75 deleteFile: async (path: string) => {
76 if (existsSync(path)) {
77 await unlink(path)
78 }
79 },
80
81 readFile: async (path: string) => {
82 return (await readFile(path)).toString()
83 },
84 exists: (path: string) => existsSync(path),
85 isDirectory: (path: string) => statSync(path).isDirectory(),
86 readdir: async (path: string) => readdir(path),
87 rimraf: async (path: string) => {
88 await rimraf(path)
89 },
90
91 appName: 'TanStack',
92
93 startStep: () => {},
94 finishStep: () => {},
95
96 intro: () => {},
97 outro: () => {},
98 info: () => {},
99 error: () => {},
100 warn: () => {},
101 confirm: () => Promise.resolve(true),
102 spinner: () => ({
103 start: () => {},
104 stop: () => {},
105 }),
106 }
107}
108
109export function createMemoryEnvironment(returnPathsRelativeTo: string = '') {
110 const { environment, output } =
111 createEdgeMemoryEnvironment(returnPathsRelativeTo)
112 const resolvePath = (path: string) => resolve(process.cwd(), path)
113
114 const appendFile = environment.appendFile
115 environment.appendFile = (path, contents) =>
116 appendFile(resolvePath(path), contents)
117
118 const copyFile = environment.copyFile
119 environment.copyFile = (from, to) =>
120 copyFile(resolvePath(from), resolvePath(to))
121
122 const writeFile = environment.writeFile
123 environment.writeFile = (path, contents) =>
124 writeFile(resolvePath(path), contents)
125
126 const writeFileBase64 = environment.writeFileBase64
127 environment.writeFileBase64 = (path, contents) =>
128 writeFileBase64(resolvePath(path), contents)
129
130 const deleteFile = environment.deleteFile
131 environment.deleteFile = (path) => deleteFile(resolvePath(path))
132
133 const readFile = environment.readFile
134 environment.readFile = (path) => readFile(resolvePath(path))
135
136 const exists = environment.exists
137 environment.exists = (path) => exists(resolvePath(path))
138
139 const isDirectory = environment.isDirectory
140 environment.isDirectory = (path) => isDirectory(resolvePath(path))
141
142 const readdir = environment.readdir
143 environment.readdir = (path) => readdir(resolvePath(path))
144
145 const rimraf = environment.rimraf
146 environment.rimraf = (path) => rimraf(resolvePath(path))
147
148 return { environment, output }
149}