Skip to content

Instantly share code, notes, and snippets.

@timoxley
Created January 28, 2012 05:35
Show Gist options
  • Save timoxley/1692870 to your computer and use it in GitHub Desktop.
Save timoxley/1692870 to your computer and use it in GitHub Desktop.
figure out how to write file from src/dir file/dir status
// figures out how to write the file based on source, destination
// eg
// source = a/f.jpg
// dest = /tmp
// result = /tmp/f.jpg
//
// source = a/f.jpg
// dest = tmp/g.jpg
// result = tmp/g.jpg
function getWritePathFromSource(source, destination, callback) {
var path = require('path')
var fs = require('fs')
path.exists(destination, function(err, exists) {
if (exists) {
fs.stat(destination, function(stat) {
stat.isFile(function(err, isFile) {
if (isFile) {
callback(null, path.normalize(destination))
} else {
var filename = path.basename(source)
callback(null, path.join(destination,filename))
}
})
})
} else {
if (path.extname(destination) === '') {
var filename = path.basename(source)
callback(null, path.join(destination, filename))
} else {
callback(null, path.normalize(destination))
}
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment