simpleserver.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. generateTestSamplesTask();
  12. const SERVER_ROOT = path.normalize(path.join(REPO_ROOT, '../'));
  13. createSimpleServer(SERVER_ROOT, 8080);
  14. createSimpleServer(SERVER_ROOT, 8088);
  15. function generateTestSamplesTask() {
  16. const sampleNames = fs.readdirSync(path.join(REPO_ROOT, 'test/manual/samples'));
  17. let samples = sampleNames.map((sampleName) => {
  18. const samplePath = path.join(REPO_ROOT, 'test/manual/samples', sampleName);
  19. const sampleContent = fs.readFileSync(samplePath).toString();
  20. return {
  21. name: sampleName,
  22. content: sampleContent
  23. };
  24. });
  25. // Add samples from website
  26. {
  27. let sampleNames = fs.readdirSync(path.join(REPO_ROOT, 'website/index/samples'));
  28. sampleNames = sampleNames.filter((name) => /^sample/.test(name));
  29. samples = samples.concat(
  30. sampleNames.map((sampleName) => {
  31. const samplePath = path.join(REPO_ROOT, 'website/index/samples', sampleName);
  32. const sampleContent = fs.readFileSync(samplePath).toString();
  33. return {
  34. name: sampleName,
  35. content: sampleContent
  36. };
  37. })
  38. );
  39. }
  40. const prefix =
  41. '//This is a generated file via `npm run simpleserver`\ndefine([], function() { return';
  42. const suffix = '; });';
  43. const destination = path.join(REPO_ROOT, 'test/manual/generated/all-samples.js');
  44. ensureDir(path.dirname(destination));
  45. fs.writeFileSync(destination, prefix + JSON.stringify(samples, null, '\t') + suffix);
  46. }
  47. function createSimpleServer(rootDir: string, port: number) {
  48. yaserver
  49. .createServer({
  50. rootDir: rootDir
  51. })
  52. .then((staticServer) => {
  53. const server = http.createServer((request, response) => {
  54. return staticServer.handle(request, response);
  55. });
  56. server.listen(port, '127.0.0.1', () => {
  57. console.log(`Running at http://127.0.0.1:${port}`);
  58. });
  59. });
  60. }