Skip to content

Commit

Permalink
Better handling of epipe errors, verbose option
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hotmann committed Apr 13, 2017
1 parent 785a64c commit a3f4750
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
18 changes: 17 additions & 1 deletion bin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
#!/usr/bin/env node
// Handle EPIPE errors when user doesn't put quotes around output file name with parameters
function epipeError(err) {
if (err.code === 'EPIPE' || err.errno === 32) return process.exit;

if (process.stdout.listeners('error').length <= 1) {
process.stdout.removeAllListeners();
process.stdout.emit('error', err);
process.stdout.on('error', epipeError);
}
}

process.stdout.on('error', epipeError);

const argv = require('yargs')
.usage('Rename-CLI v' + require('./package.json').version + '\n\nUsage:\n\n rename [options] files new-file-name')
.options({
Expand Down Expand Up @@ -32,6 +45,9 @@ const argv = require('yargs')
alias: 'noindex',
boolean: true,
describe: 'Do not append an index when renaming multiple files'
}, 'verbose' : {
boolean: true,
describe: 'Print all rename operations completed'
}
})
.help('help')
Expand Down Expand Up @@ -69,7 +85,7 @@ function parseArgs() {
console.log('');
console.log(index.getReplacements());
process.exit(0);
} else if (argv.i) {
} else if (argv.i) { // view online hlep
opn('https://github.com/jhotmann/node-rename-cli');
if (process.platform !== 'win32') {
process.exit(0);
Expand Down
26 changes: 16 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ const replacements = _.merge(defaultReplacements, userReplacements);

module.exports = {
thecommand: function(args) {
if (args.s) {
console.log('');
}
let newFileName = path.parse(_.last(args._));
let files = _.dropRight(args._);
if (files.length === 0) {
Expand Down Expand Up @@ -93,28 +90,38 @@ module.exports = {
fileObj.newName = fileObj.newName + replacements.i.function(fileObj, '1');
}

// SIMULATED if argument --s just print what the output would be
if (args.s) {
console.log(fileObj.base + ' → ' + fileObj.newName + fileObj.newNameExt);
let operationText = fileObj.base + ' → ' + fileObj.newName + fileObj.newNameExt;

if (args.s) { // SIMULATED if argument --s just print what the output would be
operations.push({ text: operationText });
} else { // RENAME the file(s)
let originalFileName = path.format({dir: fileObj.dir, base: fileObj.base});
let outputFileName = path.format({dir: fileObj.dir, base: fileObj.newName + fileObj.newNameExt});

// FILE EXISTS if output file name already exists prompt for overwrite unless --f specified.
if (!args.f && fileExists.sync(outputFileName)) {
if (!args.f && originalFileName !== outputFileName && fileExists.sync(outputFileName)) {
let response = prompt(fileObj.newName + fileObj.newNameExt + ' already exists. Would you like to replace it? (y/n) ');
if (response === 'y') {
renameFile(originalFileName, outputFileName);
operations.push({original: originalFileName, output: outputFileName});
operations.push({text: operationText, original: originalFileName, output: outputFileName});
}
} else {
renameFile(originalFileName, outputFileName);
operations.push({original: originalFileName, output: outputFileName});
operations.push({text: operationText, original: originalFileName, output: outputFileName});
}
}
fileIndex += 1;
});

// PRINT simulated or verbose text
if ((args.s || args.verbose) && operations.length > 0) {
console.log('');
_.forEach(operations, function(value) {
console.log(value.text);
});
console.log('');
}

// WRITE OPERATIONS so they can be undone if desired
if (!args.s) {
writeUndoFile(operations);
Expand All @@ -137,7 +144,6 @@ module.exports = {
fs.readJSON(undoFile, (err, packageObj) => {
if (err) throw err;
_.forEach(packageObj, function(value) {
//console.dir(value);
let originalFileName = value.original;
let outputFileName = value.output;
renameFile(outputFileName, originalFileName);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rename-cli",
"version": "2.1.0",
"version": "2.1.1",
"description": "A command line utility for renaming files",
"main": "index.js",
"preferGlobal": true,
Expand Down

0 comments on commit a3f4750

Please sign in to comment.