From 216a313e6b9d2bf6a53cfdcfe7d42d4a0bec484c Mon Sep 17 00:00:00 2001 From: Yanis Benson Date: Fri, 20 Sep 2019 18:02:12 +0300 Subject: [PATCH] changed path type, file name => filename --- index.d.ts | 11 +++-------- index.js | 18 +++++++++--------- test.js | 39 +++++++++++++++++++++++++++------------ 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/index.d.ts b/index.d.ts index 04cc66f..9a8153b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,23 +3,18 @@ import {Options as CpFileOptions} from 'cp-file'; declare namespace cpy { interface SourceFile { - /** - Path to file. - */ - readonly path: string, - /** Resolved path to file. */ - readonly resolvedPath: string, + readonly path: string, /** - File name. + Filename. */ readonly name: string, /** - File name without extension. + Filename without extension. */ readonly nameWithoutExtension: string, diff --git a/index.js b/index.js index 278d0c4..6242492 100644 --- a/index.js +++ b/index.js @@ -8,26 +8,26 @@ const pFilter = require('p-filter'); const CpyError = require('./cpy-error'); class SourceFile { - constructor(path, resolvedPath) { + constructor(relativePath, path) { this.path = path; - this.resolvedPath = resolvedPath; + Object.defineProperty(this, 'relativePath', {enumerable: false, value: relativePath}); Object.freeze(this); } get name() { - return path.basename(this.path); + return path.basename(this.relativePath); } get nameWithoutExtension() { - return path.basename(this.path, this.extension); + return path.basename(this.relativePath, this.extension); } get extension() { - return path.extname(this.path); + return path.extname(this.relativePath); } } -const preprocessSourcePath = (source, options) => options.cwd ? path.resolve(options.cwd, source) : source; +const preprocessSourcePath = (source, options) => path.resolve(options.cwd ? options.cwd : process.cwd(), source); const preprocessDestinationPath = (source, destination, options) => { let basename = path.basename(source); @@ -113,11 +113,11 @@ module.exports = (source, destination, options = {}) => { }; return Promise.all(sources.map(async source => { - const to = preprocessDestinationPath(source.path, destination, options); + const to = preprocessDestinationPath(source.relativePath, destination, options); try { - await cpFile(source.resolvedPath, to, options).on('progress', fileProgressHandler); + await cpFile(source.path, to, options).on('progress', fileProgressHandler); } catch (error) { - throw new CpyError(`Cannot copy from \`${source.path}\` to \`${to}\`: ${error.message}`, error); + throw new CpyError(`Cannot copy from \`${source.relativePath}\` to \`${to}\`: ${error.message}`, error); } return to; diff --git a/test.js b/test.js index 4711827..26a88d0 100644 --- a/test.js +++ b/test.js @@ -52,12 +52,20 @@ test('copy array of files', async t => { test('copy array of files with filter', async t => { await cpy(['license', 'package.json'], t.context.tmp, { filter: file => { - t.is(typeof file.path, 'string'); - t.is(typeof file.resolvedPath, 'string'); - t.is(typeof file.name, 'string'); - t.is(typeof file.nameWithoutExtension, 'string'); - t.is(typeof file.extension, 'string'); - return file.path !== 'license'; + console.log(file); + if (file.path.endsWith('/license')) { + t.is(file.path, path.join(process.cwd(), '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.name, 'package.json'); + t.is(file.nameWithoutExtension, 'package'); + t.is(file.extension, '.json'); + } + + return !file.path.endsWith('/license'); } }); @@ -68,12 +76,19 @@ test('copy array of files with filter', async t => { test('copy array of files with async filter', async t => { await cpy(['license', 'package.json'], t.context.tmp, { filter: async file => { - t.is(typeof file.path, 'string'); - t.is(typeof file.resolvedPath, 'string'); - t.is(typeof file.name, 'string'); - t.is(typeof file.nameWithoutExtension, 'string'); - t.is(typeof file.extension, 'string'); - return file.path !== 'license'; + if (file.path.endsWith('/license')) { + t.is(file.path, path.join(process.cwd(), '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.name, 'package.json'); + t.is(file.nameWithoutExtension, 'package'); + t.is(file.extension, '.json'); + } + + return !file.path.endsWith('/license'); } });