1
1
#!/usr/bin/env node
2
2
3
- var download = require ( 'download-git-repo' )
4
- var program = require ( 'commander' )
5
- var exists = require ( 'fs' ) . existsSync
6
- var path = require ( 'path' )
7
- var ora = require ( 'ora' )
8
- var home = require ( 'user-home' )
9
- var tildify = require ( 'tildify' )
10
- var chalk = require ( 'chalk' )
11
- var inquirer = require ( 'inquirer' )
12
- var rm = require ( 'rimraf' ) . sync
13
- var logger = require ( '../lib/logger' )
14
- var generate = require ( '../lib/generate' )
15
- var checkVersion = require ( '../lib/check-version' )
16
- var warnings = require ( '../lib/warnings' )
17
- var localPath = require ( '../lib/local-path' )
18
-
19
- var isLocalPath = localPath . isLocalPath
20
- var getTemplatePath = localPath . getTemplatePath
3
+ const download = require ( 'download-git-repo' )
4
+ const program = require ( 'commander' )
5
+ const exists = require ( 'fs' ) . existsSync
6
+ const path = require ( 'path' )
7
+ const ora = require ( 'ora' )
8
+ const home = require ( 'user-home' )
9
+ const tildify = require ( 'tildify' )
10
+ const chalk = require ( 'chalk' )
11
+ const inquirer = require ( 'inquirer' )
12
+ const rm = require ( 'rimraf' ) . sync
13
+ const logger = require ( '../lib/logger' )
14
+ const generate = require ( '../lib/generate' )
15
+ const checkVersion = require ( '../lib/check-version' )
16
+ const warnings = require ( '../lib/warnings' )
17
+ const localPath = require ( '../lib/local-path' )
18
+
19
+ const isLocalPath = localPath . isLocalPath
20
+ const getTemplatePath = localPath . getTemplatePath
21
21
22
22
/**
23
23
* Usage.
@@ -32,7 +32,7 @@ program
32
32
* Help.
33
33
*/
34
34
35
- program . on ( '--help' , function ( ) {
35
+ program . on ( '--help' , ( ) => {
36
36
console . log ( ' Examples:' )
37
37
console . log ( )
38
38
console . log ( chalk . gray ( ' # create a new project with an official template' ) )
@@ -57,15 +57,15 @@ help()
57
57
* Settings.
58
58
*/
59
59
60
- var template = program . args [ 0 ]
61
- var hasSlash = template . indexOf ( '/' ) > - 1
62
- var rawName = program . args [ 1 ]
63
- var inPlace = ! rawName || rawName === '.'
64
- var name = inPlace ? path . relative ( '../' , process . cwd ( ) ) : rawName
65
- var to = path . resolve ( rawName || '.' )
66
- var clone = program . clone || false
60
+ let template = program . args [ 0 ]
61
+ const hasSlash = template . indexOf ( '/' ) > - 1
62
+ const rawName = program . args [ 1 ]
63
+ const inPlace = ! rawName || rawName === '.'
64
+ const name = inPlace ? path . relative ( '../' , process . cwd ( ) ) : rawName
65
+ const to = path . resolve ( rawName || '.' )
66
+ const clone = program . clone || false
67
67
68
- var tmp = path . join ( home , '.vue-templates' , template . replace ( / \/ / g, '-' ) )
68
+ const tmp = path . join ( home , '.vue-templates' , template . replace ( / \/ / g, '-' ) )
69
69
if ( program . offline ) {
70
70
console . log ( `> Use cached template at ${ chalk . yellow ( tildify ( tmp ) ) } ` )
71
71
template = tmp
@@ -76,7 +76,7 @@ if (program.offline) {
76
76
*/
77
77
78
78
console . log ( )
79
- process . on ( 'exit' , function ( ) {
79
+ process . on ( 'exit' , ( ) => {
80
80
console . log ( )
81
81
} )
82
82
@@ -87,11 +87,11 @@ if (exists(to)) {
87
87
? 'Generate project in current directory?'
88
88
: 'Target directory exists. Continue?' ,
89
89
name : 'ok'
90
- } ] , function ( answers ) {
90
+ } ] ) . then ( answers => {
91
91
if ( answers . ok ) {
92
92
run ( )
93
93
}
94
- } )
94
+ } ) . catch ( logger . fatal )
95
95
} else {
96
96
run ( )
97
97
}
@@ -103,9 +103,9 @@ if (exists(to)) {
103
103
function run ( ) {
104
104
// check if template is local
105
105
if ( isLocalPath ( template ) ) {
106
- var templatePath = getTemplatePath ( template )
106
+ const templatePath = getTemplatePath ( template )
107
107
if ( exists ( templatePath ) ) {
108
- generate ( name , templatePath , to , function ( err ) {
108
+ generate ( name , templatePath , to , err => {
109
109
if ( err ) logger . fatal ( err )
110
110
console . log ( )
111
111
logger . success ( 'Generated "%s".' , name )
@@ -114,10 +114,10 @@ function run () {
114
114
logger . fatal ( 'Local template "%s" not found.' , template )
115
115
}
116
116
} else {
117
- checkVersion ( function ( ) {
117
+ checkVersion ( ( ) => {
118
118
if ( ! hasSlash ) {
119
119
// use official templates
120
- var officialTemplate = 'vuejs-templates/' + template
120
+ const officialTemplate = 'vuejs-templates/' + template
121
121
if ( template . indexOf ( '#' ) !== - 1 ) {
122
122
downloadAndGenerate ( officialTemplate )
123
123
} else {
@@ -143,14 +143,14 @@ function run () {
143
143
*/
144
144
145
145
function downloadAndGenerate ( template ) {
146
- var spinner = ora ( 'downloading template' )
146
+ const spinner = ora ( 'downloading template' )
147
147
spinner . start ( )
148
148
// Remove if local template exists
149
149
if ( exists ( tmp ) ) rm ( tmp )
150
- download ( template , tmp , { clone : clone } , function ( err ) {
150
+ download ( template , tmp , { clone } , err => {
151
151
spinner . stop ( )
152
152
if ( err ) logger . fatal ( 'Failed to download repo ' + template + ': ' + err . message . trim ( ) )
153
- generate ( name , tmp , to , function ( err ) {
153
+ generate ( name , tmp , to , err => {
154
154
if ( err ) logger . fatal ( err )
155
155
console . log ( )
156
156
logger . success ( 'Generated "%s".' , name )
0 commit comments