<commit_before>var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var globals = require("../core/globals");

var mine = {
    "css": "text/css",
    "gif": "image/gif",
    "html": "text/html",
    "ico": "image/x-icon",
    "jpeg": "image/jpeg",
    "jpg": "image/jpeg",
    "js": "text/javascript",
    "json": "application/json",
    "pdf": "application/pdf",
    "png": "image/png",
    "svg": "image/svg+xml",
    "swf": "application/x-shockwave-flash",
    "tiff": "image/tiff",
    "txt": "text/plain",
    "wav": "audio/x-wav",
    "wma": "audio/x-ms-wma",
    "wmv": "video/x-ms-wmv",
    "xml": "text/xml"
}

function run(dir, args, opts) {
    var PORT = 3000;
    var server = http.createServer(onGet);
    server.addListener("error",function(){
        globals.exit(1501);
    })
    server.listen(PORT,function(){
        var open = require("../core/open");
        globals.joinEgretDir(dir, args[0]);
        var url = path.join("http://localhost:3000", args[0], "launcher/index.html");
        open(url);
        console.log("Server runing at port: " + PORT + ".");
        exports.projectName = args[0];
    });


}


function onGet(request, response) {
    var projectName = exports.projectName;
    var pathname = url.parse(request.url).pathname;
    if (pathname.indexOf("__compile__") > -1) {
        executeCommand(function () {
            writeText("success", response);
        }, "egret build " + projectName + " -v");
        return;
    }


    writeFile(pathname, response);
}

function writeText(text, response) {
    var contentType = "text/plain";
    response.writeHead(200, {
        'Content-Type': contentType,
        'Access-Control-Allow-Origin': '*'
    });
    response.write(text);
    response.end();
}


function writeFile(pathname, response) {
    var realPath = path.join(process.cwd(), pathname);
    var ext = path.extname(realPath);
    ext = ext ? ext.slice(1) : 'unknown';
    fs.exists(realPath, function (exists) {
        if (!exists) {
            response.writeHead(404, {
                'Content-Type': 'text/plain'
            });
            console.log(realPath);
            response.write("This request URL " + pathname + " was not found on this server.");
            response.end();
        } else {
            fs.readFile(realPath, "binary", function (err, file) {
                if (err) {
                    response.writeHead(500, {
                        'Content-Type': 'text/plain'
                    });
                    response.end(err);
                } else {
                    var contentType = mine[ext] || "text/plain";
                    response.writeHead(200, {
                        'Content-Type': contentType,
                        'Access-Control-Allow-Origin': '*'
                    });
                    response.write(file, "binary");
                    response.end();
                }
            });
        }
    });
}

exports.run = run;


function executeCommand(callback, script) {
    console.log(script);
    var cp_exec = require("child_process").exec;
    var cmd = cp_exec(script);
    cmd.stderr.on("data", function (data) {
        console.log(data);
    })
    cmd.stdout.on("data", function (data) {
        console.log(data);
    })

    cmd.on('exit', function (code) {

        console.log(code)
        if (code == 0) {
            callback();
        }
        else {
            globals.log("脚本执行失败");
        }

    });
}


function help_title() {
    return "启动HttpServer,并在默认浏览器中打开指定项目\n";
}


function help_example() {
    return "egret startserver [project_name]";
}

exports.run = run;
exports.help_title = help_title;
exports.help_example = help_example;
<commit_msg>startserver加入端口号参数<commit_after>var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var globals = require("../core/globals");

var mine = {
    "css": "text/css",
    "gif": "image/gif",
    "html": "text/html",
    "ico": "image/x-icon",
    "jpeg": "image/jpeg",
    "jpg": "image/jpeg",
    "js": "text/javascript",
    "json": "application/json",
    "pdf": "application/pdf",
    "png": "image/png",
    "svg": "image/svg+xml",
    "swf": "application/x-shockwave-flash",
    "tiff": "image/tiff",
    "txt": "text/plain",
    "wav": "audio/x-wav",
    "wma": "audio/x-ms-wma",
    "wmv": "video/x-ms-wmv",
    "xml": "text/xml"
}

function run(dir, args, opts) {
    var PORT = 3000;
    if (opts["--port"] && opts["--port"][0]) {
        PORT = opts["--port"][0];
    }
    var server = http.createServer(onGet);
    server.addListener("error", function () {
        globals.exit(1501);
    })
    server.listen(PORT, function () {
        var open = require("../core/open");
        globals.joinEgretDir(dir, args[0]);
        var url = path.join("http://localhost:" + PORT, args[0], "launcher/index.html");
        open(url);
        console.log("Server runing at port: " + PORT + ".");
        exports.projectName = args[0];
    });


}


function onGet(request, response) {
    var projectName = exports.projectName;
    var pathname = url.parse(request.url).pathname;
    if (pathname.indexOf("__compile__") > -1) {
        executeCommand(function () {
            writeText("success", response);
        }, "egret build " + projectName + " -v");
        return;
    }


    writeFile(pathname, response);
}

function writeText(text, response) {
    var contentType = "text/plain";
    response.writeHead(200, {
        'Content-Type': contentType,
        'Access-Control-Allow-Origin': '*'
    });
    response.write(text);
    response.end();
}


function writeFile(pathname, response) {
    var realPath = path.join(process.cwd(), pathname);
    var ext = path.extname(realPath);
    ext = ext ? ext.slice(1) : 'unknown';
    fs.exists(realPath, function (exists) {
        if (!exists) {
            response.writeHead(404, {
                'Content-Type': 'text/plain'
            });
            console.log(realPath);
            response.write("This request URL " + pathname + " was not found on this server.");
            response.end();
        } else {
            fs.readFile(realPath, "binary", function (err, file) {
                if (err) {
                    response.writeHead(500, {
                        'Content-Type': 'text/plain'
                    });
                    response.end(err);
                } else {
                    var contentType = mine[ext] || "text/plain";
                    response.writeHead(200, {
                        'Content-Type': contentType,
                        'Access-Control-Allow-Origin': '*'
                    });
                    response.write(file, "binary");
                    response.end();
                }
            });
        }
    });
}

exports.run = run;


function executeCommand(callback, script) {
    console.log(script);
    var cp_exec = require("child_process").exec;
    var cmd = cp_exec(script);
    cmd.stderr.on("data", function (data) {
        console.log(data);
    })
    cmd.stdout.on("data", function (data) {
        console.log(data);
    })

    cmd.on('exit', function (code) {

        console.log(code)
        if (code == 0) {
            callback();
        }
        else {
            globals.log("脚本执行失败");
        }

    });
}


function help_title() {
    return "启动HttpServer,并在默认浏览器中打开指定项目\n";
}


function help_example() {
    var result = "egret startserver [project_name]";
    result += "参数说明:\n";
    result += "    --port           指定端口号\n";
    return result;
}

exports.run = run;
exports.help_title = help_title;
exports.help_example = help_example;

{'fraction_non_alphanumeric': 0.16755146963615175, 'fraction_numerical': 0.007510034960507575, 'mean_word_length': 2.027832222657781, 'pattern_counts': {'":': 36, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '338739', 'n_tokens_mistral': 2517, 'n_tokens_neox': 2441, 'n_words': 460}