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

command-line.test.ts

300 lines8.1 KB

1import { basename, resolve } from 'node:path'
2import { beforeEach, describe, expect, it } from 'vitest'
3​
4import { normalizeOptions } from '../src/command-line.js'
5import {
6 sanitizePackageName,
7 getCurrentDirectoryName,
8} from '../src/utils.js'
9import {
10 __testRegisterFramework,
11 __testClearFrameworks,
12} from '@tanstack/cta-engine/dist/frameworks.js'
13​
14beforeEach(() => {
15 __testClearFrameworks()
16})
17​
18describe('sanitizePackageName', () => {
19 it('should convert to lowercase', () => {
20 expect(sanitizePackageName('MyProject')).toBe('myproject')
21 })
22​
23 it('should replace spaces with hyphens', () => {
24 expect(sanitizePackageName('my project')).toBe('my-project')
25 })
26​
27 it('should replace underscores with hyphens', () => {
28 expect(sanitizePackageName('my_project')).toBe('my-project')
29 })
30​
31 it('should remove invalid characters', () => {
32 expect(sanitizePackageName('my@project!')).toBe('myproject')
33 })
34​
35 it('should ensure it starts with a letter', () => {
36 expect(sanitizePackageName('123project')).toBe('project')
37 expect(sanitizePackageName('_myproject')).toBe('myproject')
38 })
39​
40 it('should collapse multiple hyphens', () => {
41 expect(sanitizePackageName('my--project')).toBe('my-project')
42 })
43​
44 it('should remove trailing hyphen', () => {
45 expect(sanitizePackageName('myproject-')).toBe('myproject')
46 })
47})
48​
49describe('getCurrentDirectoryName', () => {
50 it('should return the basename of the current working directory', () => {
51 expect(getCurrentDirectoryName()).toBe(basename(process.cwd()))
52 })
53})
54​
55describe('normalizeOptions', () => {
56 it('should return undefined if project name is not provided', async () => {
57 const options = await normalizeOptions({})
58 expect(options).toBeUndefined()
59 })
60​
61 it('should handle "." as project name by using sanitized current directory name', async () => {
62 const options = await normalizeOptions({
63 projectName: '.',
64 })
65 const expectedName = sanitizePackageName(getCurrentDirectoryName())
66 expect(options?.projectName).toBe(expectedName)
67 expect(options?.targetDir).toBe(resolve(process.cwd()))
68 })
69​
70 it('should return enable typescript based on the framework', async () => {
71 const jsOptions = await normalizeOptions({
72 projectName: 'test',
73 template: 'javascript',
74 })
75 expect(jsOptions?.typescript).toBe(false)
76 expect(jsOptions?.mode).toBe('code-router')
77​
78 const tsOptions = await normalizeOptions({
79 projectName: 'test',
80 template: 'typescript',
81 })
82 expect(tsOptions?.typescript).toBe(true)
83 expect(tsOptions?.mode).toBe('code-router')
84​
85 const frOptions = await normalizeOptions({
86 projectName: 'test',
87 template: 'file-router',
88 })
89 expect(frOptions?.typescript).toBe(true)
90 expect(frOptions?.mode).toBe('file-router')
91 })
92​
93 it('should return enable tailwind if the framework is solid', async () => {
94 const solidOptions = await normalizeOptions({
95 projectName: 'test',
96 framework: 'solid',
97 })
98 expect(solidOptions?.tailwind).toBe(true)
99​
100 const twOptions = await normalizeOptions({
101 projectName: 'test',
102 tailwind: true,
103 })
104 expect(twOptions?.tailwind).toBe(true)
105​
106 const noOptions = await normalizeOptions({
107 projectName: 'test',
108 })
109 expect(noOptions?.tailwind).toBe(false)
110 })
111​
112 it('should handle a starter url', async () => {
113 __testRegisterFramework({
114 id: 'solid',
115 name: 'Solid',
116 getAddOns: () => [
117 {
118 id: 'nitro',
119 name: 'nitro',
120 modes: ['file-router'],
121 default: true,
122 },
123 ],
124 supportedModes: {
125 'code-router': {
126 displayName: 'Code Router',
127 description: 'TanStack Router using code to define the routes',
128 forceTypescript: false,
129 },
130 'file-router': {
131 displayName: 'File Router',
132 description: 'TanStack Router using files to define the routes',
133 forceTypescript: true,
134 },
135 },
136 })
137 fetch.mockResponseOnce(
138 JSON.stringify({
139 id: 'https://github.com/cta-dev/cta-starter-solid',
140 tailwind: true,
141 typescript: false,
142 framework: 'solid',
143 mode: 'file-router',
144 type: 'starter',
145 description: 'A starter for Solid',
146 name: 'My Solid Starter',
147 dependsOn: [],
148 files: {},
149 deletedFiles: [],
150 }),
151 )
152​
153 const options = await normalizeOptions({
154 projectName: 'test',
155 starter: 'https://github.com/cta-dev/cta-starter-solid',
156 deployment: 'nitro',
157 })
158 expect(options?.mode).toBe('file-router')
159 expect(options?.tailwind).toBe(true)
160 expect(options?.typescript).toBe(true)
161 expect(options?.framework?.id).toBe('solid')
162 })
163​
164 it('should default to react-cra if no framework is provided', async () => {
165 __testRegisterFramework({
166 id: 'react-cra',
167 name: 'react',
168 })
169 const options = await normalizeOptions({
170 projectName: 'test',
171 })
172 expect(options?.framework?.id).toBe('react-cra')
173 })
174​
175 it('should handle forced addons', async () => {
176 __testRegisterFramework({
177 id: 'react-cra',
178 name: 'react',
179 getAddOns: () => [
180 {
181 id: 'foo',
182 name: 'foobar',
183 modes: ['file-router'],
184 },
185 {
186 id: 'nitro',
187 name: 'nitro',
188 modes: ['file-router'],
189 default: true,
190 },
191 ],
192 })
193 const options = await normalizeOptions(
194 {
195 projectName: 'test',
196 framework: 'react-cra',
197 },
198 'file-router',
199 ['foo'],
200 )
201 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
202 })
203​
204 it('should handle additional addons from the CLI', async () => {
205 __testRegisterFramework({
206 id: 'react-cra',
207 name: 'react',
208 getAddOns: () => [
209 {
210 id: 'foo',
211 name: 'foobar',
212 modes: ['file-router'],
213 },
214 {
215 id: 'baz',
216 name: 'baz',
217 modes: ['file-router'],
218 },
219 {
220 id: 'nitro',
221 name: 'nitro',
222 modes: ['file-router'],
223 default: true,
224 },
225 ],
226 })
227 const options = await normalizeOptions(
228 {
229 projectName: 'test',
230 addOns: ['baz'],
231 framework: 'react-cra',
232 template: 'file-router',
233 },
234 'file-router',
235 ['foo'],
236 )
237 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
238 expect(options?.chosenAddOns.map((a) => a.id).includes('baz')).toBe(true)
239 expect(options?.tailwind).toBe(true)
240 expect(options?.typescript).toBe(true)
241 })
242​
243 it('should handle toolchain as an addon', async () => {
244 __testRegisterFramework({
245 id: 'react-cra',
246 name: 'react',
247 getAddOns: () => [
248 {
249 id: 'biome',
250 name: 'Biome',
251 modes: ['file-router', 'code-router'],
252 },
253 {
254 id: 'nitro',
255 name: 'nitro',
256 modes: ['file-router', 'code-router'],
257 default: true,
258 },
259 ],
260 })
261 const options = await normalizeOptions({
262 projectName: 'test',
263 toolchain: 'biome',
264 })
265 expect(options?.chosenAddOns.map((a) => a.id).includes('biome')).toBe(true)
266 expect(options?.tailwind).toBe(true)
267 expect(options?.typescript).toBe(true)
268 })
269​
270 it('should handle the funky Windows edge case with CLI parsing', async () => {
271 __testRegisterFramework({
272 id: 'react-cra',
273 name: 'react',
274 getAddOns: () => [
275 {
276 id: 'foo',
277 name: 'foobar',
278 modes: ['file-router', 'code-router'],
279 },
280 {
281 id: 'baz',
282 name: 'baz',
283 modes: ['file-router', 'code-router'],
284 },
285 {
286 id: 'nitro',
287 name: 'nitro',
288 modes: ['file-router', 'code-router'],
289 default: true,
290 },
291 ],
292 })
293 const options = await normalizeOptions({
294 projectName: 'test',
295 addOns: ['baz foo'],
296 })
297 expect(options?.chosenAddOns.map((a) => a.id).includes('foo')).toBe(true)
298 expect(options?.chosenAddOns.map((a) => a.id).includes('baz')).toBe(true)
299 })
300})