simpleserver.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. //@ts-check
  6. const fs = require('fs');
  7. const path = require('path');
  8. const http = require('http');
  9. const yaserver = require('yaserver');
  10. const { REPO_ROOT } = require('./utils');
  11. const { ensureDir } = require('./fs');
  12. const WEBSITE_GENERATED_PATH = path.join(REPO_ROOT, 'website/playground/new-samples');
  13. generateTestSamplesTask();
  14. const SERVER_ROOT = path.normalize(path.join(REPO_ROOT, '../'));
  15. createSimpleServer(SERVER_ROOT, 8080);
  16. createSimpleServer(SERVER_ROOT, 8088);
  17. function generateTestSamplesTask() {
  18. const sampleNames = fs.readdirSync(path.join(REPO_ROOT, 'test/manual/samples'));
  19. let samples = sampleNames.map((sampleName) => {
  20. const samplePath = path.join(REPO_ROOT, 'test/manual/samples', sampleName);
  21. const sampleContent = fs.readFileSync(samplePath).toString();
  22. return {
  23. name: sampleName,
  24. content: sampleContent
  25. };
  26. });
  27. // Add samples from website
  28. {
  29. let sampleNames = fs.readdirSync(path.join(REPO_ROOT, 'website/index/samples'));
  30. sampleNames = sampleNames.filter((name) => /^sample/.test(name));
  31. samples = samples.concat(
  32. sampleNames.map((sampleName) => {
  33. const samplePath = path.join(REPO_ROOT, 'website/index/samples', sampleName);
  34. const sampleContent = fs.readFileSync(samplePath).toString();
  35. return {
  36. name: sampleName,
  37. content: sampleContent
  38. };
  39. })
  40. );
  41. }
  42. const prefix =
  43. '//This is a generated file via `npm run simpleserver`\ndefine([], function() { return';
  44. const suffix = '; });';
  45. const destination = path.join(REPO_ROOT, 'test/manual/generated/all-samples.js');
  46. ensureDir(path.dirname(destination));
  47. fs.writeFileSync(destination, prefix + JSON.stringify(samples, null, '\t') + suffix);
  48. /** @type {{ chapter: string; name: string; id: string; path: string; }[]} */
  49. const PLAY_SAMPLES = require(path.join(WEBSITE_GENERATED_PATH, 'all.js')).PLAY_SAMPLES;
  50. /** @type {{ path: string; name: string; }[]} */
  51. const locations = [];
  52. for (let i = 0; i < PLAY_SAMPLES.length; i++) {
  53. const sample = PLAY_SAMPLES[i];
  54. const sampleId = sample.id;
  55. const samplePath = path.join(WEBSITE_GENERATED_PATH, sample.path);
  56. const html = fs.readFileSync(path.join(samplePath, 'sample.html'));
  57. const js = fs.readFileSync(path.join(samplePath, 'sample.js'));
  58. const css = fs.readFileSync(path.join(samplePath, 'sample.css'));
  59. const result = [
  60. '<!DOCTYPE html>',
  61. '<!-- THIS IS A GENERATED FILE VIA `npm run simpleserver` -->',
  62. '<html>',
  63. '<head>',
  64. ' <base href="../..">',
  65. ' <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />',
  66. '</head>',
  67. '<body>',
  68. '<style>',
  69. '/*----------------------------------------SAMPLE CSS START*/',
  70. '',
  71. css,
  72. '',
  73. '/*----------------------------------------SAMPLE CSS END*/',
  74. '</style>',
  75. '<a class="loading-opts" href="generated/playground/index.html">[&lt;&lt; BACK]</a> <br/>',
  76. 'THIS IS A GENERATED FILE VIA `npm run simpleserver`',
  77. '',
  78. '<div id="bar" style="margin-bottom: 6px;"></div>',
  79. '',
  80. '<div style="clear:both"></div>',
  81. '<div id="outer-container" style="width:800px;height:450px;border: 1px solid grey">',
  82. '<!-- ----------------------------------------SAMPLE HTML START-->',
  83. '',
  84. html,
  85. '',
  86. '<!-- ----------------------------------------SAMPLE HTML END-->',
  87. '</div>',
  88. '<div style="clear:both"></div>',
  89. '',
  90. '<script src="../../metadata.js"></script>',
  91. '<script src="dev-setup.js"></script>',
  92. '<script>',
  93. 'loadEditor(function() {',
  94. '/*----------------------------------------SAMPLE JS START*/',
  95. '',
  96. js,
  97. '',
  98. '/*----------------------------------------SAMPLE JS END*/',
  99. '});',
  100. '</script>',
  101. '</body>',
  102. '</html>'
  103. ];
  104. const destination = path.join(
  105. REPO_ROOT,
  106. 'test/manual/generated/playground/' + sampleId + '.html'
  107. );
  108. ensureDir(path.dirname(destination));
  109. fs.writeFileSync(destination, result.join('\n'));
  110. locations.push({
  111. path: sampleId + '.html',
  112. name: sample.chapter + ' &gt; ' + sample.name
  113. });
  114. }
  115. const index = [
  116. '<!DOCTYPE html>',
  117. '<!-- THIS IS A GENERATED FILE VIA `npm run simpleserver` -->',
  118. '<html>',
  119. '<head>',
  120. ' <base href="../..">',
  121. '</head>',
  122. '<body>',
  123. '<a class="loading-opts" href="index.html">[&lt;&lt; BACK]</a><br/>',
  124. 'THIS IS A GENERATED FILE VIA `npm run simpleserver`<br/><br/>',
  125. locations
  126. .map(function (location) {
  127. return (
  128. '<a class="loading-opts" href="generated/playground/' +
  129. location.path +
  130. '">' +
  131. location.name +
  132. '</a>'
  133. );
  134. })
  135. .join('<br/>\n'),
  136. '<script src="../../metadata.js"></script>',
  137. '<script src="dev-setup.js"></script>',
  138. '</body>',
  139. '</html>'
  140. ];
  141. fs.writeFileSync(
  142. path.join(REPO_ROOT, 'test/manual/generated/playground/index.html'),
  143. index.join('\n')
  144. );
  145. }
  146. /**
  147. * @param {string} rootDir
  148. * @param {number} port
  149. */
  150. function createSimpleServer(rootDir, port) {
  151. yaserver
  152. .createServer({
  153. rootDir: rootDir
  154. })
  155. .then((staticServer) => {
  156. const server = http.createServer((request, response) => {
  157. return staticServer.handle(request, response);
  158. });
  159. server.listen(port, '127.0.0.1', () => {
  160. console.log(`Running at http://127.0.0.1:${port}`);
  161. });
  162. });
  163. }