Skip to content
Myr

Added confirmation for express(1) app generation. Closes #391

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 · parent f16978d · 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

1 file+74 added−1 removed

modified:bin/express+74 added-1 removed
13 unmodified lines
1415var version = '1.0.0rc2';1617/**18 * stdin stream.19 */2021var stdin;2223/**24 * Usage documentation.25 */2627var usage = ''
136 unmodified lines
164165// Generate application166167(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);183184/**185 * Create application at the given directory `path`.186 *187 * @param {String} path188 */189190function createApplicationAt(path) {191 mkdir(path, function(){192 mkdir(path + '/pids');193 mkdir(path + '/logs');194 mkdir(path + '/public/javascripts');
9 unmodified lines
204 write(path + '/test/app.test.js', appTest);205 });206 write(path + '/app.js', app);207 });179})(path);208}209210/**211 * Check if the given directory `path` is empty.212 *213 * @param {String} path214 * @param {Function} fn215 */216217function 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}223224/**225 * echo str > path.226 *227 * @param {String} path228 * @param {String} str
4 unmodified lines
233 sys.puts(' create : ' + path);234}235236/**237 * Prompt confirmation with the given `msg`.238 *239 * @param {String} msg240 * @param {Function} fn241 */242243function confirm(msg, fn) {244 prompt(msg, function(val){245 fn(/^ *y(es)?/i.test(val));246 });247}248249/**250 * Prompt input with the given `msg` and callback `fn`.251 *252 * @param {String} msg253 * @param {Function} fn254 */255256function 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}265266/**267 * Mkdir -p.268 *269 * @param {String} path270 * @param {Function} fn
20 unmodified lines