gulpfile.js 24 KB

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