Skip to content

Commit be2d407

Browse files
Merge pull request #2 from jawa-the-hutt/dev
Added prompt at invoke to check for new project; Re-organized the template directories to allow for sharing App_Resoruces
2 parents 135f567 + 82fdcf5 commit be2d407

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+136
-81
lines changed

generator/index.js

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
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+
17
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+
}
619

720
console.log('adding to package.json');
821

@@ -61,55 +74,54 @@ module.exports = (api, options, rootOptions) => {
6174

6275
console.log('doing template rendering');
6376

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)
7379

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+
}
7587

76-
// delete the 'public' directory
7788
api.onCreateComplete(() => {
89+
7890
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');
8394

95+
// // // // delete the 'public' directory
96+
// // // const publicPath = api.resolve('public')
8497
// // // if(fs.existsSync(publicPath)) {
8598
// // // rimraf.sync(publicPath)
8699
// // // }
87100

88-
// remove any webpack.config.js file that might already be there
101+
// rename any webpack.config.js file that might already be there
89102
if(fs.existsSync(webpackConfigFile)) {
90-
fs.unlink(webpackConfigFile, (err) => {
103+
fs.rename(webpackConfigFile, './webpack.config.old', (err) => {
91104
if (err) throw err;
92-
}); }
105+
});
106+
}
93107

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) => {
97111
if (err) throw err;
98112
});
99-
}
113+
}
100114

101115
// 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')) {
103117
const replaceOptions = {
104118
files: './babel.config.js',
105119
from: ' \'@vue/app\'',
106120
to: ' process.env.VUE_PLATFORM === \'web\' ? \'@vue/app\' : {}, ' + newline + ' [\'@babel/env\', { targets: { esmodules: true } }]',
107121
}
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+
});
113125
}
114126

115127
// write out environmental files
@@ -120,36 +132,39 @@ module.exports = (api, options, rootOptions) => {
120132
const productionIOS = 'NODE_ENV=production' + newline + 'VUE_PLATFORM=ios' + newline + 'VUE_APP_MODE=native';
121133
const productionWeb = 'NODE_ENV=production' + newline + 'VUE_PLATFORM=web' + newline + 'VUE_APP_MODE=web';
122134

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;});
129141

130142

131143
// write nsconfig.json
132144
const nsconfig = {
133145
'appPath': 'src',
134146
'appResourcesPath': 'src/App_Resources'
135147
}
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;});
137149

138150
// write .gitignore additions
139-
let gitignoreContent
151+
let gitignoreContent;
140152

141153
if (fs.existsSync(gitignorePath)) {
142-
gitignoreContent = fs.readFileSync(gitignorePath, { encoding: 'utf8' })
154+
gitignoreContent = fs.readFileSync(gitignorePath, { encoding: 'utf8' });
143155
} else {
144-
gitignoreContent = ''
156+
gitignoreContent = '';
145157
}
146158

147159
const gitignoreAdditions = newline + '# NativeScript application' + newline + 'hooks' + newline + 'platforms'
148160
if (gitignoreContent.indexOf(gitignoreAdditions) === -1) {
149161
gitignoreContent += gitignoreAdditions
150162

151-
fs.writeFileSync(gitignorePath, gitignoreContent, { encoding: 'utf8' })
163+
fs.writeFileSync(gitignorePath, gitignoreContent, { encoding: 'utf8' }, (err) => {if (err) throw err;});
152164
}
153165

154166
})
155-
}
167+
168+
169+
170+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
extend: '@vue/cli-service/generator/template/src/main.js'
3+
replace:
4+
- !!js/regexp /import Vue from 'vue'/
5+
- !!js/regexp /Vue.config.productionTip = false/
6+
- !!js/regexp /h\(App\)/
7+
- !!js/regexp /}\)\.\$mount\('#app'\)/
8+
---
9+
10+
<%# REPLACE %>
11+
import Vue from 'nativescript-vue'
12+
<%# END_REPLACE %>
13+
14+
<%# REPLACE %>
15+
// Set the following to `true` to hide the logs created by nativescript-vue
16+
Vue.config.silent = false
17+
// Set the following to `false` to not colorize the logs created by nativescript-vue
18+
Vue.config.debug = true
19+
<%# END_REPLACE %>
20+
21+
<%# REPLACE %>
22+
h('frame', [h(App)])
23+
<%# END_REPLACE %>
24+
25+
<%# REPLACE %>
26+
}).$start()
27+
<%# END_REPLACE %>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
extend: '@vue/cli-service/generator/template/src/main.js'
3+
replace:
4+
- !!js/regexp /import Vue from 'vue'/
5+
- !!js/regexp /Vue.config.productionTip = false/
6+
- !!js/regexp /h\(App\)/
7+
- !!js/regexp /}\)\.\$mount\('#app'\)/
8+
---
9+
10+
<%# REPLACE %>
11+
import Vue from 'nativescript-vue'
12+
<%# END_REPLACE %>
13+
14+
<%# REPLACE %>
15+
// Set the following to `true` to hide the logs created by nativescript-vue
16+
Vue.config.silent = false
17+
// Set the following to `false` to not colorize the logs created by nativescript-vue
18+
Vue.config.debug = true
19+
<%# END_REPLACE %>
20+
21+
<%# REPLACE %>
22+
h('frame', [h(App)])
23+
<%# END_REPLACE %>
24+
25+
<%# REPLACE %>
26+
}).$start()
27+
<%# END_REPLACE %>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"android": {
3+
"v8Flags": "--expose_gc",
4+
"markingMode": "none"
5+
},
6+
"main": "main.native",
7+
"name": "<%- applicationName %>",
8+
"version": "<%- applicationVersion %>"
9+
}

generator/templates/simple/src/main.native.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

generator/templates/simple/src/main.web.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

package-lock.json

Lines changed: 7 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dependencies": {},
1919
"devDependencies": {
2020
"@vue/cli-service": "^3.1.1",
21+
"fs-extra": "^7.0.0",
2122
"nativescript-dev-webpack": "^0.16.3",
2223
"nativescript-vue-template-compiler": "^1.3.1",
2324
"node-cmd": "^3.0.0",

prompts.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,11 @@ Example: com.company.app`
2424
type: 'confirm',
2525
message: 'Use HTML5 history mode? (Default: hash mode)',
2626
default: false
27+
},
28+
{
29+
name: 'isNewProject',
30+
type: 'confirm',
31+
message: 'Is this a brand new project? (Default: No)',
32+
default: false
2733
}
2834
]

0 commit comments

Comments
 (0)