forked from infinitered/ignite-bowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
boilerplate.js
326 lines (286 loc) · 10.7 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
const { merge, pipe, assoc, omit, __ } = require("ramda")
const { getReactNativeVersion } = require("./lib/react-native-version")
// We need this value here, as well as in our package.json.ejs template
const REACT_NATIVE_GESTURE_HANDLER_VERSION = "^1.3.0"
/**
* 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)
}
/**
* Let's install.
*
* @param {any} context - The gluegun context.
*/
async function install(context) {
const {
filesystem,
parameters,
ignite,
reactNative,
print,
system,
template,
prompt,
patching,
strings,
} = context
const { colors } = print
const { red, yellow, bold, gray, cyan } = colors
const isWindows = process.platform === "win32"
const isMac = process.platform === "darwin"
const perfStart = new Date().getTime()
const name = parameters.first
const spinner = print
.spin(`using the ${red("Infinite Red")} boilerplate v3 (code name 'Bowser')`)
.succeed()
let includeDetox = false
if (isMac) {
const askAboutDetox = parameters.options.detox === undefined
includeDetox = askAboutDetox
? await prompt.confirm("Would you like to include Detox end-to-end tests?")
: parameters.options.detox === true
if (includeDetox) {
// prettier-ignore
print.info(`
You'll love Detox for testing your app! There are some additional requirements to
install, so make sure to check out ${cyan('e2e/README.md')} in your generated app!
`)
}
} else {
if (parameters.options.detox === true) {
// prettier-ignore
if (isWindows) {
print.info("Skipping Detox because it is only supported on macOS, but you're running Windows")
} else {
print.info("Skipping Detox because it is only supported on macOS")
}
}
}
// attempt to install React Native or die trying
const rnInstall = await reactNative.install({
name,
version: getReactNativeVersion(context),
useNpm: !ignite.useYarn,
})
if (rnInstall.exitCode > 0) process.exit(rnInstall.exitCode)
// remove the __tests__ directory, App.js, and unnecessary config files that come with React Native
const filesToRemove = [
".babelrc",
"babel.config.js",
".buckconfig",
".eslintrc.js",
".prettierrc.js",
".flowconfig",
"App.js",
"__tests__",
]
filesToRemove.map(filesystem.remove)
// copy our App, Tests & storybook directories
spinner.text = "▸ copying files"
spinner.start()
filesystem.copy(`${__dirname}/boilerplate/app`, `${process.cwd()}/app`, {
overwrite: true,
matching: "!*.ejs",
})
filesystem.copy(`${__dirname}/boilerplate/test`, `${process.cwd()}/test`, {
overwrite: true,
matching: "!*.ejs",
})
filesystem.copy(`${__dirname}/boilerplate/storybook`, `${process.cwd()}/storybook`, {
overwrite: true,
matching: "!*.ejs",
})
filesystem.copy(`${__dirname}/boilerplate/bin`, `${process.cwd()}/bin`, {
overwrite: true,
})
includeDetox &&
filesystem.copy(`${__dirname}/boilerplate/e2e`, `${process.cwd()}/e2e`, {
overwrite: true,
matching: "!*.ejs",
})
spinner.stop()
// generate some templates
spinner.text = "▸ generating files"
//
const templates = [
{ template: "index.js.ejs", target: "index.js" },
{ template: "README.md", target: "README.md" },
{ template: ".gitignore.ejs", target: ".gitignore" },
{ template: ".env.example", target: ".env" },
{ template: ".prettierignore", target: ".prettierignore" },
{ template: ".solidarity", target: ".solidarity" },
{ template: "babel.config.js", target: "babel.config.js" },
{ template: "react-native.config.js", target: "react-native.config.js" },
{ template: "tsconfig.json", target: "tsconfig.json" },
{ template: "app/app.tsx.ejs", target: "app/app.tsx" },
{
template: "app/screens/welcome-screen/welcome-screen.tsx.ejs",
target: "app/screens/welcome-screen/welcome-screen.tsx",
},
{
template: "app/screens/demo-screen/demo-screen.tsx.ejs",
target: "app/screens/demo-screen/demo-screen.tsx",
},
{ template: "bin/postInstall", target: "bin/postInstall" },
]
const templateProps = {
name,
igniteVersion: ignite.version,
reactNativeVersion: rnInstall.version,
reactNativeGestureHandlerVersion: REACT_NATIVE_GESTURE_HANDLER_VERSION,
vectorIcons: false,
animatable: false,
i18n: false,
includeDetox,
}
await ignite.copyBatch(context, templates, templateProps, {
quiet: true,
directory: `${ignite.ignitePluginPath()}/boilerplate`,
})
/**
* Because of https://github.com/react-native-community/cli/issues/462,
* we can't detox-test the release configuration. Turn on dead-code stripping
* to fix this.
*/
if (includeDetox) {
await ignite.patchInFile(`ios/${name}.xcodeproj/xcshareddata/xcschemes/${name}.xcscheme`, {
replace: 'buildForRunning = "YES"\n buildForProfiling = "NO"',
insert: 'buildForRunning = "NO"\n buildForProfiling = "NO"',
})
}
/**
* 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 so we can replace variables
const rawJson = await template.generate({
directory: `${ignite.ignitePluginPath()}/boilerplate`,
template: "package.json.ejs",
props: { ...templateProps, kebabName: strings.kebabCase(templateProps.name) },
})
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(
omit(["@react-native-community/eslint-config"], 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()
// pass long the debug flag if we're running in that mode
const debugFlag = parameters.options.debug ? "--debug" : ""
try {
// boilerplate adds itself to get plugin.js/generators etc
// Could be directory, npm@version, or just npm name. Default to passed in values
const boilerplate = parameters.options.b || parameters.options.boilerplate || "ignite-bowser"
await system.spawn(`ignite add ${boilerplate} ${debugFlag}`, { stdio: "inherit" })
await ignite.addModule("react-native-gesture-handler", {
version: REACT_NATIVE_GESTURE_HANDLER_VERSION,
})
ignite.patchInFile(
`${process.cwd()}/android/app/src/main/java/com/${name.toLowerCase()}/MainActivity.java`,
{
after: "import com.facebook.react.ReactActivity;",
insert: `
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;`,
},
)
ignite.patchInFile(
`${process.cwd()}/android/app/src/main/java/com/${name.toLowerCase()}/MainActivity.java`,
{
after: `public class MainActivity extends ReactActivity {`,
insert:
"\n @Override\n" +
" protected ReactActivityDelegate createReactActivityDelegate() {\n" +
" return new ReactActivityDelegate(this, getMainComponentName()) {\n" +
" @Override\n" +
" protected ReactRootView createRootView() {\n" +
" return new RNGestureHandlerEnabledRootView(MainActivity.this);\n" +
" }\n" +
" };\n" +
" }",
},
)
ignite.patchInFile(`${process.cwd()}/package.json`, {
replace: `"postinstall": "solidarity",`,
insert: `"postinstall": "node ./bin/postInstall",`,
})
} catch (e) {
ignite.log(e)
print.error(`
There were errors while generating the project. Run with --debug to see verbose output.
`)
throw e
}
// re-run yarn; will also install pods, because of our postInstall script.
const installDeps = ignite.useYarn ? "yarn" : "npm install"
await system.run(installDeps)
spinner.succeed(`Installed dependencies`)
// run react-native link to link assets
await system.spawn("react-native link", { stdio: "ignore" })
spinner.succeed(`Linked assets`)
// for Windows, fix the settings.gradle file. Ref: https://github.com/oblador/react-native-vector-icons/issues/938#issuecomment-463296401
// for ease of use, just replace any backslashes with forward slashes
if (isWindows) {
await patching.update(`${process.cwd()}/android/settings.gradle`, contents => {
return contents.split("\\").join("/")
})
}
// let eslint and prettier clean things up
await system.spawn(`${ignite.useYarn ? "yarn" : "npm run"} lint`)
await system.spawn(`${ignite.useYarn ? "yarn" : "npm run"} format`)
spinner.succeed("Linted and formatted")
const perfDuration = parseInt((new Date().getTime() - perfStart) / 10) / 100
spinner.succeed(`ignited ${yellow(name)} in ${perfDuration}s`)
const androidInfo = isAndroidInstalled(context)
? ""
: `\n\nTo 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 ${bold(
"react-native run-android",
)} successfully until you have.`
const successMessage = `
${red("Ignite CLI")} ignited ${yellow(name)} in ${gray(`${perfDuration}s`)}
To get started:
cd ${name}
react-native run-ios
react-native run-android${androidInfo}
ignite --help
${cyan("Need additional help? Join our Slack community at http://community.infinite.red.")}
${bold("Now get cooking! 🍽")}
${gray(
"(Running yarn install one last time to make sure everything is installed -- please be patient!)",
)}
`
print.info(successMessage)
}
module.exports = {
install,
}