Guest User

Untitled

a guest
Jan 22nd, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. #bootstrap a ts react-native project on mac
  4.  
  5. PROJECT_NAME=$1
  6. react-native init --version 0.56.0 $PROJECT_NAME
  7. cd $PROJECT_NAME
  8.  
  9. echo "installing typescript"
  10. yarn add --dev typescript
  11.  
  12. echo "installing react-native-typescript-transformer"
  13. yarn add --dev react-native-typescript-transformer
  14.  
  15. echo "creating ts config file"
  16. #create ts config file
  17. yarn tsc --init --pretty --jsx react
  18.  
  19. echo "creating rn-cli.config.js"
  20. touch rn-cli.config.js
  21.  
  22. # not sure if this is needed
  23. yarn add --dev @types/react @types/react-native
  24.  
  25. # could be replaced with perl
  26. echo "uncommenting allowSyntheticDefaultImports in tsconfig.json file"
  27. sed 's/.*"allowSyntheticDefaultImports": true.*/ "allowSyntheticDefaultImports": true,/' tsconfig.json > \
  28. /tmp/replacedTextTmp && cat /tmp/replacedTextTmp > tsconfig.json && \
  29. rm /tmp/replacedTextTmp
  30.  
  31. echo "writing here document to rn-cli.config.js file"
  32. cat << EOF > ./rn-cli.config.js
  33. module.exports = {
  34. getTransformModulePath() {
  35. return require.resolve("react-native-typescript-transformer");
  36. },
  37. getSourceExts() {
  38. return ["ts", "tsx"];
  39. }
  40. };
  41. EOF
  42.  
  43. #rename js files to use tsx extention
  44. echo "renaming App.jsx to use .tsx extention"
  45. mv App.js App.tsx
  46. # cd __tests__
  47. # mv App.jsx App.tsx
  48. # cd ..
  49.  
  50. # remove app.js import and add ts compatible imports, could be replace with perl
  51. echo "removing: import React, { Component } from 'react'; from App.tsx file"
  52.  
  53. perl -i -0pe 's/import React, {Component} from .*react.*;//' App.tsx
  54.  
  55. TMP_APP_TSX_FILE=$(cat App.tsx)
  56. echo "adding new TS compatible import syntax to App.tsx"
  57. cat << EOF > App.tsx
  58. import React from 'react'
  59. import { Component } from 'react';
  60. ${TMP_APP_TSX_FILE}
  61. EOF
  62.  
  63. #jest ts
  64. echo "installing TS jest"
  65. yarn add --dev ts-jest
  66.  
  67. #replace
  68. echo "replacing default jest settings in package.json with TS settings using perl"
  69. #store here doc ts settings in variable
  70.  
  71. # read -r -d '' JEST_TS_SETTINGS_VAR <<-'EOF'
  72. cat <<- EOF > /tmp/jestTsSettingsTemp
  73. "jest": {
  74. "preset": "react-native",
  75. "moduleFileExtensions": [
  76. "ts",
  77. "tsx",
  78. "js"
  79. ],
  80. "transform": {
  81. "^.+\\\\.(js)$": "<rootDir>/node_modules/babel-jest",
  82. "\\\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  83. },
  84. "testRegex": "(/__tests__/.*|\\\\.(test|spec))\\\\.(ts|tsx|js)$",
  85. "testPathIgnorePatterns": [
  86. "\\\\.snap$",
  87. "<rootDir>/node_modules/"
  88. ],
  89. "cacheDirectory": ".jest/cache"
  90. }
  91. }
  92. EOF
  93.  
  94. JEST_TS_SETTINGS_VAR=$(cat /tmp/jestTsSettingsTemp)
  95.  
  96. # replace following pattern/file in place using peal
  97. # "jest": {
  98. # "preset": "react-native"
  99. # }
  100. # }
  101.  
  102. perl -i -0pe 's/"jest": {(.*\n){4}//' package.json
  103.  
  104. # append ts settings to package.json file
  105. echo "${JEST_TS_SETTINGS_VAR}" >> package.json
  106. rm /tmp/jestTsSettingsTemp
  107.  
  108. #react shapshot testing
  109. echo "installing react-addons-test-utils for snapshot testing"
  110. yarn add --dev react-addons-test-utils
  111.  
  112. # Installing Type Declaration Dependencies
  113. echo "installing TS type Declaration files"
  114. yarn add --dev @types/jest @types/react @types/react-native @types/react-test-renderer
  115.  
  116. echo "creating components directory"
  117. mkdir ./components/
  118.  
  119. echo "creating Hello.tsx demo component"
  120.  
  121. cat << EOF > ./components/Hello.tsx
  122. // components/Hello.tsx
  123. import React from "react"
  124. import { Button, StyleSheet, Text, View } from "react-native"
  125.  
  126. export interface Props {
  127. name: string
  128. enthusiasmLevel?: number
  129. onIncrement?: () => void
  130. onDecrement?: () => void
  131. }
  132.  
  133. interface State {
  134. enthusiasmLevel: number
  135. }
  136.  
  137. export class Hello extends React.Component<Props, State> {
  138. constructor(props: Props) {
  139. super(props)
  140.  
  141. if ((props.enthusiasmLevel || 0) <= 0) {
  142. throw new Error("You could be a little more enthusiastic. :D")
  143. }
  144.  
  145. this.state = {
  146. enthusiasmLevel: props.enthusiasmLevel || 1
  147. }
  148. }
  149.  
  150. onIncrement = () => this.setState({ enthusiasmLevel: this.state.enthusiasmLevel + 1 });
  151. onDecrement = () => this.setState({ enthusiasmLevel: this.state.enthusiasmLevel - 1 });
  152. getExclamationMarks = (numChars: number) => Array(numChars + 1).join("!")
  153.  
  154. render() {
  155. return (
  156. <View style={styles.root}>
  157. <Text style={styles.greeting}>
  158. Hello {this.props.name + this.getExclamationMarks(this.state.enthusiasmLevel)}
  159. </Text>
  160.  
  161. <View style={styles.buttons}>
  162. <View style={styles.button}>
  163. <Button
  164. title="-"
  165. onPress={this.onDecrement}
  166. accessibilityLabel="decrement"
  167. color="red"
  168. />
  169. </View>
  170.  
  171. <View style={styles.button}>
  172. <Button
  173. title="+"
  174. onPress={this.onIncrement}
  175. accessibilityLabel="increment"
  176. color="blue"
  177. />
  178. </View>
  179. </View>
  180. </View>
  181. )
  182. }
  183. }
  184.  
  185. // styles
  186.  
  187. const styles = StyleSheet.create({
  188. root: {
  189. alignItems: "center",
  190. alignSelf: "center"
  191. },
  192. buttons: {
  193. flexDirection: "row",
  194. minHeight: 70,
  195. alignItems: "stretch",
  196. alignSelf: "center",
  197. borderWidth: 5
  198. },
  199. button: {
  200. flex: 1,
  201. paddingVertical: 0
  202. },
  203. greeting: {
  204. color: "#999",
  205. fontWeight: "bold"
  206. }
  207. })
  208. EOF
  209.  
  210. mkdir ./components/__tests__
  211. cat << EOF > ./components/__tests__/Hello.tsx
  212. import React from 'react'
  213. import renderer from 'react-test-renderer'
  214.  
  215. import { Hello } from "../Hello"
  216.  
  217. it("renders correctly with defaults", () => {
  218. const button = renderer.create(<Hello name="World" enthusiasmLevel={1} />).toJSON()
  219. expect(button).toMatchSnapshot()
  220. })
  221. EOF
  222.  
  223. echo "creating git ignore file"
  224. touch .gitignore
  225. cat << EOF > .gitignore
  226. # Jest
  227. #
  228. .jest/
  229. node_modules/
  230. EOF
  231.  
  232. echo "initializing git"
  233. git init
  234. git add .gitignore # import to do this first, to ignore our files
  235. git add .
  236. git commit -am "Initial commit."
  237.  
  238. echo "" #simulate carriage return
  239. echo "react-native typescript project ready at $(pwd)"
Add Comment
Please, Sign In to add comment