Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Require Node.js 8
- Loading branch information
1 parent
186423a
commit 6cd7609
Showing
10 changed files
with
161 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
* text=auto | ||
*.js text eol=lf | ||
* text=auto eol=lf | ||
lib/macos-trash binary | ||
lib/win-trash.exe binary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,5 @@ os: | |
- osx | ||
language: node_js | ||
node_js: | ||
- '10' | ||
- '8' | ||
- '6' | ||
- '4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
sindresorhus
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}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
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?