diff --git a/.prettierignore b/.prettierignore index d289a030..47415c69 100644 --- a/.prettierignore +++ b/.prettierignore @@ -8,4 +8,4 @@ /tap-snapshots /.nyc_output /coverage -/benchmark +/benchmark/old-rimraf diff --git a/benchmark/create-fixture.js b/benchmark/create-fixture.js index 99e7348a..3ce172ef 100644 --- a/benchmark/create-fixture.js +++ b/benchmark/create-fixture.js @@ -1,24 +1,25 @@ -const { writeFile: writeFile_ } = require('fs') -const writeFile = async (path, data) => new Promise((res, rej) => - writeFile_(path, data, er => er ? rej(er) : res())) -const { mkdirp } = require('mkdirp') -const { resolve } = require('path') +import { writeFile as writeFile_ } from 'fs' +const writeFile = async (path, data) => + new Promise((res, rej) => + writeFile_(path, data, er => (er ? rej(er) : res())), + ) +import { mkdirp } from 'mkdirp' +import { resolve } from 'path' const create = async (path, start, end, maxDepth, depth = 0) => { await mkdirp(path) const promises = [] for (let i = start; i <= end; i++) { const c = String.fromCharCode(i) - if (depth < maxDepth && (i-start >= depth)) + if (depth < maxDepth && i - start >= depth) await create(resolve(path, c), start, end, maxDepth, depth + 1) - else - promises.push(writeFile(resolve(path, c), c)) + else promises.push(writeFile(resolve(path, c), c)) } await Promise.all(promises) return path } -module.exports = async ({ start, end, depth, name }) => { - const path = resolve(__dirname, 'fixtures', name, 'test') +export default async ({ start, end, depth, name }) => { + const path = resolve(import.meta.dirname, 'fixtures', name, 'test') return await create(path, start.charCodeAt(0), end.charCodeAt(0), depth) } diff --git a/benchmark/index.js b/benchmark/index.js index 90928a20..d5a45d75 100644 --- a/benchmark/index.js +++ b/benchmark/index.js @@ -1,17 +1,17 @@ -const cases = require('./rimrafs.js') -const runTest = require('./run-test.js') -const print = require('./print-results.js') +import cases from './rimrafs.js' +import runTest from './run-test.js' +import print from './print-results.js' -const rimraf = require('../') +import * as rimraf from '../dist/esm/index.js' const main = async () => { // cleanup first. since the windows impl works on all platforms, // use that. it's only relevant if the folder exists anyway. - rimraf.sync(__dirname + '/fixtures') + rimraf.sync(import.meta.dirname + '/fixtures') const results = {} for (const name of Object.keys(cases)) { results[name] = await runTest(name) } - rimraf.sync(__dirname + '/fixtures') + rimraf.sync(import.meta.dirname + '/fixtures') return results } diff --git a/benchmark/print-results.js b/benchmark/print-results.js index 831e3309..61f7783c 100644 --- a/benchmark/print-results.js +++ b/benchmark/print-results.js @@ -11,7 +11,7 @@ const variance = list => { const stddev = list => { const v = variance(list) if (isNaN(v)) { - console.error({list, v}) + console.error({ list, v }) throw new Error('wat?') } return sqrt(variance(list)) @@ -28,8 +28,8 @@ const nums = list => ({ }) const printEr = er => `${er.code ? er.code + ': ' : ''}${er.message}` -const failures = list => list.length === 0 ? {} - : { failures: list.map(er => printEr(er)).join('\n') } +const failures = list => + list.length === 0 ? {} : { failures: list.map(er => printEr(er)).join('\n') } const table = results => { const table = {} @@ -49,7 +49,7 @@ const table = results => { } // sort by mean time return Object.entries(table) - .sort(([, {mean:a}], [, {mean:b}]) => a - b) + .sort(([, { mean: a }], [, { mean: b }]) => a - b) .reduce((set, [key, val]) => { set[key] = val return set @@ -62,4 +62,4 @@ const print = results => { console.table(table(results)) } -module.exports = print +export default print diff --git a/benchmark/rimrafs.js b/benchmark/rimrafs.js index aefbabfe..f4da741f 100644 --- a/benchmark/rimrafs.js +++ b/benchmark/rimrafs.js @@ -1,24 +1,25 @@ // just disable the glob option, and promisify it, for apples-to-apples comp +import { promisify } from 'util' +import { createRequire } from 'module' const oldRimraf = () => { - const {promisify} = require('util') - const oldRimraf = require('./old-rimraf') + const oldRimraf = createRequire(import.meta.filename)('./old-rimraf') const pOldRimraf = promisify(oldRimraf) const rimraf = path => pOldRimraf(path, { disableGlob: true }) const sync = path => oldRimraf.sync(path, { disableGlob: true }) return Object.assign(rimraf, { sync }) } -const { spawn, spawnSync } = require('child_process') +import { spawn, spawnSync } from 'child_process' const systemRmRf = () => { - const rimraf = path => new Promise((res, rej) => { - const proc = spawn('rm', ['-rf', path]) - proc.on('close', (code, signal) => { - if (code || signal) - rej(Object.assign(new Error('command failed'), { code, signal })) - else - res() + const rimraf = path => + new Promise((res, rej) => { + const proc = spawn('rm', ['-rf', path]) + proc.on('close', (code, signal) => { + if (code || signal) + rej(Object.assign(new Error('command failed'), { code, signal })) + else res() + }) }) - }) rimraf.sync = path => { const result = spawnSync('rm', ['-rf', path]) if (result.status || result.signal) { @@ -31,10 +32,11 @@ const systemRmRf = () => { return rimraf } -module.exports = { - native: require('../').native, - posix: require('../').posix, - windows: require('../').windows, +import { native, posix, windows } from 'rimraf' +export default { + native, + posix, + windows, old: oldRimraf(), system: systemRmRf(), } diff --git a/benchmark/run-test.js b/benchmark/run-test.js index a9fe6da4..4a2f0b8b 100644 --- a/benchmark/run-test.js +++ b/benchmark/run-test.js @@ -3,16 +3,15 @@ const END = process.env.RIMRAF_TEST_END_CHAR || 'f' const DEPTH = +process.env.RIMRAF_TEST_DEPTH || 5 const N = +process.env.RIMRAF_TEST_ITERATIONS || 7 -const cases = require('./rimrafs.js') +import cases from './rimrafs.js' -const create = require('./create-fixture.js') +import create from './create-fixture.js' -const hrToMS = hr => Math.round(hr[0]*1e9 + hr[1]) / 1e6 +const hrToMS = hr => Math.round(hr[0] * 1e9 + hr[1]) / 1e6 -const runTest = async (type) => { +const runTest = async type => { const rimraf = cases[type] - if (!rimraf) - throw new Error('unknown rimraf type: ' + type) + if (!rimraf) throw new Error('unknown rimraf type: ' + type) const opt = { start: START, @@ -62,10 +61,12 @@ const runTest = async (type) => { const startAsync = process.hrtime() for (const path of asyncPaths) { const start = process.hrtime() - await rimraf(path).then( - () => asyncTimes.push(hrToMS(process.hrtime(start))), - er => asyncFails.push(er) - ).then(() => process.stderr.write('.')) + await rimraf(path) + .then( + () => asyncTimes.push(hrToMS(process.hrtime(start))), + er => asyncFails.push(er), + ) + .then(() => process.stderr.write('.')) } const asyncTotal = hrToMS(process.hrtime(startAsync)) console.error('done! (%j ms, %j failed)', asyncTotal, asyncFails.length) @@ -77,10 +78,14 @@ const runTest = async (type) => { const paraRuns = [] for (const path of paraPaths) { const start = process.hrtime() - paraRuns.push(rimraf(path).then( - () => paraTimes.push(hrToMS(process.hrtime(start))), - er => paraFails.push(er) - ).then(() => process.stderr.write('.'))) + paraRuns.push( + rimraf(path) + .then( + () => paraTimes.push(hrToMS(process.hrtime(start))), + er => paraFails.push(er), + ) + .then(() => process.stderr.write('.')), + ) } await Promise.all(paraRuns) const paraTotal = hrToMS(process.hrtime(startPara)) @@ -97,4 +102,4 @@ const runTest = async (type) => { })) } -module.exports = runTest +export default runTest