From 86465083e8dd2d2b24ae0a195b472b67b227f28d Mon Sep 17 00:00:00 2001 From: nikolay-borzov Date: Sun, 12 Apr 2020 18:06:42 +0400 Subject: [PATCH] Remove `dryRun` option from TorrentCleanConfig - Add API description to README --- README.md | 43 ++++++++++++++++++++++++++++++++++++++++++- bin/torrent-clean.js | 2 +- lib/api.js | 13 ++++++++++--- lib/load-config.js | 2 -- 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 8c0d63a..97a78f7 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/torrent-clean.js b/bin/torrent-clean.js index 2651a44..381c94d 100644 --- a/bin/torrent-clean.js +++ b/bin/torrent-clean.js @@ -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!') diff --git a/lib/api.js b/lib/api.js index f20edfb..84163e7 100644 --- a/lib/api.js +++ b/lib/api.js @@ -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 */ @@ -32,7 +34,12 @@ const { deleteFilesAndEmptyDirs } = require('./delete-files') * @param {TorrentCleanOptions} options * @returns {Promise} */ -async function cleanTorrentDir({ torrentId, directoryPath, customConfig }) { +async function cleanTorrentDir({ + torrentId, + directoryPath, + customConfig, + dryRun, +}) { /** @type {TorrentCleanConfig} */ let config @@ -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), }), } diff --git a/lib/load-config.js b/lib/load-config.js index 28defb1..0297f21 100644 --- a/lib/load-config.js +++ b/lib/load-config.js @@ -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 */