diff --git a/index.js b/index.js index 09c41fd..ddaffa7 100755 --- a/index.js +++ b/index.js @@ -3,49 +3,61 @@ var program = require('commander') var childProcess = require('child_process') var fs = require('fs') +var Listr = require('listr') var port = process.env.port || 3000 -function startCommand(command, callback) { - var child = childProcess.exec(command, callback) - child.stdout.pipe(process.stdout) - child.stderr.pipe(process.stderr) - return child -} - -function init() { - - console.log('Creating new bot...') - - fs.readFile(__dirname + '/templates/index.js', function(err, data) { - if (err) throw err - - console.log('Copying index.js...') - - fs.writeFile('index.js', data, function(err) { - if (err) throw err - - console.log('Copied index.js...') +function copyFile(src, dest) { + return new Promise((resolve, reject) => { + fs.readFile(src, function(err1, data) { + if (err1) { + reject() + return + } + + fs.writeFile(dest, data, function(err2) { + if (err2) { + reject() + return + } + + resolve(); + }) }) }) +} - fs.readFile(__dirname + '/templates/package.json', function(err, data) { - if (err) throw err - - console.log('Copying Package.json...') - - fs.writeFile('package.json', data, function(err) { - if (err) throw err - - console.log('Copied Package.json...') - console.log('Installing Dependencies...') - - startCommand('npm install') - }) - }) +function init() { + var tasks = new Listr([ + { + title: 'Copy index.js', + task: () => copyFile( + __dirname + '/templates/index.js', + 'index.js' + ), + }, + { + title: 'Copy package.json', + task: () => copyFile( + __dirname + '/templates/package.json', + 'package.json' + ), + }, + { + title: 'Installing dependencies', + task: () => new Promise((resolve, reject) => { + var child = childProcess.exec('npm install') + child.on('close', () => { + resolve(); + }); + }), + }, + ]); + + tasks.run(); } function startServer() { - return startCommand('node .', function (error, stdout, stderr) { + childProcess.exec('node .', (err, stdout, stderr) => { process.exit(1) }) } diff --git a/package.json b/package.json index acb7d5d..4145b84 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,8 @@ "main": "index.js", "dependencies": { "commander": "^2.9.0", - "eslint": "^3.7.1" + "eslint": "^3.7.1", + "listr": "^0.6.1" }, "devDependencies": {}, "scripts": {