|
9 | 9 |
|
10 | 10 | import fs from 'fs';
|
11 | 11 | import { format } from 'prettier';
|
| 12 | +import componentRender from './componentRender.util'; |
12 | 13 |
|
13 |
| -function createIndexHtml() { |
14 |
| - const fileName = 'index.html'; |
| 14 | +function createIndexHtml(path, appName) { |
| 15 | + let dir = path; |
| 16 | + let dirSrc; |
| 17 | + let dirComponent; |
| 18 | + if (!dir.match(/`${appName}`|\*$/)) { |
| 19 | + dir = `${dir}/${appName}`; |
| 20 | + if (!fs.existsSync(dir)) { |
| 21 | + fs.mkdirSync(dir); |
| 22 | + dirSrc = `${dir}/src`; |
| 23 | + fs.mkdirSync(dirSrc); |
| 24 | + dirComponent = `${dirSrc}/components`; |
| 25 | + fs.mkdirSync(dirComponent); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + const filePath = `${dir}/index.html`; |
15 | 30 | const data = `
|
16 |
| - <!DOCTYPE html> |
17 |
| - <html lang="en"> |
18 |
| - <head> |
19 |
| - <meta charset="UTF-8"> |
20 |
| - <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
21 |
| - <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
22 |
| - <title>Document</title> |
23 |
| - </head> |
24 |
| - <body> |
25 |
| - <div id='root'></div> |
26 |
| - <script src='bundle.js'></script> |
27 |
| - </body> |
28 |
| - </html> |
| 31 | +<!DOCTYPE html> |
| 32 | +<html lang="en"> |
| 33 | +<head> |
| 34 | + <meta charset="UTF-8"> |
| 35 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 36 | + <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
| 37 | + <title>Document</title> |
| 38 | +</head> |
| 39 | +<body> |
| 40 | + <div id='root'></div> |
| 41 | + <script src='bundle.js'></script> |
| 42 | +</body> |
| 43 | +</html> |
29 | 44 | `;
|
30 |
| - fs.writeFile( |
31 |
| - fileName, |
32 |
| - format(data, { |
33 |
| - singleQuote: true, |
34 |
| - trailingComma: 'es5', |
35 |
| - bracketSpacing: true, |
36 |
| - jsxBracketSameLine: true, |
37 |
| - parser: 'babel', |
38 |
| - }), |
39 |
| - (err) => { |
40 |
| - if (err) { |
41 |
| - console.log('index.html error:', err.message); |
42 |
| - } else { |
43 |
| - console.log('index.html written successfully'); |
44 |
| - } |
45 |
| - }, |
46 |
| - ); |
| 45 | + fs.writeFileSync(filePath, data, (err) => { |
| 46 | + if (err) { |
| 47 | + console.log('index.html error:', err.message); |
| 48 | + } else { |
| 49 | + console.log('index.html written successfully'); |
| 50 | + } |
| 51 | + }); |
47 | 52 | }
|
48 | 53 |
|
49 |
| -async function createApplicationUtil({ path, appName, genOption }) { |
50 |
| - if (genOption === 1) { |
51 |
| - await createIndexHtml(); |
| 54 | +export const createIndexTsx = (path, appName) => { |
| 55 | + const filePath = `${path}/${appName}/index.tsx`; |
| 56 | + const data = ` |
| 57 | +import React from 'react'; |
| 58 | +import ReactDOM form 'react-dom' |
| 59 | +
|
| 60 | +export default function App(): JSX.Element { |
| 61 | + const greeting = (a: string, b: string): string => a + b; |
| 62 | + return ( |
| 63 | + <h1>{greeting('hello ', 'world')}</h1> |
| 64 | + ) |
52 | 65 | }
|
53 |
| -} |
54 |
| -export default createApplicationUtil; |
55 | 66 |
|
56 |
| -// async function createApplicationUtil({ |
57 |
| -// path, appName, genOption |
58 |
| -// }) { |
59 |
| -// if (genOption === 1) { |
60 |
| -// return [ |
61 |
| -// await execFile('npm', ['init', '-y'], { cwd: path }), |
62 |
| -// await execFile('touch', 'index.tsx', { cwd: path }), |
63 |
| -// await execFile('touch', 'index.html', { cwd: path }), |
64 |
| -// await execFile('touch', 'webpack.config.js', { cwd: path }), |
65 |
| -// await execFile('touch', '.babelrc', { cwd: path }), |
66 |
| -// ]; |
67 |
| -// } |
68 |
| -// } |
| 67 | +const root = document.getElementById('root') |
| 68 | + |
| 69 | +ReactDOM.render(<App />, root) |
| 70 | + `; |
| 71 | + fs.writeFile(filePath, data, (err) => { |
| 72 | + if (err) { |
| 73 | + console.log('index.tsx error:', err.message); |
| 74 | + } else { |
| 75 | + console.log('index.tsx written successfully'); |
| 76 | + } |
| 77 | + }); |
| 78 | +}; |
69 | 79 |
|
70 |
| -// import util from 'util'; |
| 80 | +export const createPackage = (path, appName) => { |
| 81 | + const filePath = `${path}/${appName}/package.json`; |
| 82 | + const data = ` |
| 83 | +{ |
| 84 | + "name": "reacType-boiler-plate", |
| 85 | + "version": "1.0.0", |
| 86 | + "description": "", |
| 87 | + "main": "index.tsx", |
| 88 | + "scripts": { |
| 89 | + "start": "webpack-dev-server --open", |
| 90 | + "build": "webpack" |
| 91 | +}, |
| 92 | +"keywords": [], |
| 93 | +"author": "", |
| 94 | +"license": "ISC", |
| 95 | +"depenencies": { |
| 96 | + "react": "^16.4.1", |
| 97 | + "react-dom": "^16.4.1", |
| 98 | +}, |
| 99 | +"devDependencies": { |
| 100 | + "@babel/preset-typescript": "^7.3.3", |
| 101 | + "@types/react": "^16.8.14", |
| 102 | + "@types/react-dom": "^16.8.4", |
| 103 | + "babel-core": "^6.26.3", |
| 104 | + "babel-eslint": "^8.2.6", |
| 105 | + "babel-loader": "^7.1.4", |
| 106 | + "babel-preset-env": "^1.6.1", |
| 107 | + "babel-preset-react": "^6.24.1", |
| 108 | + "typescript": "^3.4.4", |
| 109 | + "webpack": "^4.4.0", |
| 110 | + "webpack-cli": "^3.3.0", |
| 111 | + "webpack-dev-server": "^3.3.1" |
| 112 | + } |
| 113 | +} |
| 114 | + `; |
| 115 | + fs.writeFile(filePath, data, (err) => { |
| 116 | + if (err) { |
| 117 | + console.log('package.json error:', err.message); |
| 118 | + } else { |
| 119 | + console.log('package.json written successfully'); |
| 120 | + } |
| 121 | + }); |
| 122 | +}; |
71 | 123 |
|
72 |
| -// const execFile = util.promisify(require('child_process').execFile); |
| 124 | +export const createWebpack = (path, appName) => { |
| 125 | + const filePath = `${path}/${appName}/webpack.config.js`; |
| 126 | + const data = ` |
| 127 | +const path = require('path'); |
| 128 | +const rules = [ |
| 129 | + { |
| 130 | + test: /\.tsx?/, |
| 131 | + exclude: /node_modules/, |
| 132 | + loader: 'babel-loader' |
| 133 | + } |
| 134 | +] |
73 | 135 |
|
74 |
| -// Application generation options |
75 |
| -// cosnt genOptions = [ |
76 |
| -// 'Export into existing project.', 'Export with starter repo', 'Export with create-react-app.' |
77 |
| -// ]; |
| 136 | +module.exports = { |
| 137 | + target: 'web', |
| 138 | + mode: 'development', |
| 139 | + entry: './index.tsx', |
| 140 | + output: { |
| 141 | + path: path.resolve(__dirname, 'build'), |
| 142 | + filename: 'bundle.js' |
| 143 | + }, |
| 144 | + module: { rules }, |
| 145 | + resolve: {extensions: ['.ts', '.txs', '.js', 'jsx.']}, |
| 146 | + devServer: { |
| 147 | + contentBase: './', |
| 148 | + port: 5000 |
| 149 | + } |
| 150 | +} |
| 151 | + `; |
| 152 | + fs.writeFile(filePath, data, (err) => { |
| 153 | + if (err) { |
| 154 | + console.log('webpack error:', err.message); |
| 155 | + } else { |
| 156 | + console.log('webpack written successfully'); |
| 157 | + } |
| 158 | + }); |
| 159 | +}; |
| 160 | + |
| 161 | +export const createBabel = (path, appName) => { |
| 162 | + const filePath = `${path}/${appName}/.babelrc`; |
| 163 | + const data = ` |
| 164 | +{ |
| 165 | + "presets": ["@babel/env", "@babel/react", "@babel/typescript"] |
| 166 | +} |
| 167 | +`; |
| 168 | + fs.writeFile(filePath, data, (err) => { |
| 169 | + if (err) { |
| 170 | + console.log('babelrc error:', err.message); |
| 171 | + } else { |
| 172 | + console.log('babelrc written successfully'); |
| 173 | + } |
| 174 | + }); |
| 175 | +}; |
78 | 176 |
|
79 |
| -// async function createApplicationUtil({ |
80 |
| -// path, appName, genOption |
81 |
| -// }) { |
82 |
| -// if (genOption === 2) { |
83 |
| -// return [ |
84 |
| -// await execFile('npm', ['i', '-g', 'create-react-app'], { cwd: path }), |
85 |
| -// await execFile('create-react-app', [appName], { cwd: path }), |
86 |
| -// ]; |
87 |
| -// } |
88 |
| -// } |
| 177 | +export const createTsConfig = (path, appName) => { |
| 178 | + const filePath = `${path}/${appName}/tsconfig.json`; |
| 179 | + const data = ` |
| 180 | +{ |
| 181 | + "compilerOptions": { |
| 182 | + "outDir": "./build/", |
| 183 | + "sourceMap": true, |
| 184 | + "noImplicitAny": true, |
| 185 | + "module": "commonjs", |
| 186 | + "target": "es6", |
| 187 | + "jsx": "react" |
| 188 | + }, |
| 189 | + "include": ["**/*.ts", "**/*.tsx"], |
| 190 | + "exclude": ["node_modules"] |
| 191 | +} |
| 192 | +`; |
| 193 | + fs.writeFile(filePath, data, (err) => { |
| 194 | + if (err) { |
| 195 | + console.log('TSConfig error:', err.message); |
| 196 | + } else { |
| 197 | + console.log('TSConfig written successfully'); |
| 198 | + } |
| 199 | + }); |
| 200 | +}; |
89 | 201 |
|
90 |
| -// export default createApplicationUtil; |
| 202 | +async function createApplicationUtil({ path, appName, genOption }) { |
| 203 | + if (genOption === 1) { |
| 204 | + await createIndexHtml(path, appName); |
| 205 | + await createIndexTsx(path, appName); |
| 206 | + await createPackage(path, appName); |
| 207 | + await createWebpack(path, appName); |
| 208 | + await createBabel(path, appName); |
| 209 | + await createTsConfig(path, appName); |
| 210 | + } |
| 211 | +} |
| 212 | +export default createApplicationUtil; |
0 commit comments