Skip to content
Source
Sign inSign up
TanStack/cli
Open History to see the last commit

environment.ts

179 lines4.9 KB

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