Skip to content

Commit

Permalink
Fetch fresh process.umask() on every method call (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Feb 12, 2020
1 parent 8632e52 commit dea15ae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
28 changes: 15 additions & 13 deletions index.js
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,19 @@ 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 +46,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 +102,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
@@ -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 dea15ae

Please sign in to comment.