Skip to content

Commit 6cd7609

Browse files
committedNov 6, 2018
Require Node.js 8
1 parent 186423a commit 6cd7609

File tree

10 files changed

+161
-138
lines changed

10 files changed

+161
-138
lines changed
 

‎.gitattributes

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
* text=auto
2-
*.js text eol=lf
1+
* text=auto eol=lf
32
lib/macos-trash binary
43
lib/win-trash.exe binary

‎.travis.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ os:
33
- osx
44
language: node_js
55
node_js:
6+
- '10'
67
- '8'
7-
- '6'
8-
- '4'

‎index.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,32 @@ const globby = require('globby');
55
const pTry = require('p-try');
66
const macos = require('./lib/macos');
77
const linux = require('./lib/linux');
8-
const win = require('./lib/win');
8+
const windows = require('./lib/windows');
99

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

14-
const paths = (opts.glob === false ? iterable : globby.sync(iterable, {
13+
options = {
14+
glob: true,
15+
...options
16+
};
17+
18+
// TOOD: Upgrading to latest `globby` version is blocked by https://github.com/mrmlnc/fast-glob/issues/110
19+
const paths = (options.glob === false ? iterable : globby.sync(iterable, {
1520
expandDirectories: false,
1621
nodir: false,
1722
nonull: true
1823
}))
19-
.map(x => path.resolve(x))
20-
.filter(x => {
24+
.map(filePath => path.resolve(filePath))
25+
.filter(filePath => {
2126
try {
22-
return fs.lstatSync(x);
23-
} catch (err) {
24-
if (err.code === 'ENOENT') {
27+
return fs.lstatSync(filePath);
28+
} catch (error) {
29+
if (error.code === 'ENOENT') {
2530
return false;
2631
}
2732

28-
throw err;
33+
throw error;
2934
}
3035
});
3136

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

3641
switch (process.platform) {
37-
case 'darwin': return macos(paths);
38-
case 'win32': return win(paths);
39-
default: return linux(paths);
42+
case 'darwin':
43+
return macos(paths);
44+
case 'win32':
45+
return windows(paths);
46+
default:
47+
return linux(paths);
4048
}
4149
});

‎lib/linux.js

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
'use strict';
2+
const {promisify} = require('util');
23
const os = require('os');
34
const path = require('path');
4-
const fsExtra = require('fs-extra');
5-
const pify = require('pify');
5+
const fs = require('fs');
66
const uuid = require('uuid');
77
const xdgTrashdir = require('xdg-trashdir');
88
const pMap = require('p-map');
9+
const makeDir = require('make-dir');
10+
const moveFile = require('move-file');
911

10-
const fs = pify(fsExtra);
12+
const pWriteFile = promisify(fs.writeFile);
1113

12-
function trash(src) {
13-
return xdgTrashdir(src).then(dir => {
14-
const name = uuid.v4();
15-
const dest = path.join(dir, 'files', name);
16-
const info = path.join(dir, 'info', `${name}.trashinfo`);
17-
const msg = `
14+
const trash = async filePath => {
15+
const trashDirectory = await xdgTrashdir(filePath);
16+
const name = uuid.v4();
17+
const destination = path.join(trashDirectory, 'files', name);
18+
const trashInfoPath = path.join(trashDirectory, 'info', `${name}.trashinfo`);
19+
20+
const trashInfoData = `
1821
[Trash Info]
19-
Path=${src.replace(/\s/g, '%20')}
22+
Path=${filePath.replace(/\s/g, '%20')}
2023
DeletionDate=${(new Date()).toISOString()}
2124
`.trim();
2225

23-
return Promise.all([
24-
fs.move(src, dest, {mkdirp: true}),
Has conversations. Original line has conversations.
25-
fs.outputFile(info, msg)
26-
]).then(() => ({
27-
path: dest,
28-
info
29-
}));
30-
});
31-
}
26+
await Promise.all([
27+
moveFile(filePath, destination),
28+
(async () => {
29+
// TODO: Use the `fs.mkdir` with `recursive` option when targeting Node.js 12.
30+
await makeDir(path.dirname(trashInfoPath));
31+
await pWriteFile(trashInfoPath, trashInfoData);
32+
})()
33+
]);
34+
35+
return {
36+
path: destination,
37+
info: trashInfoPath
38+
};
39+
};
3240

3341
module.exports = paths => pMap(paths, trash, {concurrency: os.cpus().length});

‎lib/macos.js

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,46 @@
11
'use strict';
2+
const {promisify} = require('util');
23
const os = require('os');
34
const path = require('path');
4-
const execFile = require('child_process').execFile;
5+
const {execFile} = require('child_process');
56
const escapeStringApplescript = require('escape-string-applescript');
67
const runApplescript = require('run-applescript');
7-
const pify = require('pify');
88

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

1112
// Binary source: https://github.com/sindresorhus/macos-trash
12-
const bin = path.join(__dirname, 'macos-trash');
13+
const binary = path.join(__dirname, 'macos-trash');
14+
15+
const legacy = async paths => {
16+
const pathString = paths.map(x => `"${escapeStringApplescript(x)}"`).join(',');
1317

14-
function legacy(paths) {
15-
const pathStr = paths.map(x => `"${escapeStringApplescript(x)}"`).join(',');
1618
const script = `
1719
set deleteList to {}
18-
repeat with currentPath in {${pathStr}}
20+
repeat with currentPath in {${pathString}}
1921
set end of deleteList to POSIX file currentPath
2022
end repeat
2123
tell app "Finder" to delete deleteList
22-
`.trim();
24+
`.trim();
25+
26+
try {
27+
await runApplescript(script);
28+
} catch (error) {
29+
let newError = error;
2330

24-
return runApplescript(script).catch(err => {
25-
if (/10010/.test(err.message)) {
26-
err = new Error('Item doesn\'t exist');
31+
if (/10010/.test(newError.message)) {
32+
newError = new Error('Item doesn\'t exist');
2733
}
2834

29-
throw err;
30-
});
31-
}
35+
throw newError;
36+
}
37+
};
3238

33-
module.exports = paths => {
34-
if (olderThanMountainLion) {
35-
return legacy(paths);
39+
module.exports = async paths => {
40+
if (isOlderThanMountainLion) {
41+
await legacy(paths);
42+
return;
3643
}
3744

38-
return pify(execFile)(bin, paths);
45+
await pExecFile(binary, paths);
3946
};

‎lib/win.js

-9
This file was deleted.

‎lib/windows.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
const {promisify} = require('util');
3+
const path = require('path');
4+
const {execFile} = require('child_process');
5+
6+
const pExecFile = promisify(execFile);
7+
8+
// Binary source: https://github.com/sindresorhus/recycle-bin
9+
const binary = path.join(__dirname, 'win-trash.exe');
10+
11+
module.exports = async paths => {
12+
await pExecFile(binary, paths);
13+
};

‎package.json

+55-55
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,57 @@
11
{
2-
"name": "trash",
3-
"version": "4.3.0",
4-
"description": "Move files and folders to the trash",
5-
"license": "MIT",
6-
"repository": "sindresorhus/trash",
7-
"author": {
8-
"name": "Sindre Sorhus",
9-
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
11-
},
12-
"engines": {
13-
"node": ">=4"
14-
},
15-
"scripts": {
16-
"test": "xo && ava"
17-
},
18-
"files": [
19-
"index.js",
20-
"lib"
21-
],
22-
"keywords": [
23-
"trash",
24-
"recycle",
25-
"bin",
26-
"rm",
27-
"rmrf",
28-
"rimraf",
29-
"remove",
30-
"delete",
31-
"del",
32-
"file",
33-
"files",
34-
"dir",
35-
"directory",
36-
"directories",
37-
"folder",
38-
"folders",
39-
"xdg"
40-
],
41-
"dependencies": {
42-
"escape-string-applescript": "^2.0.0",
43-
"fs-extra": "^0.30.0",
44-
"globby": "^7.1.1",
45-
"p-map": "^1.2.0",
46-
"p-try": "^1.0.0",
47-
"pify": "^3.0.0",
48-
"run-applescript": "^3.0.0",
49-
"uuid": "^3.1.0",
50-
"xdg-trashdir": "^2.1.1"
51-
},
52-
"devDependencies": {
53-
"ava": "*",
54-
"tempfile": "^2.0.0",
55-
"xo": "*"
56-
}
2+
"name": "trash",
3+
"version": "4.3.0",
4+
"description": "Move files and folders to the trash",
5+
"license": "MIT",
6+
"repository": "sindresorhus/trash",
7+
"author": {
8+
"name": "Sindre Sorhus",
9+
"email": "sindresorhus@gmail.com",
10+
"url": "sindresorhus.com"
11+
},
12+
"engines": {
13+
"node": ">=8"
14+
},
15+
"scripts": {
16+
"test": "xo && ava"
17+
},
18+
"files": [
19+
"index.js",
20+
"lib"
21+
],
22+
"keywords": [
23+
"trash",
24+
"recycle",
25+
"bin",
26+
"rm",
27+
"rmrf",
28+
"rimraf",
29+
"remove",
30+
"delete",
31+
"del",
32+
"file",
33+
"files",
34+
"dir",
35+
"directory",
36+
"directories",
37+
"folder",
38+
"folders",
39+
"xdg"
40+
],
41+
"dependencies": {
42+
"escape-string-applescript": "^2.0.0",
43+
"globby": "^7.1.1",
44+
"make-dir": "^1.3.0",
45+
"move-file": "^1.0.0",
46+
"p-map": "^2.0.0",
47+
"p-try": "^2.0.0",
48+
"run-applescript": "^3.0.0",
49+
"uuid": "^3.1.0",
50+
"xdg-trashdir": "^2.1.1"
51+
},
52+
"devDependencies": {
53+
"ava": "^1.0.0-rc.1",
54+
"tempfile": "^2.0.0",
55+
"xo": "^0.23.0"
56+
}
5757
}

‎readme.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ $ npm install trash
2121
```js
2222
const trash = require('trash');
2323

24-
trash(['*.png', '!rainbow.png']).then(() => {
25-
console.log('done');
26-
});
24+
(async () => {
25+
await trash(['*.png', '!rainbow.png']);
26+
})();
2727
```
2828

2929

‎test.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import fs from 'fs';
22
import path from 'path';
33
import test from 'ava';
44
import tempfile from 'tempfile';
5-
import m from '.';
5+
import trash from '.';
66

7-
const tmpdir = tempfile();
8-
fs.mkdirSync(tmpdir);
9-
process.chdir(tmpdir);
7+
const tempDirectory = tempfile();
8+
fs.mkdirSync(tempDirectory);
9+
process.chdir(tempDirectory);
1010

1111
test('files', async t => {
1212
const weirdName = process.platform === 'darwin' ? 'weird\\\\name\\"\'' : 'fixture3';
@@ -20,7 +20,7 @@ test('files', async t => {
2020
t.true(fs.existsSync(weirdName));
2121
t.true(fs.existsSync('123'));
2222

23-
await m([
23+
await trash([
2424
'fixture',
2525
'fixture2',
2626
weirdName,
@@ -39,7 +39,7 @@ test('glob', async t => {
3939
t.true(fs.existsSync('fixture.jpg'));
4040
t.true(fs.existsSync('fixture.png'));
4141

42-
await m([
42+
await trash([
4343
'*.jpg'
4444
]);
4545

@@ -51,7 +51,7 @@ test('no glob', async t => {
5151
fs.writeFileSync('fixture-noglob*.js', '');
5252
fs.writeFileSync('fixture-noglob1.js', '');
5353

54-
await m(['fixture-noglob*.js'], {glob: false});
54+
await trash(['fixture-noglob*.js'], {glob: false});
5555

5656
t.false(fs.existsSync('fixture-noglob*.js'));
5757
t.true(fs.existsSync('fixture-noglob1.js'));
@@ -65,9 +65,7 @@ test('string pattern', async t => {
6565
t.true(fs.existsSync('b'));
6666
t.true(fs.existsSync('ab'));
6767

68-
await m(
69-
'ab'
70-
);
68+
await trash('ab');
7169

7270
t.false(fs.existsSync('ab'));
7371
t.true(fs.existsSync('a'));
@@ -92,7 +90,7 @@ test('directories', async t => {
9290
t.true(fs.existsSync(d2f1));
9391
t.true(fs.existsSync(d2f2));
9492

95-
await m([
93+
await trash([
9694
'fdir',
9795
321
9896
]);
@@ -109,7 +107,7 @@ test('directories', async t => {
109107
fs.writeFileSync('file' + i, '');
110108
}
111109

112-
await t.notThrows(m(paths));
110+
await t.notThrowsAsync(trash(paths));
113111

114112
for (let i = 0; i < FILE_COUNT; i++) {
115113
t.false(fs.existsSync('file' + i));
@@ -125,7 +123,7 @@ test('symlinks', async t => {
125123
t.truthy(fs.lstatSync('bbb'));
126124
t.truthy(fs.lstatSync('ccc'));
127125

128-
await m([
126+
await trash([
129127
'bbb',
130128
'ccc'
131129
]);
@@ -142,7 +140,7 @@ if (process.platform === 'linux') {
142140
fs.writeFileSync('f2', '');
143141

144142
const info = `[Trash Info]\nPath=${path.resolve('f2')}`;
145-
const files = await m(['f2']);
143+
const files = await trash(['f2']);
146144
const infoFile = fs.readFileSync(files[0].info, 'utf8');
147145

148146
t.is(infoFile.trim().indexOf(info.trim()), 0);
@@ -154,7 +152,7 @@ if (process.platform === 'linux') {
154152
fs.writeFileSync('f3', '');
155153

156154
const statSrc = fs.statSync('f3');
157-
const files = await m(['f3']);
155+
const files = await trash(['f3']);
158156
const statDest = fs.statSync(files[0].path);
159157

160158
t.is(statSrc.mode, statDest.mode);
@@ -166,5 +164,5 @@ if (process.platform === 'linux') {
166164

167165
test('non-existent files', async t => {
168166
t.false(fs.existsSync('fixture-enoent'));
169-
await t.notThrows(m('fixture-enoent'));
167+
await t.notThrowsAsync(trash('fixture-enoent'));
170168
});

0 commit comments

Comments
 (0)
Please sign in to comment.