forked from mmckegg/loop-drop-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
178 lines (153 loc) · 4.1 KB
/
build.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var watchify = require('watchify')
var catw = require('catw')
var mcss = require('micro-css')
var Concat = require('concat-stream')
var watchViews = require('rincewind-watch')
var gaze = require('gaze')
var getResourceKey = require('unique-resource')
var fs = require('fs')
process.cwd(__dirname)
if (!fs.existsSync('build')){
fs.mkdirSync('build')
}
var pendingSteps = 0
function pendingStep(){
pendingSteps += 1
}
function popStep(){
pendingSteps -= 1
if (!pendingSteps){
if (~process.argv.indexOf('-w') || ~process.argv.indexOf('--watch')){
console.log('Build complete. Watching for changes...')
} else {
process.exit()
}
}
}
var viewPath = __dirname + '/views'
pendingStep()
watchViews({
window: viewPath + '/window.html',
nodeEditor: viewPath + '/nodes/editor.html'
}, function(views, changedFiles){
fs.writeFileSync(viewPath + '/index.js', getViewsModule(views, viewPath))
changedFiles.forEach(function(f){
if (/.js$/.exec(f) && require.cache[f]){
;delete require.cache[f]
}
})
fs.writeFile('build/index.html', views.window(), popStep)
})
function getViewsModule(views, relativeTo){
var results = Object.keys(views).map(function(key){
return key + ': View(' + views[key].stringify(relativeTo) + ')'
})
return '// generated by ../build.js\n' +
'var View = require("rincewind")\n' +
'module.exports = {\n ' + results.join(',\n ') + '\n}'
}
pendingStep()
pendingStep()
catw('package.json', function(stream){
stream.pipe(Concat(function(data){
var pkg = JSON.parse(data)
fs.writeFile('build/manifest.json', JSON.stringify({
name: 'Loop Drop',
version: pkg.version,
description: pkg.description,
author: pkg.author,
manifest_version: 2,
permissions: pkg.permissions,
app: {
background: {
scripts: ['background.js']
}
}//,
//icons: {
// 16: "icon-16.png",
// 128: "icon-128.png"
//}
}, null, 2), popStep)
fs.writeFile(
'build/background.js',
"chrome.app.runtime.onLaunched.addListener(function() { \n" +
" chrome.app.window.create('index.html', " + JSON.stringify(pkg.window || null) + ")\n" +
"})", popStep
)
}))
})
pendingStep()
gaze('**/*.mcss', function(err, watcher){
var styles = {}
var cache = {}
function update(filepath){
var key = getResourceKey(filepath, {cache: false})
var content = fs.readFileSync(filepath)
content = mcss(key + ' { ' + content + ' }')
if (content){
styles[filepath] = content
} else {
delete styles[filepath]
}
}
function dumpFile(){
var content = ''
Object.keys(styles).forEach(function(k){
content += styles[k]
})
fs.writeFile('build/resource.css', content, popStep)
}
this.watched(function(err, watched) {
for (var dir in watched){
for (var i=0;i<watched[dir].length;i++){
var file = watched[dir][i]
if (file.slice(-5) === '.mcss'){
update(file)
}
}
}
dumpFile()
})
this.on('changed', function(filepath){
update(filepath)
dumpFile()
})
})
pendingStep()
catw('styles/*.mcss', function(stream){
stream.pipe(Concat(function(data){
fs.writeFile('build/bundle.css', mcss(String(data)), popStep)
}))
})
pendingStep()
catw('styles/*.css', function(stream){
stream.pipe(fs.createWriteStream('build/extra.css')).on('finish', popStep)
})
pendingStep()
var w = watchify('./index.js')
//w.require('through')
w.on('update', bundle)
bundle()
function bundle(){
w.bundle({debug: true}).pipe(fs.createWriteStream('build/bundle.js')).on('finish', popStep)
}
function getHtml(options){
var result = ''
result += "<!doctype html>\n"
if (options.title){
result += "<title>" + options.title + "</title>\n"
}
if (options.styles){
options.styles.forEach(function(style){
result += '<link rel="stylesheet" href="' + style + '"></script>'
})
}
result += '<body>\n'
if (options.scripts){
options.scripts.forEach(function(script){
result += '<script src="' + script + '"></script>'
})
}
result += '</body>\n'
return result
}