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(cancelSignal?: AbortSignal): 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 getPackageManagerVersion: async (packageManager, cwd) => {
48 try {
49 const result = await execa(packageManager, ['--version'], { cwd, cancelSignal })
50 return result.stdout.trim()
51 } catch {
52 return undefined
53 }
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 cancelSignal,
67 stdio: 'inherit',
68 })
69 return { stdout: '' }
70 } else {
71 // For commands where we need to capture output
72 const result = await execa(command, args, {
73 cwd,
74 cancelSignal,
75 })
76 return { stdout: result.stdout }
77 }
78 } catch {
79 errors.push(
80 `Command "${command} ${args.join(' ')}" did not run successfully. Please run this manually in your project.`,
81 )
82 return { stdout: '', failed: true }
83 }
84 },
85 deleteFile: async (path: string) => {
86 if (existsSync(path)) {
87 await unlink(path)
88 }
89 },
90
91 readFile: async (path: string) => {
92 return (await readFile(path)).toString()
93 },
94 exists: (path: string) => existsSync(path),
95 isDirectory: (path: string) => statSync(path).isDirectory(),
96 readdir: async (path: string) => readdir(path),
97 rimraf: async (path: string) => {
98 await rimraf(path)
99 },
100
101 appName: 'TanStack',
102
103 startStep: () => {},
104 finishStep: () => {},
105
106 intro: () => {},
107 outro: () => {},
108 info: () => {},
109 error: () => {},
110 warn: () => {},
111 confirm: () => Promise.resolve(true),
112 spinner: () => ({
113 start: () => {},
114 stop: () => {},
115 }),
116 }
117}
118
119export function createMemoryEnvironment(returnPathsRelativeTo: string = '') {
120 const { environment, output } =
121 createEdgeMemoryEnvironment(returnPathsRelativeTo)
122 const resolvePath = (path: string) => resolve(process.cwd(), path)
123
124 const appendFile = environment.appendFile
125 environment.appendFile = (path, contents) =>
126 appendFile(resolvePath(path), contents)
127
128 const copyFile = environment.copyFile
129 environment.copyFile = (from, to) =>
130 copyFile(resolvePath(from), resolvePath(to))
131
132 const writeFile = environment.writeFile
133 environment.writeFile = (path, contents) =>
134 writeFile(resolvePath(path), contents)
135
136 const writeFileBase64 = environment.writeFileBase64
137 environment.writeFileBase64 = (path, contents) =>
138 writeFileBase64(resolvePath(path), contents)
139
140 const deleteFile = environment.deleteFile
141 environment.deleteFile = (path) => deleteFile(resolvePath(path))
142
143 const readFile = environment.readFile
144 environment.readFile = (path) => readFile(resolvePath(path))
145
146 const exists = environment.exists
147 environment.exists = (path) => exists(resolvePath(path))
148
149 const isDirectory = environment.isDirectory
150 environment.isDirectory = (path) => isDirectory(resolvePath(path))
151
152 const readdir = environment.readdir
153 environment.readdir = (path) => readdir(resolvePath(path))
154
155 const rimraf = environment.rimraf
156 environment.rimraf = (path) => rimraf(resolvePath(path))
157
158 return { environment, output }
159}