-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
119 lines (99 loc) · 3.42 KB
/
main.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
'use strict';
global.process = { env: {} };
global.Buffer = require('buffer').Buffer;
const util = require('util');
const defaultValue = `
await mongoose.connect('mongodb://localhost:27017/mongoose_test');
const schema = new mongoose.Schema({ name: String });
const TestModel = mongoose.model('Test', schema);
const doc = new TestModel({ name: 'foobar' });
console.log('Saving');
await doc.save();
console.log('Saved');
const docs = await TestModel.find();
console.log('Result', docs.map(doc => doc.toObject()));
`.trim();
const initialValue = window.location.hash ?
atob(window.location.hash.slice(1)) :
defaultValue;
const input = CodeMirror(document.querySelector('#input'), {
mode: 'javascript',
lineNumbers: true,
value: initialValue
});
input.setOption('extraKeys', {
'Shift-Enter': function() {
run();
}
});
const output = CodeMirror(document.querySelector('#output'), {
mode: 'javascript',
lineNumbers: true,
value: ''
});
window.input = input;
window.output = output;
require('mongoose/lib/driver').set(
require('@mongoosejs/in-memory-driver')
);
const _mongoose = require('mongoose/lib/mongoose');
window._mongoose = _mongoose;
console.log('Mongoose version', _mongoose.version);
const originalLog = console.log;
// https://glebbahmutov.com/blog/capture-all-the-logs/
// https://www.bayanbennett.com/posts/how-does-mdn-intercept-console-log-devlog-003/
console.log = function() {
// messages.push(JSON.stringify(arguments[0])); // toString() causes [Object object]. No string conversion causes an error
const messages = [];
for (let i = 0; i < arguments.length; i++){
const text = util.inspect(arguments[i]);
messages.push(text);
}
const form = messages.join(" ");
const currentValue = output.getValue();
output.setValue((currentValue ? currentValue + '\n' : '') + form);
originalLog.apply(console, arguments);
};
const originalError = console.error;
// https://glebbahmutov.com/blog/capture-all-the-logs/
// https://www.bayanbennett.com/posts/how-does-mdn-intercept-console-log-devlog-003/
console.log = function() {
// messages.push(JSON.stringify(arguments[0])); // toString() causes [Object object]. No string conversion causes an error
const messages = [];
for (let i = 0; i < arguments.length; i++){
const text = util.inspect(arguments[i]);
messages.push(text);
}
const form = messages.join(" ");
const currentValue = output.getValue();
output.setValue((currentValue ? currentValue + '\n' : '') + form);
originalError.apply(console, arguments);
};
document.querySelector('#run-button').addEventListener('click', () => {
setTimeout(() => main(), 0);
});
const copyLinkButton = document.querySelector('#copy-link-button');
copyLinkButton.addEventListener('click', () => {
tourl();
copyUrl();
copyLinkButton.textContent = 'Copied!';
setTimeout(() => copyLinkButton.textContent = 'Copy Link', 5000);
});
setTimeout(() => main(), 0);
function copyUrl() {
// get the container
const body = document.querySelector('body');
// Create a fake `textarea` and set the contents to the text
// you want to copy
const storage = document.createElement('textarea');
storage.value = window.location.href;
body.appendChild(storage);
// Copy the text in the fake `textarea` and remove the `textarea`
storage.select();
storage.setSelectionRange(0, 99999);
document.execCommand('copy');
body.removeChild(storage);
}
function tourl() {
window.location.hash = btoa(input.getValue());
}