Skip to content

Commit

Permalink
better empty file checkig and error msgs
Browse files Browse the repository at this point in the history
  • Loading branch information
mhkeller committed Jan 10, 2016
1 parent 19b1d9a commit 34132d1
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,23 @@ var formatters = {
return JSON.stringify(file)
},
csv: function (file) {
if (!_.isArray(file)) {
reporters.notListError('csv')
}
file = formattingPreflight(file, 'csv')
try {
return dsv.csv.format(file)
} catch (err) {
reporters.parseError('csv')
}
},
tsv: function (file) {
if (!_.isArray(file)) {
reporters.notListError('tsv')
}
file = formattingPreflight(file, 'tsv')
try {
return dsv.tsv.format(file)
} catch (err) {
reporters.parseError('tsv')
}
},
psv: function (file) {
if (!_.isArray(file)) {
reporters.notListError('psv')
}
file = formattingPreflight(file, 'psv')
try {
return dsv.dsv('|').format(file)
} catch (err) {
Expand Down Expand Up @@ -95,7 +89,7 @@ reporters.parseError = function (format) {
}

reporters.notListError = function (format) {
throw new Error(chalk.red('[indian-ocean] You passed in an object but converting to ' + chalk.bold(format) + ' requires a list of objects.') + chalk.cyan('\nIf you would like to write a one-row csv, put your object in a list like so: ' + chalk.bold('[data]') + '\n'))
throw new Error(chalk.red('[indian-ocean] You passed in an object but converting to ' + chalk.bold(format) + ' requires a list of objects.') + chalk.cyan('\nIf you would like to write a one-row csv, put your object in a list like so: `' + chalk.bold('[data]') + '`\n'))
}

/**
Expand Down Expand Up @@ -300,6 +294,16 @@ helpers.matches = function (fileName, matcher) {
}
}

// Some shared data integrity checks for formatters
function formattingPreflight (file, format) {
if (file === '') {
return []
} else if (!_.isArray(file)) {
reporters.notListError(format)
}
return file
}

// Our `readData` fns can take either a delimiter to parse a file, or a full blown parser
// Determine what they passed in with this handy function
function getDelimiterOrParser (delimiterOrParser) {
Expand Down Expand Up @@ -931,7 +935,7 @@ writers.writeData = function (outPath, data, opts_, cb) {
cb = opts_
}
if (!data) {
reporters.warn('You didn\'t pass any data to write for file: ' + chalk.bold(outPath))
reporters.warn('You didn\'t pass any data to write for file: ' + chalk.bold(outPath) + ' Writing out an empty file...')
}

if (_.isObject(opts_) && opts_.makeDirectories) {
Expand Down Expand Up @@ -968,8 +972,8 @@ writers.writeData = function (outPath, data, opts_, cb) {
*
*/
writers.writeDataSync = function (outPath, data, opts_) {
if (!data) {
reporters.warn('You didn\'t pass any data to write for file: ' + chalk.bold(outPath))
if (_.isEmpty(data)) {
reporters.warn('You didn\'t pass any data to write for file: ' + chalk.bold(outPath) + ' Writing out an empty file...')
}
if (opts_ && opts_.makeDirectories) {
helpers.makeDirectoriesSync(outPath)
Expand Down

0 comments on commit 34132d1

Please sign in to comment.