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

ESM support #974

Merged
merged 3 commits into from
Nov 28, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Installation
Usage
-----

### CommonJS

`fs-extra` is a drop in replacement for native `fs`. All methods in `fs` are attached to `fs-extra`. All `fs` methods return promises if the callback isn't passed.

You don't ever need to include the original `fs` module again:
Expand Down Expand Up @@ -55,6 +57,31 @@ const fs = require('fs')
const fse = require('fs-extra')
```

### ESM

There is also an `fs-extra/esm` import, that supports both default and named exports. However, note that `fs` methods are not included in `fs-extra/esm`; you still need to import `fs` and/or `fs/promises` seperately:

```js
import { readFileSync } from 'fs'
import { readFile } from 'fs/promises'
import { outputFile, outputFileSync } from 'fs-extra/esm'
```

Default exports are supported:

```js
import fs from 'fs'
import fse from 'fs-extra/esm'
// fse.readFileSync is not a function; must use fs.readFileSync
```

but you probably want to just use regular `fs-extra` instead of `fs-extra/esm` for default exports:

```js
import fs from 'fs-extra'
// both fs and fs-extra methods are defined
```

Sync vs Async vs Async/Await
-------------
Most methods are async by default. All async methods will return a promise if the callback isn't passed.
Expand Down Expand Up @@ -197,7 +224,8 @@ fs-extra contains hundreds of tests.

- `npm run lint`: runs the linter ([standard](http://standardjs.com/))
- `npm run unit`: runs the unit tests
- `npm test`: runs both the linter and the tests
- `npm run unit-esm`: runs tests for `fs-extra/esm` exports
- `npm test`: runs the linter and all tests


### Windows
Expand Down
2 changes: 1 addition & 1 deletion lib/copy/__tests__/ncp/broken-symlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../../..')
const ncp = require('../../copy')
const path = require('path')
const assert = require('assert')
Expand Down
2 changes: 1 addition & 1 deletion lib/copy/__tests__/ncp/ncp-error-perm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../../..')
const ncp = require('../../copy')
const path = require('path')
const assert = require('assert')
Expand Down
2 changes: 1 addition & 1 deletion lib/copy/__tests__/ncp/symlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../../..')
const ncp = require('../../copy')
const path = require('path')
const assert = require('assert')
Expand Down
2 changes: 1 addition & 1 deletion lib/empty/__tests__/empty-dir-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/empty/__tests__/empty-dir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/ensure/__tests__/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/ensure/__tests__/ensure.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/ensure/__tests__/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CWD = process.cwd()

const fs = require('graceful-fs')
const os = require('os')
const fse = require(CWD)
const fse = require('../..')
const path = require('path')
const assert = require('assert')
const ensureLink = fse.ensureLink
Expand Down
2 changes: 1 addition & 1 deletion lib/ensure/__tests__/symlink-paths.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CWD = process.cwd()

const fs = require('graceful-fs')
const os = require('os')
const fse = require(CWD)
const fse = require('../..')
const path = require('path')
const assert = require('assert')
const _symlinkPaths = require('../symlink-paths')
Expand Down
2 changes: 1 addition & 1 deletion lib/ensure/__tests__/symlink-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CWD = process.cwd()

const fs = require('graceful-fs')
const os = require('os')
const fse = require(CWD)
const fse = require('../..')
const path = require('path')
const assert = require('assert')
const _symlinkType = require('../symlink-type')
Expand Down
2 changes: 1 addition & 1 deletion lib/ensure/__tests__/symlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CWD = process.cwd()

const fs = require('graceful-fs')
const os = require('os')
const fse = require(CWD)
const fse = require('../..')
const path = require('path')
const assert = require('assert')
const _symlinkPaths = require('../symlink-paths')
Expand Down
68 changes: 68 additions & 0 deletions lib/esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import _copy from './copy/index.js'
import _empty from './empty/index.js'
import _ensure from './ensure/index.js'
import _json from './json/index.js'
import _mkdirs from './mkdirs/index.js'
import _move from './move/index.js'
import _outputFile from './output-file/index.js'
import _pathExists from './path-exists/index.js'
import _remove from './remove/index.js'

// NOTE: Only exports fs-extra's functions; fs functions must be imported from "node:fs" or "node:fs/promises"

export const copy = _copy.copy
export const copySync = _copy.copySync
export const emptyDirSync = _empty.emptyDirSync
export const emptydirSync = _empty.emptydirSync
export const emptyDir = _empty.emptyDir
export const emptydir = _empty.emptydir
export const createFile = _ensure.createFile
export const createFileSync = _ensure.createFileSync
export const ensureFile = _ensure.ensureFile
export const ensureFileSync = _ensure.ensureFileSync
export const createLink = _ensure.createLink
export const createLinkSync = _ensure.createLinkSync
export const ensureLink = _ensure.ensureLink
export const ensureLinkSync = _ensure.ensureLinkSync
export const createSymlink = _ensure.createSymlink
export const createSymlinkSync = _ensure.createSymlinkSync
export const ensureSymlink = _ensure.ensureSymlink
export const ensureSymlinkSync = _ensure.ensureSymlinkSync
export const readJson = _json.readJson
export const readJSON = _json.readJSON
export const readJsonSync = _json.readJsonSync
export const readJSONSync = _json.readJSONSync
export const writeJson = _json.writeJson
export const writeJSON = _json.writeJSON
export const writeJsonSync = _json.writeJsonSync
export const writeJSONSync = _json.writeJSONSync
export const outputJson = _json.outputJson
export const outputJSON = _json.outputJSON
export const outputJsonSync = _json.outputJsonSync
export const outputJSONSync = _json.outputJSONSync
export const mkdirs = _mkdirs.mkdirs
export const mkdirsSync = _mkdirs.mkdirsSync
export const mkdirp = _mkdirs.mkdirp
export const mkdirpSync = _mkdirs.mkdirpSync
export const ensureDir = _mkdirs.ensureDir
export const ensureDirSync = _mkdirs.ensureDirSync
export const move = _move.move
export const moveSync = _move.moveSync
export const outputFile = _outputFile.outputFile
export const outputFileSync = _outputFile.outputFileSync
export const pathExists = _pathExists.pathExists
export const pathExistsSync = _pathExists.pathExistsSync
export const remove = _remove.remove
export const removeSync = _remove.removeSync

export default {
..._copy,
..._empty,
..._ensure,
..._json,
..._mkdirs,
..._move,
..._outputFile,
..._pathExists,
..._remove
}
RyanZim marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion lib/fs/__tests__/realpath.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('realpath.native does not exist', () => {

const realpathNativeBackup = fs.realpath.native
const clearFseCache = () => {
const fsePath = path.dirname(require.resolve('../../..'))
const fsePath = path.dirname(require.resolve('../..'))
for (const entry in require.cache) {
if (entry.startsWith(fsePath)) {
delete require.cache[entry]
Expand Down
2 changes: 1 addition & 1 deletion lib/json/__tests__/jsonfile-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/json/__tests__/output-json-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/json/__tests__/output-json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/json/__tests__/promise-support.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/json/__tests__/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/clobber.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/issue-209.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const assert = require('assert')
const fse = require(process.cwd())
const fse = require('../..')

/* global describe, it */

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/issue-93.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')
const util = require('util')
Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/mkdir.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/mkdirp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/opts-undef.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/perm_sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/race.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/rel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const CWD = process.cwd()

const fs = require('fs')
const os = require('os')
const fse = require(CWD)
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/mkdirs/__tests__/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/move/__tests__/move-sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// const fs = require('graceful-fs')
const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/output-file/__tests__/output.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs')
const os = require('os')
const fse = require(process.cwd())
const fse = require('../..')
const path = require('path')
const assert = require('assert')

Expand Down
2 changes: 1 addition & 1 deletion lib/path-exists/__tests__/path-exists-sync.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
/* eslint-env mocha */

const fs = require(process.cwd())
const fs = require('../..')
const path = require('path')
const os = require('os')
const assert = require('assert')
Expand Down
2 changes: 1 addition & 1 deletion lib/path-exists/__tests__/path-exists.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'
/* eslint-env mocha */

const fs = require(process.cwd())
const fs = require('../..')
const path = require('path')
const os = require('os')
const assert = require('assert')
Expand Down
Loading