多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB

6 years ago
"use strict";
const program = require("commander");
const colors = require("colors");
const maxCpus = require("os").cpus().length;
program.on("--help", () => {
console.log("");
console.log(" Examples:".blue);
console.log("");
console.log(" $ xmysql -u username -p password -d databaseSchema".blue);
console.log("");
});
7 years ago
program
.version(module.exports.version)
6 years ago
.option("-h, --host <n>", "hostname of database / localhost by default")
.option("-u, --user <n>", "username of database / root by default")
.option("-p, --password <n>", "password of database / empty by default")
.option("-d, --database <n>", "database schema name")
.option(
"-r, --ipAddress <n>",
"IP interface of your server / localhost by default"
)
.option("-n, --portNumber <n>", "port number for app / 3000 by default")
.option("-o, --port <n>", "port number for mysql / 3306 by default")
.option("-S, --socketPath <n>", "unix socket path / not used by default")
.option(
"-s, --storageFolder <n>",
"storage folder / current working dir by default / available only with local"
)
.option("-i, --ignoreTables <n>", "comma separated table names to ignore")
.option("-a, --apiPrefix <n>", 'api url prefix / "/api/" by default')
.option("-y, --readOnly", "readonly apis / false by default")
.option(
"-c, --useCpuCores <n>",
"use number of CPU cores (using cluster) / 1 by default"
)
.parse(process.argv);
7 years ago
function paintHelp(txt) {
6 years ago
return colors.magenta(txt); //display the help text in a color
7 years ago
}
function processInvalidArguments(program) {
6 years ago
let err = "";
7 years ago
if (!program.password) {
6 years ago
err += "Error: password for database is missing\n";
7 years ago
}
if (!program.database) {
6 years ago
err += "Error: database name is missing\n";
7 years ago
}
6 years ago
if (err !== "") {
program.outputHelp(paintHelp);
console.log(err.red);
7 years ago
}
}
exports.handle = program => {
/**************** START : default values ****************/
6 years ago
program.ipAddress = program.ipAddress || "localhost";
7 years ago
program.portNumber = program.portNumber || 3000;
program.port = program.port || 3306;
6 years ago
program.user = program.user || "root";
program.password = program.password || "";
program.host = program.host || "localhost";
program.socketPath = program.socketPath || "";
program.storageFolder = program.storageFolder || process.cwd();
program.apiPrefix = program.apiPrefix || "/api/";
program.readOnly = program.readOnly || false;
program.useCpuCores = program.useCpuCores || 1;
6 years ago
if (program.useCpuCores === "0") {
program.useCpuCores = maxCpus;
}
7 years ago
if (program.ignoreTables) {
6 years ago
let ignoreTables = program.ignoreTables.split(",");
program.ignoreTables = {};
7 years ago
for (var i = 0; i < ignoreTables.length; ++i) {
program.ignoreTables[ignoreTables[i]] = ignoreTables[i];
}
} else {
6 years ago
program.ignoreTables = {};
7 years ago
}
7 years ago
program.connectionLimit = 10;
6 years ago
if (
program.host === "localhost" ||
program.host === "127.0.0.1" ||
program.host === "::1"
) {
program.dynamic = 1;
7 years ago
}
// console.log(program);
7 years ago
/**************** END : default values ****************/
if (program.database && program.host && program.user) {
//console.log('Starting server at:', 'http://' + program.host + ':' + program.portNumber)
7 years ago
} else {
6 years ago
processInvalidArguments(program);
process.exit(1);
7 years ago
}
};