-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgulpfile.coffee
More file actions
155 lines (130 loc) · 4.58 KB
/
Copy pathgulpfile.coffee
File metadata and controls
155 lines (130 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
gulp = require 'gulp'
vinyl = require 'vinyl-source-stream'
browserify = require 'browserify'
watchify = require 'watchify'
gutil = require 'gulp-util'
prettyHrtime = require 'pretty-hrtime'
notify = require 'gulp-notify'
mocha = require 'gulp-mocha'
mochaPhantomJS = require 'gulp-mocha-phantomjs'
template = require 'gulp-lodash-template'
coffee = require 'gulp-coffee'
del = require 'del'
uglify = require 'gulp-uglify'
fsExtra = require 'fs-extra'
pipeline = require('readable-stream').pipeline
walkSync = require 'klaw-sync'
path = require 'path'
startTime = null
logger =
start: ->
startTime = process.hrtime()
gutil.log 'Running', gutil.colors.green("'bundle'") + '...'
end: ->
taskTime = process.hrtime startTime
prettyTime = prettyHrtime taskTime
gutil.log 'Finished', gutil.colors.green("'bundle'"), 'in', gutil.colors.magenta(prettyTime)
handleErrors = ->
notify.onError
title: 'Compile error'
message: '<%= error.message %>'
.apply this, arguments
@emit 'end'
build = (test) ->
[output, entry, options] = if test
['tests.js', './test/index', debug: true]
else
['html-docx.js', './src/api', standalone: 'html-docx']
bundleMethod = if global.isWatching then watchify else browserify
bundler = bundleMethod
entries: [entry]
extensions: ['.coffee', '.tpl']
bundle = ->
logger.start()
bundler
.transform 'jstify', engine: 'lodash-micro', minifierOpts: false
.bundle options
.on 'error', handleErrors
.pipe vinyl(output)
.pipe gulp.dest('./build')
.on 'end', logger.end
if global.isWatching
bundler.on 'update', bundle
bundle()
testsBundle = './test/index.coffee'
clean = (cb) -> del 'build', cb
gulp.task 'clean', clean
gulp.task 'setWatch', -> global.isWatching = true
gulp.task 'build', -> build()
gulp.task 'watch', ['setWatch', 'build']
minify = ->
builtFile = './build/html-docx.js'
entry = './build/html-docx.min.js'
dest = './build'
fsExtra.copySync builtFile, entry
pipeline(
gulp.src(entry),
uglify(),
gulp.dest(dest)
)
gulp.task 'minify', minify
buildNode = (compileCoffee = true) ->
logger.start()
gulp.src('src/**/*', base: 'src').pipe(gulp.dest('build'))
gulp.src('src/templates/*.tpl').pipe(template commonjs: true).pipe(gulp.dest('build/templates'))
if compileCoffee
gulp.src('src/**/*.coffee').pipe(coffee bare: true)
.on('error', handleErrors).on('end', logger.end).pipe(gulp.dest('build'))
else
logger.end()
gulp.task 'build-node', buildNode
gulp.task 'test-node', (growl = false) ->
buildNode(false)
gulp.src(testsBundle, read: false).pipe mocha {reporter: 'spec', growl}
gulp.task 'test-node-watch', ->
sources = ['src/**', 'test/**']
gulp.watch sources, ['test-node']
gulp.task 'build-test-browserify', -> build(true)
gulp.task 'run-phantomjs', -> gulp.src('test/testbed.html').pipe(mochaPhantomJS reporter: 'spec')
gulp.task 'test-phantomjs', ['build-test-browserify', 'run-phantomjs']
cleanRelease = (cb) ->
del 'dist', cb
copyRelease = ->
jsFilesFilterFn = (item) ->
item.path.indexOf('.js') is item.path.length - '.js'.length
fsExtra.copySync './build/assets', './dist/node/assets'
templateFiles = walkSync './build/templates', filter: jsFilesFilterFn, noDir: true
templateFiles.forEach ({ path: filePath }) ->
fileName = path.basename(filePath)
fsExtra.copySync filePath, "./dist/node/templates/#{fileName}"
jsFiles = walkSync './build', filter: jsFilesFilterFn, noDir: true
jsFiles.forEach ({ path: filePath }) ->
fileName = path.basename(filePath)
outputPath = if fileName.indexOf('html-docx') isnt 0 then './dist/node' else './dist'
fsExtra.copySync filePath, "#{outputPath}/#{fileName}"
transportFile = ->
# del './test/publish'
fsExtra.copySync './build/html-docx.min.js', './test/publish/html-docx.min.js'
fsExtra.copySync './test/index.js', './test/publish/index.js'
fsExtra.copySync './test/jquery.js', './test/publish/jquery.js'
fsExtra.copySync './test/generate.js', './test/publish/generate.js'
fsExtra.copySync './test/sample.html', './test/publish/sample.html'
minFile = ->
builtFile = './test/index.js'
entry = './test/publish/index.js'
dest = './test/publish'
fsExtra.copySync builtFile, entry
pipeline(
gulp.src(entry),
uglify(),
gulp.dest(dest)
)
deletePublish = (cb) ->
del './test/publish', cb
gulp.task 'minify', minify
gulp.task 'clean-release', cleanRelease
gulp.task 'copy-release', copyRelease
gulp.task 'clean-publish', deletePublish
gulp.task 'min-file', minFile
gulp.task 'transport-file', transportFile
gulp.task 'default', ['test-node', 'test-node-watch']