Skip to content

Commit

Permalink
Remove dryRun option from TorrentCleanConfig
Browse files Browse the repository at this point in the history
- Add API description to README
  • Loading branch information
nikolay-borzov committed Apr 12, 2020
1 parent a642a23 commit 8646508
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 7 deletions.
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,48 @@ gets files' paths from `nature-pack.torrent` and compares them with files from `
`torrent-clean` allows specifying some parameters via config file (`.torrent-cleanrc`, `.torrent-cleanrc.json`, `.torrent-cleanrc.yaml`, `.torrent-cleanrc.yml` or `.torrent-cleanrc.js`). There are might be many files - `torrent-clean` will collect and merge all files up to root directory. The closer config to the directory is, the higher its priority

Parameter are:
- `ignore` - an array of globs or filenames that will be excluded from the list of extra files.
- `ignore` - an array of globs (picomatch) or filenames that will be excluded from the list of extra files.

## API
`cleanTorrentDir` accepts options object:
```javascript
{
torrentId: '6a9759bffd5c0af65319979fb7832189f4f3c35d',
// Directory to clean
directoryPath: 'C:/Downloads/wallpapers/nature',
// Do not delete files immediately. Instead return `deleteFiles` function
dryRun: true,
// Config with highest priority
customConfig: { ignore: ['**/*\(edited\)*'] }
}
```


```javascript
const cleanTorrentDir = require('torrent-clean')

const { extraFiles } = await cleanTorrentDir({
torrentId: 'C:/Downloads/torrents/nature wallpapers.torrent',
directoryPath: 'C:/Downloads/wallpapers/nature'
})

console.log('Removed', extraFiles)
```

```javascript
const cleanTorrentDir = require('torrent-clean')

const { extraFiles, deleteFiles } = await cleanTorrentDir({
torrentId: '6a9759bffd5c0af65319979fb7832189f4f3c35d',
directoryPath: 'C:/Downloads/wallpapers/nature',
dryRun: true,
customConfig: { ignore: ['**/*\(edited\)*'] }
})

console.log('Removing', extraFiles)

await deleteFiles(extraFiles)
```

## Known issues

Expand Down
2 changes: 1 addition & 1 deletion bin/torrent-clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const directoryPath = path.resolve(argv.dir)

console.log(logColor.info('Parsing torrent file...'))

cleanTorrentDir({ torrentId, directoryPath, customConfig: { dryRun: true } })
cleanTorrentDir({ torrentId, directoryPath, dryRun: true })
.then(async ({ extraFiles, deleteFiles }) => {
if (extraFiles.length === 0) {
console.log('No extra files found!')
Expand Down
13 changes: 10 additions & 3 deletions lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const { deleteFilesAndEmptyDirs } = require('./delete-files')
* @property {TorrentId} torrentId Torrent id
* (as described in {@link https://github.com/webtorrent/webtorrent/blob/master/docs/api.md#clientaddtorrentid-opts-function-ontorrent-torrent-|webtorrent api})
* @property {string} directoryPath Path to directory with downloaded files
* @property {boolean} [dryRun] Postpone deleting files until confirm function
* is called
* @property {TorrentCleanConfig} [customConfig] Config that will be merged with
* configs collected from files
*/
Expand All @@ -32,7 +34,12 @@ const { deleteFilesAndEmptyDirs } = require('./delete-files')
* @param {TorrentCleanOptions} options
* @returns {Promise<TorrentCleanResult>}
*/
async function cleanTorrentDir({ torrentId, directoryPath, customConfig }) {
async function cleanTorrentDir({
torrentId,
directoryPath,
customConfig,
dryRun,
}) {
/** @type {TorrentCleanConfig} */
let config

Expand Down Expand Up @@ -64,13 +71,13 @@ async function cleanTorrentDir({ torrentId, directoryPath, customConfig }) {
(filename) => torrentFiles.indexOf(filename) === -1
)

if (!config.dryRun) {
if (!dryRun) {
await deleteFilesAndEmptyDirs(directoryPath, extraFiles)
}

return {
extraFiles,
...(config.dryRun && {
...(dryRun && {
deleteFiles: deleteFilesAndEmptyDirs.bind(null, directoryPath),
}),
}
Expand Down
2 changes: 0 additions & 2 deletions lib/load-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const moduleName = require('../package.json').name
/**
* @typedef {object} TorrentCleanConfig
* @property {string[]} [ignore] Globs to ignore files
* @property {boolean} [dryRun] Postpone deleting files until confirm function
* is called
*/

/** Default ignore patterns */
Expand Down

0 comments on commit 8646508

Please sign in to comment.