Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- """
- ES6 INIT
- --------
- OCT 2017 - Robert Fairley <robert.fairley@rci.rogers.ca>
- A quick and dirty ES6 Javascript project environment setup script.
- TODO: Add optional parameters to manage complexity and organization
- of the project being set up.
- """
- import sys
- import subprocess
- # GLOBALS
- APP_PROMPT = "[ES6 PROJECT]"
- # TEMPLATE STRINGS
- html_template = """<!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8"/>
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0"/>
- <title>ES6 Starter Project</title>
- <link rel="stylesheet" href="public/app.css"/>
- </head>
- <body>
- <main id="root" role="main"></main>
- <script src="app.js"></script>
- </body>
- </html>
- """
- css_template = """body,html {
- font-family: -apple-system, "Helvetica Neue", "Helvetica", "Roboto", "Verdana", sans-serif;
- color: #222;
- font-size: 1.1em;
- }
- """
- javascript_template = """const el = {
- main: document.querySelector('#root'),
- }
- const domCache = []
- window.domCache = domCache;
- function cache(element = null) {
- if (typeof element !== 'object') {
- console.error("You have to pass an element object to the cache function.")
- }
- domCache.push(element)
- }
- function render(e, cb = null) {
- let h1 = document.createElement('h1')
- let p = document.createElement('p')
- h1.innerHTML = `Hello, world!`
- p.innerHTML = `Welcome to your new ES6 application!`
- el.main.appendChild(h1)
- el.main.appendChild(p)
- cache(h1)
- cache(p)
- if (typeof cb === 'function') {
- cb()
- }
- }
- window.onload = e => {
- render(e, () => {
- console.log('Application mounted.')
- })
- }
- """
- babel_rc = """{
- "presets": [ "es2015", "stage-0" ]
- }
- """
- webpack_config = """const path = require('path');
- const config = {
- entry: './src/index.js',
- output: {
- filename: 'app.js',
- path: path.resolve(__dirname)
- },
- module: {
- rules: [
- { test: /\.js?$/, loader: 'babel-loader', exclude: /node_modules/ }
- ]
- }
- };
- module.exports = config;
- """
- package_json = """{
- "name": "es6-starter-project",
- "description": "",
- "version": "1.0.0",
- "main": "app.js",
- "scripts": {},
- "keywords": [],
- "license": "MIT",
- "dependencies":
- { "webpack": "latest"
- , "babel-core": "latest"
- , "babel-loader": "latest"
- , "sass-loader": "latest"
- , "css-loader": "latest"
- , "babel-preset-es2015": "latest"
- , "babel-preset-stage-0": "latest"
- }
- }
- """
- def git_init():
- print("%s Initializing Git repo..." % APP_PROMPT)
- subprocess.check_output(['git', 'init'])
- def continue_message(msg = None):
- if msg != None:
- print("%s %s" % (APP_PROMPT, msg))
- else:
- print "%s OK. Continuing...\n" % APP_PROMPT
- def yarn_init():
- print("%s Generating NPM package file..." % APP_PROMPT)
- with open("package.json", "w+") as file:
- file.write(package_json)
- print("%s Done.") % APP_PROMPT
- print("%s Adding NPM modules" % APP_PROMPT)
- subprocess.check_call("yarn install", shell=True)
- print("%s Done." % APP_PROMPT)
- def babel_init():
- print("%s Generating Babel config file..." % APP_PROMPT)
- with open(".babelrc", "w+") as file:
- file.write(babel_rc)
- print("%s Done." % APP_PROMPT)
- def webpack_init():
- print("%s Generating Webpack config file..." % APP_PROMPT)
- with open("webpack.config.js", "w+") as file:
- file.write(webpack_config)
- print("%s Done." % APP_PROMPT)
- def tree_init():
- print("%s Setting up file tree..." % APP_PROMPT)
- subprocess.check_output(["mkdir", "src"])
- subprocess.check_output(["mkdir", "public"])
- print("%s Done." % APP_PROMPT)
- def html_init():
- print("%s HTML file setup..." % APP_PROMPT)
- print("%s Generating HTML index file..." % APP_PROMPT)
- with open("index.html", "w+") as file:
- file.write(html_template)
- def css_init():
- print("%s Generating CSS file..." % APP_PROMPT)
- with open("public/app.css", "w+") as file:
- file.write(css_template)
- print("%s Done.")
- def js_init():
- print("%s Generating base javascript file..." % APP_PROMPT)
- with open("src/index.js", "w+") as file:
- file.write(javascript_template)
- print("%s Done." % APP_PROMPT)
- def build_init():
- print("%s Building app...")
- subprocess.check_call('webpack', shell=True)
- subprocess.check_call('open index.html', shell=True)
- def main():
- git_choice = raw_input("%s Would you like to start a git repository? " % APP_PROMPT)
- if git_choice == "y":
- git_init()
- elif git_choice == "Y":
- git_init()
- elif git_choice == "n":
- continue_message()
- elif git_choice == "N":
- continue_message()
- else:
- continue_message()
- tree_init()
- yarn_init()
- babel_init()
- webpack_init()
- html_init()
- css_init()
- js_init()
- build_init()
- print("\n\n%s All done! Enjoy working on your new project!\r\n\n" % APP_PROMPT)
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment