Skip to content

Instantly share code, notes, and snippets.

@robertfairley
Created November 7, 2017 04:42
Show Gist options
  • Save robertfairley/2233f434cc19d0c3fd34f111db25a059 to your computer and use it in GitHub Desktop.
Save robertfairley/2233f434cc19d0c3fd34f111db25a059 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import subprocess
# GLOBALS
PROMPT = "[ ES5 INIT ]"
TITLE = ""
CSS = ""
TEMPLATE = {
"html": """
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=0, initial-scale=1.0" />
<title>%s</title>
%s
</head>
<body>
<main id="root" role="main"></main>
<script src="app.js"></script>
</body>
</html>
""" % (TITLE, CSS),
"css": """
<style>
html,body {
margin: 0;
padding: 0;
font-family: -apple-system, "Gill Sans", "Helvetica Neue", "Helvetica", "Verdana", sans-serif;
}
button {
appearance: none;
box-sizing: border-box;
}
button:focus {
outline: 0;
border: 1px solid blue;
}
</style>
""",
"javascript": """
const el = {
main: document.querySelector('#root'),
};
el.main.innerHTML = `
<h1>ES5/6 App - Browser Friendly!</h1>
<p>This is your new ES5 app compiled with webpack or rollup if you want!</p>
`
""",
"webpack": """
module.exports = {
entry: './src/index.js',
output: {
filename: 'app.js',
path: __dirname
},
module: {
rules: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }
]
}
};
""",
"rollup": """
import resolve from 'rollup-plugin-node-resolve'
export default {
entry: './src/index.js',
dest: 'app.js',
format: 'iife',
plugins: [
resolve({
module: true,
browser: true
})
]
}
""",
"package_json": """
{
"name": "",
"version": "0.0.1",
"description": "[ fill this in ]",
"main": "app.js",
"scripts": {
"build:wp": "webpack --progress",
"build:rp": "rollup -c",
"watch:wp": "webpack --progress --watch",
"watch:rp": "rollup -c -w",
"start": "serve -p 6060"
},
"devDependencies": {
"rollup-plugin-node-resolve": "latest",
"rollup": "latest",
"webpack": "latest",
"babel-core": "latest",
"babel-loader": "latest"
}
}
"""
}
def log(msg = None, file_name = None):
if (msg == None):
print ("%s File written: `%s`" % (PROMPT, file_name))
elif (file_name == None):
print ("%s %s" % (PROMPT, msg))
else:
print("%s %s: %s" % (PROMPT, msg, file_name))
def setup_modules():
subprocess.call(["npm", "install"])
def setup_npm():
filename = "package.json"
with open(filename, "w+") as fin:
fin.write(TEMPLATE["package_json"])
log(None, filename)
setup_modules()
def setup_webpack():
filename = "webpack.config.js"
with open(filename, "w+") as fin:
fin.write(TEMPLATE["webpack"])
log(None, filename)
def setup_rollup():
filename = "rollup.config.js"
with open(filename, "w+") as fin:
fin.write(TEMPLATE["rollup"])
log(None, filename)
def setup_html():
filename = "index.html"
with open(filename, "w+") as fin:
CSS = TEMPLATE["css"]
fin.write(TEMPLATE["html"])
log(None, filename)
def setup_javascript():
subprocess.call(["mkdir", "src"])
filename = "./src/index.js"
with open(filename, "w+") as fin:
fin.write(TEMPLATE["javascript"])
log(None, filename)
def main():
TITLE = raw_input("Enter a title for your application:\n>>> ")
if TITLE == None:
TITLE = "Blank App"
setup_npm()
setup_modules()
setup_webpack()
setup_rollup()
setup_html()
setup_javascript()
print ("%s Done!" % PROMPT)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment