Skip to content

Commit

Permalink
Add directoryMode option (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
dittyroma and sindresorhus authored Jun 13, 2021
1 parent bbabd08 commit cfc010b
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 3 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ declare namespace moveFile {
@default true
*/
readonly overwrite?: boolean;

/**
[Permissions](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) for created directories.
@default 0o777
*/
readonly directoryMode?: number;
}
}

Expand Down
10 changes: 8 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ module.exports = async (source, destination, options) => {
throw new Error(`The destination file exists: ${destination}`);
}

await fsP.mkdir(path.dirname(destination), {recursive: true});
await fsP.mkdir(path.dirname(destination), {
recursive: true,
mode: options.directoryMode
});

try {
await fsP.rename(source, destination);
Expand Down Expand Up @@ -47,7 +50,10 @@ module.exports.sync = (source, destination, options) => {
throw new Error(`The destination file exists: ${destination}`);
}

fs.mkdirSync(path.dirname(destination), {recursive: true});
fs.mkdirSync(path.dirname(destination), {
recursive: true,
mode: options.directoryMode
});

try {
fs.renameSync(source, destination);
Expand Down
22 changes: 21 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import {expectError, expectType} from 'tsd';
import moveFile = require('.');

expectType<Promise<void>>(
Expand All @@ -7,6 +7,16 @@ expectType<Promise<void>>(
expectType<Promise<void>>(
moveFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false})
);
expectType<Promise<void>>(
moveFile('source/unicorn.png', 'destination/unicorn.png', {
directoryMode: 0o700
})
);
expectError(
await moveFile('source/unicorn.png', 'destination/unicorn.png', {
directoryMode: '700'
})
);
expectType<void>(
moveFile.sync('source/unicorn.png', 'destination/unicorn.png')
);
Expand All @@ -15,3 +25,13 @@ expectType<void>(
overwrite: false
})
);
expectType<void>(
moveFile.sync('source/unicorn.png', 'destination/unicorn.png', {
directoryMode: 0o700
})
);
expectError(
moveFile.sync('source/unicorn.png', 'destination/unicorn.png', {
directoryMode: '700'
})
);
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Default: `true`

Overwrite existing destination file.

##### directoryMode

Type: `number`\
Default: `0o777`

[Permissions](https://en.wikipedia.org/wiki/File-system_permissions#Numeric_notation) for created directories. Not supported on Windows.

## Related

- [move-file-cli](https://github.com/sindresorhus/move-file-cli) - CLI for this module
Expand Down
10 changes: 10 additions & 0 deletions test/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ test('overwrite option', async t => {
/The destination file exists/
);
});

test('directoryMode option', async t => {
const root = tempy.directory();
const directory = `${root}/dir`;
const destination = `${directory}/file`;
const directoryMode = 0o700;
await moveFile(tempWrite.sync(fixture), destination, {directoryMode});
const stat = fs.statSync(directory);
t.is(stat.mode & directoryMode, directoryMode);
});
10 changes: 10 additions & 0 deletions test/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ test('overwrite option', t => {
moveFile.sync(tempWrite.sync('x'), tempWrite.sync('y'), {overwrite: false});
}, /The destination file exists/);
});

test('directoryMode option', t => {
const root = tempy.directory();
const directory = `${root}/dir`;
const destination = `${directory}/file`;
const directoryMode = 0o700;
moveFile.sync(tempWrite.sync(fixture), destination, {directoryMode});
const stat = fs.statSync(directory);
t.is(stat.mode & directoryMode, directoryMode);
});

0 comments on commit cfc010b

Please sign in to comment.