gulpfile.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. var gulp = require('gulp');
  2. var metadata = require('./metadata');
  3. var es = require('event-stream');
  4. var path = require('path');
  5. var fs = require('fs');
  6. var rimraf = require('rimraf');
  7. var cp = require('child_process');
  8. var httpServer = require('http-server');
  9. var typedoc = require("gulp-typedoc");
  10. var CleanCSS = require('clean-css');
  11. var uncss = require('uncss');
  12. var WEBSITE_GENERATED_PATH = path.join(__dirname, 'website/playground/new-samples');
  13. var MONACO_EDITOR_VERSION = (function() {
  14. var packageJsonPath = path.join(__dirname, 'package.json');
  15. var packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
  16. var version = packageJson.version;
  17. if (!/\d+\.\d+\.\d+/.test(version)) {
  18. console.log('unrecognized package.json version: ' + version);
  19. process.exit(0);
  20. }
  21. return version;
  22. })();
  23. gulp.task('clean-release', function(cb) { rimraf('release', { maxBusyTries: 1 }, cb); });
  24. gulp.task('release', ['clean-release'], function() {
  25. return es.merge(
  26. // dev folder
  27. releaseOne('dev'),
  28. // min folder
  29. releaseOne('min'),
  30. // package.json
  31. gulp.src('package.json')
  32. .pipe(es.through(function(data) {
  33. var json = JSON.parse(data.contents.toString());
  34. json.private = false;
  35. data.contents = new Buffer(JSON.stringify(json, null, ' '));
  36. this.emit('data', data);
  37. }))
  38. .pipe(gulp.dest('release')),
  39. gulp.src('CHANGELOG.md')
  40. .pipe(gulp.dest('release')),
  41. // min-maps folder
  42. gulp.src('node_modules/monaco-editor-core/min-maps/**/*').pipe(gulp.dest('release/min-maps')),
  43. // other files
  44. gulp.src([
  45. 'node_modules/monaco-editor-core/LICENSE',
  46. 'node_modules/monaco-editor-core/monaco.d.ts',
  47. 'node_modules/monaco-editor-core/ThirdPartyNotices.txt',
  48. 'README.md'
  49. ])
  50. .pipe(addPluginDTS())
  51. .pipe(addPluginThirdPartyNotices())
  52. .pipe(gulp.dest('release'))
  53. )
  54. });
  55. function releaseOne(type) {
  56. return es.merge(
  57. gulp.src('node_modules/monaco-editor-core/' + type + '/**/*')
  58. .pipe(addPluginContribs())
  59. .pipe(gulp.dest('release/' + type)),
  60. pluginStreams('release/' + type + '/')
  61. )
  62. }
  63. function pluginStreams(destinationPath) {
  64. return es.merge(
  65. metadata.METADATA.PLUGINS.map(function(plugin) {
  66. return pluginStream(plugin, destinationPath);
  67. })
  68. );
  69. }
  70. function pluginStream(plugin, destinationPath) {
  71. var contribPath = path.join(plugin.paths.npm, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
  72. return (
  73. gulp.src([
  74. plugin.paths.npm + '/**/*',
  75. '!' + contribPath,
  76. '!' + plugin.paths.npm + '/**/monaco.d.ts'
  77. ])
  78. .pipe(gulp.dest(destinationPath + plugin.modulePrefix))
  79. );
  80. }
  81. /**
  82. * Edit editor.main.js:
  83. * - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  84. * - append contribs from plugins
  85. * - append new AMD module 'vs/editor/editor.main' that stiches things together
  86. */
  87. function addPluginContribs() {
  88. return es.through(function(data) {
  89. if (!/editor\.main\.js$/.test(data.path)) {
  90. this.emit('data', data);
  91. return;
  92. }
  93. var contents = data.contents.toString();
  94. // Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  95. contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"');
  96. var extraContent = [];
  97. var allPluginsModuleIds = [];
  98. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  99. allPluginsModuleIds.push(plugin.contrib);
  100. var contribPath = path.join(__dirname, plugin.paths.npm, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
  101. var contribContents = fs.readFileSync(contribPath).toString();
  102. var contribDefineIndex = contribContents.indexOf('define("' + plugin.contrib);
  103. if (contribDefineIndex === -1) {
  104. console.error('(1) CANNOT DETERMINE AMD define location for contribution', plugin);
  105. process.exit(-1);
  106. }
  107. var depsEndIndex = contribContents.indexOf(']', contribDefineIndex);
  108. if (contribDefineIndex === -1) {
  109. console.error('(2) CANNOT DETERMINE AMD define location for contribution', plugin);
  110. process.exit(-1);
  111. }
  112. contribContents = contribContents.substring(0, depsEndIndex) + ',"vs/editor/edcore.main"' + contribContents.substring(depsEndIndex);
  113. extraContent.push(contribContents);
  114. });
  115. extraContent.push(`define("vs/editor/editor.main", ["vs/editor/edcore.main","${allPluginsModuleIds.join('","')}"], function() {});`);
  116. var insertIndex = contents.lastIndexOf('//# sourceMappingURL=');
  117. if (insertIndex === -1) {
  118. insertIndex = contents.length;
  119. }
  120. contents = contents.substring(0, insertIndex) + '\n' + extraContent.join('\n') + '\n' + contents.substring(insertIndex);
  121. data.contents = new Buffer(contents);
  122. this.emit('data', data);
  123. });
  124. }
  125. /**
  126. * Edit monaco.d.ts:
  127. * - append monaco.d.ts from plugins
  128. */
  129. function addPluginDTS() {
  130. return es.through(function(data) {
  131. if (!/monaco\.d\.ts$/.test(data.path)) {
  132. this.emit('data', data);
  133. return;
  134. }
  135. var contents = data.contents.toString();
  136. var extraContent = [];
  137. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  138. var dtsPath = path.join(plugin.paths.npm, 'monaco.d.ts');
  139. try {
  140. extraContent.push(fs.readFileSync(dtsPath).toString());
  141. } catch (err) {
  142. return;
  143. }
  144. });
  145. contents = [
  146. '/*!-----------------------------------------------------------',
  147. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  148. ' * Type definitions for monaco-editor v'+MONACO_EDITOR_VERSION,
  149. ' * Released under the MIT license',
  150. '*-----------------------------------------------------------*/',
  151. ].join('\n') + '\n' + contents + '\n' + extraContent.join('\n');
  152. // Ensure consistent indentation and line endings
  153. contents = cleanFile(contents);
  154. data.contents = new Buffer(contents);
  155. fs.writeFileSync('website/playground/monaco.d.ts.txt', contents);
  156. fs.writeFileSync('monaco.d.ts', contents);
  157. this.emit('data', data);
  158. });
  159. }
  160. /**
  161. * Normalize line endings and ensure consistent 4 spaces indentation
  162. */
  163. function cleanFile(contents) {
  164. return contents.split(/\r\n|\r|\n/).map(function(line) {
  165. var m = line.match(/^(\t+)/);
  166. if (!m) {
  167. return line;
  168. }
  169. var tabsCount = m[1].length;
  170. var newIndent = '';
  171. for (var i = 0; i < 4 * tabsCount; i++) {
  172. newIndent += ' ';
  173. }
  174. return newIndent + line.substring(tabsCount);
  175. }).join('\n');
  176. }
  177. /**
  178. * Edit ThirdPartyNotices.txt:
  179. * - append ThirdPartyNotices.txt from plugins
  180. */
  181. function addPluginThirdPartyNotices() {
  182. return es.through(function(data) {
  183. if (!/ThirdPartyNotices\.txt$/.test(data.path)) {
  184. this.emit('data', data);
  185. return;
  186. }
  187. var contents = data.contents.toString();
  188. var extraContent = [];
  189. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  190. if (!plugin.thirdPartyNotices) {
  191. return;
  192. }
  193. console.log('ADDING ThirdPartyNotices from ' + plugin.thirdPartyNotices);
  194. var thirdPartyNoticeContent = fs.readFileSync(plugin.thirdPartyNotices).toString();
  195. thirdPartyNoticeContent = thirdPartyNoticeContent.split('\n').slice(8).join('\n');
  196. extraContent.push(thirdPartyNoticeContent);
  197. });
  198. contents += '\n' + extraContent.join('\n');
  199. data.contents = new Buffer(contents);
  200. this.emit('data', data);
  201. });
  202. }
  203. // --- website
  204. gulp.task('clean-website', function(cb) { rimraf('../monaco-editor-website', { maxBusyTries: 1 }, cb); });
  205. gulp.task('website', ['clean-website'], function() {
  206. function replaceWithRelativeResource(dataPath, contents, regex, callback) {
  207. return contents.replace(regex, function(_, m0) {
  208. var filePath = path.join(path.dirname(dataPath), m0);
  209. return callback(m0, fs.readFileSync(filePath));
  210. });
  211. }
  212. var waiting = 0;
  213. var done = false;
  214. return (
  215. es.merge(
  216. gulp.src([
  217. 'website/**/*',
  218. '!website/typedoc-theme/**'
  219. ], { dot: true })
  220. .pipe(es.through(function(data) {
  221. if (!data.contents || !/\.(html)$/.test(data.path) || /new-samples/.test(data.path)) {
  222. return this.emit('data', data);
  223. }
  224. var contents = data.contents.toString();
  225. contents = contents.replace(/\.\.\/release\/dev/g, 'node_modules/monaco-editor/min');
  226. contents = contents.replace(/{{version}}/g, MONACO_EDITOR_VERSION);
  227. // contents = contents.replace('&copy; 2017 Microsoft', '&copy; 2017 Microsoft [' + builtTime + ']');
  228. // Preload xhr contents
  229. contents = replaceWithRelativeResource(data.path, contents, /<pre data-preload="([^"]+)".*/g, function(m0, fileContents) {
  230. return (
  231. '<pre data-preload="' + m0 + '" style="display:none">'
  232. + fileContents.toString('utf8')
  233. .replace(/&/g, '&amp;')
  234. .replace(/</g, '&lt;')
  235. .replace(/>/g, '&gt;')
  236. + '</pre>'
  237. );
  238. });
  239. // Inline fork.png
  240. contents = replaceWithRelativeResource(data.path, contents, /src="(\.\/fork.png)"/g, function(m0, fileContents) {
  241. return (
  242. 'src="data:image/png;base64,' + fileContents.toString('base64') + '"'
  243. );
  244. });
  245. var allCSS = '';
  246. var tmpcontents = replaceWithRelativeResource(data.path, contents, /<link data-inline="yes-please" href="([^"]+)".*/g, function(m0, fileContents) {
  247. allCSS += fileContents.toString('utf8');
  248. return '';
  249. });
  250. tmpcontents = tmpcontents.replace(/<script.*/g, '');
  251. tmpcontents = tmpcontents.replace(/<link.*/g, '');
  252. waiting++;
  253. uncss(tmpcontents, {
  254. raw: allCSS,
  255. ignore: [/\.alert\b/, /\.alert-error\b/, /\.playground-page\b/]
  256. }, function(err, output) {
  257. waiting--;
  258. if (!err) {
  259. output = new CleanCSS().minify(output).styles;
  260. var isFirst = true;
  261. contents = contents.replace(/<link data-inline="yes-please" href="([^"]+)".*/g, function(_, m0) {
  262. if (isFirst) {
  263. isFirst = false;
  264. return '<style>' + output + '</style>';
  265. }
  266. return '';
  267. });
  268. }
  269. // Inline javascript
  270. contents = replaceWithRelativeResource(data.path, contents, /<script data-inline="yes-please" src="([^"]+)".*/g, function(m0, fileContents) {
  271. return '<script>' + fileContents.toString('utf8') + '</script>';
  272. });
  273. data.contents = new Buffer(contents.split(/\r\n|\r|\n/).join('\n'));
  274. this.emit('data', data);
  275. if (done && waiting === 0) {
  276. this.emit('end');
  277. }
  278. }.bind(this));
  279. }, function() {
  280. done = true;
  281. if (waiting === 0) {
  282. this.emit('end');
  283. }
  284. }))
  285. .pipe(gulp.dest('../monaco-editor-website')),
  286. gulp.src('monaco.d.ts')
  287. .pipe(typedoc({
  288. mode: 'file',
  289. out: '../monaco-editor-website/api',
  290. includeDeclarations: true,
  291. theme: 'website/typedoc-theme',
  292. entryPoint: 'monaco',
  293. name: 'Monaco Editor API v' + MONACO_EDITOR_VERSION,
  294. readme: 'none',
  295. hideGenerator: true
  296. }))
  297. )
  298. .pipe(es.through(function(data) {
  299. this.emit('data', data);
  300. }, function() {
  301. // temporarily create package.json so that npm install doesn't bark
  302. fs.writeFileSync('../monaco-editor-website/package.json', '{}');
  303. fs.writeFileSync('../monaco-editor-website/.nojekyll', '');
  304. cp.execSync('npm install monaco-editor', {
  305. cwd: path.join(__dirname, '../monaco-editor-website')
  306. });
  307. fs.unlink('../monaco-editor-website/package.json');
  308. cp.execSync('git init', {
  309. cwd: path.join(__dirname, '../monaco-editor-website')
  310. });
  311. cp.execSync('git checkout -b gh-pages', {
  312. cwd: path.join(__dirname, '../monaco-editor-website')
  313. });
  314. cp.execSync('git add .', {
  315. cwd: path.join(__dirname, '../monaco-editor-website')
  316. });
  317. cp.execSync('git commit -m "Publish website"', {
  318. cwd: path.join(__dirname, '../monaco-editor-website')
  319. });
  320. cp.execSync('git remote add origin https://github.com/Microsoft/monaco-editor.git', {
  321. cwd: path.join(__dirname, '../monaco-editor-website')
  322. });
  323. console.log('RUN monaco-editor-website>git push origin gh-pages --force')
  324. this.emit('end');
  325. }))
  326. );
  327. });
  328. gulp.task('generate-test-samples', function() {
  329. var sampleNames = fs.readdirSync(path.join(__dirname, 'test/samples'));
  330. var samples = sampleNames.map(function(sampleName) {
  331. var samplePath = path.join(__dirname, 'test/samples', sampleName);
  332. var sampleContent = fs.readFileSync(samplePath).toString();
  333. return {
  334. name: sampleName,
  335. content: sampleContent
  336. };
  337. });
  338. var prefix = '//This is a generated file via gulp generate-test-samples\ndefine([], function() { return';
  339. var suffix = '; });'
  340. fs.writeFileSync(path.join(__dirname, 'test/samples-all.generated.js'), prefix + JSON.stringify(samples, null, '\t') + suffix );
  341. var PLAY_SAMPLES = require(path.join(WEBSITE_GENERATED_PATH, 'all.js')).PLAY_SAMPLES;
  342. var locations = [];
  343. for (var i = 0; i < PLAY_SAMPLES.length; i++) {
  344. var sample = PLAY_SAMPLES[i];
  345. var sampleId = sample.id;
  346. var samplePath = path.join(WEBSITE_GENERATED_PATH, sample.path);
  347. var html = fs.readFileSync(path.join(samplePath, 'sample.html'));
  348. var js = fs.readFileSync(path.join(samplePath, 'sample.js'));
  349. var css = fs.readFileSync(path.join(samplePath, 'sample.css'));
  350. var result = [
  351. '<!DOCTYPE html>',
  352. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  353. '<html>',
  354. '<head>',
  355. ' <base href="..">',
  356. ' <meta http-equiv="X-UA-Compatible" content="IE=edge" />',
  357. ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />',
  358. '</head>',
  359. '<body>',
  360. '<style>',
  361. '/*----------------------------------------SAMPLE CSS START*/',
  362. '',
  363. css,
  364. '',
  365. '/*----------------------------------------SAMPLE CSS END*/',
  366. '</style>',
  367. '<a class="loading-opts" href="playground.generated/index.html">[&lt;&lt; BACK]</a> <br/>',
  368. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples',
  369. '',
  370. '<div id="bar" style="margin-bottom: 6px;"></div>',
  371. '',
  372. '<div style="clear:both"></div>',
  373. '<div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">',
  374. '<!-- ----------------------------------------SAMPLE HTML START-->',
  375. '',
  376. html,
  377. '',
  378. '<!-- ----------------------------------------SAMPLE HTML END-->',
  379. '</div>',
  380. '<div style="clear:both"></div>',
  381. '',
  382. '<script src="../metadata.js"></script>',
  383. '<script src="dev-setup.js"></script>',
  384. '<script>',
  385. 'loadEditor(function() {',
  386. '/*----------------------------------------SAMPLE JS START*/',
  387. '',
  388. js,
  389. '',
  390. '/*----------------------------------------SAMPLE CSS END*/',
  391. '});',
  392. '</script>',
  393. '</body>',
  394. '</html>',
  395. ];
  396. fs.writeFileSync(path.join(__dirname, 'test/playground.generated/' + sampleId + '.html'), result.join('\n'));
  397. locations.push({
  398. path: sampleId + '.html',
  399. name: sample.chapter + ' &gt; ' + sample.name
  400. })
  401. }
  402. var index = [
  403. '<!DOCTYPE html>',
  404. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  405. '<html>',
  406. '<head>',
  407. ' <base href="..">',
  408. '</head>',
  409. '<body>',
  410. '<a class="loading-opts" href="index.html">[&lt;&lt; BACK]</a><br/>',
  411. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples<br/><br/>',
  412. locations.map(function(location) {
  413. return '<a class="loading-opts" href="playground.generated/' + location.path + '">' + location.name + '</a>';
  414. }).join('<br/>\n'),
  415. '<script src="../metadata.js"></script>',
  416. '<script src="dev-setup.js"></script>',
  417. '</body>',
  418. '</html>',
  419. ]
  420. fs.writeFileSync(path.join(__dirname, 'test/playground.generated/index.html'), index.join('\n'));
  421. });
  422. gulp.task('simpleserver', ['generate-test-samples'], function(cb) {
  423. httpServer.createServer({ root: '../', cache: 5 }).listen(8080);
  424. httpServer.createServer({ root: '../', cache: 5 }).listen(8088);
  425. console.log('LISTENING on 8080 and 8088');
  426. });