Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ export class DomStack {
this.#src = src
this.#dest = dest

const copyDirs = opts?.copy ?? []
const copyDirs = (opts?.copy ?? []).map(dir => resolve(dir))

this.opts = {
...opts,
copy: copyDirs,
ignore: [
...DEFAULT_IGNORES,
basename(dest),
Expand All @@ -172,12 +173,11 @@ export class DomStack {
],
}

if (copyDirs && copyDirs.length > 0) {
if (copyDirs.length > 0) {
const absDest = resolve(this.#dest)
for (const copyDir of copyDirs) {
// Copy dirs can be in the src dir (nested builds), but not in the dest dir.
const absCopyDir = resolve(copyDir)
const relToDest = relative(absDest, absCopyDir)
const relToDest = relative(absDest, copyDir)
if (relToDest === '' || !relToDest.startsWith('..')) {
throw new Error(`copyDir ${copyDir} is within the dest directory`)
}
Expand Down
41 changes: 41 additions & 0 deletions test-cases/constructor-copy-paths/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { test } from 'node:test'
import assert from 'node:assert'
import { isAbsolute, resolve, join } from 'node:path'
import { tmpdir } from 'node:os'
import { DomStack } from '../../index.js'

const tmpSrc = join(tmpdir(), 'domstack-test-src')
const tmpDest = join(tmpdir(), 'domstack-test-dest')

test.describe('DomStack constructor - copy path resolution', () => {
test('resolves a relative copy path to an absolute path', () => {
const ds = new DomStack(tmpSrc, tmpDest, {
copy: ['some-relative-copy-dir'],
})

assert.strictEqual(ds.opts.copy.length, 1, 'one copy entry')
const copyPath = /** @type {string} */ (ds.opts.copy[0])
assert.ok(isAbsolute(copyPath), `copy path should be absolute, got: "${copyPath}"`)
})

test('leaves an already-absolute copy path normalized', () => {
const absPath = join(tmpdir(), 'absolute', 'copy', 'dir')
const ds = new DomStack(tmpSrc, tmpDest, {
copy: [absPath],
})

assert.strictEqual(ds.opts.copy[0], resolve(absPath), 'absolute path is preserved and normalized')
})

test('resolves multiple mixed copy paths', () => {
const absPath = join(tmpdir(), 'absolute', 'dir')
const ds = new DomStack(tmpSrc, tmpDest, {
copy: ['relative-dir', absPath],
})

assert.strictEqual(ds.opts.copy.length, 2, 'two copy entries')
for (const p of ds.opts.copy) {
assert.ok(isAbsolute(p), `each copy path should be absolute, got: "${p}"`)
}
})
})