Skip to content

Commit 73d35d3

Browse files
committed
refactor: use fs/promises.copyFile instead of shell exec in createEnvFile
No child process needed for a simple file copy. Uses Node.js native copyFile which is faster and avoids shell buffering/quoting pitfalls.
1 parent 5a8a646 commit 73d35d3

2 files changed

Lines changed: 15 additions & 12 deletions

File tree

source/__tests__/operations/createEnvFile.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

3-
vi.mock('../../operations/exec.js', () => ({
4-
exec: vi.fn().mockResolvedValue(''),
5-
execFile: vi.fn().mockResolvedValue(''),
3+
vi.mock('node:fs/promises', () => ({
4+
copyFile: vi.fn().mockResolvedValue(undefined),
65
}))
76

8-
const { exec } = await import('../../operations/exec.js')
7+
const { copyFile } = await import('node:fs/promises')
98
const { createEnvFile } = await import('../../operations/createEnvFile.js')
109

1110
describe('createEnvFile', () => {
1211
beforeEach(() => {
1312
vi.clearAllMocks()
1413
})
1514

16-
it('copies .env.example to .env.local', async () => {
15+
it('copies .env.example to .env.local in the project folder', async () => {
1716
await createEnvFile('/project/my_app')
1817

19-
expect(exec).toHaveBeenCalledWith('cp .env.example .env.local', { cwd: '/project/my_app' })
18+
expect(copyFile).toHaveBeenCalledWith(
19+
'/project/my_app/.env.example',
20+
'/project/my_app/.env.local',
21+
)
2022
})
2123

22-
it('uses the provided project folder as cwd', async () => {
24+
it('uses the provided project folder for both paths', async () => {
2325
await createEnvFile('/other/path')
2426

25-
expect(exec).toHaveBeenCalledWith(expect.any(String), { cwd: '/other/path' })
27+
expect(copyFile).toHaveBeenCalledWith('/other/path/.env.example', '/other/path/.env.local')
2628
})
2729

28-
it('propagates errors from exec', async () => {
29-
vi.mocked(exec).mockRejectedValueOnce(new Error('.env.example not found'))
30+
it('propagates errors from copyFile', async () => {
31+
vi.mocked(copyFile).mockRejectedValueOnce(new Error('.env.example not found'))
3032

3133
await expect(createEnvFile('/project/my_app')).rejects.toThrow('.env.example not found')
3234
})

source/operations/createEnvFile.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { exec } from './exec.js'
1+
import { copyFile } from 'node:fs/promises'
2+
import { join } from 'node:path'
23

34
export async function createEnvFile(projectFolder: string): Promise<void> {
4-
await exec('cp .env.example .env.local', { cwd: projectFolder })
5+
await copyFile(join(projectFolder, '.env.example'), join(projectFolder, '.env.local'))
56
}

0 commit comments

Comments
 (0)