Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cwd option #46

Merged
merged 11 commits into from
May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ declare namespace cpFile {
@default 0o777
*/
readonly directoryMode?: number;

/**
Base directory to find the source file and copy it to this directory.

`cwd` works by being prepended with source, destination path.
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

@default process.cwd()
*/
readonly cwd?: string;
}

interface ProgressData {
Expand Down
34 changes: 26 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,25 @@ const cpFileAsync = async (source, destination, options, progressEmitter) => {
}
};

const cpFile = (sourcePath, destinationPath, options) => {
const resolvePath = (cwd, sourcePath, destinationPath) => {
sourcePath = path.resolve(cwd, sourcePath);
destinationPath = path.resolve(cwd, destinationPath);

return {
sourcePath,
destinationPath
};
};

const cpFile = (sourcePath, destinationPath, options = {}) => {
if (!sourcePath || !destinationPath) {
return Promise.reject(new CpFileError('`source` and `destination` required'));
}

if (options && options.cwd) {
({sourcePath, destinationPath} = resolvePath(options.cwd, sourcePath, destinationPath));
}

options = {
overwrite: true,
...options
Expand Down Expand Up @@ -85,23 +99,27 @@ const checkSourceIsFile = (stat, source) => {
}
};

module.exports.sync = (source, destination, options) => {
if (!source || !destination) {
module.exports.sync = (sourcePath, destinationPath, options = {}) => {
if (!sourcePath || !destinationPath) {
throw new CpFileError('`source` and `destination` required');
}

if (options && options.cwd) {
jopemachine marked this conversation as resolved.
Show resolved Hide resolved
({sourcePath, destinationPath} = resolvePath(options.cwd, sourcePath, destinationPath));
}

options = {
overwrite: true,
...options
};

const stat = fs.statSync(source);
checkSourceIsFile(stat, source);
fs.makeDirSync(path.dirname(destination), {mode: options.directoryMode});
const stat = fs.statSync(sourcePath);
checkSourceIsFile(stat, sourcePath);
fs.makeDirSync(path.dirname(destinationPath), {mode: options.directoryMode});

const flags = options.overwrite ? null : fsConstants.COPYFILE_EXCL;
try {
fs.copyFileSync(source, destination, flags);
fs.copyFileSync(sourcePath, destinationPath, flags);
} catch (error) {
if (!options.overwrite && error.code === 'EEXIST') {
return;
Expand All @@ -110,5 +128,5 @@ module.exports.sync = (source, destination, options) => {
throw error;
}

fs.utimesSync(destination, stat.atime, stat.mtime);
fs.utimesSync(destinationPath, stat.atime, stat.mtime);
};
9 changes: 9 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ Default: `true`

Overwrite existing destination file.

##### cwd

Type: `string`\
Default: `process.cwd()`

Base directory when you use relative path to the source, destination paths.

`cp-file` finds the source file from this path, and copy it to this directory.
jopemachine marked this conversation as resolved.
Show resolved Hide resolved

sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
##### directoryMode

Type: `number`\
Expand Down
9 changes: 9 additions & 0 deletions test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,12 @@ test.serial('rethrow read after open errors', async t => {

Object.assign(fs, {createWriteStream, createReadStream});
});

test('cwd option', async t => {
const error = await t.throwsAsync(cpFile('sync.js', t.context.destination));

t.is(error.name, 'CpFileError');
t.is(error.code, 'ENOENT');

await t.notThrowsAsync(cpFile('sync.js', t.context.destination, {cwd: 'test'}));
});
13 changes: 13 additions & 0 deletions test/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,16 @@ test('rethrow utimes errors', t => {

fs.utimesSync.restore();
});

test('cwd option', t => {
const error = t.throws(() => {
cpFile.sync('sync.js', t.context.destination);
});

t.is(error.name, 'CpFileError');
t.is(error.code, 'ENOENT');

t.notThrows(() => {
cpFile.sync('sync.js', t.context.destination, {cwd: 'test'});
});
});