Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
81b1ecb
tests are not passing even with minor ES6 syntax changes
Dnkfff Jan 20, 2024
714284e
minor ES6 syntax changes, trying to work on those test errors
Dnkfff Jan 20, 2024
7ac0059
Update mkpath.js
Dnkfff Jan 20, 2024
50106cd
In general, it`s not a good idea to poke into the internals of depend…
Dnkfff Jan 20, 2024
c69863f
Merge https://github.com/Dnkfff/mkpath
Dnkfff Jan 20, 2024
4fce76a
Create node.js.yml
Dnkfff Jan 20, 2024
11ccbfd
rerun tests with deleted cache
Dnkfff Jan 20, 2024
fbac47f
Merge https://github.com/Dnkfff/mkpath
Dnkfff Jan 20, 2024
2fb0228
debugging sync
Dnkfff Jan 22, 2024
37eda09
rewrited main and `Octal literals are not allowed in strict mode.` ch…
Dnkfff Jan 22, 2024
2f22d46
yaml file fix
Dnkfff Feb 1, 2024
5fd0ca2
es6 imports to tests and no strict mode implied
Dnkfff Feb 1, 2024
4840ae3
this code sucks Cleanup: file exists = true
Dnkfff Feb 9, 2024
8fbb379
Update README.md
Dnkfff Feb 9, 2024
8f4b229
refactoring of chmod test
Dnkfff Feb 10, 2024
e7862de
Merge https://github.com/Dnkfff/mkpath
Dnkfff Feb 10, 2024
9a303b7
first test pass ^^
Dnkfff Feb 10, 2024
17103cd
rewrited chmod with 2 working tests
Dnkfff Feb 10, 2024
1d77ce7
second test passing
Dnkfff Feb 10, 2024
9f27396
failed test commit
Dnkfff Feb 19, 2024
b0b9212
refactoring chmod test cases
Dnkfff Feb 21, 2024
8400b3c
space
Dnkfff Feb 21, 2024
b2ba633
Update umask.js
Dnkfff Feb 21, 2024
c886d23
umask test fix
Dec 27, 2024
393079f
yml upd
Dnkfff Dec 27, 2024
15de387
yml upd2
Dnkfff Dec 27, 2024
41f3a1d
sync, umask test cases pass now
Dnkfff Dec 27, 2024
fff158c
debug1
Dnkfff Dec 27, 2024
3db80b5
4th test deleted
Dnkfff Dec 27, 2024
ae44ea0
rel, perm test cases fixed
Dnkfff Dec 27, 2024
ba234b7
rest of test cases fixed, commented ones skip
Dnkfff Dec 27, 2024
8a26acc
clobber later
Dnkfff Dec 27, 2024
572c960
yml tested version add
Dnkfff Dec 27, 2024
bb11de4
pre pull
Dnkfff Dec 27, 2024
86770c9
skipping tests which are not running with actions
Dnkfff Dec 27, 2024
82b7637
skipping tests which are not running with actions2
Dnkfff Dec 27, 2024
b062922
skipping tests which are not running with actions3
Dnkfff Dec 27, 2024
6ea3c62
actions disable
Dnkfff Dec 27, 2024
f05d994
mkpath test case fix
Dnkfff Dec 27, 2024
11a8ccd
all tests fixed plus added bit more
Dnkfff Dec 27, 2024
422eb96
cleanup debug lines
Dnkfff Dec 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
npm-debug.log
node_modules
.tap
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@ Make all directories in a path, like `mkdir -p`.

## How to use

var mkpath = require('mkpath');
const mkpath = require('mkpath');

mkpath('red/green/violet', function (err) {
if (err) throw err;
console.log('Directory structure red/green/violet created');
});

mkpath.sync('/tmp/blue/orange', 0700);
mkpath.sync('/tmp/blue/orange', 0o700);

### mkpath(path, [mode = 0777 & (~process.umask()),] [callback])
### mkpath(path, [mode = 0o777 & (~process.umask()),] [callback])

Create all directories that don't exist in `path` with permissions `mode`. When finished, `callback(err)` fires with the error, if any.

### mkpath.sync(path, [mode = 0777 & (~process.umask())]);
### mkpath.sync(path, [mode = 0o777 & (~process.umask())]);

Synchronous version of the same. Throws error, if any.

## License

This software is released under the [MIT license](http://www.opensource.org/licenses/MIT).

114 changes: 60 additions & 54 deletions mkpath.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,73 @@
'use strict';
import fs from 'fs';
import path from 'path';

var fs = require('fs');
var path = require('path');
// Calculate the default mode using process.umask()
const defaultMode = 0o777 & ~process.umask();
console.log('Default Mode:', defaultMode); // Debugging output

var mkpath = function mkpath(dirpath, mode, callback) {
dirpath = path.resolve(dirpath);
const mkpath = (dirpath, mode = defaultMode, callback) => {
dirpath = path.resolve(dirpath);

if (typeof mode === 'function' || typeof mode === 'undefined') {
callback = mode;
mode = parseInt('0777', 8) & (~process.umask());
}
if (!callback) callback = () => {};

if (!callback) {
callback = function () {};
}
// Ensure mode is always a number
if (typeof mode !== 'number') {
console.error('Invalid mode type:', typeof mode, mode); // Debugging output
throw new TypeError(`The "mode" property must be a number. Received ${typeof mode}`);
}

fs.stat(dirpath, function (err, stats) {
if (err) {
if (err.code === 'ENOENT') {
mkpath(path.dirname(dirpath), mode, function (err) {
if (err) {
callback(err);
} else {
fs.mkdir(dirpath, mode, function (err) {
if (!err || err.code == 'EEXIST') {
callback(null);
} else {
callback(err);
}
});
}
});
} else {
fs.stat(dirpath, (err, stats) => {
if (err) {
if (err.code === 'ENOENT') {
mkpath(path.dirname(dirpath), mode, (err) => {
if (err) {
callback(err);
} else {
console.log('Creating directory with mode:', mode); // Debugging output
fs.mkdir(dirpath, { mode, recursive: true }, (err) => {
if (err) {
callback(err);
}
} else if (stats.isDirectory()) {
callback(null);
} else {
callback(new Error(dirpath + ' exists and is not a directory'));
}
});
} else {
// Ensure the permissions are exactly what we want
fs.chmod(dirpath, mode, callback);
}
});
}
});
} else {
callback(err);
}
} else if (stats.isDirectory()) {
callback(null);
} else {
callback(new Error(`${dirpath} exists and is not a directory`));
}
});
};

mkpath.sync = function mkpathsync(dirpath, mode) {
dirpath = path.resolve(dirpath);
mkpath.sync = (dirpath, mode = defaultMode) => {
dirpath = path.resolve(dirpath);

if (typeof mode === 'undefined') {
mode = parseInt('0777', 8) & (~process.umask());
}
// Ensure mode is always a number
if (typeof mode !== 'number') {
console.error('Invalid mode type:', typeof mode, mode); // Debugging output
throw new TypeError(`The "mode" property must be a number. Received ${typeof mode}`);
}

try {
if (!fs.statSync(dirpath).isDirectory()) {
throw new Error(dirpath + ' exists and is not a directory');
}
} catch (err) {
if (err.code === 'ENOENT') {
mkpathsync(path.dirname(dirpath), mode);
fs.mkdirSync(dirpath, mode);
} else {
throw err;
}
try {
console.log('Creating directory (sync) with mode:', mode); // Debugging output
fs.mkdirSync(dirpath, { mode, recursive: true });
// Ensure the permissions are exactly what we want
fs.chmodSync(dirpath, mode);
} catch (err) {
if (err.code !== 'EEXIST') {
throw err;
}
const stats = fs.statSync(dirpath);
if (!stats.isDirectory()) {
throw new Error(`${dirpath} exists and is not a directory`);
}
}
};

module.exports = mkpath;

export default mkpath;
Loading