smhdale

webdev_setup.sh

Dec 20th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.51 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # First, check for global gulp install
  4. npm list -g | grep gulp &> /dev/null
  5. if [ $? == 1 ]; then
  6.   echo 'Please install gulp globally before running this script: sudo npm i -g gulp'
  7.   exit 1
  8. fi
  9.  
  10. # Init repo
  11. npm init
  12.  
  13. # Gulp
  14. npm i -D gulp gulp-rename gulp-server-livereload
  15.  
  16. # JS
  17. npm i -D browserify babelify babel-register babel-preset-env gulp-sourcemaps
  18.  
  19. # CSS
  20. npm i -D gulp-sass gulp-autoprefixer
  21.  
  22. # Vinyl
  23. npm i -D vinyl-source-stream vinyl-buffer
  24.  
  25. # Folder structure
  26. mkdir src
  27. touch src/index.js
  28.  
  29. mkdir src/scss
  30. touch src/scss/base.scss
  31.  
  32. mkdir dist
  33. mkdir dist/css
  34. mkdir dist/js
  35. touch dist/index.html
  36.  
  37. #
  38. # CREATE BAREBONES SITE
  39. #
  40.  
  41. # SASS
  42. cat > src/scss/base.scss <<'END'
  43. * {
  44.   margin: 0;
  45.   padding: 0;
  46.   box-sizing: border-box;
  47. }
  48.  
  49. h1 {
  50.   font-family: Verdana, sans-serif;
  51.   color: #333;
  52. }
  53. END
  54.  
  55. # JS
  56. echo "console.log('Hello world!')" > src/index.js
  57.  
  58. # HTML
  59. cat > dist/index.html <<'END'
  60. <!DOCTYPE html>
  61. <html>
  62.  
  63. <head>
  64.   <title>My website</title>
  65.   <meta charset="UTF-8">
  66.   <link type="text/css" rel="stylesheet" href="css/style.css">
  67. </head>
  68.  
  69. <body>
  70.   <h1>Hello world!</h1>
  71.   <script type="text/javascript" src="js/bundle.js"></script>
  72. </body>
  73.  
  74. </html>
  75. END
  76.  
  77. #
  78. # .BABELRC
  79. #
  80.  
  81. cat > .babelrc <<'END'
  82. {
  83.   "presets": [ "env" ]
  84. }
  85. END
  86.  
  87. #
  88. # CREATE GULPFILE
  89. #
  90.  
  91. gulpfile='gulpfile.babel.js'
  92. > $gulpfile
  93.  
  94. function add () {
  95.   echo $1 >> $gulpfile
  96. }
  97.  
  98. function indent () {
  99.   printf '%0.s ' $(seq 1 $1) >> $gulpfile
  100. }
  101.  
  102. # Add dependencies to gulpfile
  103. declare -a depnames=(browserify gulp rename sourcemaps sass autoprefixer source buffer server)
  104. declare -a dependencies=(browserify gulp gulp-rename gulp-sourcemaps gulp-sass gulp-autoprefixer vinyl-source-stream vinyl-buffer gulp-server-livereload)
  105.  
  106. add '// Dependencies'
  107. for i in {0..8}; do
  108.   add "import ${depnames[i]} from '${dependencies[i]}'"
  109. done
  110. add
  111.  
  112. # Useful paths
  113. declare -a pathnames=(serverPath sassSrc sassWatchPath sassOutputPath jsSrc jsWatchPath jsOutputPath)
  114. declare -a paths=(./dist ./src/scss/base.scss ./src/scss/**/*.scss ./dist/css ./src/index.js ./src/**/*.js ./dist/js)
  115.  
  116. add '// Paths'
  117. for i in {0..6}; do
  118.   add "const ${pathnames[i]} = '${paths[i]}'"
  119. done
  120. add
  121.  
  122. # Gulp task building functions
  123. function gulp_task_start () {
  124.   add "gulp.task('$1', () => {"
  125. }
  126. function gulp_task_pipe () {
  127.   arr=("$@")
  128.   for i in "${arr[@]}"; do
  129.     indent 4; add ".pipe($i)"
  130.   done
  131. }
  132. function gulp_task_end () {
  133.   add '})'; add
  134. }
  135.  
  136. # SASS
  137. gulp_task_start 'sass'
  138. indent 2; add 'return gulp.src(sassSrc)'
  139. gulp_task_pipe 'sourcemaps.init()' "sass().on('error', sass.logError)" "autoprefixer({browsers:['last 2 versions']})" 'sourcemaps.write()' "rename('style.css')" 'gulp.dest(sassOutputPath)'
  140. gulp_task_end
  141.  
  142. # JS
  143. gulp_task_start 'js'
  144. indent 2; add "return browserify(jsSrc, {debug: true, extensions:['es6']})"
  145. indent 4; add ".transform('babelify', {presets: ['env']})"
  146. indent 4; add ".bundle()"
  147. gulp_task_pipe "source('bundle.js')" 'buffer()' 'sourcemaps.init({loadMaps: true})' 'sourcemaps.write()' 'gulp.dest(jsOutputPath)'
  148. gulp_task_end
  149.  
  150. # Watch
  151. gulp_task_start 'watch'
  152. indent 2; add "gulp.watch(sassWatchPath, ['sass'])"
  153. indent 2; add "gulp.watch(jsWatchPath, ['js'])"
  154. gulp_task_end
  155.  
  156. # Server
  157. gulp_task_start 'webserver'
  158. indent 2; add 'gulp.src(serverPath)'
  159. indent 4; add '.pipe(server({'
  160. indent 6; add 'livereload: true,'
  161. indent 6; add 'open: true'
  162. indent 4; add '}))'
  163. gulp_task_end
  164.  
  165. # Default
  166. add "gulp.task('default', ['sass', 'js', 'watch', 'webserver'])"
  167.  
  168. # Run gulp
  169. gulp
Add Comment
Please, Sign In to add comment