From 869175895aedd058cfc24881ce9f93add5940c45 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Mon, 24 Oct 2022 15:06:10 -0400 Subject: [PATCH 1/3] Remove process.cwd() trick from test files --- lib/copy/__tests__/ncp/broken-symlink.test.js | 2 +- lib/copy/__tests__/ncp/ncp-error-perm.test.js | 2 +- lib/copy/__tests__/ncp/symlink.test.js | 2 +- lib/empty/__tests__/empty-dir-sync.test.js | 2 +- lib/empty/__tests__/empty-dir.test.js | 2 +- lib/ensure/__tests__/create.test.js | 2 +- lib/ensure/__tests__/ensure.test.js | 2 +- lib/ensure/__tests__/link.test.js | 2 +- lib/ensure/__tests__/symlink-paths.test.js | 2 +- lib/ensure/__tests__/symlink-type.test.js | 2 +- lib/ensure/__tests__/symlink.test.js | 2 +- lib/fs/__tests__/realpath.test.js | 2 +- lib/json/__tests__/jsonfile-integration.test.js | 2 +- lib/json/__tests__/output-json-sync.test.js | 2 +- lib/json/__tests__/output-json.test.js | 2 +- lib/json/__tests__/promise-support.test.js | 2 +- lib/json/__tests__/read.test.js | 2 +- lib/mkdirs/__tests__/clobber.test.js | 2 +- lib/mkdirs/__tests__/issue-209.test.js | 2 +- lib/mkdirs/__tests__/issue-93.test.js | 2 +- lib/mkdirs/__tests__/mkdir.test.js | 2 +- lib/mkdirs/__tests__/mkdirp.test.js | 2 +- lib/mkdirs/__tests__/opts-undef.test.js | 2 +- lib/mkdirs/__tests__/perm_sync.test.js | 2 +- lib/mkdirs/__tests__/race.test.js | 2 +- lib/mkdirs/__tests__/rel.test.js | 2 +- lib/mkdirs/__tests__/sync.test.js | 2 +- lib/move/__tests__/move-sync.test.js | 2 +- lib/output-file/__tests__/output.test.js | 2 +- lib/path-exists/__tests__/path-exists-sync.test.js | 2 +- lib/path-exists/__tests__/path-exists.test.js | 2 +- lib/remove/__tests__/remove-dir.test.js | 2 +- lib/remove/__tests__/remove-sync-dir.test.js | 2 +- lib/remove/__tests__/remove-sync-file.test.js | 2 +- lib/remove/__tests__/remove.test.js | 2 +- lib/util/__tests__/stat.test.js | 2 +- lib/util/__tests__/utimes.test.js | 2 +- 37 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/copy/__tests__/ncp/broken-symlink.test.js b/lib/copy/__tests__/ncp/broken-symlink.test.js index ba398084b..956daa862 100644 --- a/lib/copy/__tests__/ncp/broken-symlink.test.js +++ b/lib/copy/__tests__/ncp/broken-symlink.test.js @@ -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') diff --git a/lib/copy/__tests__/ncp/ncp-error-perm.test.js b/lib/copy/__tests__/ncp/ncp-error-perm.test.js index b3c2d9e8b..34b6af1a6 100644 --- a/lib/copy/__tests__/ncp/ncp-error-perm.test.js +++ b/lib/copy/__tests__/ncp/ncp-error-perm.test.js @@ -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') diff --git a/lib/copy/__tests__/ncp/symlink.test.js b/lib/copy/__tests__/ncp/symlink.test.js index dc1d3073a..5c188b6fe 100644 --- a/lib/copy/__tests__/ncp/symlink.test.js +++ b/lib/copy/__tests__/ncp/symlink.test.js @@ -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') diff --git a/lib/empty/__tests__/empty-dir-sync.test.js b/lib/empty/__tests__/empty-dir-sync.test.js index 92488e6a2..88387b3cf 100644 --- a/lib/empty/__tests__/empty-dir-sync.test.js +++ b/lib/empty/__tests__/empty-dir-sync.test.js @@ -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') diff --git a/lib/empty/__tests__/empty-dir.test.js b/lib/empty/__tests__/empty-dir.test.js index d00c5d976..f7dcf28ff 100644 --- a/lib/empty/__tests__/empty-dir.test.js +++ b/lib/empty/__tests__/empty-dir.test.js @@ -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') diff --git a/lib/ensure/__tests__/create.test.js b/lib/ensure/__tests__/create.test.js index 44803326f..352159846 100644 --- a/lib/ensure/__tests__/create.test.js +++ b/lib/ensure/__tests__/create.test.js @@ -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') diff --git a/lib/ensure/__tests__/ensure.test.js b/lib/ensure/__tests__/ensure.test.js index 71af227f5..fb883d7d3 100644 --- a/lib/ensure/__tests__/ensure.test.js +++ b/lib/ensure/__tests__/ensure.test.js @@ -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') diff --git a/lib/ensure/__tests__/link.test.js b/lib/ensure/__tests__/link.test.js index 83dc869b0..7557e2797 100644 --- a/lib/ensure/__tests__/link.test.js +++ b/lib/ensure/__tests__/link.test.js @@ -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 diff --git a/lib/ensure/__tests__/symlink-paths.test.js b/lib/ensure/__tests__/symlink-paths.test.js index 8db4e858f..cdaab7d4c 100644 --- a/lib/ensure/__tests__/symlink-paths.test.js +++ b/lib/ensure/__tests__/symlink-paths.test.js @@ -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') diff --git a/lib/ensure/__tests__/symlink-type.test.js b/lib/ensure/__tests__/symlink-type.test.js index 7d6f450ad..7a854f1b4 100644 --- a/lib/ensure/__tests__/symlink-type.test.js +++ b/lib/ensure/__tests__/symlink-type.test.js @@ -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') diff --git a/lib/ensure/__tests__/symlink.test.js b/lib/ensure/__tests__/symlink.test.js index 5292904d0..413158ff4 100644 --- a/lib/ensure/__tests__/symlink.test.js +++ b/lib/ensure/__tests__/symlink.test.js @@ -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') diff --git a/lib/fs/__tests__/realpath.test.js b/lib/fs/__tests__/realpath.test.js index b03249b2a..f170841a0 100644 --- a/lib/fs/__tests__/realpath.test.js +++ b/lib/fs/__tests__/realpath.test.js @@ -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] diff --git a/lib/json/__tests__/jsonfile-integration.test.js b/lib/json/__tests__/jsonfile-integration.test.js index 15abe9fa8..e94059036 100644 --- a/lib/json/__tests__/jsonfile-integration.test.js +++ b/lib/json/__tests__/jsonfile-integration.test.js @@ -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') diff --git a/lib/json/__tests__/output-json-sync.test.js b/lib/json/__tests__/output-json-sync.test.js index 935a824fa..08e9da286 100644 --- a/lib/json/__tests__/output-json-sync.test.js +++ b/lib/json/__tests__/output-json-sync.test.js @@ -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') diff --git a/lib/json/__tests__/output-json.test.js b/lib/json/__tests__/output-json.test.js index 6a5de69f8..5aa3288a2 100644 --- a/lib/json/__tests__/output-json.test.js +++ b/lib/json/__tests__/output-json.test.js @@ -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') diff --git a/lib/json/__tests__/promise-support.test.js b/lib/json/__tests__/promise-support.test.js index f688f06c7..11ed11bbc 100644 --- a/lib/json/__tests__/promise-support.test.js +++ b/lib/json/__tests__/promise-support.test.js @@ -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') diff --git a/lib/json/__tests__/read.test.js b/lib/json/__tests__/read.test.js index f26f221c8..0b54c5b20 100644 --- a/lib/json/__tests__/read.test.js +++ b/lib/json/__tests__/read.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/clobber.test.js b/lib/mkdirs/__tests__/clobber.test.js index be4ab1016..e84020100 100644 --- a/lib/mkdirs/__tests__/clobber.test.js +++ b/lib/mkdirs/__tests__/clobber.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/issue-209.test.js b/lib/mkdirs/__tests__/issue-209.test.js index bfe400314..4169a860f 100644 --- a/lib/mkdirs/__tests__/issue-209.test.js +++ b/lib/mkdirs/__tests__/issue-209.test.js @@ -1,7 +1,7 @@ 'use strict' const assert = require('assert') -const fse = require(process.cwd()) +const fse = require('../..') /* global describe, it */ diff --git a/lib/mkdirs/__tests__/issue-93.test.js b/lib/mkdirs/__tests__/issue-93.test.js index ec0270b82..229502b8c 100644 --- a/lib/mkdirs/__tests__/issue-93.test.js +++ b/lib/mkdirs/__tests__/issue-93.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/mkdir.test.js b/lib/mkdirs/__tests__/mkdir.test.js index 35cb7e67b..bf727d15a 100644 --- a/lib/mkdirs/__tests__/mkdir.test.js +++ b/lib/mkdirs/__tests__/mkdir.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/mkdirp.test.js b/lib/mkdirs/__tests__/mkdirp.test.js index 2af572a3a..baa75db6b 100644 --- a/lib/mkdirs/__tests__/mkdirp.test.js +++ b/lib/mkdirs/__tests__/mkdirp.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/opts-undef.test.js b/lib/mkdirs/__tests__/opts-undef.test.js index d5bd0744e..8ac8adc24 100644 --- a/lib/mkdirs/__tests__/opts-undef.test.js +++ b/lib/mkdirs/__tests__/opts-undef.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/perm_sync.test.js b/lib/mkdirs/__tests__/perm_sync.test.js index 2c75967cf..ffdca25a7 100644 --- a/lib/mkdirs/__tests__/perm_sync.test.js +++ b/lib/mkdirs/__tests__/perm_sync.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/race.test.js b/lib/mkdirs/__tests__/race.test.js index 1d075f464..594202fcd 100644 --- a/lib/mkdirs/__tests__/race.test.js +++ b/lib/mkdirs/__tests__/race.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/rel.test.js b/lib/mkdirs/__tests__/rel.test.js index bb00031e0..428c41340 100644 --- a/lib/mkdirs/__tests__/rel.test.js +++ b/lib/mkdirs/__tests__/rel.test.js @@ -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') diff --git a/lib/mkdirs/__tests__/sync.test.js b/lib/mkdirs/__tests__/sync.test.js index 1b7fe092b..26336aef4 100644 --- a/lib/mkdirs/__tests__/sync.test.js +++ b/lib/mkdirs/__tests__/sync.test.js @@ -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') diff --git a/lib/move/__tests__/move-sync.test.js b/lib/move/__tests__/move-sync.test.js index 506b62ddb..5f5b6dc65 100644 --- a/lib/move/__tests__/move-sync.test.js +++ b/lib/move/__tests__/move-sync.test.js @@ -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') diff --git a/lib/output-file/__tests__/output.test.js b/lib/output-file/__tests__/output.test.js index 023aa60f4..705a8570a 100644 --- a/lib/output-file/__tests__/output.test.js +++ b/lib/output-file/__tests__/output.test.js @@ -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') diff --git a/lib/path-exists/__tests__/path-exists-sync.test.js b/lib/path-exists/__tests__/path-exists-sync.test.js index dbde2b44b..37ec00ece 100644 --- a/lib/path-exists/__tests__/path-exists-sync.test.js +++ b/lib/path-exists/__tests__/path-exists-sync.test.js @@ -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') diff --git a/lib/path-exists/__tests__/path-exists.test.js b/lib/path-exists/__tests__/path-exists.test.js index abc1e6341..2c47f55e9 100644 --- a/lib/path-exists/__tests__/path-exists.test.js +++ b/lib/path-exists/__tests__/path-exists.test.js @@ -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') diff --git a/lib/remove/__tests__/remove-dir.test.js b/lib/remove/__tests__/remove-dir.test.js index 344311d84..b2713dc1a 100644 --- a/lib/remove/__tests__/remove-dir.test.js +++ b/lib/remove/__tests__/remove-dir.test.js @@ -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') diff --git a/lib/remove/__tests__/remove-sync-dir.test.js b/lib/remove/__tests__/remove-sync-dir.test.js index 5372bd66d..ed217a192 100644 --- a/lib/remove/__tests__/remove-sync-dir.test.js +++ b/lib/remove/__tests__/remove-sync-dir.test.js @@ -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') diff --git a/lib/remove/__tests__/remove-sync-file.test.js b/lib/remove/__tests__/remove-sync-file.test.js index 68a229669..31880fcc6 100644 --- a/lib/remove/__tests__/remove-sync-file.test.js +++ b/lib/remove/__tests__/remove-sync-file.test.js @@ -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') diff --git a/lib/remove/__tests__/remove.test.js b/lib/remove/__tests__/remove.test.js index 27efe262a..15588850b 100644 --- a/lib/remove/__tests__/remove.test.js +++ b/lib/remove/__tests__/remove.test.js @@ -5,7 +5,7 @@ const fs = require('fs') const os = require('os') const path = require('path') const randomBytes = require('crypto').randomBytes -const fse = require(process.cwd()) +const fse = require('../..') /* global afterEach, beforeEach, describe, it */ diff --git a/lib/util/__tests__/stat.test.js b/lib/util/__tests__/stat.test.js index 58b3bd7f1..c344032ab 100644 --- a/lib/util/__tests__/stat.test.js +++ b/lib/util/__tests__/stat.test.js @@ -1,6 +1,6 @@ 'use strict' -const fs = require(process.cwd()) +const fs = require('../..') const os = require('os') const path = require('path') const assert = require('assert') diff --git a/lib/util/__tests__/utimes.test.js b/lib/util/__tests__/utimes.test.js index eb072b193..d28ece197 100644 --- a/lib/util/__tests__/utimes.test.js +++ b/lib/util/__tests__/utimes.test.js @@ -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') const proxyquire = require('proxyquire') From de273d9feedb5d429075fe759a2a70f70c27f573 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Mon, 24 Oct 2022 15:07:02 -0400 Subject: [PATCH 2/3] BREAKING: Switch from main to exports --- package.json | 2 +- test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index c05dc2bfe..ee3c6b519 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "read-dir-files": "^0.1.1", "standard": "^16.0.3" }, - "main": "./lib/index.js", + "exports": "./lib/index.js", "files": [ "lib/", "!lib/**/__tests__/" diff --git a/test.js b/test.js index b50b44ec3..bb36520fc 100644 --- a/test.js +++ b/test.js @@ -26,6 +26,6 @@ klaw('./lib').on('readable', function () { } }).on('end', () => { mocha.run(failures => { - require('./').remove(path.join(os.tmpdir(), 'fs-extra'), () => process.exit(failures)) + require('./lib').remove(path.join(os.tmpdir(), 'fs-extra'), () => process.exit(failures)) }) }) From bc29f91e1aff180af4cc855a4aafbceb37fc9aa0 Mon Sep 17 00:00:00 2001 From: Ryan Zimmerman Date: Tue, 25 Oct 2022 15:44:24 -0400 Subject: [PATCH 3/3] Add fs-extra/esm ESM named import module, with just fs-extra methods Fixes #746 --- README.md | 30 ++++++++++++- lib/esm.mjs | 68 ++++++++++++++++++++++++++++++ package.json | 10 +++-- test.mjs | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 lib/esm.mjs create mode 100644 test.mjs diff --git a/README.md b/README.md index 6ed8b6ae5..6bd23d3f8 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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. @@ -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 diff --git a/lib/esm.mjs b/lib/esm.mjs new file mode 100644 index 000000000..27b7a1f73 --- /dev/null +++ b/lib/esm.mjs @@ -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 +} diff --git a/package.json b/package.json index ee3c6b519..40e0f3723 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,10 @@ "read-dir-files": "^0.1.1", "standard": "^16.0.3" }, - "exports": "./lib/index.js", + "exports": { + ".": "./lib/index.js", + "./esm": "./lib/esm.mjs" + }, "files": [ "lib/", "!lib/**/__tests__/" @@ -59,8 +62,9 @@ "scripts": { "lint": "standard", "test-find": "find ./lib/**/__tests__ -name *.test.js | xargs mocha", - "test": "npm run lint && npm run unit", - "unit": "nyc node test.js" + "test": "npm run lint && npm run unit && npm run unit-esm", + "unit": "nyc node test.js", + "unit-esm": "node test.mjs" }, "sideEffects": false } diff --git a/test.mjs b/test.mjs new file mode 100644 index 000000000..333f9fde3 --- /dev/null +++ b/test.mjs @@ -0,0 +1,116 @@ +import assert from 'assert' +import fsLegacy from './lib/index.js' +// NOTE: eslint comments needed because we're importing the same file multiple times +import fsDefault from './lib/esm.mjs' // eslint-disable-line +import * as fsStar from './lib/esm.mjs' +import { + copy, + copySync, + emptyDirSync, + emptydirSync, + emptyDir, + emptydir, + createFile, + createFileSync, + ensureFile, + ensureFileSync, + createLink, + createLinkSync, + ensureLink, + ensureLinkSync, + createSymlink, + createSymlinkSync, + ensureSymlink, + ensureSymlinkSync, + readJson, + readJsonSync, + writeJson, + writeJsonSync, + outputJson, + outputJsonSync, + outputJSON, + outputJSONSync, + writeJSON, + writeJSONSync, + readJSON, + readJSONSync, + mkdirs, + mkdirsSync, + mkdirp, + mkdirpSync, + ensureDir, + ensureDirSync, + move, + moveSync, + outputFile, + outputFileSync, + pathExists, + pathExistsSync, + remove, + removeSync +} from './lib/esm.mjs' // eslint-disable-line +const fsNamed = [ + copy, + copySync, + emptyDirSync, + emptydirSync, + emptyDir, + emptydir, + createFile, + createFileSync, + ensureFile, + ensureFileSync, + createLink, + createLinkSync, + ensureLink, + ensureLinkSync, + createSymlink, + createSymlinkSync, + ensureSymlink, + ensureSymlinkSync, + readJson, + readJsonSync, + writeJson, + writeJsonSync, + outputJson, + outputJsonSync, + outputJSON, + outputJSONSync, + writeJSON, + writeJSONSync, + readJSON, + readJSONSync, + mkdirs, + mkdirsSync, + mkdirp, + mkdirpSync, + ensureDir, + ensureDirSync, + move, + moveSync, + outputFile, + outputFileSync, + pathExists, + pathExistsSync, + remove, + removeSync +] + +const keys = Object.keys(fsDefault) + +assert.deepStrictEqual(Object.values(fsDefault), fsNamed, 'named and default exports should match') +assert.deepStrictEqual( + Object.entries(fsStar) + .filter(([name]) => name !== 'default') // remove "default" property here + .sort(([nameA], [nameB]) => keys.indexOf(nameA) - keys.indexOf(nameB)) // sort for exact match + .map(([name, fn]) => fn), + Object.values(fsDefault), + 'star and default exports should match' +) + +// default exports a subset of the legacy implementation, but functions are the same +Object.entries(fsDefault).forEach(([name, fn]) => { + assert.strictEqual(fn, fsLegacy[name], `${name}() should match legacy implementation`) +}) + +console.warn('ESM tests pass!')