1
+ const path = require ( 'path' ) ;
2
+ const fs = require ( 'fs' )
3
+ const globby = require ( 'globby' ) ;
4
+ const rimraf = require ( 'rimraf' )
5
+ const replace = require ( 'replace-in-file' ) ;
6
+
1
7
module . exports = ( api , options , rootOptions ) => {
2
- const fs = require ( 'fs' )
3
- const rimraf = require ( 'rimraf' )
4
- const replace = require ( 'replace-in-file' ) ;
5
- const path = require ( 'path' ) ;
8
+
9
+ const commonRendorOptions = {
10
+ applicationName : api . generator . pkg . name ,
11
+ applicationVersion : api . generator . pkg . version ,
12
+ applicationAndroidVersionCode : api . generator . pkg . version . split ( '.' ) . join ( '0' ) ,
13
+ applicationDescription : api . generator . pkg . description || api . generator . pkg . name ,
14
+ applicationLicense : api . generator . pkg . license || 'MIT' ,
15
+ applicationId : options . applicationId ,
16
+ historyMode : options . historyMode || false ,
17
+ doesCompile : api . hasPlugin ( 'babel' ) || api . hasPlugin ( 'typescript' )
18
+ }
6
19
7
20
console . log ( 'adding to package.json' ) ;
8
21
@@ -61,55 +74,54 @@ module.exports = (api, options, rootOptions) => {
61
74
62
75
console . log ( 'doing template rendering' ) ;
63
76
64
- api . render ( './templates/simple' , {
65
- applicationName : api . generator . pkg . name ,
66
- applicationVersion : api . generator . pkg . version ,
67
- applicationAndroidVersionCode : api . generator . pkg . version . split ( '.' ) . join ( '0' ) ,
68
- applicationDescription : api . generator . pkg . description || api . generator . pkg . name ,
69
- applicationLicense : api . generator . pkg . license || 'MIT' ,
70
- applicationId : options . applicationId ,
71
- historyMode : options . historyMode || false ,
72
- } )
77
+ // render the App_Resources files
78
+ api . render ( './templates/App_Resources' , commonRendorOptions )
73
79
74
- console . log ( 'onCreateComplete' ) ;
80
+ // use the answer from the invoke prompt and if it's a new project use the new template
81
+ // and if it is an existing project, use the existing template
82
+ if ( options . isNewProject ) {
83
+ api . render ( './templates/simple/new' , commonRendorOptions )
84
+ } else {
85
+ api . render ( './templates/simple/existing' , commonRendorOptions )
86
+ }
75
87
76
- // delete the 'public' directory
77
88
api . onCreateComplete ( ( ) => {
89
+
78
90
const newline = process . platform === 'win32' ? '\r\n' : '\n' ;
79
- // // // const publicPath = api.resolve('public')
80
- const webpackConfigFile = api . resolve ( './webpack.config.js' )
81
- const main = api . resolve ( 'src/main.js' ) ;
82
- const gitignorePath = api . resolve ( '.gitignore' )
91
+ const webpackConfigFile = api . resolve ( './webpack.config.js' ) ;
92
+ const src = api . resolve ( 'src' ) ;
93
+ const gitignorePath = api . resolve ( '.gitignore' ) ;
83
94
95
+ // // // // delete the 'public' directory
96
+ // // // const publicPath = api.resolve('public')
84
97
// // // if(fs.existsSync(publicPath)) {
85
98
// // // rimraf.sync(publicPath)
86
99
// // // }
87
100
88
- // remove any webpack.config.js file that might already be there
101
+ // rename any webpack.config.js file that might already be there
89
102
if ( fs . existsSync ( webpackConfigFile ) ) {
90
- fs . unlink ( webpackConfigFile , ( err ) => {
103
+ fs . rename ( webpackConfigFile , './webpack.config.old' , ( err ) => {
91
104
if ( err ) throw err ;
92
- } ) ; }
105
+ } ) ;
106
+ }
93
107
94
- // delete main.js
95
- if ( fs . existsSync ( main ) ) {
96
- fs . unlink ( main , ( err ) => {
108
+ // rename main.js to main.web .js
109
+ if ( fs . existsSync ( path . resolve ( src , ' main.js' ) ) ) {
110
+ fs . rename ( path . resolve ( src , ' main.js' ) , path . resolve ( src , 'main.web.js' ) , ( err ) => {
97
111
if ( err ) throw err ;
98
112
} ) ;
99
- }
113
+ }
100
114
101
115
// setup string replacement options for babel.config.js file
102
- if ( fs . existsSync ( './babel.config.js' ) ) {
116
+ if ( api . hasPlugin ( 'babel' ) && fs . existsSync ( './babel.config.js' ) ) {
103
117
const replaceOptions = {
104
118
files : './babel.config.js' ,
105
119
from : ' \'@vue/app\'' ,
106
120
to : ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]' ,
107
121
}
108
- replace ( replaceOptions , ( error , changes ) => {
109
- if ( error ) {
110
- return console . error ( 'Error occurred:' , error ) ;
111
- }
112
- } )
122
+ replace ( replaceOptions , ( err , changes ) => {
123
+ if ( err ) throw err ;
124
+ } ) ;
113
125
}
114
126
115
127
// write out environmental files
@@ -120,36 +132,39 @@ module.exports = (api, options, rootOptions) => {
120
132
const productionIOS = 'NODE_ENV=production' + newline + 'VUE_PLATFORM=ios' + newline + 'VUE_APP_MODE=native' ;
121
133
const productionWeb = 'NODE_ENV=production' + newline + 'VUE_PLATFORM=web' + newline + 'VUE_APP_MODE=web' ;
122
134
123
- fs . writeFileSync ( './.env.development.android' , developmentAndroid , { encoding : 'utf8' } )
124
- fs . writeFileSync ( './.env.development.ios' , developmentIOS , { encoding : 'utf8' } )
125
- fs . writeFileSync ( './.env.development.web' , developmentWeb , { encoding : 'utf8' } )
126
- fs . writeFileSync ( './.env.production.android' , productionAndroid , { encoding : 'utf8' } )
127
- fs . writeFileSync ( './.env.production.ios' , productionIOS , { encoding : 'utf8' } )
128
- fs . writeFileSync ( './.env.production.web' , productionWeb , { encoding : 'utf8' } )
135
+ fs . writeFileSync ( './.env.development.android' , developmentAndroid , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
136
+ fs . writeFileSync ( './.env.development.ios' , developmentIOS , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
137
+ fs . writeFileSync ( './.env.development.web' , developmentWeb , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
138
+ fs . writeFileSync ( './.env.production.android' , productionAndroid , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
139
+ fs . writeFileSync ( './.env.production.ios' , productionIOS , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
140
+ fs . writeFileSync ( './.env.production.web' , productionWeb , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
129
141
130
142
131
143
// write nsconfig.json
132
144
const nsconfig = {
133
145
'appPath' : 'src' ,
134
146
'appResourcesPath' : 'src/App_Resources'
135
147
}
136
- fs . writeFileSync ( './nsconfig.json' , JSON . stringify ( nsconfig , null , 2 ) , { encoding : 'utf8' } ) ;
148
+ fs . writeFileSync ( './nsconfig.json' , JSON . stringify ( nsconfig , null , 2 ) , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
137
149
138
150
// write .gitignore additions
139
- let gitignoreContent
151
+ let gitignoreContent ;
140
152
141
153
if ( fs . existsSync ( gitignorePath ) ) {
142
- gitignoreContent = fs . readFileSync ( gitignorePath , { encoding : 'utf8' } )
154
+ gitignoreContent = fs . readFileSync ( gitignorePath , { encoding : 'utf8' } ) ;
143
155
} else {
144
- gitignoreContent = ''
156
+ gitignoreContent = '' ;
145
157
}
146
158
147
159
const gitignoreAdditions = newline + '# NativeScript application' + newline + 'hooks' + newline + 'platforms'
148
160
if ( gitignoreContent . indexOf ( gitignoreAdditions ) === - 1 ) {
149
161
gitignoreContent += gitignoreAdditions
150
162
151
- fs . writeFileSync ( gitignorePath , gitignoreContent , { encoding : 'utf8' } )
163
+ fs . writeFileSync ( gitignorePath , gitignoreContent , { encoding : 'utf8' } , ( err ) => { if ( err ) throw err ; } ) ;
152
164
}
153
165
154
166
} )
155
- }
167
+
168
+
169
+
170
+ }
0 commit comments