/
write_sample_boilerplate.js
228 lines (200 loc) · 5.94 KB
/
write_sample_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
const fs = require('fs')
const path = require('path')
const APP_FILE_CONTENT = `
import React from 'react'
import ReactDOM from 'react-dom'
import Example from './example'
import { DragDropContextProvider } from 'react-dnd'
import HTML5Backend from 'react-dnd-html5-backend'
function App() {
return (
<div className="App">
<DragDropContextProvider backend={HTML5Backend}>
<Example />
</DragDropContextProvider>
</div>
)
}
const rootElement = document.getElementById('root')
ReactDOM.render(<App />, rootElement)
`
const MANIFEST_FILE_CONTENT = `
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}`
const HTML_FILE_CONTENT = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<meta name="theme-color" content="#000000" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
`
const makePackageJson = (index, isTS) => {
const result = {
name: `react-dnd-example-${index}`,
main: `src/index.${isTS ? 'tsx' : 'js'}`,
description: 'auto-generated package for codesandbox import',
private: true,
version: '0.0.0',
scripts: {
start: 'react-scripts start',
build: 'react-scripts build',
test: 'react-scripts test --env=jsdom',
eject: 'react-scripts eject',
},
dependencies: {
react: '16.8.4',
'react-dom': '16.8.4',
'react-scripts': '2.1.8',
'react-dnd': '*',
'react-dnd-html5-backend': '*',
'babel-jest': '23.6.0',
},
browserslist: ['>0.2%', 'not dead', 'not ie <= 11', 'not op_mini all'],
}
if (isTS) {
result.dependencies = {
...result.dependencies,
typescript: '^3.3.3333',
'@types/react': '^16.8.7',
'@types/react-dom': '^16.8.2',
'@types/jest': '^24.0.9',
'@types/node': '^11.12.0',
}
}
return result
}
function walk_examples(dir, look_for, done) {
var results = []
fs.readdir(dir, (err, list) => {
if (err) {
return done(err)
}
let pending = list.length
if (!pending) {
return done(null, results)
}
list.forEach(file => {
file = path.resolve(dir, file)
fs.stat(file, (err, stat) => {
if (stat && stat.isDirectory()) {
walk_examples(file, look_for, (err, res) => {
results = results.concat(res)
if (!--pending) done(null, results)
})
} else {
if (file.endsWith(look_for)) {
results.push(file)
}
if (!--pending) done(null, results)
}
})
})
})
}
// Write JS Examples
walk_examples(path.join(__dirname, 'static/examples_js'), 'index.jsx', function(
err,
results,
) {
if (err) throw err
results.forEach((exampleIndex, index) => {
const exampleDir = path.dirname(exampleIndex)
console.log('processing example', exampleDir)
// Move example code into `src`
const srcDir = path.join(exampleDir, 'src')
const publicDir = path.join(exampleDir, 'public')
fs.mkdirSync(srcDir, { recursive: true })
fs.mkdirSync(publicDir, { recursive: true })
const exampleFiles = fs.readdirSync(exampleDir)
exampleFiles.forEach(file => {
if (file.endsWith('.js') || file.endsWith('.jsx')) {
const sourcePath = path.join(exampleDir, file)
const targetPath = path.join(srcDir, file)
fs.renameSync(sourcePath, targetPath)
}
})
// Rename index.jsx to example.jsx. Reserve index. for the bootstrapping codes
fs.renameSync(
path.join(srcDir, 'index.jsx'),
path.join(srcDir, 'example.jsx'),
)
const packageJsonFile = path.join(exampleDir, 'package.json')
fs.writeFileSync(packageJsonFile, JSON.stringify(makePackageJson(index)))
const sandboxAppFile = path.join(srcDir, 'index.js')
fs.writeFileSync(sandboxAppFile, APP_FILE_CONTENT)
const htmlFile = path.join(publicDir, 'index.html')
fs.writeFileSync(htmlFile, HTML_FILE_CONTENT)
const manifestFile = path.join(publicDir, 'manifest.json')
fs.writeFileSync(manifestFile, MANIFEST_FILE_CONTENT)
const envFile = path.join(exampleDir, '.env')
fs.writeFileSync(envFile, `SKIP_PREFLIGHT_CHECK = true`)
})
})
// Write TS Examples
walk_examples(path.join(__dirname, 'static/examples_ts'), 'index.tsx', function(
err,
results,
) {
if (err) throw err
results.forEach((exampleIndex, index) => {
const exampleDir = path.dirname(exampleIndex)
console.log('processing example', exampleDir)
// Move example code into `src`
const srcDir = path.join(exampleDir, 'src')
const publicDir = path.join(exampleDir, 'public')
fs.mkdirSync(srcDir, { recursive: true })
fs.mkdirSync(publicDir, { recursive: true })
const exampleFiles = fs.readdirSync(exampleDir)
exampleFiles.forEach(file => {
if (file.endsWith('.ts') || file.endsWith('.tsx')) {
const sourcePath = path.join(exampleDir, file)
const targetPath = path.join(srcDir, file)
fs.renameSync(sourcePath, targetPath)
}
})
// Rename index.jsx to example.jsx. Reserve index. for the bootstrapping codes
fs.renameSync(
path.join(srcDir, 'index.tsx'),
path.join(srcDir, 'example.tsx'),
)
const packageJsonFile = path.join(exampleDir, 'package.json')
fs.writeFileSync(
packageJsonFile,
JSON.stringify(makePackageJson(index, true)),
)
const sandboxAppFile = path.join(srcDir, 'index.tsx')
fs.writeFileSync(sandboxAppFile, APP_FILE_CONTENT)
const htmlFile = path.join(publicDir, 'index.html')
fs.writeFileSync(htmlFile, HTML_FILE_CONTENT)
const manifestFile = path.join(publicDir, 'manifest.json')
fs.writeFileSync(manifestFile, MANIFEST_FILE_CONTENT)
const envFile = path.join(exampleDir, '.env')
fs.writeFileSync(envFile, `SKIP_PREFLIGHT_CHECK = true`)
})
})