del-repos
Version:
A command-line tool for bulk deleting GitHub or Gitee repositories
81 lines (80 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasConfig = exports.saveToken = exports.getToken = exports.writeConfig = exports.readConfig = void 0;
const fs_1 = require("fs");
const path_1 = require("path");
const os_1 = require("os");
const CONFIG_DIR = (0, path_1.join)((0, os_1.homedir)(), '.del-repos');
const CONFIG_FILE = (0, path_1.join)(CONFIG_DIR, 'config.json');
/**
* 确保配置目录存在
*/
const ensureConfigDir = () => {
if (!(0, fs_1.existsSync)(CONFIG_DIR)) {
(0, fs_1.mkdirSync)(CONFIG_DIR, { recursive: true });
}
};
/**
* 读取配置文件
* @returns 配置对象
*/
const readConfig = () => {
if (!(0, fs_1.existsSync)(CONFIG_FILE)) {
return {};
}
try {
const configContent = (0, fs_1.readFileSync)(CONFIG_FILE, 'utf-8');
return JSON.parse(configContent);
}
catch (error) {
console.warn('Warning: Failed to read config file, using default config');
return {};
}
};
exports.readConfig = readConfig;
/**
* 写入配置文件
* @param config 配置对象
*/
const writeConfig = (config) => {
ensureConfigDir();
try {
(0, fs_1.writeFileSync)(CONFIG_FILE, JSON.stringify(config, null, 2), 'utf-8');
}
catch (error) {
console.error('Error: Failed to write config file');
throw error;
}
};
exports.writeConfig = writeConfig;
/**
* 获取平台对应的token
* @param platform 平台名称
* @returns token字符串或undefined
*/
const getToken = (platform) => {
const config = (0, exports.readConfig)();
const tokenKey = platform.toLowerCase() === 'github' ? 'github_token' : 'gitee_token';
return config[tokenKey];
};
exports.getToken = getToken;
/**
* 保存平台token
* @param platform 平台名称
* @param token token字符串
*/
const saveToken = (platform, token) => {
const config = (0, exports.readConfig)();
const tokenKey = platform.toLowerCase() === 'github' ? 'github_token' : 'gitee_token';
config[tokenKey] = token;
(0, exports.writeConfig)(config);
};
exports.saveToken = saveToken;
/**
* 检查是否存在配置文件
* @returns 是否存在配置文件
*/
const hasConfig = () => {
return (0, fs_1.existsSync)(CONFIG_FILE);
};
exports.hasConfig = hasConfig;