Created
May 14, 2023 08:17
-
-
Save ranjian0/c1627a01d457845f8155af3ee9d41334 to your computer and use it in GitHub Desktop.
create a minimal react + parcel project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { execSync } = require('child_process'); | |
const fs = require('fs'); | |
const path = require('path'); | |
function execCommand(command) { | |
try { | |
execSync(command, { stdio: 'inherit' }); | |
} catch (err) { | |
console.error(`Error executing command: ${command}`); | |
process.exit(1); | |
} | |
} | |
function createDirectory(dir) { | |
try { | |
fs.mkdirSync(dir, { recursive: true }); | |
} catch (err) { | |
console.error(`Error creating directory: ${dir}`); | |
process.exit(1); | |
} | |
} | |
function createFile(file, content = '') { | |
try { | |
fs.writeFileSync(file, content, 'utf8'); | |
} catch (err) { | |
console.error(`Error creating file: ${file}`); | |
process.exit(1); | |
} | |
} | |
function updatePackageJson() { | |
const packageJsonPath = path.resolve('package.json'); | |
let packageJson = {}; | |
try { | |
packageJson = require(packageJsonPath); | |
} catch (err) { | |
console.error(`Error reading package.json`); | |
process.exit(1); | |
} | |
// Remove main and scripts.test keys | |
delete packageJson.main; | |
if (packageJson.scripts && packageJson.scripts.test) { | |
delete packageJson.scripts.test; | |
} | |
packageJson.scripts = { | |
...packageJson.scripts, | |
start: 'parcel', | |
build: 'parcel build', | |
}; | |
packageJson.source = 'src/index.html'; | |
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), 'utf8'); | |
} | |
const appJsContent = ` | |
export function App() { | |
return <h1>Hello, World!</h1>; | |
} | |
`; | |
const indexJsContent = ` | |
import {createRoot} from "react-dom"; | |
import {App} from "./App"; | |
const container = document.getElementById("app"); | |
const root = createRoot(container); | |
root.render(<App />); | |
`; | |
const indexHtmlContent = ` | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>React + Parcel</title> | |
</head> | |
<body> | |
<div id="app"></div> | |
<script type="module" src="index.js"></script> | |
</body> | |
</html> | |
`; | |
execCommand('npm init -y'); | |
execCommand('npm install react react-dom'); | |
execCommand('npm install --save-dev parcel'); | |
updatePackageJson(); | |
createDirectory('src'); | |
createFile('src/index.html', indexHtmlContent); | |
createFile('src/index.js', indexJsContent); | |
createFile('src/app.js', appJsContent); | |
console.log('Setup complete!'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment