From 5a83ffab9447525c8bb413c955e14b7eaaa42931 Mon Sep 17 00:00:00 2001 From: skv Date: Fri, 15 May 2015 10:51:55 +0300 Subject: [PATCH] ask confirmation on init command if directory already exist --- react-native-cli/index.js | 30 ++++++++++++++++++++++++++++++ react-native-cli/package.json | 3 +++ 2 files changed, 33 insertions(+) diff --git a/react-native-cli/index.js b/react-native-cli/index.js index 7bd237e02..02a1cfdd2 100755 --- a/react-native-cli/index.js +++ b/react-native-cli/index.js @@ -7,6 +7,7 @@ var fs = require('fs'); var path = require('path'); var spawn = require('child_process').spawn; +var prompt = require("prompt"); var CLI_MODULE_PATH = function() { return path.resolve( @@ -65,6 +66,35 @@ function init(name) { process.exit(1); } + if (fs.existsSync(name)) { + createAfterConfirmation(name) + } else { + createProject(name); + } +} + +function createAfterConfirmation(name) { + prompt.start(); + + var property = { + name: 'yesno', + message: 'Directory ' + name + ' already exist. Continue?', + validator: /y[es]*|n[o]?/, + warning: 'Must respond yes or no', + default: 'no' + }; + + prompt.get(property, function (err, result) { + if (result.yesno[0] === 'y') { + createProject(name); + } else { + console.log('Project initialization canceled'); + process.exit(); + } + }); +} + +function createProject(name) { var root = path.resolve(name); var projectName = path.basename(root); diff --git a/react-native-cli/package.json b/react-native-cli/package.json index 8644e7eae..90450c3ea 100644 --- a/react-native-cli/package.json +++ b/react-native-cli/package.json @@ -5,5 +5,8 @@ "main": "index.js", "bin": { "react-native": "index.js" + }, + "dependencies": { + "prompt": "^0.2.14" } }