Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Require Node.js 8
  • Loading branch information
sindresorhus committed Nov 6, 2018
1 parent 186423a commit 6cd7609
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 138 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,4 +1,3 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
lib/macos-trash binary
lib/win-trash.exe binary
3 changes: 1 addition & 2 deletions .travis.yml
Expand Up @@ -3,6 +3,5 @@ os:
- osx
language: node_js
node_js:
- '10'
- '8'
- '6'
- '4'
36 changes: 22 additions & 14 deletions index.js
Expand Up @@ -5,27 +5,32 @@ const globby = require('globby');
const pTry = require('p-try');
const macos = require('./lib/macos');
const linux = require('./lib/linux');
const win = require('./lib/win');
const windows = require('./lib/windows');

module.exports = (iterable, opts) => pTry(() => {
iterable = Array.from(typeof iterable === 'string' ? [iterable] : iterable).map(String);
opts = Object.assign({glob: true}, opts);
module.exports = (iterable, options) => pTry(() => {
iterable = [...(typeof iterable === 'string' ? [iterable] : iterable)].map(String);

const paths = (opts.glob === false ? iterable : globby.sync(iterable, {
options = {
glob: true,
...options
};

// TOOD: Upgrading to latest `globby` version is blocked by https://github.com/mrmlnc/fast-glob/issues/110
const paths = (options.glob === false ? iterable : globby.sync(iterable, {
expandDirectories: false,
nodir: false,
nonull: true
}))
.map(x => path.resolve(x))
.filter(x => {
.map(filePath => path.resolve(filePath))
.filter(filePath => {
try {
return fs.lstatSync(x);
} catch (err) {
if (err.code === 'ENOENT') {
return fs.lstatSync(filePath);
} catch (error) {
if (error.code === 'ENOENT') {
return false;
}

throw err;
throw error;
}
});

Expand All @@ -34,8 +39,11 @@ module.exports = (iterable, opts) => pTry(() => {
}

switch (process.platform) {
case 'darwin': return macos(paths);
case 'win32': return win(paths);
default: return linux(paths);
case 'darwin':
return macos(paths);
case 'win32':
return windows(paths);
default:
return linux(paths);
}
});
46 changes: 27 additions & 19 deletions lib/linux.js
@@ -1,33 +1,41 @@
'use strict';
const {promisify} = require('util');
const os = require('os');
const path = require('path');
const fsExtra = require('fs-extra');
const pify = require('pify');
const fs = require('fs');
const uuid = require('uuid');
const xdgTrashdir = require('xdg-trashdir');
const pMap = require('p-map');
const makeDir = require('make-dir');
const moveFile = require('move-file');

const fs = pify(fsExtra);
const pWriteFile = promisify(fs.writeFile);

function trash(src) {
return xdgTrashdir(src).then(dir => {
const name = uuid.v4();
const dest = path.join(dir, 'files', name);
const info = path.join(dir, 'info', `${name}.trashinfo`);
const msg = `
const trash = async filePath => {
const trashDirectory = await xdgTrashdir(filePath);
const name = uuid.v4();
const destination = path.join(trashDirectory, 'files', name);
const trashInfoPath = path.join(trashDirectory, 'info', `${name}.trashinfo`);

const trashInfoData = `
[Trash Info]
Path=${src.replace(/\s/g, '%20')}
Path=${filePath.replace(/\s/g, '%20')}
DeletionDate=${(new Date()).toISOString()}
`.trim();

return Promise.all([
fs.move(src, dest, {mkdirp: true}),

This comment has been minimized.

Copy link
@fregante

fregante Jan 25, 2021

Contributor

What problem did move-file solve here? Can the trash actually be on a separate volume? And if that's the case, wouldn't that potentially be extremely slow for a "move to trash" operation?

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Jan 25, 2021

Author Owner
fs.outputFile(info, msg)
]).then(() => ({
path: dest,
info
}));
});
}
await Promise.all([
moveFile(filePath, destination),
(async () => {
// TODO: Use the `fs.mkdir` with `recursive` option when targeting Node.js 12.
await makeDir(path.dirname(trashInfoPath));
await pWriteFile(trashInfoPath, trashInfoData);
})()
]);

return {
path: destination,
info: trashInfoPath
};
};

module.exports = paths => pMap(paths, trash, {concurrency: os.cpus().length});
43 changes: 25 additions & 18 deletions lib/macos.js
@@ -1,39 +1,46 @@
'use strict';
const {promisify} = require('util');
const os = require('os');
const path = require('path');
const execFile = require('child_process').execFile;
const {execFile} = require('child_process');
const escapeStringApplescript = require('escape-string-applescript');
const runApplescript = require('run-applescript');
const pify = require('pify');

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

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

const legacy = async paths => {
const pathString = paths.map(x => `"${escapeStringApplescript(x)}"`).join(',');

function legacy(paths) {
const pathStr = paths.map(x => `"${escapeStringApplescript(x)}"`).join(',');
const script = `
set deleteList to {}
repeat with currentPath in {${pathStr}}
repeat with currentPath in {${pathString}}
set end of deleteList to POSIX file currentPath
end repeat
tell app "Finder" to delete deleteList
`.trim();
`.trim();

try {
await runApplescript(script);
} catch (error) {
let newError = error;

return runApplescript(script).catch(err => {
if (/10010/.test(err.message)) {
err = new Error('Item doesn\'t exist');
if (/10010/.test(newError.message)) {
newError = new Error('Item doesn\'t exist');
}

throw err;
});
}
throw newError;
}
};

module.exports = paths => {
if (olderThanMountainLion) {
return legacy(paths);
module.exports = async paths => {
if (isOlderThanMountainLion) {
await legacy(paths);
return;
}

return pify(execFile)(bin, paths);
await pExecFile(binary, paths);
};
9 changes: 0 additions & 9 deletions lib/win.js

This file was deleted.

13 changes: 13 additions & 0 deletions lib/windows.js
@@ -0,0 +1,13 @@
'use strict';
const {promisify} = require('util');
const path = require('path');
const {execFile} = require('child_process');

const pExecFile = promisify(execFile);

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

module.exports = async paths => {
await pExecFile(binary, paths);
};
110 changes: 55 additions & 55 deletions package.json
@@ -1,57 +1,57 @@
{
"name": "trash",
"version": "4.3.0",
"description": "Move files and folders to the trash",
"license": "MIT",
"repository": "sindresorhus/trash",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"lib"
],
"keywords": [
"trash",
"recycle",
"bin",
"rm",
"rmrf",
"rimraf",
"remove",
"delete",
"del",
"file",
"files",
"dir",
"directory",
"directories",
"folder",
"folders",
"xdg"
],
"dependencies": {
"escape-string-applescript": "^2.0.0",
"fs-extra": "^0.30.0",
"globby": "^7.1.1",
"p-map": "^1.2.0",
"p-try": "^1.0.0",
"pify": "^3.0.0",
"run-applescript": "^3.0.0",
"uuid": "^3.1.0",
"xdg-trashdir": "^2.1.1"
},
"devDependencies": {
"ava": "*",
"tempfile": "^2.0.0",
"xo": "*"
}
"name": "trash",
"version": "4.3.0",
"description": "Move files and folders to the trash",
"license": "MIT",
"repository": "sindresorhus/trash",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js",
"lib"
],
"keywords": [
"trash",
"recycle",
"bin",
"rm",
"rmrf",
"rimraf",
"remove",
"delete",
"del",
"file",
"files",
"dir",
"directory",
"directories",
"folder",
"folders",
"xdg"
],
"dependencies": {
"escape-string-applescript": "^2.0.0",
"globby": "^7.1.1",
"make-dir": "^1.3.0",
"move-file": "^1.0.0",
"p-map": "^2.0.0",
"p-try": "^2.0.0",
"run-applescript": "^3.0.0",
"uuid": "^3.1.0",
"xdg-trashdir": "^2.1.1"
},
"devDependencies": {
"ava": "^1.0.0-rc.1",
"tempfile": "^2.0.0",
"xo": "^0.23.0"
}
}
6 changes: 3 additions & 3 deletions readme.md
Expand Up @@ -21,9 +21,9 @@ $ npm install trash
```js
const trash = require('trash');

trash(['*.png', '!rainbow.png']).then(() => {
console.log('done');
});
(async () => {
await trash(['*.png', '!rainbow.png']);
})();
```


Expand Down

0 comments on commit 6cd7609

Please sign in to comment.