Skip to content

Commit

Permalink
Fetch fresh process.umask() on every method call
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
RyanZim committed Feb 1, 2020
1 parent 9de6474 commit 47b9640
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ const path = require('path');
const {promisify} = require('util');
const semver = require('semver');

const defaults = {
mode: 0o777 & (~process.umask()),
fs
};

const useNativeRecursiveOption = semver.satisfies(process.version, '>=10.12.0');

// https://github.com/nodejs/node/issues/8987
Expand All @@ -25,6 +20,18 @@ const checkPath = pth => {
}
};

const processOptions = options => {
// https://github.com/sindresorhus/make-dir/issues/18
const defaults = {
mode: 0o777 & (~process.umask()),
fs
};
return {
...defaults,
...options
};
};

const permissionError = pth => {
// This replicates the exception of `fs.mkdir` with native the
// `recusive` option when run on an invalid drive under Windows.
Expand All @@ -38,10 +45,7 @@ const permissionError = pth => {

const makeDir = async (input, options) => {
checkPath(input);
options = {
...defaults,
...options
};
options = processOptions(options);

const mkdir = promisify(options.fs.mkdir);
const stat = promisify(options.fs.stat);
Expand Down Expand Up @@ -97,10 +101,7 @@ module.exports = makeDir;

module.exports.sync = (input, options) => {
checkPath(input);
options = {
...defaults,
...options
};
options = processOptions(options);

if (useNativeRecursiveOption && options.fs.mkdirSync === fs.mkdirSync) {
const pth = path.resolve(input);
Expand Down
19 changes: 19 additions & 0 deletions test/umask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import test from 'ava';
import {getFixture, assertDirectory} from './helpers/util';
import makeDir from '..';

test.before(() => {
process.umask(0);
});

test('async', async t => {
const dir = getFixture();
await makeDir(dir);
assertDirectory(t, dir, 0o777 & (~process.umask()));
});

test('sync', t => {
const dir = getFixture();
makeDir.sync(dir);
assertDirectory(t, dir, 0o777 & (~process.umask()));
});

0 comments on commit 47b9640

Please sign in to comment.