1 | process.title = 'cakefile quickdom'
|
2 | global.Promise = require('bluebird').config warnings:false, longStackTraces:false
|
3 | promiseBreak = require 'promise-break'
|
4 | execa = require('execa')
|
5 | extend = require 'smart-extend'
|
6 | fs = require 'fs-jetpack'
|
7 | chalk = require 'chalk'
|
8 | Path = require 'path'
|
9 | process.env.SOURCE_MAPS ?= 1
|
10 | buildModules = ['google-closure-compiler-js','uglify-js@3.0.24', 'listr', 'simplywatch@3.0.0', 'simplyimport@4.0.9', 'babelify', '@babel/core', '@babel/preset-env']
|
11 | coverageModules = ['istanbul', 'badge-gen', 'coffee-coverage']
|
12 | testModules = [
|
13 | 'mocha', 'chai', 'github:danielkalen/chai-style'
|
14 | 'electron', 'karma@1.6.0', 'karma-chrome-launcher', 'karma-coverage', 'karma-electron', 'karma-firefox-launcher',
|
15 | 'karma-ie-launcher', 'karma-mocha', 'karma-mocha-reporter', 'karma-opera-launcher', 'karma-safari-launcher', 'github:danielkalen/karma-sauce-launcher'
|
16 | ]
|
17 | MEASURE_LOG = './.config/measure.json'
|
18 | PACKAGE = './package.json'
|
19 |
|
20 | option '-d', '--debug', 'run in debug mode'
|
21 | option '-t', '--target [target]', 'target measure dir'
|
22 |
|
23 |
|
24 | task 'build', ()->
|
25 | Promise.resolve()
|
26 | .then ()-> invoke 'install:build'
|
27 | .then ()-> invoke 'build:js'
|
28 | .then ()-> invoke 'build:test'
|
29 |
|
30 |
|
31 | task 'build:js', (options)->
|
32 | debug = if options.debug then '.debug' else ''
|
33 | Promise.resolve(['index.coffee'])
|
34 | .map (file)->
|
35 | {src:"src/index.coffee", dest:"build/quickdom#{debug}.js"}
|
36 |
|
37 | .map (file)->
|
38 | title: "Compiling quickdom"
|
39 | task: ()-> compileJS(file, debug:options.debug, sourceMap:false, umd:'quickdom', target:'browser')
|
40 |
|
41 | .then runTaskList
|
42 |
|
43 |
|
44 | task 'build:test', (options)->
|
45 | Promise.resolve(['index.coffee'])
|
46 | .tap ()-> invoke 'install:test'
|
47 | .map (file)->
|
48 | {src:"test/test.coffee", dest:"test/test.js"}
|
49 |
|
50 | .map (file)->
|
51 | title: "Compiling test"
|
52 | task: ()-> compileJS(file, debug:true, noPkgConfig:true, usePaths:true)
|
53 |
|
54 | .then runTaskList
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | task 'watch', ()->
|
61 | Promise.resolve()
|
62 | .then ()-> invoke 'install:build'
|
63 | .then ()->
|
64 | invoke 'watch:js'
|
65 | invoke 'watch:test'
|
66 |
|
67 |
|
68 |
|
69 | task 'watch:js', (options)->
|
70 | debug = if options.debug then '.debug' else ''
|
71 | require('simplywatch')
|
72 | globs: "src/*.coffee"
|
73 | bufferTimeout: 1
|
74 | haltSerial: true
|
75 | command: (file, params)->
|
76 | Promise.resolve()
|
77 | .then ()-> {src:"src/index.coffee", dest:"build/quickdom#{debug}.js"}
|
78 | .then (file)-> compileJS(file, debug:options.debug, sourceMap:false, umd:'quickdom', target:'browser')
|
79 |
|
80 |
|
81 | task 'watch:test', (options)->
|
82 | require('simplywatch')
|
83 | globs: "test/*.coffee"
|
84 | bufferTimeout: 1
|
85 | haltSerial: true
|
86 | command: (file, params)->
|
87 | Promise.resolve()
|
88 | .then ()-> {src:"test/test.coffee", dest:"test/test.js"}
|
89 | .then (file)-> compileJS(file, noPkgConfig:true, debug:true, usePaths:true)
|
90 |
|
91 |
|
92 |
|
93 | task 'install', ()->
|
94 | Promise.resolve()
|
95 | .then ()-> invoke 'install:build'
|
96 | .then ()-> invoke 'install:test'
|
97 | .then ()-> invoke 'install:coverage'
|
98 | .then ()-> invoke 'install:measure'
|
99 |
|
100 |
|
101 | task 'install:build', ()->
|
102 | Promise.resolve()
|
103 | .then ()-> buildModules.filter (module)-> not moduleInstalled(module)
|
104 | .tap (missingModules)-> promiseBreak() if missingModules.length is 0
|
105 | .tap (missingModules)-> installModules(missingModules)
|
106 | .catch promiseBreak.end
|
107 |
|
108 |
|
109 | task 'install:test', ()->
|
110 | Promise.resolve()
|
111 | .then ()-> testModules.filter (module)-> not moduleInstalled(module)
|
112 | .tap (missingModules)-> promiseBreak() if missingModules.length is 0
|
113 | .tap (missingModules)-> installModules(missingModules)
|
114 | .catch promiseBreak.end
|
115 |
|
116 |
|
117 | task 'install:coverage', ()->
|
118 | Promise.resolve()
|
119 | .then ()-> coverageModules.filter (module)-> not moduleInstalled(module)
|
120 | .tap (missingModules)-> promiseBreak() if missingModules.length is 0
|
121 | .tap (missingModules)-> installModules(missingModules)
|
122 | .catch promiseBreak.end
|
123 |
|
124 |
|
125 | task 'install:measure', ()->
|
126 | Promise.resolve()
|
127 | .then ()-> ['gzipped', 'sugar'].filter (module)-> not moduleInstalled(module)
|
128 | .tap (missingModules)-> promiseBreak() if missingModules.length is 0
|
129 | .tap (missingModules)-> installModules(missingModules)
|
130 | .catch promiseBreak.end
|
131 |
|
132 |
|
133 |
|
134 | task 'measure', (options)->
|
135 | Promise.resolve()
|
136 | .then ()-> fs.writeAsync(MEASURE_LOG, {}) if not fs.exists(MEASURE_LOG)
|
137 | .then ()-> invoke 'install:measure'
|
138 | .then ()->
|
139 | DIR = if options.target then options.target else 'build'
|
140 | measure {debug:"./#{DIR}/quickdom.debug.js", release:"./#{DIR}/quickdom.js"}
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | runTaskList = (tasks)->
|
159 | (new (require 'listr')(tasks, concurrent:true)).run()
|
160 |
|
161 |
|
162 | measure = (file)->
|
163 | gzipped = Promise.promisifyAll require('gzipped')
|
164 | bytes = require 'sugar/number/bytes'
|
165 | isEqual = require 'sugar/object/isEqual'
|
166 | results = debug:null, release:null
|
167 |
|
168 | Promise.resolve()
|
169 | .then ()-> gzipped.calculateAsync fs.createReadStream(file.debug)
|
170 | .then (result)-> results.debug = 'orig':bytes(result.original,2), 'gzip':bytes(result.compressed,2)
|
171 |
|
172 | .then ()-> gzipped.calculateAsync fs.createReadStream(file.release)
|
173 | .then (result)-> results.release = 'orig':bytes(result.original,2), 'gzip':bytes(result.compressed,2)
|
174 |
|
175 | .then ()-> Promise.all [fs.readAsync(MEASURE_LOG,'json'), fs.readAsync(PACKAGE,'json').get('version')]
|
176 | .then ([log, version])->
|
177 | log[version] ?= []
|
178 | lastResult = log[version].slice(-1)[0]
|
179 | return log if lastResult and isEqual(lastResult, results)
|
180 | log[version].push(results)
|
181 | return log
|
182 |
|
183 | .then (updatedLog)-> fs.writeAsync MEASURE_LOG, updatedLog
|
184 | .then ()->
|
185 | console.log "#{chalk.dim 'DEBUG '} #{chalk.green results.debug.gzip} (#{chalk.yellow results.debug.orig})"
|
186 | console.log "#{chalk.dim 'RELEASE'} #{chalk.green results.release.gzip} (#{chalk.yellow results.release.orig})"
|
187 | console.log '\n'
|
188 |
|
189 |
|
190 | compileJS = (file, options)->
|
191 | Promise.resolve()
|
192 | .then ()-> require('simplyimport')(extend {file:file.src}, options)
|
193 | .then (result)-> fs.writeAsync(file.dest, result)
|
194 | .catch (err)->
|
195 | console.error(err)# if err not instanceof Error
|
196 | throw err
|
197 |
|
198 |
|
199 |
|
200 | installModules = (targetModules)->
|
201 | targetModules = targetModules
|
202 | .filter (module)-> if typeof module is 'string' then true else module[1]()
|
203 | .map (module)-> if typeof module is 'string' then module else module[0]
|
204 |
|
205 | return if not targetModules.length
|
206 | console.log "#{chalk.yellow('Installing')} #{chalk.dim targetModules.join ', '}"
|
207 |
|
208 | execa('npm', ['install', '--no-save', '--no-purne', targetModules...], {stdio:'inherit'})
|
209 |
|
210 |
|
211 | moduleInstalled = (targetModule)->
|
212 | targetModule = targetModule[0] if typeof targetModule is 'object'
|
213 | if (split=targetModule.split('@')) and split[0].length
|
214 | targetModule = split[0]
|
215 | targetVersion = split[1]
|
216 |
|
217 | if /^github:.+?\//.test(targetModule)
|
218 | targetModule = targetModule.replace /^github:.+?\//, ''
|
219 |
|
220 | pkgFile = Path.resolve('node_modules',targetModule,'package.json')
|
221 | exists = fs.exists(pkgFile)
|
222 |
|
223 | if exists and targetVersion?
|
224 | currentVersion = fs.read(pkgFile, 'json').version
|
225 | exists = require('semver').gte(currentVersion, targetVersion)
|
226 |
|
227 | return exists
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|