/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ //@ts-check const fs = require('fs'); const path = require('path'); const http = require('http'); const yaserver = require('yaserver'); const { REPO_ROOT, ensureDir } = require('./utils'); const WEBSITE_GENERATED_PATH = path.join(REPO_ROOT, 'website/playground/new-samples'); generateTestSamplesTask(); const SERVER_ROOT = path.normalize(path.join(REPO_ROOT, '../')); createSimpleServer(SERVER_ROOT, 8080); createSimpleServer(SERVER_ROOT, 8088); function generateTestSamplesTask() { const sampleNames = fs.readdirSync(path.join(REPO_ROOT, 'test/manual/samples')); let samples = sampleNames.map((sampleName) => { const samplePath = path.join(REPO_ROOT, 'test/manual/samples', sampleName); const sampleContent = fs.readFileSync(samplePath).toString(); return { name: sampleName, content: sampleContent }; }); // Add samples from website { let sampleNames = fs.readdirSync(path.join(REPO_ROOT, 'website/index/samples')); sampleNames = sampleNames.filter((name) => /^sample/.test(name)); samples = samples.concat( sampleNames.map((sampleName) => { const samplePath = path.join(REPO_ROOT, 'website/index/samples', sampleName); const sampleContent = fs.readFileSync(samplePath).toString(); return { name: sampleName, content: sampleContent }; }) ); } const prefix = '//This is a generated file via `npm run simpleserver`\ndefine([], function() { return'; const suffix = '; });'; const destination = path.join(REPO_ROOT, 'test/manual/generated/all-samples.js'); ensureDir(path.dirname(destination)); fs.writeFileSync(destination, prefix + JSON.stringify(samples, null, '\t') + suffix); /** @type {{ chapter: string; name: string; id: string; path: string; }[]} */ const PLAY_SAMPLES = require(path.join(WEBSITE_GENERATED_PATH, 'all.js')).PLAY_SAMPLES; /** @type {{ path: string; name: string; }[]} */ const locations = []; for (let i = 0; i < PLAY_SAMPLES.length; i++) { const sample = PLAY_SAMPLES[i]; const sampleId = sample.id; const samplePath = path.join(WEBSITE_GENERATED_PATH, sample.path); const html = fs.readFileSync(path.join(samplePath, 'sample.html')); const js = fs.readFileSync(path.join(samplePath, 'sample.js')); const css = fs.readFileSync(path.join(samplePath, 'sample.css')); const result = [ '', '', '', '', ' ', ' ', '', '', '', '[<< BACK]
', 'THIS IS A GENERATED FILE VIA `npm run simpleserver`', '', '
', '', '
', '
', '', '', html, '', '', '
', '
', '', '', '', '', '', '' ]; const destination = path.join( REPO_ROOT, 'test/manual/generated/playground/' + sampleId + '.html' ); ensureDir(path.dirname(destination)); fs.writeFileSync(destination, result.join('\n')); locations.push({ path: sampleId + '.html', name: sample.chapter + ' > ' + sample.name }); } const index = [ '', '', '', '', ' ', '', '', '[<< BACK]
', 'THIS IS A GENERATED FILE VIA `npm run simpleserver`

', locations .map(function (location) { return ( '' + location.name + '' ); }) .join('
\n'), '', '', '', '' ]; fs.writeFileSync( path.join(REPO_ROOT, 'test/manual/generated/playground/index.html'), index.join('\n') ); } /** * @param {string} rootDir * @param {number} port */ function createSimpleServer(rootDir, port) { yaserver .createServer({ rootDir: rootDir }) .then((staticServer) => { const server = http.createServer((request, response) => { return staticServer.handle(request, response); }); server.listen(port, '127.0.0.1', () => { console.log(`Running at http://127.0.0.1:${port}`); }); }); }