|
| 1 | +import fs from 'node:fs'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { writeEditorConfigs } from '../editor.js'; |
| 8 | + |
| 9 | +const tempDirs: string[] = []; |
| 10 | + |
| 11 | +function createTempDir() { |
| 12 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'vp-editor-config-')); |
| 13 | + tempDirs.push(dir); |
| 14 | + return dir; |
| 15 | +} |
| 16 | + |
| 17 | +afterEach(() => { |
| 18 | + for (const dir of tempDirs.splice(0, tempDirs.length)) { |
| 19 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 20 | + } |
| 21 | +}); |
| 22 | + |
| 23 | +describe('writeEditorConfigs', () => { |
| 24 | + it('writes vscode settings that align formatter config with vite.config.ts', async () => { |
| 25 | + const projectRoot = createTempDir(); |
| 26 | + |
| 27 | + await writeEditorConfigs({ |
| 28 | + projectRoot, |
| 29 | + editorId: 'vscode', |
| 30 | + interactive: false, |
| 31 | + silent: true, |
| 32 | + }); |
| 33 | + |
| 34 | + const settings = JSON.parse( |
| 35 | + fs.readFileSync(path.join(projectRoot, '.vscode', 'settings.json'), 'utf8'), |
| 36 | + ) as Record<string, unknown>; |
| 37 | + |
| 38 | + expect(settings['editor.defaultFormatter']).toBe('oxc.oxc-vscode'); |
| 39 | + expect(settings['oxc.fmt.configPath']).toBe('./vite.config.ts'); |
| 40 | + expect(settings['editor.formatOnSave']).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + it('writes zed settings that align formatter config with vite.config.ts', async () => { |
| 44 | + const projectRoot = createTempDir(); |
| 45 | + |
| 46 | + await writeEditorConfigs({ |
| 47 | + projectRoot, |
| 48 | + editorId: 'zed', |
| 49 | + interactive: false, |
| 50 | + silent: true, |
| 51 | + }); |
| 52 | + |
| 53 | + const settings = JSON.parse( |
| 54 | + fs.readFileSync(path.join(projectRoot, '.zed', 'settings.json'), 'utf8'), |
| 55 | + ) as { |
| 56 | + lsp?: { |
| 57 | + oxfmt?: { |
| 58 | + initialization_options?: { |
| 59 | + settings?: { |
| 60 | + configPath?: string; |
| 61 | + }; |
| 62 | + }; |
| 63 | + }; |
| 64 | + }; |
| 65 | + }; |
| 66 | + |
| 67 | + expect(settings.lsp?.oxfmt?.initialization_options?.settings?.configPath).toBe( |
| 68 | + './vite.config.ts', |
| 69 | + ); |
| 70 | + }); |
| 71 | +}); |
0 commit comments