优化存储代码和修改日志

This commit is contained in:
wallace5303
2021-02-03 12:26:17 +08:00
parent 61c95b1ce5
commit 94c0be126f
9 changed files with 30 additions and 43 deletions

20
app.js
View File

@@ -4,15 +4,6 @@
*/
'use strict';
global.CODE = require('./app/const/statusCode');
const fs = require('fs');
const path = require('path');
const lowdb = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const utils = require('./app/utils/utils');
const os = require('os');
const pkg = require('./package.json');
const storageDir = path.normalize(os.userInfo().homedir + '/' + pkg.name + '/');
const storageDb = 'db.json';
class AppBootHook {
constructor(app) {
@@ -42,17 +33,6 @@ class AppBootHook {
async didReady() {
// Worker is ready, can do some things
// don't need to block the app boot.
// 数据库
if (!fs.existsSync(storageDir)) {
utils.mkdir(storageDir);
utils.chmodPath(storageDir, '777');
}
const file = storageDir + storageDb;
const adapter = new FileSync(file);
const db = lowdb(adapter);
if (!db.has('default').value()) {
db.set('default', {}).write();
}
}
async serverDidReady() {

View File

@@ -9,7 +9,6 @@ const storageKey = require('../const/storageKey');
const fs = require('fs');
const os = require('os');
const pkg = require('../../package.json');
const storageDir = path.normalize(os.userInfo().homedir + '/' + pkg.name + '/');
const storageDb = 'db.json';
class StorageService extends BaseService {
@@ -18,7 +17,12 @@ class StorageService extends BaseService {
*/
instance(file = null) {
if (!file) {
file = path.normalize(storageDir + storageDb);
const storageDir = this.getStorageDir();
if (!fs.existsSync(storageDir)) {
utils.mkdir(storageDir);
utils.chmodPath(storageDir, '777');
}
file = path.normalize(storageDir + storageDb);
}
const isExist = fs.existsSync(file);
if (!isExist) {
@@ -47,6 +51,9 @@ class StorageService extends BaseService {
* getStorageDir
*/
getStorageDir() {
const userHomeDir = os.userInfo().homedir;
const storageDir = path.normalize(userHomeDir + '/' + pkg.name + '/');
return storageDir;
}

View File

@@ -1,6 +0,0 @@
'use strict';
// 本地环境-配置文件
exports.logger = {
dir: './logs/beta',
};

View File

@@ -1,6 +1,12 @@
'use strict';
// 本地环境-配置文件
// const path = require('path');
// const os = require('os');
// const pkg = require('../package.json');
// const storageDir = path.normalize(os.userInfo().homedir + '/' + pkg.name + '/');
const storageDir = require('../electron/storage').getStorageDir();
/*
* 远程调用
*/
@@ -8,5 +14,5 @@ exports.outApi = {
login: 'http://local.com/api/login',
};
exports.logger = {
dir: './logs/local',
dir: storageDir + 'logs/local',
};

View File

@@ -1,7 +0,0 @@
'use strict';
// 本地环境-配置文件
exports.logger = {
dir: './logs/preview',
};

View File

@@ -1,6 +1,8 @@
'use strict';
// 本地环境-配置文件
const storageDir = require('../electron/storage').getStorageDir();
/*
* 远程调用
*/
@@ -8,6 +10,6 @@ exports.outApi = {
login: 'http://api.local.com/api/login',
};
exports.logger = {
dir: './logs/prod',
dir: storageDir + 'logs/prod',
};

View File

@@ -24,7 +24,7 @@ const config = {
},
log: {
file: {
fileName: path.normalize('./logs/electron-' + dayjs().format('YYYY-MM-DD') + '.log'),
fileName: path.normalize(storage.getStorageDir() + 'logs/electron-' + dayjs().format('YYYY-MM-DD') + '.log'),
level: 'silly', // error, warn, info, verbose, debug, silly
format: '[{y}-{m}-{d} {h}:{i}:{s}.{ms}] [{level}] {text}',
maxSize: '1048576' // 1048576 (1mb) by default.

View File

@@ -9,11 +9,10 @@ const utils = require('../app/utils/utils');
const storageKey = require('../app/const/storageKey');
const os = require('os');
const pkg = require('../package.json');
const storageDir = path.normalize(os.userInfo().homedir + '/' + pkg.name + '/');
const storageDb = 'db.json';
exports.setup = function () {
// console.log('storageDir', storageDir);
const storageDir = this.getStorageDir();
if (!fs.existsSync(storageDir)) {
utils.mkdir(storageDir);
utils.chmodPath(storageDir, '777');
@@ -31,7 +30,8 @@ exports.setup = function () {
exports.instance = function (file = null) {
if (!file) {
file = path.normalize(storageDir + storageDb);
const storageDir = this.getStorageDir();
file = path.normalize(storageDir + storageDb);
}
const isExist = fs.existsSync(file);
if (!isExist) {
@@ -76,4 +76,11 @@ exports.setIpcDynamicPort = async function () {
return dynamicPort;
};
exports.getStorageDir = function () {
const userHomeDir = os.userInfo().homedir;
const storageDir = path.normalize(userHomeDir + '/' + pkg.name + '/');
return storageDir;
}
exports = module.exports;

2
storage/.gitignore vendored
View File

@@ -1,2 +0,0 @@
*
!.gitignore