From 8149a5e6567e04c644c8b8079d4bda899d428224 Mon Sep 17 00:00:00 2001 From: Yanis Benson Date: Sun, 1 Mar 2020 13:43:21 +0300 Subject: [PATCH] add relativePath --- index.d.ts | 8 +++++++- index.js | 6 +++--- test.js | 9 ++++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 6466572..3b056aa 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,10 +5,16 @@ declare namespace cpy { interface SourceFile { /** Resolved path to file. - @example `/tmp/foo.bar` + @example `/tmp/dir/foo.bar` */ readonly path: string, + /** + Relative path to file from `cwd`. + @example 'dir/foo.bar' if `cwd` was '/tmp' + */ + readonly relativePath: string, + /** Filename. @example `foo.bar` diff --git a/index.js b/index.js index 883ce74..80817ff 100644 --- a/index.js +++ b/index.js @@ -18,7 +18,7 @@ const defaultOptions = { class SourceFile { constructor(relativePath, path) { this.path = path; - Object.defineProperty(this, 'relativePath', {enumerable: false, value: relativePath}); + this.relativePath = relativePath; Object.freeze(this); } @@ -27,11 +27,11 @@ class SourceFile { } get nameWithoutExtension() { - return path.basename(this.relativePath, this.extension); + return path.basename(this.relativePath, path.extname(this.relativePath)); } get extension() { - return path.extname(this.relativePath); + return path.extname(this.relativePath).slice(1); } } diff --git a/test.js b/test.js index e374539..de7ca74 100644 --- a/test.js +++ b/test.js @@ -57,17 +57,18 @@ test('throws on invalid concurrency value', async t => { test('copy array of files with filter', async t => { await cpy(['license', 'package.json'], t.context.tmp, { filter: file => { - console.log(file); if (file.path.endsWith('/license')) { t.is(file.path, path.join(process.cwd(), 'license')); + t.is(file.relativePath, 'license'); t.is(file.name, 'license'); t.is(file.nameWithoutExtension, 'license'); t.is(file.extension, ''); } else if (file.path.endsWith('/package.json')) { t.is(file.path, path.join(process.cwd(), 'package.json')); + t.is(file.relativePath, 'package.json'); t.is(file.name, 'package.json'); t.is(file.nameWithoutExtension, 'package'); - t.is(file.extension, '.json'); + t.is(file.extension, 'json'); } return !file.path.endsWith('/license'); @@ -83,14 +84,16 @@ test('copy array of files with async filter', async t => { filter: async file => { if (file.path.endsWith('/license')) { t.is(file.path, path.join(process.cwd(), 'license')); + t.is(file.relativePath, 'license'); t.is(file.name, 'license'); t.is(file.nameWithoutExtension, 'license'); t.is(file.extension, ''); } else if (file.path.endsWith('/package.json')) { t.is(file.path, path.join(process.cwd(), 'package.json')); + t.is(file.relativePath, 'package.json'); t.is(file.name, 'package.json'); t.is(file.nameWithoutExtension, 'package'); - t.is(file.extension, '.json'); + t.is(file.extension, 'json'); } return !file.path.endsWith('/license');