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

0 commit comments

Comments
 (0)