diff --git a/index.js b/index.js index 3a062db..3bd4293 100644 --- a/index.js +++ b/index.js @@ -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), @@ -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`) } diff --git a/test-cases/constructor-copy-paths/index.test.js b/test-cases/constructor-copy-paths/index.test.js new file mode 100644 index 0000000..b43af32 --- /dev/null +++ b/test-cases/constructor-copy-paths/index.test.js @@ -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}"`) + } + }) +})