Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.69 KB | None | 0 0
  1. connect = require 'connect'
  2. gulp = require 'gulp'
  3. lr = require 'connect-livereload'
  4. link = require './link.json'
  5. server = (require 'tiny-lr')()
  6. tasks = (require 'gulp-load-plugins')()
  7. watch = require 'gulp-watch'
  8. plumber = require 'gulp-plumber'
  9. cached = require 'gulp-cached'
  10. fs = require 'fs'
  11.  
  12. ### ###
  13.  
  14. coffee = []
  15. js = []
  16. sass = []
  17. css = []
  18. copy = []
  19.  
  20. ### ###
  21.  
  22.  
  23. getCoffee = (src, dest, prefix='') ->
  24. for s in src._scripts_ when /\.coffee$/.test s
  25. text = "#{prefix}#{s}"
  26. unless text in dest
  27. dest.push text
  28.  
  29.  
  30. gulp.task 'bower', ->
  31. tasks.bower link.lib
  32. .pipe gulp.dest link.lib
  33.  
  34. gulp.task 'clean:out', ->
  35. gulp.src link.out, read: false
  36. .pipe tasks.clean()
  37.  
  38. gulp.task 'lint', ->
  39. gulp.src (s for s in link._scripts_ when /\.coffee$/.test s)
  40. .pipe tasks.coffeelint()
  41. .pipe tasks.coffeelint.reporter()
  42.  
  43. gulp.task 'parse', ['bower'], ->
  44. coffee = []
  45. js = []
  46. sass = []
  47. css = []
  48. copy = []
  49.  
  50. getSass = (src, dest, prefix='') ->
  51. for s in src._styles_
  52. text = "#{prefix}#{s}"
  53. unless text in dest
  54. dest.push text
  55.  
  56. getCopy = (src, dest, prefix='') ->
  57. for s in src._to_copy_
  58. text = "#{prefix}#{s}"
  59. unless text in dest
  60. dest.push text
  61.  
  62. getCss = (src, dest) ->
  63. for s in src._styles_
  64. text = '<link rel="stylesheet" href="' + s.replace(/.*\*\*\//, '').replace(/\.scss$/, '.css') + '"/>'
  65. unless text in dest
  66. dest.push text
  67.  
  68. getJs = (src, dest) ->
  69. for s in src._scripts_
  70. if /\.js$/.test s
  71. text = '<script src="../' + s + '"></script>'
  72. else
  73. text = '<script src="' + s.replace(/.*\*\*\//, '').replace(/\.coffee$/, '.js') + '"></script>'
  74. unless text in dest
  75. dest.push text
  76.  
  77. getAll = (file, prefix='') ->
  78. if file._external_?
  79. for l in file._external_
  80. e_link = require "#{l}link.json"
  81. getAll e_link, l
  82.  
  83. getCoffee file, coffee, prefix
  84. getSass file, sass, prefix
  85. getCopy file, copy, prefix
  86. getCss file, css
  87. getJs file, js
  88.  
  89. getAll link
  90. getCoffee link._local_, coffee
  91. getJs link._local_, js
  92.  
  93. gulp.task 'copy', ['parse', 'bower'], ->
  94. gulp.src copy
  95. .pipe gulp.dest link.temp
  96. .pipe tasks.livereload server
  97.  
  98. gulp.task 'sass', ['parse', 'bower'], ->
  99. gulp.src sass
  100. .pipe tasks.sass()
  101. .pipe gulp.dest link.temp
  102. .pipe tasks.livereload server
  103.  
  104. gulp.task 'coffee', ['parse', 'bower'], ->
  105. gulp.src coffee
  106. .pipe cached('coffee')
  107. .pipe tasks.sourcemaps.init()
  108. .pipe tasks.coffee(bare: true).on 'error', tasks.util.log
  109. .pipe tasks.sourcemaps.write()
  110. .pipe gulp.dest link.temp
  111. .pipe tasks.livereload server
  112.  
  113. gulp.task 'link', ['parse', 'sass', 'coffee', 'copy', 'makeSettings'], ->
  114. gulp.src link.index
  115. .pipe tasks.replace(/<!--\s*scripts\s*-->/, js.join '\n ')
  116. .pipe tasks.replace(/<!--\s*styles\s*-->/, css.join '\n ')
  117. .pipe gulp.dest link.temp
  118.  
  119. gulp.task 'http-server', (cb) ->
  120. connect()
  121. .use lr()
  122. .use connect.static require('path').resolve ''
  123. .listen '8888'
  124. cb()
  125.  
  126. gulp.task 'open', ['link', 'http-server'], (cb) ->
  127. gulp.src link.index
  128. .pipe tasks.open '', {app: 'chrome', url: "http://localhost:8888/#{link.temp}index.html"}
  129. cb()
  130.  
  131. gulp.task 'serve', ['open'], ->
  132. server.listen(35729, ->
  133. gulp.watch link._to_copy_, ['copy']
  134. gulp.watch link._styles_, ['sass']
  135. gulp.watch link._scripts_, ['coffee']
  136. )
  137.  
  138. gulp.task 'test', ['bower', 'lint'], ->
  139. gulp.src link.test
  140. .pipe tasks.karma configFile: 'test/karma.conf.js'
  141.  
  142. gulp.task 'dev', ->
  143. files = []
  144. getCoffee link, files
  145. getCoffee link._local_, files
  146.  
  147. gulp.src files
  148. .pipe watch()
  149. .pipe plumber()
  150. .pipe tasks.sourcemaps.init()
  151. .pipe tasks.coffee(bare: true).on 'error', tasks.util.log
  152. .pipe tasks.sourcemaps.write()
  153. .pipe gulp.dest link.temp
  154.  
  155. gulp.task 'makeSettings', ['bower', 'clean:out'], (cb) ->
  156. settings = {}
  157.  
  158. extend = (dst, src) ->
  159. for own key of src
  160. if typeof dst[key] is 'object'
  161. extend dst[key], src[key]
  162. else
  163. dst[key] = src[key]
  164.  
  165. for l in link._external_
  166. e_link = require "#{l}link.json"
  167. if e_link.settings
  168. extend settings, require "#{l}#{e_link.settings}"
  169. if link.settings
  170. extend settings, require "./#{link.settings}"
  171.  
  172. unless fs.existsSync link.temp
  173. fs.mkdirSync link.temp
  174. fs.writeFile "#{link.temp}settings.json", JSON.stringify settings, null, ' '
  175. cb()
  176.  
  177. gulp.task 'copy:production', ['parse', 'bower', 'clean:out'], ->
  178. copy.push ".tmp/settings.json"
  179. gulp.src copy
  180. .pipe gulp.dest link.dst.production
  181.  
  182. gulp.task 'copy:dev', ['parse', 'bower', 'clean:out'], ->
  183. copy.push ".tmp/settings.json"
  184. gulp.src copy
  185. .pipe gulp.dest link.dst.dev
  186.  
  187. gulp.task 'imagemin:production', ['copy:production'], ->
  188. gulp.src "#{link.dst.production}**"
  189. .pipe tasks.imagemin()
  190. .pipe gulp.dest link.dst.production
  191.  
  192. gulp.task 'imagemin:dev', ['copy:dev'], ->
  193. gulp.src "#{link.dst.dev}**"
  194. .pipe tasks.imagemin()
  195. .pipe gulp.dest link.dst.dev
  196.  
  197. gulp.task 'usemin:production', ['link', 'clean:out'], ->
  198. gulp.src "#{link.temp}index.html"
  199. .pipe tasks.usemin js: [tasks.uglify()], css: [tasks.minifyCss(), 'concat'], html: [tasks.minifyHtml()]
  200. .pipe gulp.dest link.dst.production
  201.  
  202. gulp.task 'usemin:dev', ['link', 'clean:out'], ->
  203. gulp.src "#{link.temp}index.html"
  204. .pipe tasks.usemin css: [tasks.minifyCss(), 'concat']
  205. .pipe gulp.dest link.dst.dev
  206.  
  207. gulp.task 'build:production', ['imagemin:production', 'usemin:production']
  208. gulp.task 'build:dev', ['imagemin:dev', 'usemin:dev']
  209. gulp.task 'build', ['build:production', 'build:dev']
  210. gulp.task 'default', ['test']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement