Skip to content

Commit faacfda

Browse files
committed
Add function to get all envrionment names
1 parent e8ffe9b commit faacfda

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

packages/cli-kit/src/public/node/environments.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,47 @@ describe('loading environments', async () => {
132132
})
133133
})
134134
})
135+
136+
describe('getEnvironmentNames', () => {
137+
test('returns all environment names', async () => {
138+
await inTemporaryDirectory(async (tmpDir) => {
139+
// Given
140+
const filePath = joinPath(tmpDir, fileName)
141+
await writeFile(filePath, tomlEncode({environments: {environment1, environment2}}))
142+
143+
// When
144+
const names = await environments.getEnvironmentNames(fileName, {from: tmpDir})
145+
146+
// Then
147+
expect(names).toEqual(['environment1', 'environment2'])
148+
})
149+
})
150+
151+
describe('when no environment file exists', () => {
152+
test('returns empty array', async () => {
153+
await inTemporaryDirectory(async (tmpDir) => {
154+
// When
155+
const names = await environments.getEnvironmentNames(fileName, {from: tmpDir})
156+
157+
// Then
158+
expect(names).toEqual([])
159+
})
160+
})
161+
})
162+
163+
describe('when environment file is empty', () => {
164+
test('returns empty array', async () => {
165+
await inTemporaryDirectory(async (tmpDir) => {
166+
// Given
167+
const filePath = joinPath(tmpDir, fileName)
168+
await writeFile(filePath, '# no content')
169+
170+
// When
171+
const names = await environments.getEnvironmentNames(fileName, {from: tmpDir})
172+
173+
// Then
174+
expect(names).toEqual([])
175+
})
176+
})
177+
})
178+
})

packages/cli-kit/src/public/node/environments.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,19 @@ async function decodeEnvironments(fileName: string, options?: LoadEnvironmentOpt
9595

9696
return environments
9797
}
98+
99+
/**
100+
* Gets all available environment names from a file.
101+
* @param fileName - The file name to load environments from.
102+
* @param options - Optional configuration for loading.
103+
* @returns Array of environment names, or empty array if none found.
104+
*/
105+
export async function getEnvironmentNames(fileName: string, options?: LoadEnvironmentOptions): Promise<string[]> {
106+
const environments = await decodeEnvironments(fileName, options)
107+
108+
if (!environments) {
109+
return []
110+
}
111+
112+
return Object.keys(environments)
113+
}

0 commit comments

Comments
 (0)