simpleserver.ts 5.2 KB

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