Skip to content

Commit

Permalink
add relativePath
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanis Benson committed Mar 1, 2020
1 parent 27177d5 commit 8149a5e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
8 changes: 7 additions & 1 deletion index.d.ts
Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions index.js
Expand Up @@ -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);
}

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

Expand Down
9 changes: 6 additions & 3 deletions test.js
Expand Up @@ -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');
Expand All @@ -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');
Expand Down

0 comments on commit 8149a5e

Please sign in to comment.