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

environment.ts

157 lines4.5 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, 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 getPackageManagerVersion: async (packageManager, cwd) => {
48 try {
49 const result = await execa(packageManager, ['--version'], { cwd })
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 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 }
116​
117export function createMemoryEnvironment(returnPathsRelativeTo: string = '') {
118 const { environment, output } =
119 createEdgeMemoryEnvironment(returnPathsRelativeTo)
120 const resolvePath = (path: string) => resolve(process.cwd(), path)
121​
122 const appendFile = environment.appendFile
123 environment.appendFile = (path, contents) =>
124 appendFile(resolvePath(path), contents)
125​
126 const copyFile = environment.copyFile
127 environment.copyFile = (from, to) =>
128 copyFile(resolvePath(from), resolvePath(to))
129​
130 const writeFile = environment.writeFile
131 environment.writeFile = (path, contents) =>
132 writeFile(resolvePath(path), contents)
133​
134 const writeFileBase64 = environment.writeFileBase64
135 environment.writeFileBase64 = (path, contents) =>
136 writeFileBase64(resolvePath(path), contents)
137​
138 const deleteFile = environment.deleteFile
139 environment.deleteFile = (path) => deleteFile(resolvePath(path))
140​
141 const readFile = environment.readFile
142 environment.readFile = (path) => readFile(resolvePath(path))
143​
144 const exists = environment.exists
145 environment.exists = (path) => exists(resolvePath(path))
146​
147 const isDirectory = environment.isDirectory
148 environment.isDirectory = (path) => isDirectory(resolvePath(path))
149​
150 const readdir = environment.readdir
151 environment.readdir = (path) => readdir(resolvePath(path))
152​
153 const rimraf = environment.rimraf
154 environment.rimraf = (path) => rimraf(resolvePath(path))
155​
156 return { environment, output }