Merge branch 'confirmation'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents f16978d, ec286ee · Browse files · 1 file·+74 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+74 added−1 removed
- other:+74 added−1 removed
Provenance
- Commit
21a9454- Tree
dcc0650- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent 1
f16978d- Parent 2
ec286ee
1 file changed+74 added−1 removed
modified:bin/express+74 added-1 removed
13 unmodified lines14 15 var version = '1.0.0rc2';16 17 /**18+ * stdin stream.19+ */20+21+var stdin;22+23+/**24 * Usage documentation.25 */26 27 var usage = ''136 unmodified lines164 165 // Generate application166 167 (function createApplication(path) {168+ emptyDirectory(path, function(empty){169+ if (empty) {170+ createApplicationAt(path);171+ } else {172+ confirm('destination is not empty, continue? ', function(ok){173+ if (ok) {174+ stdin.destroy();175+ createApplicationAt(path);176+ } else {177+ abort('aborting');178+ }179+ });180+ }181+ });182+})(path);183+184+/**185+ * Create application at the given directory `path`.186+ *187+ * @param {String} path188+ */189+190+function createApplicationAt(path) {191 mkdir(path, function(){192 mkdir(path + '/pids');193 mkdir(path + '/logs');194 mkdir(path + '/public/javascripts');9 unmodified lines204 write(path + '/test/app.test.js', appTest);205 });206 write(path + '/app.js', app);207 });179-})(path);208+}209 210 /**211+ * Check if the given directory `path` is empty.212+ *213+ * @param {String} path214+ * @param {Function} fn215+ */216+217+function emptyDirectory(path, fn) {218+ fs.readdir(path, function(err, files){219+ if (err && err.errno !== process.ENOENT) throw err;220+ fn(!files || !files.length);221+ });222+}223+224+/**225 * echo str > path.226 *227 * @param {String} path228 * @param {String} str4 unmodified lines233 sys.puts(' create : ' + path);234 }235 236 /**237+ * Prompt confirmation with the given `msg`.238+ *239+ * @param {String} msg240+ * @param {Function} fn241+ */242+243+function confirm(msg, fn) {244+ prompt(msg, function(val){245+ fn(/^ *y(es)?/i.test(val));246+ });247+}248+249+/**250+ * Prompt input with the given `msg` and callback `fn`.251+ *252+ * @param {String} msg253+ * @param {Function} fn254+ */255+256+function prompt(msg, fn) {257+ stdin = stdin || process.openStdin();258+ sys[msg[msg.length - 1] == ' ' ? 'print' : 'puts'](msg);259+ stdin.setEncoding('ascii');260+ stdin.addListener('data', function(data){261+ fn(data);262+ stdin.removeListener('data', arguments.callee);263+ });264+}265+266+/**267 * Mkdir -p.268 *269 * @param {String} path270 * @param {Function} fn20 unmodified lines