Skip to content

Commit bc48764

Browse files
use env var to specify config path
1 parent 07f9f4d commit bc48764

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

packages/vite-plugin-tempest/src/config.test.ts

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1+
import fs from 'node:fs'
2+
import path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
14
import { expect, test } from 'vitest'
25
import { loadTempestConfiguration } from './config'
36
import { mockTempestConfiguration } from './test-utils'
47

58
test('the configuration can be loaded', async () => {
6-
mockTempestConfiguration()
9+
const spy = mockTempestConfiguration()
710

8-
const config = await loadTempestConfiguration()
11+
try {
12+
const config = await loadTempestConfiguration()
913

10-
expect(config).toHaveProperty('build_directory')
11-
expect(config).toHaveProperty('bridge_file_name')
12-
expect(config).toHaveProperty('manifest')
13-
expect(config).toHaveProperty('entrypoints')
14+
expect(config).toHaveProperty('build_directory')
15+
expect(config).toHaveProperty('bridge_file_name')
16+
expect(config).toHaveProperty('manifest')
17+
expect(config).toHaveProperty('entrypoints')
18+
} finally {
19+
spy.mockRestore()
20+
}
1421
})
1522

1623
test('the configuration can be overriden', async () => {
@@ -28,3 +35,29 @@ test('the configuration can be overriden', async () => {
2835
expect(config).toHaveProperty('manifest')
2936
expect(config).toHaveProperty('entrypoints')
3037
})
38+
39+
test('the configuration can be loaded from a file path environment variable', async () => {
40+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
41+
const tempConfigPath = path.join(__dirname, 'tempest-vite.test.json')
42+
const mockConfig = {
43+
build_directory: 'build/from-file',
44+
bridge_file_name: 'test-bridge',
45+
manifest: 'test-manifest.json',
46+
entrypoints: ['resources/js/test.js'],
47+
}
48+
49+
try {
50+
fs.writeFileSync(tempConfigPath, JSON.stringify(mockConfig))
51+
process.env.TEMPEST_PLUGIN_CONFIGURATION_PATH = tempConfigPath
52+
53+
const config = await loadTempestConfiguration()
54+
55+
expect(config.build_directory).toBe('build/from-file')
56+
expect(config.bridge_file_name).toBe('test-bridge')
57+
expect(config.manifest).toBe('test-manifest.json')
58+
expect(config.entrypoints).toEqual(['resources/js/test.js'])
59+
} finally {
60+
fs.rmSync(tempConfigPath, { force: true })
61+
delete process.env.TEMPEST_PLUGIN_CONFIGURATION_PATH
62+
}
63+
})

packages/vite-plugin-tempest/src/config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
import { readFileSync } from 'node:fs'
2+
import { resolve } from 'node:path'
13
import type { TempestViteConfiguration } from './types'
24
import { exec, php } from './utils'
35

46
const TEMPEST_BIN = 'tempest'
57
const VITE_CONFIG_COMMAND = 'vite:config'
68

79
export async function loadTempestConfiguration(): Promise<TempestViteConfiguration> {
10+
const path = process.env.TEMPEST_PLUGIN_CONFIGURATION_PATH
11+
if (path) {
12+
try {
13+
const filePath = resolve(process.cwd(), path)
14+
const fileContent = readFileSync(filePath, 'utf-8')
15+
16+
return JSON.parse(fileContent)
17+
} catch (e) {
18+
console.error(`[vite-plugin-tempest] Error: Failed to read or parse the file at [${path}].`)
19+
20+
throw e
21+
}
22+
}
23+
824
try {
925
const override = process.env.TEMPEST_PLUGIN_CONFIGURATION_OVERRIDE
1026
if (override) {

0 commit comments

Comments
 (0)