Open History to see the last commit
1import { beforeEach, describe, expect, it, vi } from 'vitest'
2import { fs, vol } from 'memfs'
3import { resolve } from 'node:path'
4
5import {
6 readConfigFileFromEnvironment,
7 writeConfigFileToEnvironment,
8} from '../src/config-file.js'
9import { CONFIG_FILE } from '../src/constants.js'
10import { createMemoryEnvironment } from '../src/environment.js'
11import type { AddOn, Environment, Framework, Options } from '../src/types.js'
12
13vi.mock('node:fs', () => fs)
14vi.mock('node:fs/promises', () => fs.promises)
15
16beforeEach(() => {
17 vol.reset()
18})
19
20describe('writeConfigFile', () => {
21 it('should write the config file', async () => {
22 const targetDir = 'test-dir'
23 const options = {
24 framework: {
25 id: 'react',
26 getAddOns: () => [],
27 } as unknown as Framework,
28 chosenAddOns: [
29 {
30 id: 'add-on-1',
31 description: 'Add-on 1',
32 modes: ['file-router'],
33 } as AddOn,
34 ],
35 targetDir,
36 envVarValues: {
37 API_SECRET: 'do-not-persist',
38 },
39 } as unknown as Options
40 const persistedOptions = {
41 version: 1,
42 framework: options.framework.id,
43 chosenAddOns: options.chosenAddOns.map((addOn) => addOn.id),
44 }
45 const env = {
46 writeFile: (path, optionsString) => {
47 expect(path).toEqual(resolve(targetDir, CONFIG_FILE))
48 expect(optionsString).toEqual(JSON.stringify(persistedOptions, null, 2))
49 },
50 } as Environment
51 await writeConfigFileToEnvironment(env, options)
52 })
53})
54
55describe('readConfigFileFromEnvironment', () => {
56 it('should read the config file', async () => {
57 const targetDir = 'test-dir'
58 const persistedOptions = {
59 version: 1,
60 framework: 'react',
61 chosenAddOns: ['add-on-1'],
62 }
63 const { environment } = createMemoryEnvironment()
64 environment.writeFile(
65 resolve(targetDir, CONFIG_FILE),
66 JSON.stringify(persistedOptions, null, 2),
67 )
68 const config = await readConfigFileFromEnvironment(environment, targetDir)
69 expect(config).toEqual({ ...persistedOptions, bundler: 'vite' })
70 })
71
72 it('should upgrade old config files', async () => {
73 const targetDir = 'test-dir'
74 const persistedOptions = {
75 framework: 'react',
76 projectName: 'foo',
77 typescript: false,
78 tailwind: false,
79 packageManager: 'pnpm',
80 toolchain: 'none',
81 mode: 'code-router',
82 git: true,
83 variableValues: {},
84 envVarValues: {
85 API_SECRET: 'legacy-secret',
86 },
87 version: 1,
88 existingAddOns: [],
89 }
90 const { output, environment } = createMemoryEnvironment()
91 environment.writeFile(
92 resolve(targetDir, CONFIG_FILE),
93 JSON.stringify(persistedOptions, null, 2),
94 )
95 const config = await readConfigFileFromEnvironment(environment, targetDir)
96
97 environment.finishRun()
98
99 expect(config).toEqual({
100 framework: 'react',
101 bundler: 'vite',
102 projectName: 'foo',
103 typescript: false,
104 tailwind: false,
105 packageManager: 'pnpm',
106 chosenAddOns: [],
107 git: true,
108 mode: 'code-router',
109 version: 1,
110 })
111 })
112})