58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
#!/usr/bin/env node
|
|
// ponytail — shared configuration resolver
|
|
//
|
|
// Resolution order for default mode:
|
|
// 1. PONYTAIL_DEFAULT_MODE environment variable
|
|
// 2. Config file defaultMode field:
|
|
// - $XDG_CONFIG_HOME/ponytail/config.json (any platform, if set)
|
|
// - ~/.config/ponytail/config.json (macOS / Linux fallback)
|
|
// - %APPDATA%\ponytail\config.json (Windows fallback)
|
|
// 3. 'full'
|
|
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
|
|
const VALID_MODES = ['off', 'lite', 'full', 'ultra', 'review'];
|
|
|
|
function getConfigDir() {
|
|
if (process.env.XDG_CONFIG_HOME) {
|
|
return path.join(process.env.XDG_CONFIG_HOME, 'ponytail');
|
|
}
|
|
if (process.platform === 'win32') {
|
|
return path.join(
|
|
process.env.APPDATA || path.join(os.homedir(), 'AppData', 'Roaming'),
|
|
'ponytail'
|
|
);
|
|
}
|
|
return path.join(os.homedir(), '.config', 'ponytail');
|
|
}
|
|
|
|
function getConfigPath() {
|
|
return path.join(getConfigDir(), 'config.json');
|
|
}
|
|
|
|
function getDefaultMode() {
|
|
// 1. Environment variable (highest priority)
|
|
const envMode = process.env.PONYTAIL_DEFAULT_MODE;
|
|
if (envMode && VALID_MODES.includes(envMode.toLowerCase())) {
|
|
return envMode.toLowerCase();
|
|
}
|
|
|
|
// 2. Config file
|
|
try {
|
|
const configPath = getConfigPath();
|
|
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
|
if (config.defaultMode && VALID_MODES.includes(config.defaultMode.toLowerCase())) {
|
|
return config.defaultMode.toLowerCase();
|
|
}
|
|
} catch (e) {
|
|
// Config file doesn't exist or is invalid — fall through
|
|
}
|
|
|
|
// 3. Default
|
|
return 'full';
|
|
}
|
|
|
|
module.exports = { getDefaultMode, getConfigDir, getConfigPath, VALID_MODES };
|