From 97e19957cee6c1f0289331a8440fd8806645785a Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Fri, 24 Feb 2017 23:39:31 -0700 Subject: [PATCH] * Default upload descriptions based on file names if no FILE_ID.DIZ/etc. could be found * Add default desc & prompt for desc to oputil fb scan --- core/file_base_area.js | 9 ++++++++ core/file_entry.js | 5 +++-- core/oputil/oputil_common.js | 9 +++++++- core/oputil/oputil_file_base.js | 38 ++++++++++++++++++++++++++++++++- core/oputil/oputil_help.js | 10 ++++----- core/oputil/oputil_main.js | 2 +- mods/file_area_list.js | 2 +- mods/upload.js | 5 +++-- 8 files changed, 67 insertions(+), 13 deletions(-) diff --git a/core/file_base_area.js b/core/file_base_area.js index c2d83c22..48b98c24 100644 --- a/core/file_base_area.js +++ b/core/file_base_area.js @@ -32,6 +32,7 @@ exports.getFileEntryPath = getFileEntryPath; exports.changeFileAreaWithOptions = changeFileAreaWithOptions; exports.scanFile = scanFile; exports.scanFileAreaForChanges = scanFileAreaForChanges; +exports.getDescFromFileName = getDescFromFileName; const WellKnownAreaTags = exports.WellKnownAreaTags = { Invalid : '', @@ -670,3 +671,11 @@ function scanFileAreaForChanges(areaInfo, options, iterator, cb) { return cb(err); }); } + +function getDescFromFileName(fileName) { + // :TODO: this method could use some more logic to really be nice. + const ext = paths.extname(fileName); + const name = paths.basename(fileName, ext); + + return _.upperFirst(name.replace(/[\-_.+]/g, ' ').replace(/\s+/g, ' ')); +} \ No newline at end of file diff --git a/core/file_entry.js b/core/file_entry.js index 3402ddbf..2baee1e2 100644 --- a/core/file_entry.js +++ b/core/file_entry.js @@ -290,8 +290,9 @@ module.exports = class FileEntry { } } - // :TODO: Use static get accessor: - static getWellKnownMetaValues() { return Object.keys(FILE_WELL_KNOWN_META); } + static get WellKnownMetaValues() { + return Object.keys(FILE_WELL_KNOWN_META); + } static findFileBySha(sha, cb) { // full or partial SHA-256 diff --git a/core/oputil/oputil_common.js b/core/oputil/oputil_common.js index dd17aecc..21e1a6d0 100644 --- a/core/oputil/oputil_common.js +++ b/core/oputil/oputil_common.js @@ -23,7 +23,14 @@ const exitCodes = exports.ExitCodes = { BAD_ARGS : -3, }; -const argv = exports.argv = require('minimist')(process.argv.slice(2)); +const argv = exports.argv = require('minimist')(process.argv.slice(2), { + alias : { + h : 'help', + v : 'version', + c : 'config', + n : 'no-prompt', + } +}); function printUsageAndSetExitCode(errMsg, exitCode) { if(_.isUndefined(exitCode)) { diff --git a/core/oputil/oputil_file_base.js b/core/oputil/oputil_file_base.js index 17cdf3d1..bc1d6381 100644 --- a/core/oputil/oputil_file_base.js +++ b/core/oputil/oputil_file_base.js @@ -9,12 +9,14 @@ const initConfigAndDatabases = require('./oputil_common.js').initConfigAndDataba const getHelpFor = require('./oputil_help.js').getHelpFor; const getAreaAndStorage = require('./oputil_common.js').getAreaAndStorage; const Errors = require('../../core/enig_error.js').Errors; +const getDescFromFileName = require('../../core/file_base_area.js').getDescFromFileName; const async = require('async'); const fs = require('fs'); const paths = require('path'); const _ = require('lodash'); const moment = require('moment'); +const inq = require('inquirer'); exports.handleFileBaseCommand = handleFileBaseCommand; @@ -33,6 +35,40 @@ exports.handleFileBaseCommand = handleFileBaseCommand; let fileArea; // required during init +function finalizeEntryAndPersist(fileEntry, cb) { + async.series( + [ + function getDescIfNeeded(callback) { + if(false === argv.prompt || ( fileEntry.desc && fileEntry.desc.length > 0 ) ) { + return callback(null); + } + + const questions = [ + { + name : 'desc', + message : `Description for ${fileEntry.fileName}:`, + type : 'input', + default : getDescFromFileName(fileEntry.fileName), + } + ]; + + inq.prompt(questions).then( answers => { + fileEntry.desc = answers.desc; + return callback(null); + }); + }, + function persist(callback) { + fileEntry.persist( err => { + return callback(err); + }); + } + ], + err => { + return cb(err); + } + ); +} + function scanFileAreaForChanges(areaInfo, options, cb) { const storageLocations = fileArea.getAreaStorageLocations(areaInfo).filter(sl => { @@ -94,7 +130,7 @@ function scanFileAreaForChanges(areaInfo, options, cb) { }); } - fileEntry.persist( err => { + finalizeEntryAndPersist(fileEntry, err => { return nextFile(err); }); } diff --git a/core/oputil/oputil_help.js b/core/oputil/oputil_help.js index bd6093f1..4ee6d7ac 100644 --- a/core/oputil/oputil_help.js +++ b/core/oputil/oputil_help.js @@ -12,13 +12,13 @@ const usageHelp = exports.USAGE_HELP = { [] global args: - --config PATH : specify config path (${getDefaultConfigPath()}) - --no-prompt : assume defaults/don't prompt for input where possible + -c, --config PATH : specify config path (${getDefaultConfigPath()}) + -n, --no-prompt : assume defaults/don't prompt for input where possible where is one of: - user : user utilities - config : config file management - fb : file base management + user : user utilities + config : config file management + fb : file base management `, User : diff --git a/core/oputil/oputil_main.js b/core/oputil/oputil_main.js index dad7e4e6..83af6b5e 100644 --- a/core/oputil/oputil_main.js +++ b/core/oputil/oputil_main.js @@ -22,7 +22,7 @@ module.exports = function() { if(0 === argv._.length || 'help' === argv._[0]) { - printUsageAndSetExitCode(getHelpFor('General'), ExitCodes.SUCCESS); + return printUsageAndSetExitCode(getHelpFor('General'), ExitCodes.SUCCESS); } switch(argv._[0]) { diff --git a/mods/file_area_list.js b/mods/file_area_list.js index 88163b0e..0a0e2134 100644 --- a/mods/file_area_list.js +++ b/mods/file_area_list.js @@ -229,7 +229,7 @@ exports.getModule = class FileAreaList extends MenuModule { // We need the entry object to contain meta keys even if they are empty as // consumers may very likely attempt to use them // - const metaValues = FileEntry.getWellKnownMetaValues(); + const metaValues = FileEntry.WellKnownMetaValues; metaValues.forEach(name => { const value = !_.isUndefined(currEntry.meta[name]) ? currEntry.meta[name] : 'N/A'; entryInfo[_.camelCase(name)] = value; diff --git a/mods/upload.js b/mods/upload.js index 5faf0d76..b043ebd2 100644 --- a/mods/upload.js +++ b/mods/upload.js @@ -8,6 +8,7 @@ const getSortedAvailableFileAreas = require('../core/file_base_area.js').getSor const getAreaDefaultStorageDirectory = require('../core/file_base_area.js').getAreaDefaultStorageDirectory; const scanFile = require('../core/file_base_area.js').scanFile; const getFileAreaByTag = require('../core/file_base_area.js').getFileAreaByTag; +const getDescFromFileName = require('../core/file_base_area.js').getDescFromFileName; const ansiGoto = require('../core/ansi_term.js').goto; const moveFileWithCollisionHandling = require('../core/file_util.js').moveFileWithCollisionHandling; const pathWithTerminatingSeparator = require('../core/file_util.js').pathWithTerminatingSeparator; @@ -673,7 +674,7 @@ exports.getModule = class UploadModule extends MenuModule { tagsView.setText( Array.from(fileEntry.hashTags).join(',') ); // :TODO: optional 'hashTagsSep' like file list/browse yearView.setText(fileEntry.meta.est_release_year || ''); - + if(self.fileEntryHasDetectedDesc(fileEntry)) { descView.setPropertyValue('mode', 'preview'); descView.setText(fileEntry.desc); @@ -681,7 +682,7 @@ exports.getModule = class UploadModule extends MenuModule { self.viewControllers.fileDetails.switchFocus(MciViewIds.fileDetails.tags); } else { descView.setPropertyValue('mode', 'edit'); - descView.setText(''); + descView.setText(getDescFromFileName(fileEntry.fileName)); // try to come up with something good as a default descView.acceptsFocus = true; self.viewControllers.fileDetails.switchFocus(MciViewIds.fileDetails.desc); }