From e103408bfeabfffef8862454e8640c1346696a2e Mon Sep 17 00:00:00 2001 From: Kyungil Park Date: Sat, 29 Jun 2019 02:57:45 +0900 Subject: [PATCH] Exception handling for `files` argument fix #133, #205 --- lib/cli.js | 5 +++++ lib/util.js | 2 +- test/util_test.js | 6 +++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/cli.js b/lib/cli.js index bbfeea2d..9986f5e6 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -94,5 +94,10 @@ function parseArgs(options) {// \/\\*(?:(?!\\*\/)|.|\\n)*?\\*\/ // what's left in argv args.files = process.argv.slice(parser.optind()); + if (args.files.length < 1) { + console.log("One or more files ​​are required."); + info.help(); + process.exit(1); + } return args; } diff --git a/lib/util.js b/lib/util.js index 31e638ae..1da7e428 100644 --- a/lib/util.js +++ b/lib/util.js @@ -11,7 +11,7 @@ var log = new Logger(Logger.WARNING); var path = require('path'); exports.findCommonBase = function(files) { - if (!files || files.length === 1) return ''; + if (!files || files.length < 2) return ''; var lastSlash = files[0].lastIndexOf(path.sep); if (!lastSlash) return ''; var first = files[0].substr(0, lastSlash + 1); diff --git a/test/util_test.js b/test/util_test.js index 27fa2760..85848585 100644 --- a/test/util_test.js +++ b/test/util_test.js @@ -35,7 +35,7 @@ exports['util'] = { // Store value of current path separator (environment-specific) var sep = path.sep; - test.expect(6); + test.expect(7); // Explicitly set path for OSX/*nix environment path prefixing path.sep = '/'; @@ -71,6 +71,10 @@ exports['util'] = { ]; test.equal(util.findCommonBase(files), '', 'should not find a prefix for files in the current directory'); + files = []; + test.equal(util.findCommonBase(files), '', 'should not cause an error for no files'); + + // Explicitly set path for Windows environment path prefixing path.sep = '\\';