forked from rstuven/OpenTweetFilter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cakefile
118 lines (105 loc) · 3.65 KB
/
Cakefile
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
fs = require 'fs'
util = require 'util'
{execFile, exec} = require 'child_process'
appFiles = [
# omit 'src/' and '.coffee' to make the below lines a little shorter
'messages'
'messages.en'
'messages.es'
'messages.pt'
'models/DialogViewModel'
'views/View'
'views/PhoenixT1DialogView'
'providers/Provider'
'providers/PhoenixT1Provider'
'providers/HighlineProvider'
'Extension'
].map (file) -> "src/#{file}.coffee"
build = ->
appContents = new Array remaining = appFiles.length
for file, index in appFiles then do (file, index) ->
fs.readFile file, 'utf8', (err, fileContents) ->
return logerr err if err
appContents[index] = fileContents
link() if --remaining is 0
link = ->
fs.writeFile 'build/data/js/filter.coffee', appContents.join('\n\n'), 'utf8', (err) ->
return logerr err if err
exec "coffee --compile #{__dirname}/build/data/js/filter.coffee", (err, stdout, stderr) ->
return logerr err if err
console.log stdout + stderr
fs.unlink 'build/data/js/filter.coffee', (err) ->
return logerr err if err
console.log 'Built.\n'
task 'build', 'Build single application file from source files.', ->
build()
task 'watch', 'Watch source files changes and build.', ->
build()
@event = null
@filename = null
for file, index in appFiles then do (file, index) ->
fs.watch file, (event, filename) =>
if event != @event or filename != @filename
console.log event + ' ' + filename
@event = event
@filename = filename
throttle 100, =>
build()
@event = null
@filename = null
task 'pack:crx', 'Create or update a Chrome extension package.', ->
args = ["--pack-extension=#{__dirname}/build"]
pemFile = "#{__dirname}/build.pem";
if fs.existsSync pemFile
args.push '--pack-extension-key=' + pemFile
execFile process.env.LOCALAPPDATA + '/Google/Chrome/Application/chrome.exe', args, (err, stdout, stderr) ->
return logerr err if err
console.log stdout + stderr
task 'pack:cws', 'Update an extension package for Chrome Web Store.', ->
manifestContent = fs.readFileSync "#{__dirname}/build/manifest.json", 'utf8'
manifest = JSON.parse manifestContent
zipFile = "package-cws-#{manifest.version}.zip"
console.log "\nGenerating #{zipFile}...\n"
fs.unlink zipFile, (err) ->
exec "cd build/ & zip -r9 ../#{zipFile} *", (err, stdout, stderr) ->
return logerr err if err
console.log stdout + stderr
task 'pack:clear', 'Remove all package related files.', ->
walk
dir: "#{__dirname}"
recursive: false
matcher: (file) ->
file.match(/\/package-(.+)\.zip$/) or file.match(/\.crx$/) or file.match(/\.pem$/) or file.match(/\.xpi$/)
action: (file) -> fs.unlink file, (err) ->
return logerr err if err and err.code isnt 'ENOENT' # ENOENT: No such file or directory
# Utils
logerr = (err) ->
console.log err.message + '\n'
throttle = do ->
timeout = null
(delay, fn) ->
clearTimeout timeout
timeout = setTimeout fn, delay
fs.copy = (src, dst, ow, cb) ->
fs.stat dst, (err) ->
return cb new Error "File #{dst} exists." if not err and not ow
fs.stat src, (err) ->
return cb err if err
ist = fs.createReadStream src
ost = fs.createWriteStream dst
util.pump ist, ost, cb
# Based on Rosetta Code
walk = (options) ->
dir = options.dir
f_match = options.matcher
f_visit = options.action
recursive = options.recursive ? true
_walk = (dir) ->
fns = fs.readdirSync dir
for fn in fns
fn = dir + '/' + fn
if f_match fn
f_visit fn
if recursive and fs.statSync(fn).isDirectory()
_walk fn
_walk(dir)