添加 kit 子目录
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* 系统配置
|
||||
*/
|
||||
const os = require('os');
|
||||
const process = require('process');
|
||||
const path = require("path");
|
||||
const fs = require('fs');
|
||||
|
||||
module.exports = class SystemConfig {
|
||||
|
||||
constructor() {
|
||||
}
|
||||
|
||||
static isWindows(){
|
||||
return os.platform() === 'win32'
|
||||
}
|
||||
|
||||
static isLinux(){
|
||||
return os.platform() === 'linux'
|
||||
}
|
||||
|
||||
static getShellEncode(){
|
||||
if ( this.isWindows() ) {
|
||||
return "GBK"
|
||||
} else {
|
||||
return "UTF-8"
|
||||
}
|
||||
}
|
||||
|
||||
static getLocalKitPath(){
|
||||
let path = "./"
|
||||
if ( this.isWindows() ) {
|
||||
path = require('path').dirname(require.main.filename)
|
||||
} else {
|
||||
path = process.execPath;
|
||||
}
|
||||
this.affirmDir(path)
|
||||
$logger.debug(`LocalKitPath=${path}`)
|
||||
return path;
|
||||
}
|
||||
|
||||
static getLocalStorePath(){
|
||||
const path = require("path")
|
||||
let dir = path.resolve(this.getLocalKitPath(), ".kit")
|
||||
this.affirmDir(dir)
|
||||
return dir;
|
||||
}
|
||||
|
||||
static getUserPath() {
|
||||
let userDirectory = '~';
|
||||
if ( this.isWindows() ) {
|
||||
userDirectory = process.env.USERPROFILE;
|
||||
}
|
||||
return userDirectory;
|
||||
}
|
||||
|
||||
static getUserConfigPath() {
|
||||
const path = require("path")
|
||||
const dir = path.resolve( this.getUserPath() , ".kit");
|
||||
this.affirmDir(dir)
|
||||
return dir;
|
||||
}
|
||||
|
||||
static getUserConfigFilePath() {
|
||||
const path = require("path")
|
||||
return path.resolve( this.getUserPath() , ".kit", "config.json");
|
||||
}
|
||||
|
||||
static getLogPath() {
|
||||
const path = require("path")
|
||||
let logs = path.resolve( this.getUserPath() , ".kit", "logs");
|
||||
this.affirmDir(logs)
|
||||
return logs
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保目录存在
|
||||
* @param path
|
||||
*/
|
||||
static affirmDir(path){
|
||||
// 检查文件夹是否存在
|
||||
const exists = fs.existsSync(path);
|
||||
|
||||
if (! exists) {
|
||||
// 文件夹不存在
|
||||
try {
|
||||
fs.mkdirSync(path, { recursive: true });
|
||||
} catch (err) {
|
||||
// 处理错误
|
||||
log.error(`affirmDir ${path} error ${ err }`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* 读写用户配置
|
||||
* 实时维护状态
|
||||
*/
|
||||
const fs = require("fs")
|
||||
const JSON5 = require('json5')
|
||||
module.exports = class UserConfig {
|
||||
constructor(path) {
|
||||
this.path = path
|
||||
this.init()
|
||||
}
|
||||
|
||||
init(){
|
||||
let txt = "{}";
|
||||
try {
|
||||
txt = fs.readFileSync(this.path, 'utf8')
|
||||
} catch ( err ) {
|
||||
if (err.code === 'ENOENT') {
|
||||
// File does not exist.
|
||||
txt = '{}';
|
||||
} else {
|
||||
// Some other error occurred. Throw it.
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this.configs = JSON5.parse( txt )
|
||||
this.setDefault("man", "https://flowus.cn/fullstack/share/acd5b26f-b3d1-4d41-a58e-e4f23daf9db8")
|
||||
|
||||
}
|
||||
|
||||
get(key) {
|
||||
return this.configs[key]
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
this.configs[key] = value
|
||||
this.write()
|
||||
}
|
||||
|
||||
setDefault(key, value){
|
||||
if ( ! this.configs[key] ) {
|
||||
this.configs[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
write(){
|
||||
fs.writeFileSync(this.path, JSON5.stringify(this.configs, undefined, 4))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* config 模块处理配置相关事务
|
||||
*/
|
||||
const SystemConfig = require("./SystemConfig")
|
||||
const UserConfig = require("./UserConfig")
|
||||
|
||||
|
||||
module.exports = {
|
||||
SystemConfig, UserConfig
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user