-
-
Save BinaryMuse/77ce3468303351c06f29 to your computer and use it in GitHub Desktop.
SO comment code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var prompt = require('prompt'); | |
// Just a stub to test functionality | |
fs.fakeExists = function(file, callback) { | |
callback(true); | |
} | |
// Another stub so we're not actually creating files | |
fs.fakeWriteFile = function(file, data, callback) { | |
callback(); | |
} | |
function startProcess(callback) { | |
prompt.get("file", function(err, results) { | |
fs.fakeExists(results.file, function(exists) { | |
if (exists) askOverwrite(results.file, callback); | |
else createFile(results.file, callback); | |
}); | |
}); | |
} | |
function askOverwrite(file, callback) { | |
console.log("File '" + file + "' exists."); | |
prompt.get("overwrite", function(err, results) { | |
if (err) return callback(err); | |
if (results.overwrite == 'yes') createFile(file, callback); | |
else abort(); | |
}); | |
} | |
function createFile(file, callback) { | |
fs.fakeWriteFile(file, "Some data", callback); | |
} | |
function abort() { | |
console.log("Aborting creation."); | |
process.exit(1); | |
} | |
startProcess(function() { | |
console.log("All done!"); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
btilley@mbair ~/src/sandbox/nodeasync node waterfall.js | |
prompt: file: test.txt | |
File 'test.txt' exists. | |
prompt: overwrite: no | |
Aborting creation. | |
btilley@mbair ~/src/sandbox/nodeasync node waterfall.js prompt: file: test.txt | |
File 'test.txt' exists. | |
prompt: overwrite: yes | |
All done! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment