Guest User

Untitled

a guest
Oct 24th, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. ES6 INIT
  5. --------
  6.  
  7. OCT 2017 - Robert Fairley <robert.fairley@rci.rogers.ca>
  8.  
  9. A quick and dirty ES6 Javascript project environment setup script.
  10.  
  11. TODO: Add optional parameters to manage complexity and organization
  12. of the project being set up.
  13. """
  14. import sys
  15. import subprocess
  16.  
  17. # GLOBALS
  18. APP_PROMPT = "[ES6 PROJECT]"
  19.  
  20.  
  21. # TEMPLATE STRINGS
  22. html_template = """<!DOCTYPE html>
  23. <html lang="en">
  24. <head>
  25. <meta charset="utf-8"/>
  26. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0"/>
  27. <title>ES6 Starter Project</title>
  28. <link rel="stylesheet" href="public/app.css"/>
  29. </head>
  30. <body>
  31. <main id="root" role="main"></main>
  32. <script src="app.js"></script>
  33. </body>
  34. </html>
  35. """
  36.  
  37. css_template = """body,html {
  38. font-family: -apple-system, "Helvetica Neue", "Helvetica", "Roboto", "Verdana", sans-serif;
  39. color: #222;
  40. font-size: 1.1em;
  41. }
  42. """
  43.  
  44. javascript_template = """const el = {
  45. main: document.querySelector('#root'),
  46. }
  47.  
  48. const domCache = []
  49. window.domCache = domCache;
  50.  
  51. function cache(element = null) {
  52. if (typeof element !== 'object') {
  53. console.error("You have to pass an element object to the cache function.")
  54. }
  55.  
  56. domCache.push(element)
  57. }
  58.  
  59. function render(e, cb = null) {
  60. let h1 = document.createElement('h1')
  61. let p = document.createElement('p')
  62.  
  63. h1.innerHTML = `Hello, world!`
  64. p.innerHTML = `Welcome to your new ES6 application!`
  65.  
  66. el.main.appendChild(h1)
  67. el.main.appendChild(p)
  68.  
  69. cache(h1)
  70. cache(p)
  71.  
  72. if (typeof cb === 'function') {
  73. cb()
  74. }
  75.  
  76. }
  77.  
  78. window.onload = e => {
  79. render(e, () => {
  80. console.log('Application mounted.')
  81. })
  82. }
  83. """
  84.  
  85. babel_rc = """{
  86. "presets": [ "es2015", "stage-0" ]
  87. }
  88. """
  89.  
  90. webpack_config = """const path = require('path');
  91.  
  92. const config = {
  93. entry: './src/index.js',
  94. output: {
  95. filename: 'app.js',
  96. path: path.resolve(__dirname)
  97. },
  98. module: {
  99. rules: [
  100. { test: /\.js?$/, loader: 'babel-loader', exclude: /node_modules/ }
  101. ]
  102. }
  103. };
  104.  
  105.  
  106. module.exports = config;
  107. """
  108.  
  109. package_json = """{
  110. "name": "es6-starter-project",
  111. "description": "",
  112. "version": "1.0.0",
  113. "main": "app.js",
  114. "scripts": {},
  115. "keywords": [],
  116. "license": "MIT",
  117. "dependencies":
  118. { "webpack": "latest"
  119. , "babel-core": "latest"
  120. , "babel-loader": "latest"
  121. , "sass-loader": "latest"
  122. , "css-loader": "latest"
  123. , "babel-preset-es2015": "latest"
  124. , "babel-preset-stage-0": "latest"
  125. }
  126. }
  127. """
  128.  
  129. def git_init():
  130. print("%s Initializing Git repo..." % APP_PROMPT)
  131. subprocess.check_output(['git', 'init'])
  132.  
  133. def continue_message(msg = None):
  134. if msg != None:
  135. print("%s %s" % (APP_PROMPT, msg))
  136. else:
  137. print "%s OK. Continuing...\n" % APP_PROMPT
  138.  
  139. def yarn_init():
  140.  
  141. print("%s Generating NPM package file..." % APP_PROMPT)
  142. with open("package.json", "w+") as file:
  143. file.write(package_json)
  144. print("%s Done.") % APP_PROMPT
  145. print("%s Adding NPM modules" % APP_PROMPT)
  146.  
  147. subprocess.check_call("yarn install", shell=True)
  148. print("%s Done." % APP_PROMPT)
  149.  
  150. def babel_init():
  151. print("%s Generating Babel config file..." % APP_PROMPT)
  152. with open(".babelrc", "w+") as file:
  153. file.write(babel_rc)
  154. print("%s Done." % APP_PROMPT)
  155.  
  156. def webpack_init():
  157. print("%s Generating Webpack config file..." % APP_PROMPT)
  158. with open("webpack.config.js", "w+") as file:
  159. file.write(webpack_config)
  160. print("%s Done." % APP_PROMPT)
  161.  
  162. def tree_init():
  163. print("%s Setting up file tree..." % APP_PROMPT)
  164. subprocess.check_output(["mkdir", "src"])
  165. subprocess.check_output(["mkdir", "public"])
  166. print("%s Done." % APP_PROMPT)
  167.  
  168. def html_init():
  169. print("%s HTML file setup..." % APP_PROMPT)
  170. print("%s Generating HTML index file..." % APP_PROMPT)
  171. with open("index.html", "w+") as file:
  172. file.write(html_template)
  173.  
  174. def css_init():
  175. print("%s Generating CSS file..." % APP_PROMPT)
  176. with open("public/app.css", "w+") as file:
  177. file.write(css_template)
  178. print("%s Done.")
  179.  
  180. def js_init():
  181. print("%s Generating base javascript file..." % APP_PROMPT)
  182. with open("src/index.js", "w+") as file:
  183. file.write(javascript_template)
  184. print("%s Done." % APP_PROMPT)
  185.  
  186. def build_init():
  187. print("%s Building app...")
  188. subprocess.check_call('webpack', shell=True)
  189. subprocess.check_call('open index.html', shell=True)
  190.  
  191. def main():
  192. git_choice = raw_input("%s Would you like to start a git repository? " % APP_PROMPT)
  193.  
  194. if git_choice == "y":
  195. git_init()
  196. elif git_choice == "Y":
  197. git_init()
  198. elif git_choice == "n":
  199. continue_message()
  200. elif git_choice == "N":
  201. continue_message()
  202. else:
  203. continue_message()
  204.  
  205. tree_init()
  206. yarn_init()
  207. babel_init()
  208. webpack_init()
  209. html_init()
  210. css_init()
  211. js_init()
  212. build_init()
  213.  
  214. print("\n\n%s All done! Enjoy working on your new project!\r\n\n" % APP_PROMPT)
  215.  
  216.  
  217.  
  218. if __name__ == "__main__":
  219. main()
Add Comment
Please, Sign In to add comment