From cfc010b40c2527e71a2c23b144464e695d806dc1 Mon Sep 17 00:00:00 2001 From: dittyroma <59260041+dittyroma@users.noreply.github.com> Date: Sun, 13 Jun 2021 00:48:19 -0600 Subject: [PATCH] Add `directoryMode` option (#14) Co-authored-by: Sindre Sorhus --- index.d.ts | 7 +++++++ index.js | 10 ++++++++-- index.test-d.ts | 22 +++++++++++++++++++++- readme.md | 7 +++++++ test/async.js | 10 ++++++++++ test/sync.js | 10 ++++++++++ 6 files changed, 63 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 45616d0..f46dab4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; } } diff --git a/index.js b/index.js index 09e31ac..6a3794c 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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); diff --git a/index.test-d.ts b/index.test-d.ts index 5e36281..4192ba3 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,4 +1,4 @@ -import {expectType} from 'tsd'; +import {expectError, expectType} from 'tsd'; import moveFile = require('.'); expectType>( @@ -7,6 +7,16 @@ expectType>( expectType>( moveFile('source/unicorn.png', 'destination/unicorn.png', {overwrite: false}) ); +expectType>( + moveFile('source/unicorn.png', 'destination/unicorn.png', { + directoryMode: 0o700 + }) +); +expectError( + await moveFile('source/unicorn.png', 'destination/unicorn.png', { + directoryMode: '700' + }) +); expectType( moveFile.sync('source/unicorn.png', 'destination/unicorn.png') ); @@ -15,3 +25,13 @@ expectType( overwrite: false }) ); +expectType( + moveFile.sync('source/unicorn.png', 'destination/unicorn.png', { + directoryMode: 0o700 + }) +); +expectError( + moveFile.sync('source/unicorn.png', 'destination/unicorn.png', { + directoryMode: '700' + }) +); diff --git a/readme.md b/readme.md index ad8f350..bee046e 100644 --- a/readme.md +++ b/readme.md @@ -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 diff --git a/test/async.js b/test/async.js index 2821278..69a1048 100644 --- a/test/async.js +++ b/test/async.js @@ -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); +}); diff --git a/test/sync.js b/test/sync.js index 18436c7..5894e57 100644 --- a/test/sync.js +++ b/test/sync.js @@ -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); +});