|
| 1 | +// Create all files necessary to run a gatsby.js application |
| 2 | + |
| 3 | +import createGatsbyFiles from './createGatsbyFiles.util'; |
| 4 | +import { Component } from '../interfaces/Interfaces'; |
| 5 | + |
| 6 | +const camelToKebab= (camel:string) => { |
| 7 | + return camel.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase(); |
| 8 | +}; |
| 9 | + |
| 10 | +const compToCSS = (component: Component) => { |
| 11 | + const name = component.name; |
| 12 | + const styleObj = component.style; |
| 13 | + let cssClass = ` |
| 14 | + .${name} { |
| 15 | + `; |
| 16 | + Object.keys(styleObj).forEach(property => { |
| 17 | + let cssStyle = `${camelToKebab(property)}: ${styleObj[property]}; |
| 18 | + `; |
| 19 | + cssClass += cssStyle; |
| 20 | + }) |
| 21 | + cssClass += `} |
| 22 | + `; |
| 23 | + return cssClass; |
| 24 | +} |
| 25 | + |
| 26 | +//createPackage |
| 27 | +export const createPackage = (path, appName) => { |
| 28 | + const filePath = `${path}/${appName}/package.json`; |
| 29 | + const data = ` |
| 30 | +{ |
| 31 | + "name": "reactype-gatsby", |
| 32 | + "version": "1.0.0", |
| 33 | + "description": "", |
| 34 | + "scripts": { |
| 35 | + "dev": "gatsby develop", |
| 36 | + "build": "gatsby build", |
| 37 | + "start": "npm run dev" |
| 38 | + }, |
| 39 | + "dependencies": { |
| 40 | + "gatsby": "^2.26.1", |
| 41 | + "react": "16.13.1", |
| 42 | + "react-dom": "16.13.1" |
| 43 | + }, |
| 44 | + "devDependencies": { |
| 45 | + "@types/node": "^14.0.20", |
| 46 | + "@types/react": "^16.9.41", |
| 47 | + "typescript": "^3.9.6" |
| 48 | + } |
| 49 | +} |
| 50 | + `; |
| 51 | + window.api.writeFile(filePath, data, err => { |
| 52 | + if (err) { |
| 53 | + console.log('package.json error:', err.message); |
| 54 | + } else { |
| 55 | + console.log('package.json written successfully'); |
| 56 | + } |
| 57 | + }); |
| 58 | +}; |
| 59 | +//createTSConfig (empty) |
| 60 | +export const createTsConfig = (path, appName) => { |
| 61 | + const filePath = `${path}/${appName}/tsconfig.json`; |
| 62 | + //running 'gatsby dev' will autopopulate this with default values |
| 63 | + window.api.writeFile(filePath, '', err => { |
| 64 | + if (err) { |
| 65 | + console.log('TSConfig error:', err.message); |
| 66 | + } else { |
| 67 | + console.log('TSConfig written successfully'); |
| 68 | + } |
| 69 | + }); |
| 70 | +}; |
| 71 | + |
| 72 | +//createDefaultCSS |
| 73 | +export const createDefaultCSS = (path, appName, components) => { |
| 74 | + const filePath = `${path}/${appName}/global.css`; |
| 75 | + let data = ` |
| 76 | + #__gatsby div { |
| 77 | + box-sizing: border-box; |
| 78 | + width: 100%; |
| 79 | + border: 1px solid rgba(0,0,0,0.25); |
| 80 | + padding: 12px; |
| 81 | + text-align: center; |
| 82 | + font-family: Helvetica, Arial; |
| 83 | + } |
| 84 | + `; |
| 85 | + components.forEach(comp => { |
| 86 | + data += compToCSS(comp); |
| 87 | + }) |
| 88 | + window.api.writeFile(filePath, data, err => { |
| 89 | + if (err) { |
| 90 | + console.log('global.css error:', err.message); |
| 91 | + } else { |
| 92 | + console.log('global.css written successfully'); |
| 93 | + } |
| 94 | + }); |
| 95 | +} |
| 96 | + |
| 97 | +export const initFolders = (path:string, appName: string) => { |
| 98 | + let dir = path; |
| 99 | + let dirPages; |
| 100 | + let dirComponents; |
| 101 | + dir = `${dir}/${appName}`; |
| 102 | + if (!window.api.existsSync(dir)) { |
| 103 | + window.api.mkdirSync(dir); |
| 104 | + window.api.mkdirSync(`${dir}/src`) |
| 105 | + dirPages = `${dir}/src/pages`; |
| 106 | + window.api.mkdirSync(dirPages); |
| 107 | + dirComponents = `${dir}/src/components`; |
| 108 | + window.api.mkdirSync(dirComponents); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +//createBaseTsx |
| 113 | +export const createBaseTsx = (path, appName) => { |
| 114 | + |
| 115 | + const filePath:string = `${path}/${appName}/src/pages/_app.tsx`; |
| 116 | + const data:string = ` |
| 117 | + import React from 'react'; |
| 118 | + import '../global.css'; |
| 119 | +
|
| 120 | + const Base = ({ Component }):JSX.Element => { |
| 121 | + return ( |
| 122 | + <> |
| 123 | + <Component /> |
| 124 | + </> |
| 125 | + ) |
| 126 | + } |
| 127 | +
|
| 128 | + export default Base; |
| 129 | + `; |
| 130 | + window.api.writeFile(filePath, data, err => { |
| 131 | + if (err) { |
| 132 | + console.log('_app.tsx error:', err.message); |
| 133 | + } else { |
| 134 | + console.log('_app.tsx written successfully'); |
| 135 | + } |
| 136 | + }); |
| 137 | +}; |
| 138 | + |
| 139 | +async function createGatsbyAppUtil({ |
| 140 | + path, |
| 141 | + appName, |
| 142 | + components, |
| 143 | + rootComponents |
| 144 | +}: { |
| 145 | + path: string; |
| 146 | + appName: string; |
| 147 | + components: Component[]; |
| 148 | + rootComponents: number[]; |
| 149 | +}) { |
| 150 | + console.log('in the createGatsbyApplication util'); |
| 151 | + |
| 152 | + await initFolders(path, appName); |
| 153 | + await createBaseTsx(path, appName); |
| 154 | + await createDefaultCSS(path, appName, components); |
| 155 | + await createPackage(path, appName); |
| 156 | + await createTsConfig(path, appName); |
| 157 | + await createGatsbyFiles(components, path, appName, rootComponents); |
| 158 | + |
| 159 | +} |
| 160 | +export default createGatsbyAppUtil; |
0 commit comments