init for deploy files
This commit is contained in:
33
src/module/get-config.ts
Normal file
33
src/module/get-config.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
const envisionPath = path.join(os.homedir(), '.config', 'envision');
|
||||
const configPath = path.join(os.homedir(), '.config', 'envision', 'config.json');
|
||||
|
||||
export const checkFileExists = (filePath: string) => {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.F_OK);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
export const getConfig = () => {
|
||||
if (!checkFileExists(envisionPath)) {
|
||||
fs.mkdirSync(envisionPath, { recursive: true });
|
||||
}
|
||||
if (checkFileExists(configPath)) {
|
||||
const config = fs.readFileSync(configPath, 'utf-8');
|
||||
try {
|
||||
return JSON.parse(config);
|
||||
} catch (e) {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
return {};
|
||||
};
|
||||
|
||||
export const writeConfig = (config: Record<string, any>) => {
|
||||
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
|
||||
};
|
||||
3
src/module/index.ts
Normal file
3
src/module/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './query.ts';
|
||||
|
||||
export * from './get-config.ts';
|
||||
18
src/module/query.ts
Normal file
18
src/module/query.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Query } from '@kevisual/query';
|
||||
import { getConfig } from './get-config.ts';
|
||||
const config = getConfig();
|
||||
export const baseURL = config?.baseURL || 'https://envision.xiongxiao.me';
|
||||
|
||||
export const query = new Query({
|
||||
url: `${baseURL}/api/router`,
|
||||
});
|
||||
|
||||
query.beforeRequest = async (config) => {
|
||||
if (config.headers) {
|
||||
const token = await getConfig()?.token;
|
||||
if (token) {
|
||||
config.headers['Authorization'] = 'Bearer ' + token;
|
||||
}
|
||||
}
|
||||
return config;
|
||||
};
|
||||
Reference in New Issue
Block a user