gulpfile.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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('node_modules/monaco-editor-core/esm/**/*')
  179. .pipe(ESM_addImportSuffix())
  180. .pipe(ESM_addPluginContribs('release/esm'))
  181. .pipe(gulp.dest('release/esm')),
  182. ESM_pluginStreams('release/esm/')
  183. )
  184. }
  185. /**
  186. * Release plugins to `esm`.
  187. */
  188. function ESM_pluginStreams(destinationPath) {
  189. return es.merge(
  190. metadata.METADATA.PLUGINS.map(function(plugin) {
  191. return ESM_pluginStream(plugin, destinationPath);
  192. })
  193. );
  194. }
  195. /**
  196. * Release a plugin to `esm`.
  197. * Adds a dependency to 'vs/editor/editor.api' in contrib files in order for `monaco` to be defined.
  198. * Rewrites imports for 'monaco-editor-core/**'
  199. */
  200. function ESM_pluginStream(plugin, destinationPath) {
  201. const DESTINATION = path.join(__dirname, destinationPath);
  202. let pluginPath = plugin.paths[`esm`];
  203. return (
  204. gulp.src([
  205. pluginPath + '/**/*'
  206. ])
  207. .pipe(es.through(function(data) {
  208. if (!/\.js$/.test(data.path)) {
  209. this.emit('data', data);
  210. return;
  211. }
  212. let contents = data.contents.toString();
  213. const info = ts.preProcessFile(contents);
  214. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  215. const importText = info.importedFiles[i].fileName;
  216. const pos = info.importedFiles[i].pos;
  217. const end = info.importedFiles[i].end;
  218. if (!/(^\.\/)|(^\.\.\/)/.test(importText)) {
  219. // non-relative import
  220. if (!/^monaco-editor-core/.test(importText)) {
  221. console.error(`Non-relative import for unknown module: ${importText} in ${data.path}`);
  222. process.exit(0);
  223. }
  224. const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
  225. const importFilePath = path.join(DESTINATION, importText.substr('monaco-editor-core/esm/'.length));
  226. let relativePath = path.relative(path.dirname(myFileDestPath), importFilePath);
  227. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  228. relativePath = './' + relativePath;
  229. }
  230. contents = (
  231. contents.substring(0, pos + 1)
  232. + relativePath
  233. + contents.substring(end + 1)
  234. );
  235. }
  236. }
  237. data.contents = new Buffer(contents);
  238. this.emit('data', data);
  239. }))
  240. .pipe(es.through(function(data) {
  241. if (!/monaco\.contribution\.js$/.test(data.path)) {
  242. this.emit('data', data);
  243. return;
  244. }
  245. const myFileDestPath = path.join(DESTINATION, plugin.modulePrefix, data.relative);
  246. const apiFilePath = path.join(DESTINATION, 'vs/editor/editor.api');
  247. let relativePath = path.relative(path.dirname(myFileDestPath), apiFilePath);
  248. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  249. relativePath = './' + relativePath;
  250. }
  251. let contents = data.contents.toString();
  252. contents = (
  253. `import '${relativePath}';\n` +
  254. contents
  255. );
  256. data.contents = new Buffer(contents);
  257. this.emit('data', data);
  258. }))
  259. .pipe(ESM_addImportSuffix())
  260. .pipe(gulp.dest(destinationPath + plugin.modulePrefix))
  261. );
  262. }
  263. function ESM_addImportSuffix() {
  264. return es.through(function(data) {
  265. if (!/\.js$/.test(data.path)) {
  266. this.emit('data', data);
  267. return;
  268. }
  269. let contents = data.contents.toString();
  270. const info = ts.preProcessFile(contents);
  271. for (let i = info.importedFiles.length - 1; i >= 0; i--) {
  272. const importText = info.importedFiles[i].fileName;
  273. const pos = info.importedFiles[i].pos;
  274. const end = info.importedFiles[i].end;
  275. if (/\.css$/.test(importText)) {
  276. continue;
  277. }
  278. contents = (
  279. contents.substring(0, pos + 1)
  280. + importText + '.js'
  281. + contents.substring(end + 1)
  282. );
  283. }
  284. data.contents = new Buffer(contents);
  285. this.emit('data', data);
  286. });
  287. }
  288. /**
  289. * - Rename esm/vs/editor/editor.main.js to esm/vs/editor/edcore.main.js
  290. * - Create esm/vs/editor/editor.main.js that that stiches things together
  291. */
  292. function ESM_addPluginContribs(dest) {
  293. const DESTINATION = path.join(__dirname, dest);
  294. return es.through(function(data) {
  295. if (!/editor\.main\.js$/.test(data.path)) {
  296. this.emit('data', data);
  297. return;
  298. }
  299. this.emit('data', new File({
  300. path: data.path.replace(/editor\.main/, 'edcore.main'),
  301. base: data.base,
  302. contents: data.contents
  303. }));
  304. const mainFileDestPath = path.join(DESTINATION, 'vs/editor/editor.main.js');
  305. let mainFileImports = [];
  306. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  307. const contribDestPath = path.join(DESTINATION, plugin.contrib);
  308. let relativePath = path.relative(path.dirname(mainFileDestPath), contribDestPath);
  309. if (!/(^\.\/)|(^\.\.\/)/.test(relativePath)) {
  310. relativePath = './' + relativePath;
  311. }
  312. mainFileImports.push(relativePath);
  313. });
  314. let mainFileContents = (
  315. mainFileImports.map((name) => `import '${name}';`).join('\n')
  316. + `\n\nexport * from './edcore.main';`
  317. );
  318. this.emit('data', new File({
  319. path: data.path,
  320. base: data.base,
  321. contents: new Buffer(mainFileContents)
  322. }));
  323. });
  324. }
  325. /**
  326. * Edit monaco.d.ts:
  327. * - append monaco.d.ts from plugins
  328. */
  329. function addPluginDTS() {
  330. return es.through(function(data) {
  331. if (!/monaco\.d\.ts$/.test(data.path)) {
  332. this.emit('data', data);
  333. return;
  334. }
  335. var contents = data.contents.toString();
  336. var extraContent = [];
  337. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  338. var pluginPath = plugin.paths[`npm/min`]; // npm/dev or npm/min
  339. var dtsPath = path.join(pluginPath, '../monaco.d.ts');
  340. try {
  341. extraContent.push(fs.readFileSync(dtsPath).toString());
  342. } catch (err) {
  343. return;
  344. }
  345. });
  346. contents = [
  347. '/*!-----------------------------------------------------------',
  348. ' * Copyright (c) Microsoft Corporation. All rights reserved.',
  349. ' * Type definitions for monaco-editor v'+MONACO_EDITOR_VERSION,
  350. ' * Released under the MIT license',
  351. '*-----------------------------------------------------------*/',
  352. ].join('\n') + '\n' + contents + '\n' + extraContent.join('\n');
  353. // Ensure consistent indentation and line endings
  354. contents = cleanFile(contents);
  355. data.contents = new Buffer(contents);
  356. fs.writeFileSync('website/playground/monaco.d.ts.txt', contents);
  357. fs.writeFileSync('monaco.d.ts', contents);
  358. this.emit('data', data);
  359. });
  360. }
  361. /**
  362. * Normalize line endings and ensure consistent 4 spaces indentation
  363. */
  364. function cleanFile(contents) {
  365. return contents.split(/\r\n|\r|\n/).map(function(line) {
  366. var m = line.match(/^(\t+)/);
  367. if (!m) {
  368. return line;
  369. }
  370. var tabsCount = m[1].length;
  371. var newIndent = '';
  372. for (var i = 0; i < 4 * tabsCount; i++) {
  373. newIndent += ' ';
  374. }
  375. return newIndent + line.substring(tabsCount);
  376. }).join('\n');
  377. }
  378. /**
  379. * Edit ThirdPartyNotices.txt:
  380. * - append ThirdPartyNotices.txt from plugins
  381. */
  382. function addPluginThirdPartyNotices() {
  383. return es.through(function(data) {
  384. if (!/ThirdPartyNotices\.txt$/.test(data.path)) {
  385. this.emit('data', data);
  386. return;
  387. }
  388. var contents = data.contents.toString();
  389. var extraContent = [];
  390. metadata.METADATA.PLUGINS.forEach(function(plugin) {
  391. if (!plugin.thirdPartyNotices) {
  392. return;
  393. }
  394. console.log('ADDING ThirdPartyNotices from ' + plugin.thirdPartyNotices);
  395. var thirdPartyNoticeContent = fs.readFileSync(plugin.thirdPartyNotices).toString();
  396. thirdPartyNoticeContent = thirdPartyNoticeContent.split('\n').slice(8).join('\n');
  397. extraContent.push(thirdPartyNoticeContent);
  398. });
  399. contents += '\n' + extraContent.join('\n');
  400. data.contents = new Buffer(contents);
  401. this.emit('data', data);
  402. });
  403. }
  404. // --- website
  405. gulp.task('clean-website', function(cb) { rimraf('../monaco-editor-website', { maxBusyTries: 1 }, cb); });
  406. gulp.task('website', ['clean-website'], function() {
  407. function replaceWithRelativeResource(dataPath, contents, regex, callback) {
  408. return contents.replace(regex, function(_, m0) {
  409. var filePath = path.join(path.dirname(dataPath), m0);
  410. return callback(m0, fs.readFileSync(filePath));
  411. });
  412. }
  413. var waiting = 0;
  414. var done = false;
  415. return (
  416. es.merge(
  417. gulp.src([
  418. 'website/**/*',
  419. '!website/typedoc-theme/**'
  420. ], { dot: true })
  421. .pipe(es.through(function(data) {
  422. if (!data.contents || !/\.(html)$/.test(data.path) || /new-samples/.test(data.path)) {
  423. return this.emit('data', data);
  424. }
  425. var contents = data.contents.toString();
  426. contents = contents.replace(/\.\.\/release\/dev/g, 'node_modules/monaco-editor/min');
  427. contents = contents.replace(/{{version}}/g, MONACO_EDITOR_VERSION);
  428. // contents = contents.replace('&copy; 2017 Microsoft', '&copy; 2017 Microsoft [' + builtTime + ']');
  429. // Preload xhr contents
  430. contents = replaceWithRelativeResource(data.path, contents, /<pre data-preload="([^"]+)".*/g, function(m0, fileContents) {
  431. return (
  432. '<pre data-preload="' + m0 + '" style="display:none">'
  433. + fileContents.toString('utf8')
  434. .replace(/&/g, '&amp;')
  435. .replace(/</g, '&lt;')
  436. .replace(/>/g, '&gt;')
  437. + '</pre>'
  438. );
  439. });
  440. // Inline fork.png
  441. contents = replaceWithRelativeResource(data.path, contents, /src="(\.\/fork.png)"/g, function(m0, fileContents) {
  442. return (
  443. 'src="data:image/png;base64,' + fileContents.toString('base64') + '"'
  444. );
  445. });
  446. var allCSS = '';
  447. var tmpcontents = replaceWithRelativeResource(data.path, contents, /<link data-inline="yes-please" href="([^"]+)".*/g, function(m0, fileContents) {
  448. allCSS += fileContents.toString('utf8');
  449. return '';
  450. });
  451. tmpcontents = tmpcontents.replace(/<script.*/g, '');
  452. tmpcontents = tmpcontents.replace(/<link.*/g, '');
  453. waiting++;
  454. uncss(tmpcontents, {
  455. raw: allCSS,
  456. ignore: [/\.alert\b/, /\.alert-error\b/, /\.playground-page\b/]
  457. }, function(err, output) {
  458. waiting--;
  459. if (!err) {
  460. output = new CleanCSS().minify(output).styles;
  461. var isFirst = true;
  462. contents = contents.replace(/<link data-inline="yes-please" href="([^"]+)".*/g, function(_, m0) {
  463. if (isFirst) {
  464. isFirst = false;
  465. return '<style>' + output + '</style>';
  466. }
  467. return '';
  468. });
  469. }
  470. // Inline javascript
  471. contents = replaceWithRelativeResource(data.path, contents, /<script data-inline="yes-please" src="([^"]+)".*/g, function(m0, fileContents) {
  472. return '<script>' + fileContents.toString('utf8') + '</script>';
  473. });
  474. data.contents = new Buffer(contents.split(/\r\n|\r|\n/).join('\n'));
  475. this.emit('data', data);
  476. if (done && waiting === 0) {
  477. this.emit('end');
  478. }
  479. }.bind(this));
  480. }, function() {
  481. done = true;
  482. if (waiting === 0) {
  483. this.emit('end');
  484. }
  485. }))
  486. .pipe(gulp.dest('../monaco-editor-website')),
  487. gulp.src('monaco.d.ts')
  488. .pipe(typedoc({
  489. mode: 'file',
  490. out: '../monaco-editor-website/api',
  491. includeDeclarations: true,
  492. theme: 'website/typedoc-theme',
  493. entryPoint: 'monaco',
  494. name: 'Monaco Editor API v' + MONACO_EDITOR_VERSION,
  495. readme: 'none',
  496. hideGenerator: true
  497. }))
  498. )
  499. .pipe(es.through(function(data) {
  500. this.emit('data', data);
  501. }, function() {
  502. // temporarily create package.json so that npm install doesn't bark
  503. fs.writeFileSync('../monaco-editor-website/package.json', '{}');
  504. fs.writeFileSync('../monaco-editor-website/.nojekyll', '');
  505. cp.execSync('npm install monaco-editor', {
  506. cwd: path.join(__dirname, '../monaco-editor-website')
  507. });
  508. fs.unlink('../monaco-editor-website/package.json');
  509. cp.execSync('git init', {
  510. cwd: path.join(__dirname, '../monaco-editor-website')
  511. });
  512. cp.execSync('git checkout -b gh-pages', {
  513. cwd: path.join(__dirname, '../monaco-editor-website')
  514. });
  515. cp.execSync('git add .', {
  516. cwd: path.join(__dirname, '../monaco-editor-website')
  517. });
  518. cp.execSync('git commit -m "Publish website"', {
  519. cwd: path.join(__dirname, '../monaco-editor-website')
  520. });
  521. cp.execSync('git remote add origin https://github.com/Microsoft/monaco-editor.git', {
  522. cwd: path.join(__dirname, '../monaco-editor-website')
  523. });
  524. console.log('RUN monaco-editor-website>git push origin gh-pages --force')
  525. this.emit('end');
  526. }))
  527. );
  528. });
  529. gulp.task('generate-test-samples', function() {
  530. var sampleNames = fs.readdirSync(path.join(__dirname, 'test/samples'));
  531. var samples = sampleNames.map(function(sampleName) {
  532. var samplePath = path.join(__dirname, 'test/samples', sampleName);
  533. var sampleContent = fs.readFileSync(samplePath).toString();
  534. return {
  535. name: sampleName,
  536. content: sampleContent
  537. };
  538. });
  539. var prefix = '//This is a generated file via gulp generate-test-samples\ndefine([], function() { return';
  540. var suffix = '; });'
  541. fs.writeFileSync(path.join(__dirname, 'test/samples-all.generated.js'), prefix + JSON.stringify(samples, null, '\t') + suffix );
  542. var PLAY_SAMPLES = require(path.join(WEBSITE_GENERATED_PATH, 'all.js')).PLAY_SAMPLES;
  543. var locations = [];
  544. for (var i = 0; i < PLAY_SAMPLES.length; i++) {
  545. var sample = PLAY_SAMPLES[i];
  546. var sampleId = sample.id;
  547. var samplePath = path.join(WEBSITE_GENERATED_PATH, sample.path);
  548. var html = fs.readFileSync(path.join(samplePath, 'sample.html'));
  549. var js = fs.readFileSync(path.join(samplePath, 'sample.js'));
  550. var css = fs.readFileSync(path.join(samplePath, 'sample.css'));
  551. var result = [
  552. '<!DOCTYPE html>',
  553. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  554. '<html>',
  555. '<head>',
  556. ' <base href="..">',
  557. ' <meta http-equiv="X-UA-Compatible" content="IE=edge" />',
  558. ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />',
  559. '</head>',
  560. '<body>',
  561. '<style>',
  562. '/*----------------------------------------SAMPLE CSS START*/',
  563. '',
  564. css,
  565. '',
  566. '/*----------------------------------------SAMPLE CSS END*/',
  567. '</style>',
  568. '<a class="loading-opts" href="playground.generated/index.html">[&lt;&lt; BACK]</a> <br/>',
  569. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples',
  570. '',
  571. '<div id="bar" style="margin-bottom: 6px;"></div>',
  572. '',
  573. '<div style="clear:both"></div>',
  574. '<div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">',
  575. '<!-- ----------------------------------------SAMPLE HTML START-->',
  576. '',
  577. html,
  578. '',
  579. '<!-- ----------------------------------------SAMPLE HTML END-->',
  580. '</div>',
  581. '<div style="clear:both"></div>',
  582. '',
  583. '<script src="../metadata.js"></script>',
  584. '<script src="dev-setup.js"></script>',
  585. '<script>',
  586. 'loadEditor(function() {',
  587. '/*----------------------------------------SAMPLE JS START*/',
  588. '',
  589. js,
  590. '',
  591. '/*----------------------------------------SAMPLE CSS END*/',
  592. '});',
  593. '</script>',
  594. '</body>',
  595. '</html>',
  596. ];
  597. fs.writeFileSync(path.join(__dirname, 'test/playground.generated/' + sampleId + '.html'), result.join('\n'));
  598. locations.push({
  599. path: sampleId + '.html',
  600. name: sample.chapter + ' &gt; ' + sample.name
  601. })
  602. }
  603. var index = [
  604. '<!DOCTYPE html>',
  605. '<!-- THIS IS A GENERATED FILE VIA gulp generate-test-samples -->',
  606. '<html>',
  607. '<head>',
  608. ' <base href="..">',
  609. '</head>',
  610. '<body>',
  611. '<a class="loading-opts" href="index.html">[&lt;&lt; BACK]</a><br/>',
  612. 'THIS IS A GENERATED FILE VIA gulp generate-test-samples<br/><br/>',
  613. locations.map(function(location) {
  614. return '<a class="loading-opts" href="playground.generated/' + location.path + '">' + location.name + '</a>';
  615. }).join('<br/>\n'),
  616. '<script src="../metadata.js"></script>',
  617. '<script src="dev-setup.js"></script>',
  618. '</body>',
  619. '</html>',
  620. ]
  621. fs.writeFileSync(path.join(__dirname, 'test/playground.generated/index.html'), index.join('\n'));
  622. });
  623. gulp.task('simpleserver', ['generate-test-samples'], function(cb) {
  624. httpServer.createServer({ root: '../', cache: 5 }).listen(8080);
  625. httpServer.createServer({ root: '../', cache: 5 }).listen(8088);
  626. console.log('LISTENING on 8080 and 8088');
  627. });