add install base

This commit is contained in:
2025-05-17 03:32:38 +08:00
parent 717e434ce0
commit 035ddc248c
28 changed files with 667 additions and 260 deletions

View File

@@ -3,7 +3,7 @@ import { homedir } from 'os';
import fs from 'fs';
import { checkFileExists, createDir } from '../file/index.ts';
import { ProxyInfo } from '../proxy/proxy.ts';
import dotenv from 'dotenv';
/**
* 助手配置文件路径, 全局配置文件目录
*/
@@ -18,7 +18,7 @@ export const initConfig = (configRootPath: string) => {
const configDir = createDir(path.join(configRootPath, 'assistant-app'));
const configPath = path.join(configDir, 'assistant-config.json');
const pageConfigPath = path.join(configDir, 'assistant-page-config.json');
const pageDir = createDir(path.join(configDir, 'page'));
const pagesDir = createDir(path.join(configDir, 'pages'));
const appsDir = createDir(path.join(configDir, 'apps'));
const appsConfigPath = path.join(configDir, 'assistant-apps-config.json');
const appPidPath = path.join(configDir, 'assistant-app.pid');
@@ -43,7 +43,7 @@ export const initConfig = (configRootPath: string) => {
/**
* 应用目录, 前端应用目录
*/
pageDir,
pagesDir,
/**
* 应用配置文件路径, assistant-page-config.json
*/
@@ -207,6 +207,20 @@ export class AssistantConfig {
getAppList() {
return this.getPageConfig().list;
}
getEnvConfig() {
const envConfigPath = this.configPath.envConfigPath;
if (!checkFileExists(envConfigPath)) {
return {};
}
const envConfig = dotenv.parse(fs.readFileSync(envConfigPath));
return envConfig;
}
setEnvConfig(config: string) {
const envConfigPath = this.configPath.envConfigPath;
fs.writeFileSync(envConfigPath, config);
const envConfig = dotenv.parse(fs.readFileSync(envConfigPath));
return envConfig;
}
/**
* process.env.ASSISTANT_CONFIG_DIR || process.cwd()
* configDir是助手配置文件目录文件内部包函
@@ -245,3 +259,58 @@ export class AssistantConfig {
type AppConfig = {
list: any[];
};
export function parseArgs(args: string[]) {
const result: { root?: string; home?: boolean; help?: boolean } = {};
for (let i = 0; i < args.length; i++) {
const arg = args[i];
// 处理 root 参数
if (arg === '--root') {
if (i + 1 < args.length && !args[i + 1].startsWith('-')) {
result.root = args[i + 1];
i++; // 跳过下一个参数,因为它是值
}
}
// 处理 home 参数
if (arg === '--home') {
result.home = true;
}
if (arg === '--help' || arg === '-h') {
result.help = true;
}
}
return result;
}
/**
* 手动解析命令行参数
* @param homedir
* @returns
*/
export const parseHomeArg = (homedir?: string) => {
const args = process.argv.slice(2);
const options = parseArgs(args);
let _configDir = undefined;
if (options.home && homedir) {
_configDir = homedir;
} else if (options.root) {
_configDir = options.root;
}
return {
options,
configDir: _configDir,
};
};
export const parseHelpArg = () => {
const args = process.argv.slice(2);
const options = parseArgs(args);
return !!options?.help;
};
export const parseIfJson = (content: string) => {
try {
return JSON.parse(content);
} catch (error) {
console.error('parseIfJson', error);
return {};
}
};

View File

@@ -34,6 +34,7 @@ export class HttpsPem {
return pemDir;
}
getCert() {
if (!this.assistantConfig.init) return;
const pemDir = this.getPemDir();
const pemPath = {
key: path.join(pemDir, 'https-private-key.pem'),

View File

@@ -1,19 +1,41 @@
import { Manager } from '@kevisual/local-app-manager/manager';
import { AssistantConfig } from '@/module/assistant/index.ts';
import type { AssistantConfig } from '@/module/assistant/index.ts';
import { parseIfJson } from '@/module/assistant/index.ts';
import path from 'node:path';
import fs from 'node:fs';
import glob from 'fast-glob';
export class AssistantApp extends Manager {
config: AssistantConfig;
pagesPath: string;
constructor(config: AssistantConfig) {
config.checkMounted();
const appsPath = config?.configPath?.appsDir || path.join(process.cwd(), 'apps');
const pagesPath = config?.configPath?.pagesDir || path.join(process.cwd(), 'pages');
const appsConfigPath = config.configPath?.appsConfigPath;
const configFimename = path.basename(appsConfigPath || '');
super({
appsPath,
configFilename: configFimename,
});
this.pagesPath = pagesPath;
this.config = config;
}
pageList() {
const pages = glob.sync('*/*/package.json', {
cwd: this.pagesPath,
onlyFiles: true,
});
const pagesParse = pages.map((page) => {
const [user, app] = page.split('/');
const contentStr = fs.readFileSync(path.join(this.pagesPath, page), 'utf-8');
const content = parseIfJson(contentStr);
return {
user,
app,
version: content?.version,
content,
};
});
return pagesParse;
}
}

View File

@@ -1,35 +1,6 @@
export class Logger {
level: string;
constructor(level: string) {
this.level = level;
}
info(message: string, data?: any, ...args: any[]) {
if (this.level === 'info') {
console.log(`INFO: ${message}`, data, ...args);
}
}
error(message: string, data?: any, ...args: any[]) {
if (this.level === 'error') {
console.error(`ERROR: ${message}`, data, ...args);
}
}
warn(message: string, data?: any, ...args: any[]) {
if (this.level === 'warn') {
console.warn(`WARN: ${message}`, data, ...args);
}
}
debug(message: string, data?: any, ...args: any[]) {
if (this.level === 'debug') {
console.debug(`DEBUG: ${message}`, data, ...args);
}
}
log(message: string, ...args: any[]) {
if (this.level === 'log') {
console.log(`LOG: ${message}`, ...args);
}
}
}
const logger = new Logger(process.env.LOG_LEVEL || 'info');
import { Logger } from '@kevisual/logger';
const level = process.env.LOG_LEVEL || 'info';
const logger = new Logger({ level: level as any });
export const console = {
log: logger.info,