-
Notifications
You must be signed in to change notification settings - Fork 9
/
boilerplate.js
231 lines (204 loc) · 6.97 KB
/
boilerplate.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const options = require('./options')
const { merge, pipe, assoc, omit, __ } = require('ramda')
/**
* Is Android installed?
*
* $ANDROID_HOME/tools folder has to exist.
*
* @param {*} context - The gluegun context.
* @returns {boolean}
*/
const isAndroidInstalled = function (context) {
const androidHome = process.env['ANDROID_HOME']
const hasAndroidEnv = !context.strings.isBlank(androidHome)
const hasAndroid = hasAndroidEnv && context.filesystem.exists(`${androidHome}/tools`) === 'dir'
return Boolean(hasAndroid)
}
const finish = async function (context) {
const { parameters, system, print, ignite } = context
const name = parameters.third
if (parameters.options['skip-git'] !== true) {
// initial git
if (system.which('git')) {
const spinner = print.spin('configuring git')
ignite.log('git init .')
await system.run('git init .')
ignite.log('git add .')
await system.run('git add .')
ignite.log('git commit')
await system.run('git commit -m "Initial commit."')
// setup husky git hooks
spinner.text = 'setting up git hooks'
system.run(`node node_modules/husky/bin/install .`)
spinner.succeed('configured git')
}
}
// Wrap it up with our success message.
print.info('')
print.info('🍽 Time to get cooking!')
print.info('')
print.info('To run in iOS:')
print.info(print.colors.bold(` cd ${name}`))
print.info(print.colors.bold(' react-native run-ios'))
print.info('')
if (isAndroidInstalled(context)) {
print.info('To run in Android:')
} else {
print.info(`To run in Android, make sure you've followed the latest react-native setup instructions at https://facebook.github.io/react-native/docs/getting-started.html before using ignite.\nYou won't be able to run ${print.colors.bold('react-native run-android')} successfully until you have. Then:`)
}
print.info(print.colors.bold(` cd ${name}`))
print.info(print.colors.bold(' react-native run-android'))
print.info('')
print.info('To see what ignite can do for you:')
print.info(print.colors.bold(` cd ${name}`))
print.info(print.colors.bold(' ignite'))
print.info('')
}
/**
* Let's install.
*
* @param {any} context - The gluegun context.
*/
async function install (context) {
const {
filesystem,
parameters,
ignite,
reactNative,
print,
system,
prompt,
template
} = context
const name = parameters.third
const spinner = print
.spin(`using the ${print.colors.red('Appclon')} boilerplate`)
.succeed()
// attempt to install React Native or die trying
const rnInstall = await reactNative.install({ name, skipJest: true })
if (rnInstall.exitCode > 0) process.exit(rnInstall.exitCode)
// remove the __tests__ directory that come with React Native
filesystem.remove('__tests__')
// copy our App & Tests directories
spinner.text = '▸ copying files'
spinner.start()
filesystem.copy(`${__dirname}/boilerplate/App`, `${process.cwd()}/App`, {
overwrite: true
})
filesystem.copy(`${__dirname}/boilerplate/Tests`, `${process.cwd()}/Tests`, {
overwrite: true
})
spinner.stop()
// generate some templates
spinner.text = '▸ generating files'
const templates = [
{ template: 'index.js.ejs', target: 'index.ios.js' },
{ template: 'index.js.ejs', target: 'index.android.js' },
{ template: 'README.md', target: 'README.md' },
{ template: 'ignite.json.ejs', target: 'ignite/ignite.json' },
{ template: '.editorconfig', target: '.editorconfig' },
{ template: 'babelrc.ejs', target: '.babelrc' },
{
template: 'App/Config/AppConfig.js.ejs',
target: 'App/Config/AppConfig.js'
}
]
const templateProps = {
name,
igniteVersion: ignite.version,
reactNativeVersion: rnInstall.version
}
await ignite.copyBatch(context, templates, templateProps, {
quiet: true,
directory: `${ignite.ignitePluginPath()}/boilerplate`
})
/**
* Append to files
*/
// https://github.com/facebook/react-native/issues/12724
filesystem.appendAsync('.gitattributes', '*.bat text eol=crlf')
/**
* Merge the package.json from our template into the one provided from react-native init.
*/
async function mergePackageJsons () {
// transform our package.json incase we need to replace variables
const rawJson = await template.generate({
directory: `${ignite.ignitePluginPath()}/boilerplate`,
template: 'package.json.ejs',
props: templateProps
})
const newPackageJson = JSON.parse(rawJson)
// read in the react-native created package.json
const currentPackage = filesystem.read('package.json', 'json')
// deep merge, lol
const newPackage = pipe(
assoc(
'dependencies',
merge(currentPackage.dependencies, newPackageJson.dependencies)
),
assoc(
'devDependencies',
merge(currentPackage.devDependencies, newPackageJson.devDependencies)
),
assoc('scripts', merge(currentPackage.scripts, newPackageJson.scripts)),
merge(
__,
omit(['dependencies', 'devDependencies', 'scripts'], newPackageJson)
)
)(currentPackage)
// write this out
filesystem.write('package.json', newPackage, { jsonIndent: 2 })
}
await mergePackageJsons()
spinner.stop()
// --max, --min, interactive
let answers
if (parameters.options.max) {
answers = options.answers.max
} else if (parameters.options.min) {
answers = options.answers.min
} else {
answers = await prompt.ask(options.questions)
}
spinner.text = '▸ installing ignite dependencies'
spinner.start()
if (context.ignite.useYarn) {
await system.run('yarn')
} else {
await system.run('npm i')
}
spinner.stop()
// react native link -- must use spawn & stdio: ignore or it hangs!! :(
spinner.text = `▸ linking native libraries`
spinner.start()
await system.spawn('react-native link', { stdio: 'ignore' })
spinner.stop()
// pass long the debug flag if we're running in that mode
const debugFlag = parameters.options.debug ? '--debug' : ''
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// NOTE(steve): I'm re-adding this here because boilerplates now hold permanent files
// TODO(steve):
// * this needs to get planned a little better.
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
try {
// await system.spawn(`ignite add ir-next ${debugFlag}`, { stdio: 'inherit' })
if (answers['vector-icons'] === 'react-native-vector-icons') {
await system.spawn(`ignite add vector-icons ${debugFlag}`, {
stdio: 'inherit'
})
}
if (answers['i18n'] === 'react-native-i18n') {
await system.spawn(`ignite add i18n ${debugFlag}`, { stdio: 'inherit' })
}
if (answers['animatable'] === 'react-native-animatable') {
await system.spawn(`ignite add animatable ${debugFlag}`, {
stdio: 'inherit'
})
}
} catch (e) {
ignite.log(e)
throw e
}
await finish(context)
}
module.exports = { install }