Skip to content

feat: add fs/mkdir #2198

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
248 changes: 248 additions & 0 deletions lib/node_modules/@stdlib/fs/mkdir/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
<!--

@license Apache-2.0

Copyright (c) 2024 The Stdlib Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

# Create Directory

> Create a directory and any necessary subdirectories.

<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

<!-- run-disable -->

```javascript
var mkdir = require( '@stdlib/fs/mkdir' );
```

#### mkdir( path\[, options], clbk )

Asynchronously creates a directory and any necessary subdirectories.

```javascript
mkdir( './foo', onDir );

function onDir( error, path ) {
if ( error ) {
throw error;
} else {
console.log( path );
}
}
```

The function accepts the same `options` and has the same defaults as [`fs.mkdir()`][node-fs].

The `path` presents only if the `recursive` option is `true`, which is the first directory path created.

#### mkdir.sync( path\[, options] )

Synchronously creates a directory and any necessary subdirectories.

```javascript
var out = mkdir.sync( './foo' );
if ( out instanceof Error ) {
throw out;
}
console.log( out );
```

The function accepts the same `options` and has the same defaults as [`fs.mkdirSync()`][node-fs].

The function returns `undefined`, or if `recursive` option is `true`, the first directory path created.


</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- The difference between this API and [`fs.mkdirSync()`][node-fs] is that [`fs.mkdirSync()`][node-fs] will throw if an `error` is encountered (e.g., if given a existent `path`) and this API will return an `error`. Hence, the following anti-pattern

```javascript
var fs = require( 'fs' );

var dir = './foo';

// Check for existence to prevent an error being thrown...
if ( !fs.existsSync( dir ) ) {
fs.mkdirSync( dir );
}
```

can be replaced by an approach which addresses existence via `error` handling.

```javascript
var mkdir = require( '@stdlib/fs/mkdir' );

var dir = './foo';

// Explicitly handle the error...
var out = mkdir.sync( dir );
if ( out instanceof Error ) {
// You choose what to do...
console.error( out.message );
}
```

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

<!-- run-disable -->

```javascript
var mkdir = require( '@stdlib/fs/mkdir' );

var opts = {
'mode': parseInt( '0755', 8 ),
'recursive': true
};

/* Sync */

var out = mkdir.sync( './foo' );
// returns undefined

console.log( out instanceof Error );
// => false

out = mkdir.sync( './bar/baz', opts );
// returns <string> || undefined

console.log( out instanceof Error );
// => false

/* Async */

mkdir( './foo', onDir );
mkdir( './bar/baz', opts, onDir );

function onDir( error, path ) {
if ( error ) {
throw error;
}
console.log( path );
}
```

</section>

<!-- /.examples -->

* * *

<section class="cli">

## CLI

<section class="usage">

### Usage

```text
Usage: mkdir [options] <path>

Options:

-h, --help Print this message.
-V, --version Print the package version.
--mode mode Directory mode. Default: 0o777.
-p, --recursive Create parent directories.
```

</section>

<!-- /.usage -->

<section class="notes">

### Notes

- Relative file paths are resolved relative to the current working directory.
- Errors are written to `stderr`.
- File contents are written to `stdout`.

</section>

<!-- /.notes -->

<section class="examples">

### Examples

```bash
$ mkdir ./tmp
```

</section>

<!-- /.examples -->

</section>

<!-- /.cli -->

<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[node-fs]: https://nodejs.org/api/fs.html

<!-- <related-links> -->

<!-- </related-links> -->

</section>

<!-- /.links -->
124 changes: 124 additions & 0 deletions lib/node_modules/@stdlib/fs/mkdir/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var rmdirSync = require( 'fs' ).rmdirSync; // eslint-disable-line node/no-sync
var join = require( 'path' ).join;
var bench = require( '@stdlib/bench' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var pkg = require( './../package.json' ).name;
var mkdir = require( './../lib' );


// VARIABLES //

var DIR = join( __dirname, 'fixtures' );


// FUNCTIONS //

/**
* Removes the directory created during a benchmark.
*
* @private
* @param {string} dir - directory to remove
*/
function remove( dir ) {
var opts;

opts = {
'recursive': true,
'force': true
};
rmdirSync( dir, opts);
}


// MAIN //

bench( pkg, function benchmark( b ) {
var opts;
var dir;
var i;

opts = {
'mode': parseInt( '0755', 8 ),
'recursive': true
};
i = 0;
b.tic();

return next();

function next() {
i += 1;
if ( i <= b.iterations ) {
dir = DIR + '/' + i.toString();
return mkdir(dir, opts, onDir);
}
b.toc();
b.pass( 'benchmark finished' );

remove( dir );
b.end();
}

function onDir( error, path ) {
if ( error ) {
b.fail( error );
}
if ( !isString( path ) && path !== void 0) {
b.fail( 'should be a string or undefined' );
}
next();
}
});

bench( pkg+':sync', function benchmark( b ) {
var opts;
var out;
var dir;
var i;

opts = {
'mode': parseInt( '0755', 8 ),
'recursive': true
};

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
dir = DIR + '/' + i.toString();
out = mkdir.sync( dir, opts );
if ( out instanceof Error) {
b.fail( out );
}
if ( !isString( out ) && out !== void 0 ) {
b.fail( 'should be a string or undefined' );
}
remove( dir );
}
b.toc();
if ( out instanceof Error) {
b.fail( out );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading