Skip to content

Commit fd81ea4

Browse files
committed
WIP supports Typescript, drops Component
1 parent 5b3d6e4 commit fd81ea4

File tree

11 files changed

+1161
-178
lines changed

11 files changed

+1161
-178
lines changed

StaticServerExample/.eslintrc.js

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

StaticServerExample/App.js

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

StaticServerExample/App.tsx

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* Sample React Native Static Server
3+
* https://github.com/futurepress/react-native-static-server
4+
* @flow
5+
*/
6+
7+
import React, { useState, useEffect } from 'react'
8+
import {
9+
StyleSheet,
10+
Text,
11+
View,
12+
Image
13+
} from 'react-native'
14+
15+
// requires react-native-webview, see: https://github.com/uuidjs/uuid#getrandomvalues-not-supported
16+
import 'react-native-get-random-values'
17+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
18+
import { v4 as uuidv4 } from 'uuid'
19+
import StaticServer from 'react-native-static-server'
20+
import RNFetchBlob from 'rn-fetch-blob'
21+
import { WebView } from 'react-native-webview'
22+
23+
interface ITestViewProps {
24+
port?: number
25+
root?: string
26+
file?: string
27+
}
28+
29+
export default function App (props: ITestViewProps): JSX.Element {
30+
const [origin, setOrigin] = useState<string>('')
31+
const [server, setServer] = useState<StaticServer>(null)
32+
const port = typeof props.port !== 'undefined' ? props.port : 3030
33+
const root = typeof props.root !== 'undefined' ? props.root : 'www/'
34+
const file = typeof props.file !== 'undefined' ? props.file : 'index.html'
35+
36+
useEffect(() => {
37+
if (origin === '') {
38+
// eslint-disable-next-line @typescript-eslint/no-var-requires
39+
const index = require('./index.html')
40+
const { uri } = Image.resolveAssetSource(index)
41+
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + root
42+
const dest = path + file
43+
const startServer = async (): Promise<void> => {
44+
const newServer = new StaticServer(port, root, { localOnly: true })
45+
const origin = await newServer.start()
46+
setOrigin(origin)
47+
setServer(newServer)
48+
}
49+
const prepare = async (): Promise<void> => {
50+
try {
51+
await RNFetchBlob.fs.mkdir(path)
52+
} catch (e) {
53+
console.log(`directory is created ${path}`)
54+
}
55+
let added: Promise<boolean>
56+
if (uri.includes('file://')) {
57+
// Copy file in release
58+
const result = await RNFetchBlob.fs.exists(dest)
59+
if (!result) {
60+
added = RNFetchBlob.fs.cp(uri, dest)
61+
}
62+
} else {
63+
// Download for development
64+
const result = await RNFetchBlob.config({ fileCache: true }).fetch('GET', uri)
65+
added = RNFetchBlob.fs.mv(result.path(), dest)
66+
}
67+
try {
68+
await added
69+
} catch (e) {
70+
console.log(e)
71+
}
72+
await startServer()
73+
}
74+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
75+
prepare()
76+
return () => {
77+
if (server !== null) {
78+
server.kill()
79+
}
80+
}
81+
}
82+
}, [])
83+
84+
if (origin !== '') {
85+
return <WebView
86+
source={{ uri: `${origin}/${file}` }}
87+
style={styles.webview}
88+
/>
89+
}
90+
return <View style={styles.container}>
91+
<Text>Loading...</Text>
92+
</View>
93+
}
94+
95+
const styles = StyleSheet.create({
96+
container: {
97+
flex: 1,
98+
justifyContent: 'center',
99+
alignItems: 'center',
100+
backgroundColor: '#F5FCFF'
101+
},
102+
webview: {
103+
marginTop: 20,
104+
flex: 1
105+
}
106+
})

StaticServerExample/__tests__/App-test.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
* @format
33
*/
44

5-
import 'react-native';
6-
import React from 'react';
7-
import App from '../App';
5+
import 'react-native'
6+
import React from 'react'
7+
import App from '../App'
88

99
// Note: test renderer must be required after react-native.
10-
import renderer from 'react-test-renderer';
10+
import renderer from 'react-test-renderer'
1111

12+
// eslint-disable-next-line no-undef
1213
it('renders correctly', () => {
13-
renderer.create(<App />);
14-
});
14+
renderer.create(<App />)
15+
})

StaticServerExample/babel.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
presets: ['module:metro-react-native-babel-preset'],
3-
};
2+
presets: ['module:metro-react-native-babel-preset']
3+
}

StaticServerExample/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
* @format
33
*/
44

5-
import {AppRegistry} from 'react-native';
6-
import App from './App';
7-
import {name as appName} from './app.json';
5+
import { AppRegistry } from 'react-native'
6+
import App from './App'
7+
import { name as appName } from './app.json'
88

9-
AppRegistry.registerComponent(appName, () => App);
9+
AppRegistry.registerComponent(appName, () => App)

StaticServerExample/package.json

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,48 @@
2020
"devDependencies": {
2121
"@babel/core": "^7.9.0",
2222
"@babel/runtime": "^7.9.2",
23+
"@types/react": "^16.9.34",
24+
"@types/react-native": "^0.62.2",
25+
"@typescript-eslint/eslint-plugin": "^2.28.0",
2326
"babel-jest": "^25.3.0",
27+
"eslint": "^6.8.0",
28+
"eslint-config-standard-react": "^9.2.0",
29+
"eslint-config-standard-with-typescript": "^15.0.1",
30+
"eslint-plugin-import": "^2.20.2",
31+
"eslint-plugin-node": "^11.1.0",
32+
"eslint-plugin-promise": "^4.2.1",
33+
"eslint-plugin-react": "^7.19.0",
34+
"eslint-plugin-standard": "^4.0.1",
2435
"jest": "^25.3.0",
2536
"metro-react-native-babel-preset": "^0.59.0",
26-
"react-test-renderer": "16.11.0"
37+
"react-test-renderer": "16.11.0",
38+
"typescript": "^3.8.3"
2739
},
2840
"jest": {
29-
"preset": "react-native"
30-
}
41+
"preset": "react-native",
42+
"transform": {
43+
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
44+
}
45+
},
46+
"eslintConfig": {
47+
"extends": [
48+
"standard-with-typescript",
49+
"standard-react"
50+
],
51+
"parserOptions": {
52+
"project": "./tsconfig.json"
53+
},
54+
"rules": {
55+
"react/jsx-closing-bracket-location": "off",
56+
"react/jsx-closing-tag-location": "off"
57+
}
58+
},
59+
"eslintIgnore": [
60+
"node_modules/*",
61+
"dist/*",
62+
"coverage/*",
63+
"**/*.d.ts",
64+
"/src/public/",
65+
"/src/types/"
66+
]
3167
}

StaticServerExample/tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"experimentalDecorators": true,
4+
"allowSyntheticDefaultImports": true,
5+
"downlevelIteration": true,
6+
"target": "ESNext",
7+
"module": "esnext",
8+
"jsx": "react-native",
9+
"lib": ["dom", "ESNext"],
10+
"moduleResolution": "node",
11+
"noEmit": true,
12+
"skipLibCheck": true,
13+
"types": ["jest"],
14+
"declaration": true
15+
}
16+
}

0 commit comments

Comments
 (0)