From 6f8c323557988fc02a09d1e5f8407cb27348375d Mon Sep 17 00:00:00 2001 From: Book Pauk Date: Wed, 17 Aug 2022 01:13:09 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BF=D0=B0=D1=80=D1=81=D0=B8=D0=BD=D0=B3=20cli-?= =?UTF-8?q?=D0=B0=D1=80=D0=B3=D1=83=D0=BC=D0=B5=D0=BD=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/index.js | 86 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/server/index.js b/server/index.js index e02a7ef..1e9c055 100644 --- a/server/index.js +++ b/server/index.js @@ -1,5 +1,6 @@ const fs = require('fs-extra'); -const argv = require('minimist')(process.argv.slice(2)); +const path = require('path'); + const express = require('express'); const compression = require('compression'); const http = require('http'); @@ -8,12 +9,30 @@ const WebSocket = require ('ws'); const ayncExit = new (require('./core/AsyncExit'))(); const utils = require('./core/utils'); -let log = null; -let config = null; - const maxPayloadSize = 50;//in MB +let log; +let config; +let argv; +const argvStrings = ['lib-dir', 'inpx']; + +function showHelp() { + console.log(utils.versionText(config)); + console.log( +`Usage: ${config.name} [options] + +Options: + --help Print ${config.name} command line options + --lib-dir= Set library directory, default: the same as ${config.name} executable + --inpx= Set INPX collection file, default: the one that found in library dir + --recreate Force recreation of the search database on start +` + ); +} + async function init() { + argv = require('minimist')(process.argv.slice(2), {string: argvStrings}); + //config const configManager = new (require('./config'))();//singleton await configManager.init(); @@ -26,11 +45,52 @@ async function init() { await appLogger.init(config); log = appLogger.log; - if (!argv.help) { + //cli + if (argv.help) { + showHelp(); + ayncExit.exit(0); + } else { log(utils.versionText(config)); log('Initializing'); } + const libDir = argv['lib-dir']; + if (libDir) { + if (await fs.pathExists(libDir)) { + config.libDir = libDir; + } else { + throw new Error(`Directory "${libDir}" not exists`); + } + } else { + config.libDir = config.execDir; + } + + if (argv.inpx) { + if (await fs.pathExists(argv.inpx)) { + config.inpxFile = argv.inpx; + } else { + throw new Error(`File "${argv.inpx}" not found`); + } + } else { + const inpxFiles = []; + await utils.findFiles((file) => { + if (path.extname(file) == '.inpx') + inpxFiles.push(file); + }, config.libDir, false); + + if (inpxFiles.length) { + if (inpxFiles.length == 1) { + config.inpxFile = inpxFiles[0]; + } else { + throw new Error(`Found more than one .inpx files: \n${inpxFiles.join('\n')}`); + } + } else { + throw new Error(`No .inpx files found here: ${config.libDir}`); + } + } + + config.recreateDb = argv.recreate || false; + //dirs await fs.ensureDir(config.dataDir); await fs.ensureDir(config.tempDir); @@ -44,23 +104,7 @@ async function init() { } } -function showHelp() { - console.log(utils.versionText(config)); - console.log( -`Usage: ${config.name} [options] - -Options: - --help Print ${config.name} command line options -` - ); -} - async function main() { - if (argv.help) { - showHelp(); - ayncExit.exit(0); - } - const log = new (require('./core/AppLogger'))().log;//singleton //server