-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
107 lines (90 loc) 路 3.72 KB
/
Copy pathapp.js
File metadata and controls
107 lines (90 loc) 路 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env node
/* Only working on macOS, how could i handle it on Windows and Linux ? */
var ncp = require('ncp').ncp
var program = require('commander')
var chalk = require('chalk')
var sys = require('util')
var exec = require('child_process').exec
var spinner = require("char-spinner")
program
.arguments('<serverName>')
.action(function(serverName) {
startProcess(serverName)
})
.parse(process.argv)
function startProcess(serverName) {
exec('mkdir ' + serverName + ' && pwd',
function(error, stdout, stderr) {
if (typeof error !== 'undefined' && error !== null) {
return errorHandler(error, undefined)
} else if (typeof stderr !== 'undefined' && stderr !== null && stderr !== '') {
return stdErrorHandler(error, undefined)
}
var to = stdout.substring(0, stdout.length - 1)
startCopy(to, serverName)
})
}
function startCopy(to, serverName) {
console.log(chalk.bold.green('馃悪 express-create is creating a new server template on ' + to + '/' + serverName))
var interval = spinner()
var from = ''
exec('echo $NVM_BIN', function(error, stdout, stderr) {
from = stdout.substring(0, stdout.length - 1)
if (typeof error !== 'undefined' && error !== null) {
return errorHandler(error, interval)
} else if (typeof stderr !== 'undefined' && stderr !== null && stderr !== '') {
return stdErrorHandler(error, interval)
} else {
return copyFiles(from, to, serverName, interval)
}
})
}
function errorHandler(error, interval) {
console.log('')
console.error(chalk.bold.red("node exec() error: "))
console.error(chalk.red(error))
clearInterval(interval)
}
function stdErrorHandler(error, interval) {
console.log('')
console.error(chalk.bold.red("stderr error: "))
console.error(chalk.red(stderr))
clearInterval(interval)
}
function ncpError(err, interval) {
console.log('')
console.error(chalk.bold.red("ncp error: "))
console.error(chalk.red(err))
clearInterval(interval)
}
function copyFiles(from, to, serverName, interval) {
if (typeof from !== 'undefined' && from !== null && from !== '') {
console.log('')
console.log(chalk.bold.cyan('Detecting NVM enviroment on your computer.'))
console.log(chalk.bold.cyan('Taking the template from the corresponding node modules...'))
from = from + "/../lib/node_modules/create-express/template/"
} else {
console.log('')
console.log(chalk.bold.cyan('Detecting non NVM enviroment on your computer.'))
console.log(chalk.bold.cyan('Taking the template from the corresponding node modules...'))
from = '/usr/local/lib/node_modules/create-express/template/'
//TODO: This is the only one?
// Could there be other paths for node_modules in a UNIX enviroment?
// Is enoghth for Linux users too?
//from = '/usr/local/node_modules/create-express/template/'
//from = '/usr/lib/node_modules/create-express/template/'
//from = '/usr/local/bin/create-express/template/'
}
ncp(from, to + '/' + serverName, function(err) {
if (err) {
return ncpError(err)
}
clearInterval(interval)
console.log('')
console.log(chalk.bold.green('Your template is ready! 馃嵒 '))
console.log('')
console.log(chalk.bold.white('Start making some awsome things, to start coding just make:'))
console.log('')
console.log(chalk.bold.cyan('cd ' + serverName) + chalk.white(' && ') + chalk.bold.cyan('yarn install') + chalk.white(' && ') + chalk.bold.cyan('npm run dev'))
})
}