Browse Source

Рефакторинг

Book Pauk 6 năm trước cách đây
mục cha
commit
1c136b3a34

+ 2 - 2
server/config/index.js

@@ -1,4 +1,4 @@
-const fs = require('fs');
+const fs = require('fs-extra');
 const utils = require('../core/utils');
 const utils = require('../core/utils');
 
 
 const branchFilename = __dirname + '/application_env';
 const branchFilename = __dirname + '/application_env';
@@ -18,6 +18,6 @@ fs.accessSync(confFilename);
 
 
 const config = require(confFilename);
 const config = require(confFilename);
 
 
-utils.mkDirIfNotExistsSync(config.dataDir);
+//fs.ensureDirSync(config.dataDir);
 
 
 module.exports = config;
 module.exports = config;

+ 8 - 10
server/core/Logger.js

@@ -1,9 +1,7 @@
 /*
 /*
   Журналирование с буферизацией вывода
   Журналирование с буферизацией вывода
 */
 */
-
-const Promise = require('bluebird');
-const fs = Promise.promisifyAll(require('fs'));
+const fs = require('fs-extra');
 
 
 global.LM_OK = 0;
 global.LM_OK = 0;
 global.LM_INFO = 1;
 global.LM_INFO = 1;
@@ -120,25 +118,25 @@ class FileLog extends BaseLog {
         if (i > 0)
         if (i > 0)
             fn += `.${i}`;
             fn += `.${i}`;
         let tn = fileName + '.' + (i + 1);
         let tn = fileName + '.' + (i + 1);
-        let exists = await fs.accessAsync(tn).then(() => true).catch(() => false);
+        let exists = await fs.access(tn).then(() => true).catch(() => false);
         if (exists) {
         if (exists) {
             if (i >= LOG_ROTATE_FILE_DEPTH - 1) {
             if (i >= LOG_ROTATE_FILE_DEPTH - 1) {
-                await fs.unlinkAsync(tn);
+                await fs.unlink(tn);
             } else {
             } else {
                 await this.rotateFile(fileName, i + 1);
                 await this.rotateFile(fileName, i + 1);
             }
             }
         }
         }
-        await fs.renameAsync(fn, tn);
+        await fs.rename(fn, tn);
     }
     }
 
 
     async doFileRotationIfNeeded() {
     async doFileRotationIfNeeded() {
         this.rcid = 0;
         this.rcid = 0;
 
 
-        let stat = await fs.fstatAsync(this.fd);
+        let stat = await fs.fstat(this.fd);
         if (stat.size > LOG_ROTATE_FILE_LENGTH) {
         if (stat.size > LOG_ROTATE_FILE_LENGTH) {
-            await fs.closeAsync(this.fd);
+            await fs.close(this.fd);
             await this.rotateFile(this.fileName, 0);
             await this.rotateFile(this.fileName, 0);
-            this.fd = await fs.openAsync(this.fileName, "a");
+            this.fd = await fs.open(this.fileName, "a");
         }
         }
     }
     }
 
 
@@ -153,7 +151,7 @@ class FileLog extends BaseLog {
             }, LOG_ROTATE_FILE_CHECK_INTERVAL);
             }, LOG_ROTATE_FILE_CHECK_INTERVAL);
         };
         };
 
 
-        await fs.writeAsync(this.fd, Buffer.from(data.join('')));
+        await fs.write(this.fd, Buffer.from(data.join('')));
     }
     }
 
 
     flushImplSync(data) {
     flushImplSync(data) {

+ 2 - 1
server/core/SqliteConnectionPool.js

@@ -1,4 +1,5 @@
 const Promise = require('bluebird');
 const Promise = require('bluebird');
+const fs = require('fs-extra');
 const utils = require('./utils');
 const utils = require('./utils');
 const sqlite = require('sqlite');
 const sqlite = require('sqlite');
 
 
@@ -11,7 +12,7 @@ class SqliteConnectionPool {
     }
     }
 
 
     async init() {
     async init() {
-        utils.mkDirIfNotExistsSync(this.config.dataDir);
+        fs.ensureDirSync(this.config.dataDir);
         const dbFileName = this.config.dataDir + '/' + this.config.dbFileName;
         const dbFileName = this.config.dataDir + '/' + this.config.dbFileName;
 
 
         this.connections = [];
         this.connections = [];

+ 2 - 2
server/core/getLogger.js

@@ -1,4 +1,4 @@
-const utils = require('./utils');
+const fs = require('fs-extra');
 const Logger = require('./Logger');
 const Logger = require('./Logger');
 
 
 let logger = null;
 let logger = null;
@@ -10,7 +10,7 @@ function initLogger(config) {
     let loggerParams = null;
     let loggerParams = null;
 
 
     if (config.loggingEnabled) {
     if (config.loggingEnabled) {
-        utils.mkDirIfNotExistsSync(config.logDir);
+        fs.ensureDirSync(config.logDir);
         loggerParams = [
         loggerParams = [
             {log: 'ConsoleLog'},
             {log: 'ConsoleLog'},
             {log: 'FileLog', fileName: `${config.logDir}/${config.name}.log`},
             {log: 'FileLog', fileName: `${config.logDir}/${config.name}.log`},

+ 1 - 20
server/core/utils.js

@@ -1,28 +1,9 @@
 const Promise = require('bluebird');
 const Promise = require('bluebird');
-const fs = require('fs');
 
 
 function sleep(ms) {
 function sleep(ms) {
     return new Promise(resolve => setTimeout(resolve, ms));
     return new Promise(resolve => setTimeout(resolve, ms));
 }
 }
 
 
-function statPathSync(path) {
-    try {
-        return fs.statSync(path);
-    } catch (ex) {}
-    return false;
-}
-
-function mkDirIfNotExistsSync(path) {
-    let exists = statPathSync(path);
-    if (!exists) {
-        fs.mkdirSync(path, {recursive: true});
-    } else if (!exists.isDirectory()) {
-        throw new Error(`Not a directory: ${path}`);
-    }
-}
-
 module.exports = {
 module.exports = {
-    sleep,
-    statPathSync,
-    mkDirIfNotExistsSync,
+    sleep
 };
 };