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

Fetch fresh process.umask() on every method call #19

Merged
merged 2 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 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,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
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()));
});