Skip to content

Commit

Permalink
changed path type, file name => filename
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanis Benson committed Sep 20, 2019
1 parent d1a7ecf commit 216a313
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 29 deletions.
11 changes: 3 additions & 8 deletions index.d.ts
Expand Up @@ -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,

Expand Down
18 changes: 9 additions & 9 deletions index.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
39 changes: 27 additions & 12 deletions test.js
Expand Up @@ -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');
}
});

Expand All @@ -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');
}
});

Expand Down

0 comments on commit 216a313

Please sign in to comment.