Skip to content

Commit

Permalink
Display filename number
Browse files Browse the repository at this point in the history
- Update dependencies
- Apply new prettier defaults
  • Loading branch information
nikolay-borzov committed Mar 28, 2020
1 parent abfc01a commit e0a93e3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 46 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ gets files' paths from `nature-pack.torrent` and compares them with files from `

`--dir` (or `-d`) - Path to directory with downloaded files

`--verbose` - Output all outdated filenames. By default only first 20 filenames are displayed

`--version` - Outputs the app version

## Config files
Expand Down
43 changes: 25 additions & 18 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "torrent-clean",
"version": "1.4.2",
"version": "1.5.0",
"description": "Removes files that are not specified in selected torrent file",
"author": "Nikolay Borzov <[email protected]>",
"license": "MIT",
Expand Down Expand Up @@ -37,13 +37,13 @@
"devDependencies": {
"babel-eslint": "^10.1.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-config-standard": "^14.1.0",
"eslint-config-prettier": "^6.10.1",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"prettier": "^1.19.1"
"prettier": "^2.0.2"
}
}
6 changes: 3 additions & 3 deletions src/config-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ function loadConfig(searchFrom) {
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`
]
`.${moduleName}rc.js`,
],
})

const results = []
Expand All @@ -37,7 +37,7 @@ function loadConfig(searchFrom) {
}

const mergedConfig = results
.map(result => result.config)
.map((result) => result.config)
.reverse()
.reduce(merge, {})

Expand Down
2 changes: 1 addition & 1 deletion src/delete-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const logColor = require('./log-color')

async function deleteFiles(filenames) {
try {
const deletePromises = filenames.map(filename => unlink(filename))
const deletePromises = filenames.map((filename) => unlink(filename))
await Promise.all(deletePromises)
} catch (error) {
console.log(logColor.error('Cannot delete files'), error)
Expand Down
60 changes: 60 additions & 0 deletions src/file-select-prompt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { MultiSelect } = require('enquirer')
const utils = require('enquirer/lib/utils')

module.exports = class FilesSelect extends MultiSelect {
constructor(options) {
super(options)

this.digitsCount = (Math.log10(options.choices.length) + 1) | 0
}

// https://github.com/enquirer/enquirer/blob/master/lib/prompts/select.js#L46
async renderChoice(choice, i) {
await this.onChoice(choice, i)

const focused = this.index === i
const pointer = await this.pointer(choice, i)
const check = (await this.indicator(choice, i)) + (choice.pad || '')
let hint = await this.resolve(choice.hint, this.state, choice, i)

if (hint && !utils.hasColor(hint)) {
hint = this.styles.muted(hint)
}

const ind = this.indent(choice)
let msg = await this.choiceMessage(choice, i)

const choiceNumber = `${(choice.index + 1)
.toString()
.padStart(this.digitsCount)}|`

const line = () =>
[
this.margin[3],
ind + pointer + check,
choiceNumber,
msg,
this.margin[1],
hint,
]
.filter(Boolean)
.join(' ')

if (choice.role === 'heading') {
return line()
}

if (choice.disabled) {
if (!utils.hasColor(msg)) {
msg = this.styles.disabled(msg)
}
return line()
}

if (focused) {
msg = this.styles.em(msg)
}

return line()
}
}
31 changes: 17 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,42 @@
const os = require('os')
const path = require('path')
const recursive = require('recursive-readdir')
const { Confirm, MultiSelect } = require('enquirer')
const { Confirm } = require('enquirer')
const chalk = require('chalk')

const packageJson = require('../package.json')
const logColor = require('./log-color')
const loadConfig = require('./config-loader')
const parseTorrent = require('./parse-torrent')
const deleteFilesAndEmptyFolders = require('./delete-files')
const FilesSelect = require('./file-select-prompt')

const FILES_ON_SCREEN_LIMIT = 20

const argv = require('minimist')(process.argv.slice(2), {
alias: { torrent: ['t'], dir: ['d'], version: ['v'] },
default: { dir: process.cwd() }
default: { dir: process.cwd() },
})

if (argv.version) {
console.log(packageJson.version)
process.exit()
process.exit(0)
}

console.log(logColor.info.bold('dir:'.padEnd(10)), argv.dir)
console.log(logColor.info.bold('torrent:'.padEnd(10)), argv.torrent, os.EOL)

if (!argv.torrent) {
console.log(logColor.error(`${chalk.bold('torrent')} argument is required`))
process.exit()
process.exit(1)
}

let config
try {
config = loadConfig(argv.dir)
} catch (error) {
console.log(logColor.error('Cannot parse config file'), error)
process.exit()
process.exit(1)
}

const torrentId = argv.torrent
Expand All @@ -56,7 +57,7 @@ Promise.all([parseTorrent(torrentId), recursive(directoryPath, config.ignore)])

const rootDir = `${name}${path.sep}`
// Get absolute paths of torrent files
const torrentFiles = files.map(file =>
const torrentFiles = files.map((file) =>
path.join(directoryPath, file.replace(rootDir, ''))
)

Expand All @@ -74,14 +75,14 @@ Promise.all([parseTorrent(torrentId), recursive(directoryPath, config.ignore)])

const dirRoot = `${directoryPath}${path.sep}`

const filesChoices = extraFiles.map(filename => ({
const filesChoices = extraFiles.map((filename) => ({
name: filename.replace(dirRoot, ''),
value: filename
value: filename,
}))

const filesToDeleteMultiSelect = new MultiSelect({
const filesToDeleteMultiSelect = new FilesSelect({
name: 'selectFilesToDelete',
message: `Select file(s) to delete (Use 'Space')`,
message: `Select file(s) to delete ('space' - toggle item selection, 'i' - invert selection)`,
choices: filesChoices,
limit: FILES_ON_SCREEN_LIMIT,
indicator: '■',
Expand All @@ -92,8 +93,8 @@ Promise.all([parseTorrent(torrentId), recursive(directoryPath, config.ignore)])
},
// Get values from selected names https://github.com/enquirer/enquirer/issues/121
result(names) {
return names.map(name => this.find(name).value)
}
return names.map((name) => this.find(name).value)
},
})

const filesToDelete = await filesToDeleteMultiSelect.run()
Expand All @@ -105,7 +106,7 @@ Promise.all([parseTorrent(torrentId), recursive(directoryPath, config.ignore)])
const deleteConfirm = new Confirm({
name: 'delete',
message: 'Delete extra files?',
initial: true
initial: true,
})

const deleteFilesAnswer = await deleteConfirm.run()
Expand All @@ -122,6 +123,8 @@ Promise.all([parseTorrent(torrentId), recursive(directoryPath, config.ignore)])
console.log('No extra files found!')
}
})
.catch(error => {
.catch((error) => {
console.log(logColor.error('Error ocurred'), error)

process.exitCode = 1
})
6 changes: 3 additions & 3 deletions src/parse-torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ function parseTorrent(torrentId, onDone, onError) {

// Use memory-chunk-store to avoid creating directories inside tmp/webtorrent (https://github.com/webtorrent/webtorrent/issues/1562)
const torrent = client.add(torrentId, {
store: memoryChunkStore
store: memoryChunkStore,
})

torrent.on('error', error => {
torrent.on('error', (error) => {
onError(error)

client.destroy()
Expand All @@ -24,7 +24,7 @@ function parseTorrent(torrentId, onDone, onError) {
torrent.on('metadata', () => {
onDone({
name: torrent.name,
files: torrent.files.map(file => file.path)
files: torrent.files.map((file) => file.path),
})

client.destroy()
Expand Down

0 comments on commit e0a93e3

Please sign in to comment.