"use strict";
/*********************************************************************
 *
 *  $Id: app.ts 32624 2018-10-10 13:23:29Z seb $
 *
 *  Yoctopuce trivial HTTP server for testing TypeScript/HTML examples;
 *  Also runs a TypeScript watcher in background
 *
 *  You can run it using 'npm run app'
 *
 *  - When run at root of yoctolib-ts, open a browser on the full URL
 *    of the example that you want to open, eg:
 *    http://127.0.0.1:3000/example_html/Doc-Inventory/app.html
 *  - When run within a subdirectory of example_html, the server will
 *    automatically redirect to the current directory app.html
 *
 *********************************************************************/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
require("process");
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const url = __importStar(require("url"));
const http = __importStar(require("http"));
const childProcess = __importStar(require("child_process"));
const HTTP_PORT = 3000;
const HTTP_ROOT = process.cwd();
const DEMO_PATH = (process.env.INIT_CWD ? process.env.INIT_CWD.slice(HTTP_ROOT.length + 1) : '.');
const DEMO_ROOT = path.join(HTTP_ROOT, DEMO_PATH);
const DEFAULT_FILE = path.join(DEMO_PATH, 'dist/es2017/app.html');
const ROOT_URL = 'http://127.0.0.1:' + HTTP_PORT;
const MimeTypes = {
    '.js': 'application/javascript',
    '.ts': 'application/typescript',
    '.map': 'application/json',
    '.html': 'text/html',
    '.css': 'text/css',
    '.png': 'image/png',
    '.svg': 'image/svg+xml',
    '.jpg': 'image/jpeg',
    '.jpeg': 'image/jpeg'
};
function listener(message, response) {
    let srvpath = url.parse(message.url).pathname || '/';
    let relpath = srvpath.slice(1);
    let abspath = path.join(HTTP_ROOT, relpath);
    let extension = path.extname(relpath);
    let mimetype = MimeTypes[extension.toLowerCase()];
    // Redirect server root to our demo subdirectory
    if (!relpath) {
        response.writeHead(302, { 'Location': DEFAULT_FILE, 'Content-Type': 'text/plain' });
        response.write("302 Found: [" + DEFAULT_FILE + "]");
        response.end();
        return;
    }
    // Make sure we know about this file
    //console.log('Request for ['+abspath+']');
    let fileexists = fs.existsSync(abspath);
    if (!fileexists || !mimetype) {
        response.writeHead(404, { 'Content-Type': 'text/plain' });
        if (!fileexists) {
            response.write("404 Not Found: [" + abspath + "]");
        }
        else {
            response.write("404 Not Found: unsupported MIME type [" + extension + "]");
        }
        response.end();
        return;
    }
    // Output file to browser at once
    response.writeHead(200, { 'Content-Type': MimeTypes[extension] });
    fs.readFile(abspath, (err, data) => {
        if (err)
            throw err;
        response.write(data, 'utf8');
        response.end();
    });
}
// Function to open an URL in the default browser, inspired from https://github.com/sindresorhus/open
function OpenBrowser(target) {
    let command;
    let cliArguments = [];
    let childProcessOptions = {};
    if (process.platform === 'darwin') {
        command = 'open';
        cliArguments = ['--background'];
    }
    else if (process.platform == 'win32') {
        command = process.env.SYSTEMROOT + '\\System32\\WindowsPowerShell\\v1.0\\powershell';
        cliArguments = ['-NoProfile', '-NonInteractive', '–ExecutionPolicy', 'Bypass', '-Command', 'Start'];
        childProcessOptions = { windowsVerbatimArguments: true };
    }
    else {
        command = 'xdg-open';
        childProcessOptions = { stdio: 'ignore', detached: true };
    }
    cliArguments.push(target);
    const subprocess = childProcess.spawn(command, cliArguments, childProcessOptions);
    subprocess.unref();
}
console.info('------------------------------------------------------------------------------------');
console.info('---         This is a small HTTP server to test Yocto-Visualization-4web');
console.info('---      (source code available in Yocto-Visualization-4web/bin/app-server.ts)');
console.info('---');
console.info('--- Serving files in ' + DEMO_PATH + ' on ' + ROOT_URL);
console.info('------------------------------------------------------------------------------------');
http.createServer(listener).listen(HTTP_PORT);
let args = process.argv.slice(2);
if (args.length == 0) {
    OpenBrowser(ROOT_URL);
}
else {
    OpenBrowser(ROOT_URL + '/' + args[0]);
}
//# sourceMappingURL=app-server.js.map