Skip to content

Instantly share code, notes, and snippets.

@rigwild
Last active July 10, 2018 15:33
Show Gist options
  • Save rigwild/ff03bb700a7a0e6055932e3b18b6f386 to your computer and use it in GitHub Desktop.
Save rigwild/ff03bb700a7a0e6055932e3b18b6f386 to your computer and use it in GitHub Desktop.
Quickly create a web project with node.
/*
## Usage :
node create_web_project.js projectName
*/
'use strict'
const fs = require('fs')
if (process.argv.length <= 2) {
console.log('You need to specify the name of the project in the first parameter.')
process.exit(0)
}
const indexContent =
`<!DOCTYPE html>
<html lang="en">
<head>
<title>{{project-title}}</title>
<meta name="description" content="" />
<meta name="keywords" lang="en" content="" />
<meta name="author" content="" />
<meta name="robots" content="index,follow" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="./assets/css/style.css">
</head>
<body>
<div style='width:100%;text-align:center;font-family:Arial'>
<h1>Welcome to your project !</h1>
<h2>{{project-title}}</h2>
</div>
<script src="./assets/js/script.js"></script>
</body>
</html>`
const cleanFileName = fileName => fileName.substring(0, 200).replace(/([^a-z0-9éèà-]+)/gi, '_');
const createDir = path => !fs.existsSync(path) ? fs.mkdirSync(path) : console.error(`Error ! The directory ${path} already exists !`)
const createFile = (path, content = '') => !fs.existsSync(path) ? fs.writeFileSync(path, content, 'utf8') : console.error(`Error ! The file ${path} already exists !`)
const projectName = cleanFileName(process.argv[2])
console.log(`Creating the project '${projectName}' ...`)
;[
projectName,
`${projectName}/assets`,
`${projectName}/assets/js`,
`${projectName}/assets/css`,
`${projectName}/assets/img`
].forEach(path => createDir(path))
createFile(`${projectName}/index.html`, indexContent.replace(/{{project-title}}/g, projectName))
createFile(`${projectName}/assets/js/script.js`)
createFile(`${projectName}/assets/css/style.css`)
console.log(`\nThe project '${projectName}' was created.`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment