Skip to content

Commit da713b0

Browse files
committed
Rename copyDirs to copyGlobs in buildCopy, add file path tests
Renames the local variable in buildCopy to copyGlobs to clarify that the array now contains either directory globs or file paths, not just dirs. Adds two tests: one asserting existing files are passed through as-is, and one asserting existing directories still get /** appended.
1 parent a9613c4 commit da713b0

2 files changed

Lines changed: 30 additions & 5 deletions

File tree

lib/build-copy/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ export async function buildCopy (_src, dest, _siteData, opts) {
4848
warnings: [],
4949
}
5050

51-
const copyDirs = await getCopyDirs(opts?.copy)
51+
const copyGlobs = await getCopyDirs(opts?.copy)
5252

53-
const copyTasks = copyDirs.map((copyDir) => {
54-
return copy(copyDir, dest)
53+
const copyTasks = copyGlobs.map((copyGlob) => {
54+
return copy(copyGlob, dest)
5555
})
5656

5757
const settled = await Promise.allSettled(copyTasks)
5858

5959
for (const [index, result] of Object.entries(settled)) {
6060
// @ts-expect-error
61-
const copyDir = copyDirs[index]
61+
const copyGlob = copyGlobs[index]
6262
if (result.status === 'rejected') {
6363
const buildError = new Error('Error copying copy folders', { cause: result.reason })
6464
results.errors.push(buildError)
6565
} else {
66-
results.report[copyDir] = result.value
66+
results.report[copyGlob] = result.value
6767
}
6868
}
6969
return results

lib/build-copy/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { test } from 'node:test'
22
import assert from 'node:assert'
3+
import { mkdtemp, writeFile, rm } from 'node:fs/promises'
4+
import { join } from 'node:path'
5+
import os from 'node:os'
36
import { getCopyDirs } from './index.js'
47

58
test.describe('build-copy', () => {
@@ -8,4 +11,26 @@ test.describe('build-copy', () => {
811

912
assert.deepStrictEqual(copyDirs, ['fixtures/**'])
1013
})
14+
15+
test('getCopyDirs returns file path as-is for existing files', async () => {
16+
const dir = await mkdtemp(join(os.tmpdir(), 'domstack-copy-test-'))
17+
const file = join(dir, 'sw.js')
18+
try {
19+
await writeFile(file, 'self.addEventListener("fetch", () => {})')
20+
const result = await getCopyDirs([file])
21+
assert.deepStrictEqual(result, [file])
22+
} finally {
23+
await rm(dir, { recursive: true, force: true })
24+
}
25+
})
26+
27+
test('getCopyDirs appends ** for existing directories', async () => {
28+
const dir = await mkdtemp(join(os.tmpdir(), 'domstack-copy-test-'))
29+
try {
30+
const result = await getCopyDirs([dir])
31+
assert.deepStrictEqual(result, [join(dir, '**')])
32+
} finally {
33+
await rm(dir, { recursive: true, force: true })
34+
}
35+
})
1136
})

0 commit comments

Comments
 (0)