Skip to content

Commit

Permalink
Allow deleting tons of files on macOS and Windows (#113)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
fregante and sindresorhus authored Jan 27, 2021
1 parent f61611f commit 5606166
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
12 changes: 12 additions & 0 deletions lib/chunked-exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const {promisify} = require('util');
const {execFile} = require('child_process');

const pExecFile = promisify(execFile);

module.exports = async (binary, paths, maxPaths) => {
// TODO: Use https://github.com/sindresorhus/chunkify when targeting Node.js 12.
for (let group = 0; paths.length > group; group += maxPaths) {
// eslint-disable-next-line no-await-in-loop
await pExecFile(binary, paths.slice(group, group + maxPaths));
}
};
6 changes: 2 additions & 4 deletions lib/macos.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
const {promisify} = require('util');
const os = require('os');
const path = require('path');
const {execFile} = require('child_process');
const chunkedExec = require('./chunked-exec');

const isOlderThanMountainLion = Number(os.release().split('.')[0]) < 12;
const pExecFile = promisify(execFile);

// Binary source: https://github.com/sindresorhus/macos-trash
const binary = path.join(__dirname, 'macos-trash');
Expand All @@ -15,5 +13,5 @@ module.exports = async paths => {
throw new Error('macOS 10.12 or later required');
}

await pExecFile(binary, paths);
await chunkedExec(binary, paths, 1000);
};
7 changes: 2 additions & 5 deletions lib/windows.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
'use strict';
const {promisify} = require('util');
const path = require('path');
const {execFile} = require('child_process');

const pExecFile = promisify(execFile);
const chunkedExec = require('./chunked-exec');

// Binary source: https://github.com/sindresorhus/recycle-bin
const binary = path.join(__dirname, 'windows-trash.exe');

module.exports = async paths => {
await pExecFile(binary, paths);
await chunkedExec(binary, paths, 200);
};
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,15 @@ test('directories', async t => {
t.false(fs.existsSync(321));
});

(process.platform === 'linux' ? test : test.failing)('tons of files', async t => {
test('tons of files', async t => {
const FILE_COUNT = 5000;
const paths = [];
for (let i = 0; i < FILE_COUNT; i++) {
paths.push('file' + i);
fs.writeFileSync('file' + i, '');
}

await t.notThrowsAsync(trash(paths));
await trash(paths);

for (let i = 0; i < FILE_COUNT; i++) {
t.false(fs.existsSync('file' + i));
Expand Down

0 comments on commit 5606166

Please sign in to comment.