Skip to content

Commit

Permalink
Require Node.js 18 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 2, 2024
1 parent 57aa7aa commit 120a81e
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 204 deletions.
4 changes: 0 additions & 4 deletions .github/funding.yml

This file was deleted.

12 changes: 2 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,14 @@ jobs:
node-version:
- 20
- 18
- 16
- 14
- 12
- 10
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- uses: codecov/codecov-action@v3
if: matrix.os == 'ubuntu-latest' && matrix.node-version == 14
with:
fail_ci_if_error: true
115 changes: 58 additions & 57 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,66 +1,67 @@
/// <reference types="node"/>
import * as fs from 'fs';
import type * as fs from 'node:fs';

declare namespace makeDir {
interface Options {
/**
Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/).
@default 0o777
*/
readonly mode?: number;

/**
Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs).
Using a custom `fs` implementation will block the use of the native `recursive` option if `fs.mkdir` or `fs.mkdirSync` is not the native function.
@default require('fs')
*/
readonly fs?: typeof fs;
}
}

declare const makeDir: {
export type Options = {
/**
Make a directory and its parents if needed - Think `mkdir -p`.
@param path - Directory to create.
@returns The path to the created directory.
@example
```
import makeDir = require('make-dir');
(async () => {
const path = await makeDir('unicorn/rainbow/cake');
console.log(path);
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
// Multiple directories:
const paths = await Promise.all([
makeDir('unicorn/rainbow'),
makeDir('foo/bar')
]);
console.log(paths);
// [
// '/Users/sindresorhus/fun/unicorn/rainbow',
// '/Users/sindresorhus/fun/foo/bar'
// ]
})();
```
The directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/).
@default 0o777
*/
(path: string, options?: makeDir.Options): Promise<string>;
readonly mode?: number;

/**
Synchronously make a directory and its parents if needed - Think `mkdir -p`.
Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs).
Using a custom `fs` implementation will block the use of the native `recursive` option if `fs.mkdir` or `fs.mkdirSync` is not the native function.
@param path - Directory to create.
@returns The path to the created directory.
Default: `import fs from 'node:fs'`
*/
sync(path: string, options?: makeDir.Options): string;
readonly fs?: typeof fs;
};

export = makeDir;
/**
Make a directory and its parents if needed - Think `mkdir -p`.
@param path - The directory to create.
@returns The path to the created directory.
@example
```
import {makeDirectory} from 'make-dir';
const path = await makeDirectory('unicorn/rainbow/cake');
console.log(path);
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
// Multiple directories:
const paths = await Promise.all([
makeDirectory('unicorn/rainbow'),
makeDirectory('foo/bar')
]);
console.log(paths);
// [
// '/Users/sindresorhus/fun/unicorn/rainbow',
// '/Users/sindresorhus/fun/foo/bar'
// ]
```
*/
export function makeDirectory(path: string, options?: Options): Promise<string>;

/**
Synchronously make a directory and its parents if needed - Think `mkdir -p`.
@param path - The directory to create.
@returns The path to the created directory.
@example
```
import {makeDirectorySync} from 'make-dir';
const path = makeDirectorySync('unicorn/rainbow/cake');
console.log(path);
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
```
*/
export function makeDirectorySync(path: string, options?: Options): string;
42 changes: 17 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
'use strict';
const fs = require('fs');
const path = require('path');
const {promisify} = require('util');
const semverGte = require('semver/functions/gte');

const useNativeRecursiveOption = semverGte(process.version, '10.12.0');
import process from 'node:process';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';

// https://github.com/nodejs/node/issues/8987
// https://github.com/libuv/libuv/pull/1088
Expand All @@ -23,12 +20,12 @@ const checkPath = pth => {
const processOptions = options => {
const defaults = {
mode: 0o777,
fs
fs,
};

return {
...defaults,
...options
...options,
};
};

Expand All @@ -43,27 +40,24 @@ const permissionError = pth => {
return error;
};

const makeDir = async (input, options) => {
export async function makeDirectory(input, options) {
checkPath(input);
options = processOptions(options);

const mkdir = promisify(options.fs.mkdir);
const stat = promisify(options.fs.stat);

if (useNativeRecursiveOption && options.fs.mkdir === fs.mkdir) {
if (options.fs.mkdir === fs.mkdir) {
const pth = path.resolve(input);

await mkdir(pth, {
await fsPromises.mkdir(pth, {
mode: options.mode,
recursive: true
recursive: true,
});

return pth;
}

const make = async pth => {
try {
await mkdir(pth, options.mode);
await fsPromises.mkdir(pth, options.mode);

return pth;
} catch (error) {
Expand All @@ -86,7 +80,7 @@ const makeDir = async (input, options) => {
}

try {
const stats = await stat(pth);
const stats = await fsPromises.stat(pth);
if (!stats.isDirectory()) {
throw new Error('The path is not a directory');
}
Expand All @@ -99,20 +93,18 @@ const makeDir = async (input, options) => {
};

return make(path.resolve(input));
};
}

module.exports = makeDir;

module.exports.sync = (input, options) => {
export function makeDirectorySync(input, options) {
checkPath(input);
options = processOptions(options);

if (useNativeRecursiveOption && options.fs.mkdirSync === fs.mkdirSync) {
if (options.fs.mkdirSync === fs.mkdirSync) {
const pth = path.resolve(input);

fs.mkdirSync(pth, {
mode: options.mode,
recursive: true
recursive: true,
});

return pth;
Expand Down Expand Up @@ -152,4 +144,4 @@ module.exports.sync = (input, options) => {
};

return make(path.resolve(input));
};
}
21 changes: 10 additions & 11 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import * as fs from 'node:fs';
import {expectType} from 'tsd';
import makeDir = require('.');
import {sync as makeDirSync} from '.';
import * as fs from 'fs';
import * as gfs from 'graceful-fs';
import {makeDirectory, makeDirectorySync} from './index.js';

// MakeDir
expectType<Promise<string>>(makeDir('path/to/somewhere'));
expectType<Promise<string>>(makeDirectory('path/to/somewhere'));

expectType<Promise<string>>(
makeDir('path/to/somewhere', {mode: 0o777})
makeDirectory('path/to/somewhere', {mode: 0o777}),
);
expectType<Promise<string>>(makeDir('path/to/somewhere', {fs}));
expectType<Promise<string>>(makeDir('path/to/somewhere', {fs: gfs}));
expectType<Promise<string>>(makeDirectory('path/to/somewhere', {fs}));
expectType<Promise<string>>(makeDirectory('path/to/somewhere', {fs: gfs}));

// MakeDir (sync)
expectType<string>(makeDirSync('path/to/somewhere'));
expectType<string>(makeDirectorySync('path/to/somewhere'));

expectType<string>(
makeDirSync('path/to/somewhere', {mode: 0o777})
makeDirectorySync('path/to/somewhere', {mode: 0o777}),
);
expectType<string>(makeDirSync('path/to/somewhere', {fs}));
expectType<string>(makeDirSync('path/to/somewhere', {fs: gfs}));
expectType<string>(makeDirectorySync('path/to/somewhere', {fs}));
expectType<string>(makeDirectorySync('path/to/somewhere', {fs: gfs}));
38 changes: 18 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"sideEffects": false,
"engines": {
"node": ">=10"
"node": ">=18"
},
"scripts": {
"test": "xo && nyc ava && tsd"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
Expand All @@ -39,25 +45,17 @@
"filesystem",
"file-system"
],
"dependencies": {
"semver": "^7.5.3"
},
"devDependencies": {
"@types/graceful-fs": "^4.1.3",
"@types/node": "^14.14.6",
"ava": "^2.4.0",
"codecov": "^3.2.0",
"graceful-fs": "^4.1.15",
"nyc": "^15.0.0",
"path-type": "^4.0.0",
"tempy": "^1.0.0",
"tsd": "^0.13.1",
"xo": "^0.34.2"
"@types/graceful-fs": "^4.1.9",
"@types/node": "^20.12.8",
"ava": "^6.1.2",
"graceful-fs": "^4.2.11",
"path-type": "^5.0.0",
"tempy": "^3.1.0",
"tsd": "^0.31.0",
"xo": "^0.58.0"
},
"nyc": {
"reporter": [
"text",
"lcov"
]
"ava": {
"workerThreads": false
}
}
Loading

0 comments on commit 120a81e

Please sign in to comment.