-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathindex.js
350 lines (297 loc) · 10.3 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// explicity list globals here
var $ = window.$
var config = require('./config')
var elementClass = require('element-class')
var createSandbox = require('browser-module-sandbox')
var url = require('url')
var request = require('browser-request')
var detective = require('detective')
var keydown = require('keydown')
var uglify = require('uglify-js')
var cookie = require('./lib/cookie')
var Gist = require('./lib/github-gist.js')
var ui = require('./lib/ui-controller')
var editors = window.editors = require('./lib/editors')
initialize()
function initialize () {
var sandbox
var gistID
var githubGist = new Gist({
token: cookie.get('oauth-token'),
auth: 'oauth'
})
var packagejson = {'name': 'requirebin-sketch', 'version': '1.0.0'}
var parsedURL = url.parse(window.location.href, true)
var gistTokens = Gist.fromUrl(parsedURL)
window.packagejson = packagejson
// dom nodes
var outputEl = document.querySelector('#play')
var howTo = document.querySelector('#howto')
var share = document.querySelector('#share')
var loggedIn = false
if (cookie.get('oauth-token')) loggedIn = true
if (gistTokens) {
gistID = gistTokens.id
ui.enableShare(gistID)
}
// special parameter `code` is used to perform the auth + redirection
// so no need to load the code
if (parsedURL.query.code) return authenticate()
var currentHost = parsedURL.protocol + '//' + parsedURL.hostname
if (parsedURL.port) currentHost += ':' + parsedURL.port
function doBundle () {
sandbox.iframeHead = editors.get('head').getValue()
sandbox.iframeBody = editors.get('body').getValue()
sandbox.bundle(editors.get('bundle').getValue(), packagejson.dependencies)
}
// todo: move to auth.js
function authenticate () {
if (cookie.get('oauth-token')) {
return
}
var match = window.location.href.match(/\?code=([a-z0-9]*)/)
// Handle Code
if (!match) return false
var authURL = config.GATEKEEPER + '/authenticate/' + match[1]
request({url: authURL, json: true}, function (err, resp, data) {
if (err) return console.error(err)
console.log('auth response', resp, data)
if (data.token === 'undefined') return console.error('Auth failed to acquire token')
cookie.set('oauth-token', data.token)
// Adjust URL
var regex = new RegExp('\\?code=' + match[1])
window.location.href = window.location.href.replace(regex, '').replace('&state=', '') + '?save=true'
})
return true
}
function stringifyPackageJson () {
return JSON.stringify(packagejson, null, ' ')
}
function saveGist (id, opts) {
ui.$spinner.show()
var entry = editors.get('bundle').getValue()
opts = opts || {}
opts.isPublic = 'isPublic' in opts ? opts.isPublic : true
doBundle()
sandbox.on('bundleEnd', function (bundle) {
var minified
try {
minified = uglify.minify(bundle.script, {fromString: true, mangle: false, compress: false}).code
} catch (e) {
minified = bundle.script
}
var gist = {
'description': 'requirebin sketch',
'public': opts.isPublic,
'files': {
'index.js': {
'content': entry
},
'minified.js': {
'content': minified
},
'requirebin.md': {
'content': 'made with [requirebin](http://requirebin.com)'
},
'package.json': {
'content': stringifyPackageJson()
}
}
}
// the gist can't have empty fields or the github api request will fail
if (sandbox.iframeHead) gist.files['page-head.html'] = {'content': sandbox.iframeHead}
if (sandbox.iframeBody) gist.files['page-body.html'] = {'content': sandbox.iframeBody}
githubGist.save(gist, id, opts, function (err, newGist) {
var newGistId = newGist.id
if (newGist.user && newGist.user.login) {
newGistId = newGist.user.login + '/' + newGistId
}
ui.$spinner.hide()
if (err) ui.tooltipMessage('error', err.toString())
if (newGistId) window.location.href = '/?gist=' + newGistId
})
})
}
ui.$spinner.show()
// if gistID is not set, fallback to specific queryParams, local storage
githubGist.getCode(gistID, function (err, code) {
ui.$spinner.hide()
if (err) return ui.tooltipMessage('error', JSON.stringify(err))
editors.init(code)
editors.setActive('bundle')
// actions done with the meta editor:
// - update the value of the editor whenever it's focused (it always has a valid json)
// - the runButton is disabled if the value it has is invalid
function updatePackageJson () {
var code = editors.get('meta').editor.getValue()
try {
ui.$runButton.removeClass('disabled')
window.packagejson = packagejson = JSON.parse(code)
} catch (e) {
// don't allow running the code if package.json is invalid
ui.$runButton.addClass('disabled')
}
}
// perform an initial package.json check
updatePackageJson()
editors.get('meta')
.on('afterFocus', function (editor) {
editor.setValue(stringifyPackageJson())
})
editors.get('meta')
.on('change', updatePackageJson)
// remove the `disabled` class from the save button when any editor is updated
editors.all(function (editor) {
editor.on('change', function (e) {
ui.$runButton.removeClass('disabled')
})
})
var packageTags = $('.tagsinput')
editors.get('bundle').on('valid', function (valid) {
if (!valid) return
ui.$runButton.removeClass('hidden')
$('.editor-picker').removeClass('hidden')
packageTags.html('')
var modules = detective(editors.get('bundle').getValue())
modules.map(function (module) {
var tag =
'<span class="tag"><a target="_blank" href="http://npmjs.org/' +
module + '"><span>' + module + '  </span></a></span>'
packageTags.append(tag)
})
if (modules.length === 0) packageTags.append('<div class="tagsinput-add">No Modules Required Yet</div>')
})
var sandboxOpts = {
cdn: config.BROWSERIFYCDN,
container: outputEl,
iframeStyle: 'body, html { height: 100% width: 100% }'
}
if (parsedURL.query.save) {
// use memdown here to avoid indexeddb transaction bugs :(
sandboxOpts.cacheOpts = { inMemory: true }
sandbox = createSandbox(sandboxOpts)
saveGist(gistID, {
'isPublic': !parsedURL.query['private']
})
} else {
sandbox = createSandbox(sandboxOpts)
}
// sandbox actions
sandbox.on('modules', function (modules) {
if (!modules) return
packagejson.dependencies = {}
modules.forEach(function (mod) {
if (mod.core) return
packagejson.dependencies[mod.name] = mod.version
})
})
sandbox.on('bundleStart', function () {
ui.$spinner.show()
})
sandbox.on('bundleEnd', function (bundle) {
ui.$spinner.hide()
})
sandbox.on('bundleError', function (err) {
ui.$spinner.hide()
ui.tooltipMessage('error', 'Bundling error: \n\n' + err)
})
if (parsedURL.query.save) return
// UI actions
// TODO: move them to ui-controller.js
document.querySelector('.hide-howto').addEventListener('click', function () {
elementClass(howTo).add('hidden')
})
var actionsMenu = $('.actionsMenu')
actionsMenu.dropkick({
change: function (value, label) {
if (value === 'noop') return
if (value in actions) actions[value]()
setTimeout(function () {
actionsMenu.dropkick('reset')
}, 0)
}
})
$('.actionsButtons a').click(function () {
var target = $(this)
var action = target.attr('data-action')
if (action in actions) actions[action]()
})
// call actions.play from the button located in the instructions
$('.run-btn').click(function (e) {
e.preventDefault()
$('a[data-action="play"]').click()
return false
})
var actions = {
play: function () {
// only execute play if any editor is dirty
var isDirty = editors.asArray()
.filter(function (editor) {
return !editor.editor.isClean()
})
.length > 0
if (!isDirty) {
return
}
// mark all the editors as clean
editors.all(function (editor) {
editor.editor.markClean()
})
ui.$runButton.addClass('disabled')
ui.$spinner.hide()
doBundle()
},
save: function () {
if (loggedIn) return saveGist(gistID)
ui.$spinner.show()
var loginURL = 'https://github.com/login/oauth/authorize' +
'?client_id=' + config.GITHUB_CLIENT +
'&scope=gist' +
'&redirect_uri=' + currentHost
window.location.href = loginURL
},
'save-private': function () {
if (loggedIn) return saveGist(gistID, { 'isPublic': false })
ui.$spinner.show()
var loginURL = 'https://github.com/login/oauth/authorize' +
'?client_id=' + config.GITHUB_CLIENT +
'&scope=gist' +
'&private=true' +
'&redirect_uri=' + currentHost
window.location.href = loginURL
},
howto: function () {
elementClass(howTo).remove('hidden')
elementClass(share).add('hidden')
},
share: function () {
elementClass(howTo).add('hidden')
elementClass(share).remove('hidden')
},
'show-forks': function () {
gistID && ui.showForks(githubGist.forks, githubGist.parent)
},
'github': function () {
window.location.href = 'https://github.com/maxogden/requirebin'
}
}
keydown(['<meta>', '<enter>']).on('pressed', actions.play)
keydown(['<control>', '<enter>']).on('pressed', actions.play)
// UI actions when there's no Gist
if (!gistID) {
// enable localStorage save when the user is working on a new gist
editors.all(function (editor) {
editor.on('change', function () {
var code = editor.editor.getValue()
window.localStorage.setItem(editor.name + 'Code', code)
})
})
// hide the forks option in the dropdown
$('a[data-dk-dropdown-value="show-forks"]').parent('li').hide()
}
// loads the current code on load
setTimeout(function () {
actions.play()
}, 500)
})
}