pr-sizewise
Version:
A CLI tool that measures and reports pull request sizes for GitHub and GitLab, helping teams maintain manageable code changes.
116 lines • 4.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConfigFile = createConfigFile;
exports.loadConfig = loadConfig;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const logger_1 = require("../utils/logger");
const index_1 = require("../index");
const logger = (0, logger_1.createDefaultLogger)();
/**
* Creates a configuration file in the appropriate directory
*/
function createConfigFile(platform, force = false) {
const targetDir = platform === 'github' ? '.github' : '.gitlab';
const configPath = path_1.default.join(process.cwd(), targetDir, 'sizewise.config.json');
// Ensure the target directory exists
const dirPath = path_1.default.dirname(configPath);
if (!fs_1.default.existsSync(dirPath)) {
fs_1.default.mkdirSync(dirPath, { recursive: true });
}
// Check if config already exists and force is not set
if (fs_1.default.existsSync(configPath) && !force) {
logger.warning(`Configuration file already exists at ${configPath}`);
logger.dim('Use --force to overwrite');
return;
}
// Read the example configuration file
const exampleConfigPath = path_1.default.join(__dirname, '..', '..', 'example.config.json');
let configContent;
try {
if (fs_1.default.existsSync(exampleConfigPath)) {
configContent = fs_1.default.readFileSync(exampleConfigPath, 'utf8');
}
else {
// Fallback to default config if example.config.json is not found
const configTemplate = {
...index_1.DEFAULT_CONFIG,
comment: {
...index_1.DEFAULT_CONFIG.comment,
enabled: true,
},
label: {
...index_1.DEFAULT_CONFIG.label,
enabled: true,
},
};
configContent = JSON.stringify(configTemplate, null, 2);
}
}
catch (error) {
logger.logError('Failed to read example configuration', error);
process.exit(1);
}
// Write the configuration file
try {
fs_1.default.writeFileSync(configPath, configContent);
if (force && fs_1.default.existsSync(configPath)) {
logger.success(`Successfully overwritten configuration file at ${configPath}`);
}
else {
logger.success(`Successfully created configuration file at ${configPath}`);
}
logger.dim('Edit this file to customize your size analysis thresholds and behavior.');
}
catch (error) {
logger.logError('Failed to create configuration file', error);
process.exit(1);
}
}
/**
* Loads configuration from file or returns default
*/
function loadConfig(configPath) {
let config = { ...index_1.DEFAULT_CONFIG };
const defaultConfigPath = path_1.default.resolve(process.cwd(), 'sizewise.config.json');
const gitlabConfigPath = path_1.default.resolve(process.cwd(), '.gitlab/sizewise.config.json');
const githubConfigPath = path_1.default.resolve(process.cwd(), '.github/sizewise.config.json');
const finalConfigPath = configPath ??
(fs_1.default.existsSync(defaultConfigPath) ? defaultConfigPath :
(fs_1.default.existsSync(gitlabConfigPath) ? gitlabConfigPath :
(fs_1.default.existsSync(githubConfigPath) ? githubConfigPath : undefined)));
if (finalConfigPath !== undefined) {
try {
const userConfig = JSON.parse(fs_1.default.readFileSync(finalConfigPath, 'utf8'));
config = {
...index_1.DEFAULT_CONFIG,
...userConfig,
thresholds: {
...index_1.DEFAULT_CONFIG.thresholds,
...userConfig.thresholds,
},
comment: {
...index_1.DEFAULT_CONFIG.comment,
...userConfig.comment,
},
label: {
...index_1.DEFAULT_CONFIG.label,
...userConfig.label,
},
logging: {
...index_1.DEFAULT_CONFIG.logging,
...userConfig.logging,
},
};
}
catch (error) {
logger.warning(`Failed to load config file: ${finalConfigPath}`);
logger.dim(`Using default configuration. Error: ${error}`);
}
}
return config;
}
//# sourceMappingURL=config.js.map