gulpfile.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. const gulp = require('gulp');
  2. const metadata = require('./metadata');
  3. const es = require('event-stream');
  4. const path = require('path');
  5. const fs = require('fs');
  6. const rimraf = require('rimraf');
  7. const cp = require('child_process');
  8. const os = require('os');
  9. const yaserver = require('yaserver');
  10. const http = require('http');
  11. const typedoc = require("gulp-typedoc");
  12. const CleanCSS = require('clean-css');
  13. const uncss = require('uncss');
  14. const File = require('vinyl');
  15. const ts = require('typescript');
  16. const WEBSITE_GENERATED_PATH = path.join(__dirname, 'website/playground/new-samples');
  17. const MONACO_EDITOR_VERSION = (function() {
  18. const packageJsonPath = path.join(__dirname, 'package.json');
  19. const packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
  20. const version = packageJson.version;
  21. if (!/\d+\.\d+\.\d+/.test(version)) {
  22. console.log('unrecognized package.json version: ' + version);
  23. process.exit(0);
  24. }
  25. return version;
  26. })();
  27. async function _execute(task) {
  28. // Always invoke as if it were a callback task
  29. return new Promise((resolve, reject) => {
  30. if (task.length === 1) {
  31. // this is a calback task
  32. task((err) => {
  33. if (err) {
  34. return reject(err);
  35. }
  36. resolve();
  37. });
  38. return;
  39. }
  40. const taskResult = task();
  41. if (typeof taskResult === 'undefined') {
  42. // this is a sync task
  43. resolve();
  44. return;
  45. }
  46. if (typeof taskResult.then === 'function') {
  47. // this is a promise returning task
  48. taskResult.then(resolve, reject);
  49. return;
  50. }
  51. // this is a stream returning task
  52. taskResult.on('end', _ => resolve());
  53. taskResult.on('error', err => reject(err));
  54. });
  55. }
  56. function taskSeries(...tasks) {
  57. return async () => {
  58. for (let i = 0; i < tasks.length; i++) {
  59. await _execute(tasks[i]);
  60. }
  61. };
  62. }
  63. const cleanReleaseTask = function(cb) { rimraf('release', { maxBusyTries: 1 }, cb); };
  64. gulp.task('release', taskSeries(cleanReleaseTask, function() {
  65. return es.merge(
  66. // dev folder
  67. releaseOne('dev'),
  68. // min folder
  69. releaseOne('min'),
  70. // esm folder
  71. ESM_release(),
  72. // package.json
  73. gulp.src('package.json')
  74. .pipe(es.through(function(data) {
  75. var json = JSON.parse(data.contents.toString());
  76. json.private = false;
  77. data.contents = Buffer.from(JSON.stringify(json, null, ' '));
  78. this.emit('data', data);
  79. }))
  80. .pipe(gulp.dest('release')),
  81. gulp.src('CHANGELOG.md')
  82. .pipe(gulp.dest('release')),
  83. // min-maps folder
  84. gulp.src('node_modules/monaco-editor-core/min-maps/**/*')
  85. .pipe(gulp.dest('release/min-maps')),
  86. // other files
  87. gulp.src([
  88. 'node_modules/monaco-editor-core/LICENSE',
  89. 'node_modules/monaco-editor-core/monaco.d.ts',
  90. 'node_modules/monaco-editor-core/ThirdPartyNotices.txt',
  91. 'README.md'
  92. ])
  93. .pipe(addPluginDTS())
  94. .pipe(addPluginThirdPartyNotices())
  95. .pipe(gulp.dest('release'))
  96. )
  97. }));
  98. /**
  99. * Release to `dev` or `min`.
  100. */
  101. function releaseOne(type) {
  102. return es.merge(
  103. gulp.src('node_modules/monaco-editor-core/' + type + '/**/*')
  104. .pipe(addPluginContribs(type))
  105. .pipe(gulp.dest('release/' + type)),
  106. pluginStreams(type, 'release/' + type + '/')
  107. )
  108. }
  109. /**
  110. * Release plugins to `dev` or `min`.
  111. */
  112. function pluginStreams(type, destinationPath) {
  113. return es.merge(
  114. metadata.METADATA.PLUGINS.map(function(plugin) {
  115. return pluginStream(plugin, type, destinationPath);
  116. })
  117. );
  118. }
  119. /**
  120. * Release a plugin to `dev` or `min`.
  121. */
  122. function pluginStream(plugin, type, destinationPath) {
  123. var pluginPath = plugin.paths[`npm/${type}`]; // npm/dev or npm/min
  124. var contribPath = path.join(pluginPath, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
  125. return (
  126. gulp.src([
  127. pluginPath + '/**/*',
  128. '!' + contribPath
  129. ])
  130. .pipe(es.through(function(data) {
  131. if (!/_\.contribution/.test(data.path)) {
  132. this.emit('data', data);
  133. return;
  134. }
  135. let contents = data.contents.toString();
  136. contents = contents.replace('define(["require", "exports"],', 'define(["require", "exports", "vs/editor/editor.api"],');
  137. data.contents = Buffer.from(contents);
  138. this.emit('data', data);
  139. }))
  140. .pipe(gulp.dest(destinationPath + plugin.modulePrefix))
  141. );
  142. }
  143. /**
  144. * Edit editor.main.js:
  145. * - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  146. * - append monaco.contribution modules from plugins
  147. * - append new AMD module 'vs/editor/editor.main' that stiches things together
  148. */
  149. function addPluginContribs(type) {
  150. return es.through(function(data) {
  151. if (!/editor\.main\.js$/.test(data.path)) {
  152. this.emit('data', data);
  153. return;
  154. }
  155. var contents = data.contents.toString();
  156. // Rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
  157. contents = contents.replace(/"vs\/editor\/editor\.main\"/, '"vs/editor/edcore.main"');
  158. var extraContent = [];
  159. var allPluginsModuleIds = [];
  160. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  161. allPluginsModuleIds.push(plugin.contrib);
  162. var pluginPath = plugin.paths[`npm/${type}`]; // npm/dev or npm/min
  163. var contribPath = path.join(__dirname, pluginPath, plugin.contrib.substr(plugin.modulePrefix.length)) + '.js';
  164. var contribContents = fs.readFileSync(contribPath).toString();
  165. contribContents = contribContents.replace(
  166. /define\((['"][a-z\/\-]+\/fillers\/monaco-editor-core['"]),\[\],/,
  167. 'define($1,[\'vs/editor/editor.api\'],'
  168. );
  169. extraContent.push(contribContents);
  170. });
  171. extraContent.push(`define("vs/editor/editor.main", ["vs/editor/edcore.main","${allPluginsModuleIds.join('","')}"], function(api) { return api; });`);
  172. var insertIndex = contents.lastIndexOf('//# sourceMappingURL=');
  173. if (insertIndex === -1) {
  174. insertIndex = contents.length;
  175. }
  176. contents = contents.substring(0, insertIndex) + '\n' + extraContent.join('\n') + '\n' + contents.substring(insertIndex);
  177. data.contents = Buffer.from(contents);
  178. this.emit('data', data);
  179. });
  180. }
  181. function ESM_release() {
  182. return es.merge(
  183. gulp.src([
  184. 'node_modules/monaco-editor-core/esm/**/*',
  185. // we will create our own editor.api.d.ts which also contains the plugins API
  186. '!node_modules/monaco-editor-core/esm/vs/editor/editor.api.d.ts'
  187. ])
  188. .pipe(ESM_addImportSuffix())
  189. .pipe(ESM_addPluginContribs('release/esm'))
  190. .pipe(gulp.dest('release/esm')),
  191. ESM_pluginStreams('release/esm/')
  192. )
  193. }
  194. /**
  195. * Release plugins to `esm`.
  196. */
  197. function ESM_pluginStreams(destinationPath) {
  198. return es.merge(
  199. metadata.METADATA.PLUGINS.map(function(plugin) {
  200. return ESM_pluginStream(plugin, destinationPath);
  201. })
  202. );
  203. }
  204. /**
  205. * Release a plugin to `esm`.
  206. * Adds a dependency to 'vs/editor/editor.api' in contrib files in order for `monaco` to be defined.
  207. * Rewrites imports for 'monaco-editor-core/**'
  208. */
  209. function ESM_pluginStream(plugin, destinationPath) {
  210. const DESTINATION = path.join(__dirname, destinationPath);
  211. let pluginPath = plugin.paths[`esm`];
  212. return (
  213. gulp.src([
  214. pluginPath + '/**/*'
  215. ])
  216. .pipe(es.through(function(data) {
  217. if (!/\.js$/.test(data.path)) {
  218. this.emit('data', data);
  219. return;
  220. }
  221. let contents = data.contents.toString();
  222. const info = ts.preProcessFile(contents);
  223. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  224. let importText = info.importedFiles[i].fileName;
  225. const pos = info.importedFiles[i].pos;
  226. const end = info.importedFiles[i].end;
  227. if (!/(^\.\/)|(^\.\.\/)/.test(importText)) {
  228. // non-relative import
  229. if (!/^monaco-editor-core/.test(importText)) {
  230. console.error(`Non-relative import for unknown module: ${importText} in ${data.path}`);
  231. process.exit(0);
  232. }
  233. if (importText === 'monaco-editor-core') {
  234. importText = 'monaco-editor-core/esm/vs/editor/editor.api';
  235. }
  236. const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
  237. const importFilePath = path.join(DESTINATION, importText.substr('monaco-editor-core/esm/'.length));
  238. let relativePath = path.relative(path.dirname(myFileDestPath), importFilePath).replace(/\\/g, '/');
  239. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  240. relativePath = './' + relativePath;
  241. }
  242. contents = (
  243. contents.substring(0, pos + 1)
  244. + relativePath
  245. + contents.substring(end + 1)
  246. );
  247. }
  248. }
  249. contents = contents.replace(/\/\/# sourceMappingURL=.*((\r?\n)|$)/g, '');
  250. data.contents = Buffer.from(contents);
  251. this.emit('data', data);
  252. }))
  253. .pipe(es.through(function(data) {
  254. if (!/monaco\.contribution\.js$/.test(data.path)) {
  255. this.emit('data', data);
  256. return;
  257. }
  258. const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
  259. const apiFilePath = path.join(DESTINATION, 'vs/editor/editor.api');
  260. let relativePath = path.relative(path.dirname(myFileDestPath), apiFilePath).replace(/\\/g, '/');
  261. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  262. relativePath = './' + relativePath;
  263. }
  264. let contents = data.contents.toString();
  265. contents = (
  266. `import '${relativePath}';\n` +
  267. contents
  268. );
  269. data.contents = Buffer.from(contents);
  270. this.emit('data', data);
  271. }))
  272. .pipe(ESM_addImportSuffix())
  273. .pipe(gulp.dest(destinationPath + plugin.modulePrefix))
  274. );
  275. }
  276. function ESM_addImportSuffix() {
  277. return es.through(function(data) {
  278. if (!/\.js$/.test(data.path)) {
  279. this.emit('data', data);
  280. return;
  281. }
  282. let contents = data.contents.toString();
  283. const info = ts.preProcessFile(contents);
  284. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  285. const importText = info.importedFiles[i].fileName;
  286. const pos = info.importedFiles[i].pos;
  287. const end = info.importedFiles[i].end;
  288. if (/\.css$/.test(importText)) {
  289. continue;
  290. }
  291. contents = (
  292. contents.substring(0, pos + 1)
  293. + importText + '.js'
  294. + contents.substring(end + 1)
  295. );
  296. }
  297. data.contents = Buffer.from(contents);
  298. this.emit('data', data);
  299. });
  300. }
  301. /**
  302. * - Rename esm/vs/editor/editor.main.js to esm/vs/editor/edcore.main.js
  303. * - Create esm/vs/editor/editor.main.js that that stiches things together
  304. */
  305. function ESM_addPluginContribs(dest) {
  306. const DESTINATION = path.join(__dirname, dest);
  307. return es.through(function(data) {
  308. if (!/editor\.main\.js$/.test(data.path)) {
  309. this.emit('data', data);
  310. return;
  311. }
  312. this.emit('data', new File({
  313. path: data.path.replace(/editor\.main/, 'edcore.main'),
  314. base: data.base,
  315. contents: data.contents
  316. }));
  317. const mainFileDestPath = path.join(DESTINATION, 'vs/editor/editor.main.js');
  318. let mainFileImports = [];
  319. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  320. const contribDestPath = path.join(DESTINATION, plugin.contrib);
  321. let relativePath = path.relative(path.dirname(mainFileDestPath), contribDestPath).replace(/\\/g, '/');
  322. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  323. relativePath = './' + relativePath;
  324. }
  325. mainFileImports.push(relativePath);
  326. });
  327. let mainFileContents = (
  328. mainFileImports.map((name) => `import '${name}';`).join('\n')
  329. + `\n\nexport * from './edcore.main';`
  330. );
  331. this.emit('data', new File({
  332. path: data.path,
  333. base: data.base,
  334. contents: Buffer.from(mainFileContents)
  335. }));
  336. });
  337. }
  338. /**
  339. * Edit monaco.d.ts:
  340. * - append monaco.d.ts from plugins
  341. */
  342. function addPluginDTS() {
  343. return es.through(function(data) {
  344. if (!/monaco\.d\.ts$/.test(data.path)) {
  345. this.emit('data', data);
  346. return;
  347. }
  348. var contents = data.contents.toString();
  349. var extraContent = [];
  350. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  351. var pluginPath = plugin.paths[`npm/min`]; // npm/dev or npm/min
  352. var dtsPath = path.join(pluginPath, '../monaco.d.ts');
  353. try {
  354. let plugindts = fs.readFileSync(dtsPath).toString();
  355. plugindts = plugindts.replace(/\/\/\/ <reference.*\n/m, '');
  356. extraContent.push(plugindts);
  357. } catch (err) {
  358. return;
  359. }
  360. });
  361. contents = [
  362. '/*!-----------------------------------------------------------',
  363. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  364. ' * Type definitions for monaco-editor',
  365. ' * Released under the MIT license',
  366. '*-----------------------------------------------------------*/',
  367. ].join('\n') + '\n' + contents + '\n' + extraContent.join('\n');
  368. // Ensure consistent indentation and line endings
  369. contents = cleanFile(contents);
  370. data.contents = Buffer.from(contents);
  371. this.emit('data', new File({
  372. path: path.join(path.dirname(data.path), 'esm/vs/editor/editor.api.d.ts'),
  373. base: data.base,
  374. contents: Buffer.from(toExternalDTS(contents))
  375. }));
  376. fs.writeFileSync('website/playground/monaco.d.ts.txt', contents);
  377. fs.writeFileSync('monaco.d.ts', contents);
  378. this.emit('data', data);
  379. });
  380. }
  381. function toExternalDTS(contents) {
  382. let lines = contents.split(/\r\n|\r|\n/);
  383. let killNextCloseCurlyBrace = false;
  384. for (let i = 0; i < lines.length; i++) {
  385. let line = lines[i];
  386. if (killNextCloseCurlyBrace) {
  387. if ('}' === line) {
  388. lines[i] = '';
  389. killNextCloseCurlyBrace = false;
  390. continue;
  391. }
  392. if (line.indexOf(' ') === 0) {
  393. lines[i] = line.substr(4);
  394. } else if (line.charAt(0) === '\t') {
  395. lines[i] = line.substr(1);
  396. }
  397. continue;
  398. }
  399. if ('declare namespace monaco {' === line) {
  400. lines[i] = '';
  401. killNextCloseCurlyBrace = true;
  402. continue;
  403. }
  404. if (line.indexOf('declare namespace monaco.') === 0) {
  405. lines[i] = line.replace('declare namespace monaco.', 'export namespace ');
  406. }
  407. if (line.indexOf('declare let MonacoEnvironment') === 0) {
  408. lines[i] = `declare global {\n let MonacoEnvironment: Environment | undefined;\n}`;
  409. // lines[i] = line.replace('declare namespace monaco.', 'export namespace ');
  410. }
  411. }
  412. return lines.join('\n').replace(/\n\n\n+/g, '\n\n');
  413. }
  414. /**
  415. * Normalize line endings and ensure consistent 4 spaces indentation
  416. */
  417. function cleanFile(contents) {
  418. return contents.split(/\r\n|\r|\n/).map(function(line) {
  419. var m = line.match(/^(\t+)/);
  420. if (!m) {
  421. return line;
  422. }
  423. var tabsCount = m[1].length;
  424. var newIndent = '';
  425. for (var i = 0; i < 4 * tabsCount; i++) {
  426. newIndent += ' ';
  427. }
  428. return newIndent + line.substring(tabsCount);
  429. }).join('\n');
  430. }
  431. /**
  432. * Edit ThirdPartyNotices.txt:
  433. * - append ThirdPartyNotices.txt from plugins
  434. */
  435. function addPluginThirdPartyNotices() {
  436. return es.through(function(data) {
  437. if (!/ThirdPartyNotices\.txt$/.test(data.path)) {
  438. this.emit('data', data);
  439. return;
  440. }
  441. var contents = data.contents.toString();
  442. var extraContent = [];
  443. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  444. if (!plugin.thirdPartyNotices) {
  445. return;
  446. }
  447. console.log('ADDING ThirdPartyNotices from ' + plugin.thirdPartyNotices);
  448. var thirdPartyNoticeContent = fs.readFileSync(plugin.thirdPartyNotices).toString();
  449. thirdPartyNoticeContent = thirdPartyNoticeContent.split('\n').slice(8).join('\n');
  450. extraContent.push(thirdPartyNoticeContent);
  451. });
  452. contents += '\n' + extraContent.join('\n');
  453. data.contents = Buffer.from(contents);
  454. this.emit('data', data);
  455. });
  456. }
  457. // --- website
  458. const cleanWebsiteTask = function(cb) { rimraf('../monaco-editor-website', { maxBusyTries: 1 }, cb); };
  459. const buildWebsiteTask = taskSeries(cleanWebsiteTask, function() {
  460. const initialCWD = process.cwd();
  461. function replaceWithRelativeResource(dataPath, contents, regex, callback) {
  462. return contents.replace(regex, function(_, m0) {
  463. var filePath = path.join(path.dirname(dataPath), m0);
  464. return callback(m0, fs.readFileSync(filePath));
  465. });
  466. }
  467. var waiting = 0;
  468. var done = false;
  469. return (
  470. es.merge(
  471. gulp.src([
  472. 'website/**/*',
  473. '!website/typedoc-theme/**'
  474. ], { dot: true })
  475. .pipe(es.through(function(data) {
  476. if (!data.contents || !/\.(html)$/.test(data.path) || /new-samples/.test(data.path)) {
  477. return this.emit('data', data);
  478. }
  479. var contents = data.contents.toString();
  480. contents = contents.replace(/\.\.\/release\/dev/g, 'node_modules/monaco-editor/min');
  481. contents = contents.replace(/{{version}}/g, MONACO_EDITOR_VERSION);
  482. contents = contents.replace(/{{year}}/g, new Date().getFullYear());
  483. // Preload xhr contents
  484. contents = replaceWithRelativeResource(data.path, contents, /<pre data-preload="([^"]+)".*/g, function(m0, fileContents) {
  485. return (
  486. '<pre data-preload="' + m0 + '" style="display:none">'
  487. + fileContents.toString('utf8')
  488. .replace(/&/g, '&amp;')
  489. .replace(/</g, '&lt;')
  490. .replace(/>/g, '&gt;')
  491. + '</pre>'
  492. );
  493. });
  494. // Inline fork.png
  495. contents = replaceWithRelativeResource(data.path, contents, /src="(\.\/fork.png)"/g, function(m0, fileContents) {
  496. return (
  497. 'src="data:image/png;base64,' + fileContents.toString('base64') + '"'
  498. );
  499. });
  500. var allCSS = '';
  501. var tmpcontents = replaceWithRelativeResource(data.path, contents, /<link data-inline="yes-please" href="([^"]+)".*/g, function(m0, fileContents) {
  502. allCSS += fileContents.toString('utf8');
  503. return '';
  504. });
  505. tmpcontents = tmpcontents.replace(/<script.*/g, '');
  506. tmpcontents = tmpcontents.replace(/<link.*/g, '');
  507. waiting++;
  508. uncss(tmpcontents, {
  509. raw: allCSS,
  510. ignore: [/\.alert\b/, /\.alert-error\b/, /\.playground-page\b/]
  511. }, function(err, output) {
  512. waiting--;
  513. if (!err) {
  514. output = new CleanCSS().minify(output).styles;
  515. var isFirst = true;
  516. contents = contents.replace(/<link data-inline="yes-please" href="([^"]+)".*/g, function(_, m0) {
  517. if (isFirst) {
  518. isFirst = false;
  519. return '<style>' + output + '</style>';
  520. }
  521. return '';
  522. });
  523. }
  524. // Inline javascript
  525. contents = replaceWithRelativeResource(data.path, contents, /<script data-inline="yes-please" src="([^"]+)".*/g, function(m0, fileContents) {
  526. return '<script>' + fileContents.toString('utf8') + '</script>';
  527. });
  528. data.contents = Buffer.from(contents.split(/\r\n|\r|\n/).join('\n'));
  529. this.emit('data', data);
  530. if (done && waiting === 0) {
  531. this.emit('end');
  532. }
  533. }.bind(this));
  534. }, function() {
  535. done = true;
  536. if (waiting === 0) {
  537. this.emit('end');
  538. }
  539. }))
  540. .pipe(gulp.dest('../monaco-editor-website')),
  541. // TypeDoc is silly and consumes the `exclude` option.
  542. // This option does not make it to typescript compiler, which ends up including /node_modules/ .d.ts files.
  543. // We work around this by changing the cwd... :O
  544. gulp.src('monaco.d.ts')
  545. .pipe(es.through(undefined, function() {
  546. process.chdir(os.tmpdir());
  547. this.emit('end');
  548. }))
  549. .pipe(typedoc({
  550. mode: 'file',
  551. out: path.join(__dirname, '../monaco-editor-website/api'),
  552. includeDeclarations: true,
  553. theme: path.join(__dirname, 'website/typedoc-theme'),
  554. entryPoint: 'monaco',
  555. name: 'Monaco Editor API v' + MONACO_EDITOR_VERSION,
  556. readme: 'none',
  557. hideGenerator: true
  558. }))
  559. .pipe(es.through(undefined, function() {
  560. process.chdir(initialCWD);
  561. this.emit('end');
  562. }))
  563. )
  564. .pipe(es.through(function(data) {
  565. this.emit('data', data);
  566. }, function() {
  567. // temporarily create package.json so that npm install doesn't bark
  568. fs.writeFileSync('../monaco-editor-website/package.json', '{}');
  569. fs.writeFileSync('../monaco-editor-website/.nojekyll', '');
  570. cp.execSync('npm install monaco-editor', {
  571. cwd: path.join(__dirname, '../monaco-editor-website')
  572. });
  573. fs.unlinkSync('../monaco-editor-website/package.json');
  574. this.emit('end');
  575. }))
  576. );
  577. });
  578. gulp.task('build-website', buildWebsiteTask);
  579. gulp.task('website', taskSeries(buildWebsiteTask, function() {
  580. cp.execSync('git init', {
  581. cwd: path.join(__dirname, '../monaco-editor-website')
  582. });
  583. let remoteUrl = cp.execSync('git remote get-url origin')
  584. let committerUserName = cp.execSync('git log --format=\'%an\' -1');
  585. let committerEmail = cp.execSync('git log --format=\'%ae\' -1');
  586. cp.execSync(`git config user.name ${committerUserName}`, {
  587. cwd: path.join(__dirname, '../monaco-editor-website')
  588. });
  589. cp.execSync(`git config user.email ${committerEmail}`, {
  590. cwd: path.join(__dirname, '../monaco-editor-website')
  591. });
  592. cp.execSync(`git remote add origin ${remoteUrl}`, {
  593. cwd: path.join(__dirname, '../monaco-editor-website')
  594. });
  595. cp.execSync('git checkout -b gh-pages', {
  596. cwd: path.join(__dirname, '../monaco-editor-website')
  597. });
  598. cp.execSync('git add .', {
  599. cwd: path.join(__dirname, '../monaco-editor-website')
  600. });
  601. cp.execSync('git commit -m "Publish website"', {
  602. cwd: path.join(__dirname, '../monaco-editor-website')
  603. });
  604. console.log('RUN monaco-editor-website>git push origin gh-pages --force');
  605. }));
  606. const generateTestSamplesTask = function() {
  607. var sampleNames = fs.readdirSync(path.join(__dirname, 'test/samples'));
  608. var samples = sampleNames.map(function(sampleName) {
  609. var samplePath = path.join(__dirname, 'test/samples', sampleName);
  610. var sampleContent = fs.readFileSync(samplePath).toString();
  611. return {
  612. name: sampleName,
  613. content: sampleContent
  614. };
  615. });
  616. var prefix = '//This is a generated file via gulp generate-test-samples\ndefine([], function() { return';
  617. var suffix = '; });'
  618. fs.writeFileSync(path.join(__dirname, 'test/samples-all.generated.js'), prefix + JSON.stringify(samples, null, '\t') + suffix );
  619. var PLAY_SAMPLES = require(path.join(WEBSITE_GENERATED_PATH, 'all.js')).PLAY_SAMPLES;
  620. var locations = [];
  621. for (var i = 0; i < PLAY_SAMPLES.length; i++) {
  622. var sample = PLAY_SAMPLES[i];
  623. var sampleId = sample.id;
  624. var samplePath = path.join(WEBSITE_GENERATED_PATH, sample.path);
  625. var html = fs.readFileSync(path.join(samplePath, 'sample.html'));
  626. var js = fs.readFileSync(path.join(samplePath, 'sample.js'));
  627. var css = fs.readFileSync(path.join(samplePath, 'sample.css'));
  628. var result = [
  629. '<!DOCTYPE html>',
  630. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  631. '<html>',
  632. '<head>',
  633. ' <base href="..">',
  634. ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />',
  635. '</head>',
  636. '<body>',
  637. '<style>',
  638. '/*----------------------------------------SAMPLE CSS START*/',
  639. '',
  640. css,
  641. '',
  642. '/*----------------------------------------SAMPLE CSS END*/',
  643. '</style>',
  644. '<a class="loading-opts" href="playground.generated/index.html">[&lt;&lt; BACK]</a> <br/>',
  645. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples',
  646. '',
  647. '<div id="bar" style="margin-bottom: 6px;"></div>',
  648. '',
  649. '<div style="clear:both"></div>',
  650. '<div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">',
  651. '<!-- ----------------------------------------SAMPLE HTML START-->',
  652. '',
  653. html,
  654. '',
  655. '<!-- ----------------------------------------SAMPLE HTML END-->',
  656. '</div>',
  657. '<div style="clear:both"></div>',
  658. '',
  659. '<script src="../metadata.js"></script>',
  660. '<script src="dev-setup.js"></script>',
  661. '<script>',
  662. 'loadEditor(function() {',
  663. '/*----------------------------------------SAMPLE JS START*/',
  664. '',
  665. js,
  666. '',
  667. '/*----------------------------------------SAMPLE JS END*/',
  668. '});',
  669. '</script>',
  670. '</body>',
  671. '</html>',
  672. ];
  673. fs.writeFileSync(path.join(__dirname, 'test/playground.generated/' + sampleId + '.html'), result.join('\n'));
  674. locations.push({
  675. path: sampleId + '.html',
  676. name: sample.chapter + ' &gt; ' + sample.name
  677. })
  678. }
  679. var index = [
  680. '<!DOCTYPE html>',
  681. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  682. '<html>',
  683. '<head>',
  684. ' <base href="..">',
  685. '</head>',
  686. '<body>',
  687. '<a class="loading-opts" href="index.html">[&lt;&lt; BACK]</a><br/>',
  688. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples<br/><br/>',
  689. locations.map(function(location) {
  690. return '<a class="loading-opts" href="playground.generated/' + location.path + '">' + location.name + '</a>';
  691. }).join('<br/>\n'),
  692. '<script src="../metadata.js"></script>',
  693. '<script src="dev-setup.js"></script>',
  694. '</body>',
  695. '</html>',
  696. ]
  697. fs.writeFileSync(path.join(__dirname, 'test/playground.generated/index.html'), index.join('\n'));
  698. };
  699. function createSimpleServer(rootDir, port) {
  700. yaserver.createServer({
  701. rootDir: rootDir
  702. }).then((staticServer) => {
  703. const server = http.createServer((request, response) => {
  704. return staticServer.handle(request, response);
  705. });
  706. server.listen(port, '127.0.0.1', () => {
  707. console.log(`Running at http://127.0.0.1:${port}`);
  708. });
  709. });
  710. }
  711. gulp.task('simpleserver', taskSeries(generateTestSamplesTask, function() {
  712. const SERVER_ROOT = path.normalize(path.join(__dirname, '../'));
  713. createSimpleServer(SERVER_ROOT, 8080);
  714. createSimpleServer(SERVER_ROOT, 8088);
  715. }));
  716. gulp.task('ciserver', taskSeries(generateTestSamplesTask, function () {
  717. const SERVER_ROOT = path.normalize(path.join(__dirname, './'));
  718. createSimpleServer(SERVER_ROOT, 8080);
  719. }));