forked from microsoft/monaco-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
487 lines (426 loc) · 15.5 KB
/
gulpfile.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
var gulp = require('gulp');
var metadata = require('./metadata');
var es = require('event-stream');
var path = require('path');
var fs = require('fs');
var rimraf = require('rimraf');
var cp = require('child_process');
var httpServer = require('http-server');
var typedoc = require("gulp-typedoc");
var CleanCSS = require('clean-css');
var uncss = require('uncss');
var WEBSITE_GENERATED_PATH = path.join(__dirname, 'website/playground/new-samples');
var MONACO_EDITOR_VERSION = (function() {
var packageJsonPath = path.join(__dirname, 'package.json');
var packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
var version = packageJson.version;
if (!/\d+\.\d+\.\d+/.test(version)) {
console.log('unrecognized package.json version: ' + version);
process.exit(0);
}
return version;
})();
gulp.task('clean-release', function(cb) { rimraf('release', { maxBusyTries: 1 }, cb); });
gulp.task('release', ['clean-release'], function() {
return es.merge(
// dev folder
releaseOne('dev'),
// min folder
releaseOne('min'),
// package.json
gulp.src('package.json')
.pipe(es.through(function(data) {
var json = JSON.parse(data.contents.toString());
json.private = false;
data.contents = new Buffer(JSON.stringify(json, null, ' '));
this.emit('data', data);
}))
.pipe(gulp.dest('release')),
gulp.src('CHANGELOG.md')
.pipe(gulp.dest('release')),
// min-maps folder
gulp.src('node_modules/monaco-editor-core/min-maps/**/*').pipe(gulp.dest('release/min-maps')),
// other files
gulp.src([
'node_modules/monaco-editor-core/LICENSE',
'node_modules/monaco-editor-core/monaco.d.ts',
'node_modules/monaco-editor-core/ThirdPartyNotices.txt',
'README.md'
])
.pipe(addPluginDTS())
.pipe(addPluginThirdPartyNotices())
.pipe(gulp.dest('release'))
)
});
function releaseOne(type) {
return es.merge(
gulp.src('node_modules/monaco-editor-core/' + type + '/**/*')
.pipe(addPluginContribs())
.pipe(gulp.dest('release/' + type)),
pluginStreams('release/' + type + '/')
)
}
function pluginStreams(destinationPath) {
return es.merge(
metadata.METADATA.PLUGINS.map(function(plugin) {
return pluginStream(plugin, destinationPath);
})
);
}
function pluginStream(plugin, destinationPath) {
var contribPath = path.join(plugin.paths.npm, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
return (
gulp.src([
plugin.paths.npm + '/**/*',
'!' + contribPath,
'!' + plugin.paths.npm + '/**/monaco.d.ts'
])
.pipe(gulp.dest(destinationPath + plugin.modulePrefix))
);
}
/**
* Edit editor.main.js:
* - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
* - append contribs from plugins
* - append new AMD module 'vs/editor/editor.main' that stiches things together
*/
function addPluginContribs() {
return es.through(function(data) {
if (!/editor\.main\.js$/.test(data.path)) {
this.emit('data', data);
return;
}
var contents = data.contents.toString();
// Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"');
var extraContent = [];
var allPluginsModuleIds = [];
metadata.METADATA.PLUGINS.forEach(function(plugin) {
allPluginsModuleIds.push(plugin.contrib);
var contribPath = path.join(__dirname, plugin.paths.npm, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
var contribContents = fs.readFileSync(contribPath).toString();
var contribDefineIndex = contribContents.indexOf('define("' + plugin.contrib);
if (contribDefineIndex === -1) {
console.error('(1) CANNOT DETERMINE AMD define location for contribution', plugin);
process.exit(-1);
}
var depsEndIndex = contribContents.indexOf(']', contribDefineIndex);
if (contribDefineIndex === -1) {
console.error('(2) CANNOT DETERMINE AMD define location for contribution', plugin);
process.exit(-1);
}
contribContents = contribContents.substring(0, depsEndIndex) + ',"vs/editor/edcore.main"' + contribContents.substring(depsEndIndex);
extraContent.push(contribContents);
});
extraContent.push(`define("vs/editor/editor.main", ["vs/editor/edcore.main","${allPluginsModuleIds.join('","')}"], function() {});`);
var insertIndex = contents.lastIndexOf('//# sourceMappingURL=');
if (insertIndex === -1) {
insertIndex = contents.length;
}
contents = contents.substring(0, insertIndex) + '\n' + extraContent.join('\n') + '\n' + contents.substring(insertIndex);
data.contents = new Buffer(contents);
this.emit('data', data);
});
}
/**
* Edit monaco.d.ts:
* - append monaco.d.ts from plugins
*/
function addPluginDTS() {
return es.through(function(data) {
if (!/monaco\.d\.ts$/.test(data.path)) {
this.emit('data', data);
return;
}
var contents = data.contents.toString();
var extraContent = [];
metadata.METADATA.PLUGINS.forEach(function(plugin) {
var dtsPath = path.join(plugin.paths.npm, 'monaco.d.ts');
try {
extraContent.push(fs.readFileSync(dtsPath).toString());
} catch (err) {
return;
}
});
contents = [
'/*!-----------------------------------------------------------',
' * Copyright (c) Microsoft Corporation. All rights reserved.',
' * Type definitions for monaco-editor v'+MONACO_EDITOR_VERSION,
' * Released under the MIT license',
'*-----------------------------------------------------------*/',
].join('\n') + '\n' + contents + '\n' + extraContent.join('\n');
// Ensure consistent indentation and line endings
contents = cleanFile(contents);
data.contents = new Buffer(contents);
fs.writeFileSync('website/playground/monaco.d.ts.txt', contents);
fs.writeFileSync('monaco.d.ts', contents);
this.emit('data', data);
});
}
/**
* Normalize line endings and ensure consistent 4 spaces indentation
*/
function cleanFile(contents) {
return contents.split(/\r\n|\r|\n/).map(function(line) {
var m = line.match(/^(\t+)/);
if (!m) {
return line;
}
var tabsCount = m[1].length;
var newIndent = '';
for (var i = 0; i < 4 * tabsCount; i++) {
newIndent += ' ';
}
return newIndent + line.substring(tabsCount);
}).join('\n');
}
/**
* Edit ThirdPartyNotices.txt:
* - append ThirdPartyNotices.txt from plugins
*/
function addPluginThirdPartyNotices() {
return es.through(function(data) {
if (!/ThirdPartyNotices\.txt$/.test(data.path)) {
this.emit('data', data);
return;
}
var contents = data.contents.toString();
var extraContent = [];
metadata.METADATA.PLUGINS.forEach(function(plugin) {
if (!plugin.thirdPartyNotices) {
return;
}
console.log('ADDING ThirdPartyNotices from ' + plugin.thirdPartyNotices);
var thirdPartyNoticeContent = fs.readFileSync(plugin.thirdPartyNotices).toString();
thirdPartyNoticeContent = thirdPartyNoticeContent.split('\n').slice(8).join('\n');
extraContent.push(thirdPartyNoticeContent);
});
contents += '\n' + extraContent.join('\n');
data.contents = new Buffer(contents);
this.emit('data', data);
});
}
// --- website
gulp.task('clean-website', function(cb) { rimraf('../monaco-editor-website', { maxBusyTries: 1 }, cb); });
gulp.task('website', ['clean-website'], function() {
function replaceWithRelativeResource(dataPath, contents, regex, callback) {
return contents.replace(regex, function(_, m0) {
var filePath = path.join(path.dirname(dataPath), m0);
return callback(m0, fs.readFileSync(filePath));
});
}
var waiting = 0;
var done = false;
return (
es.merge(
gulp.src([
'website/**/*',
'!website/typedoc-theme/**'
], { dot: true })
.pipe(es.through(function(data) {
if (!data.contents || !/\.(html)$/.test(data.path) || /new-samples/.test(data.path)) {
return this.emit('data', data);
}
var contents = data.contents.toString();
contents = contents.replace(/\.\.\/release\/dev/g, 'node_modules/monaco-editor/min');
contents = contents.replace(/{{version}}/g, MONACO_EDITOR_VERSION);
// contents = contents.replace('© 2016 Microsoft', '© 2016 Microsoft [' + builtTime + ']');
// Preload xhr contents
contents = replaceWithRelativeResource(data.path, contents, /<pre data-preload="([^"]+)".*/g, function(m0, fileContents) {
return (
'<pre data-preload="' + m0 + '" style="display:none">'
+ fileContents.toString('utf8')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
+ '</pre>'
);
});
// Inline fork.png
contents = replaceWithRelativeResource(data.path, contents, /src="(\.\/fork.png)"/g, function(m0, fileContents) {
return (
'src="data:image/png;base64,' + fileContents.toString('base64') + '"'
);
});
var allCSS = '';
var tmpcontents = replaceWithRelativeResource(data.path, contents, /<link data-inline="yes-please" href="([^"]+)".*/g, function(m0, fileContents) {
allCSS += fileContents.toString('utf8');
return '';
});
tmpcontents = tmpcontents.replace(/<script.*/g, '');
tmpcontents = tmpcontents.replace(/<link.*/g, '');
waiting++;
uncss(tmpcontents, {
raw: allCSS,
ignore: [/\.alert\b/, /\.alert-error\b/, /\.playground-page\b/]
}, function(err, output) {
waiting--;
if (!err) {
output = new CleanCSS().minify(output).styles;
var isFirst = true;
contents = contents.replace(/<link data-inline="yes-please" href="([^"]+)".*/g, function(_, m0) {
if (isFirst) {
isFirst = false;
return '<style>' + output + '</style>';
}
return '';
});
}
// Inline javascript
contents = replaceWithRelativeResource(data.path, contents, /<script data-inline="yes-please" src="([^"]+)".*/g, function(m0, fileContents) {
return '<script>' + fileContents.toString('utf8') + '</script>';
});
data.contents = new Buffer(contents.split(/\r\n|\r|\n/).join('\n'));
this.emit('data', data);
if (done && waiting === 0) {
this.emit('end');
}
}.bind(this));
}, function() {
done = true;
if (waiting === 0) {
this.emit('end');
}
}))
.pipe(gulp.dest('../monaco-editor-website')),
gulp.src('monaco.d.ts')
.pipe(typedoc({
mode: 'file',
out: '../monaco-editor-website/api',
includeDeclarations: true,
theme: 'website/typedoc-theme',
entryPoint: 'monaco',
name: 'Monaco Editor API v' + MONACO_EDITOR_VERSION,
readme: 'none',
hideGenerator: true
}))
)
.pipe(es.through(function(data) {
this.emit('data', data);
}, function() {
// temporarily create package.json so that npm install doesn't bark
fs.writeFileSync('../monaco-editor-website/package.json', '{}');
fs.writeFileSync('../monaco-editor-website/.nojekyll', '');
cp.execSync('npm install monaco-editor', {
cwd: path.join(__dirname, '../monaco-editor-website')
});
fs.unlink('../monaco-editor-website/package.json');
cp.execSync('git init', {
cwd: path.join(__dirname, '../monaco-editor-website')
});
cp.execSync('git checkout -b gh-pages', {
cwd: path.join(__dirname, '../monaco-editor-website')
});
cp.execSync('git add .', {
cwd: path.join(__dirname, '../monaco-editor-website')
});
cp.execSync('git commit -m "Publish website"', {
cwd: path.join(__dirname, '../monaco-editor-website')
});
cp.execSync('git remote add origin https://github.com/Microsoft/monaco-editor.git', {
cwd: path.join(__dirname, '../monaco-editor-website')
});
console.log('RUN monaco-editor-website>git push origin gh-pages --force')
this.emit('end');
}))
);
});
gulp.task('generate-test-samples', function() {
var sampleNames = fs.readdirSync(path.join(__dirname, 'test/samples'));
var samples = sampleNames.map(function(sampleName) {
var samplePath = path.join(__dirname, 'test/samples', sampleName);
var sampleContent = fs.readFileSync(samplePath).toString();
return {
name: sampleName,
content: sampleContent
};
});
var prefix = '//This is a generated file via gulp generate-test-samples\ndefine([], function() { return';
var suffix = '; });'
fs.writeFileSync(path.join(__dirname, 'test/samples-all.generated.js'), prefix + JSON.stringify(samples, null, '\t') + suffix );
var PLAY_SAMPLES = require(path.join(WEBSITE_GENERATED_PATH, 'all.js')).PLAY_SAMPLES;
var locations = [];
for (var i = 0; i < PLAY_SAMPLES.length; i++) {
var sample = PLAY_SAMPLES[i];
var sampleId = sample.id;
var samplePath = path.join(WEBSITE_GENERATED_PATH, sample.path);
var html = fs.readFileSync(path.join(samplePath, 'sample.html'));
var js = fs.readFileSync(path.join(samplePath, 'sample.js'));
var css = fs.readFileSync(path.join(samplePath, 'sample.css'));
var result = [
'<!DOCTYPE html>',
'<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
'<html>',
'<head>',
' <base href="..">',
' <meta http-equiv="X-UA-Compatible" content="IE=edge" />',
' <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />',
'</head>',
'<body>',
'<style>',
'/*----------------------------------------SAMPLE CSS START*/',
'',
css,
'',
'/*----------------------------------------SAMPLE CSS END*/',
'</style>',
'<a class="loading-opts" href="playground.generated/index.html">[<< BACK]</a> <br/>',
'THIS IS A GENERATED FILE VIA gulp generate-test-samples',
'',
'<div id="bar" style="margin-bottom: 6px;"></div>',
'',
'<div style="clear:both"></div>',
'<div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">',
'<!-- ----------------------------------------SAMPLE HTML START-->',
'',
html,
'',
'<!-- ----------------------------------------SAMPLE HTML END-->',
'</div>',
'<div style="clear:both"></div>',
'',
'<script src="../metadata.js"></script>',
'<script src="dev-setup.js"></script>',
'<script>',
'loadEditor(function() {',
'/*----------------------------------------SAMPLE JS START*/',
'',
js,
'',
'/*----------------------------------------SAMPLE CSS END*/',
'});',
'</script>',
'</body>',
'</html>',
];
fs.writeFileSync(path.join(__dirname, 'test/playground.generated/' + sampleId + '.html'), result.join('\n'));
locations.push({
path: sampleId + '.html',
name: sample.chapter + ' > ' + sample.name
})
}
var index = [
'<!DOCTYPE html>',
'<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
'<html>',
'<head>',
' <base href="..">',
'</head>',
'<body>',
'<a class="loading-opts" href="index.html">[<< BACK]</a><br/>',
'THIS IS A GENERATED FILE VIA gulp generate-test-samples<br/><br/>',
locations.map(function(location) {
return '<a class="loading-opts" href="playground.generated/' + location.path + '">' + location.name + '</a>';
}).join('<br/>\n'),
'<script src="../metadata.js"></script>',
'<script src="dev-setup.js"></script>',
'</body>',
'</html>',
]
fs.writeFileSync(path.join(__dirname, 'test/playground.generated/index.html'), index.join('\n'));
});
gulp.task('simpleserver', ['generate-test-samples'], function(cb) {
httpServer.createServer({ root: '../', cache: 5 }).listen(8080);
httpServer.createServer({ root: '../', cache: 5 }).listen(8088);
console.log('LISTENING on 8080 and 8088');
});