Fixes found during a full audit of the CLI: - auth logout: remote token revocation always failed with 401 because Forgejo only accepts Basic auth on the token endpoints. Logout now asks for (or accepts) the account password, supports --password, --password-file and --local-only, and clearly reports when the token is left active. - Silent password prompt actually echoed the password on a TTY: overriding rl.write does not suppress readline echo. Switched to the callback readline module and mute _writeToOutput instead (the readline/promises interface does not honor that hook). - Global --config flag was silently ignored: config paths were resolved at require time, before the preAction hook set STOKE_CONFIG_FILE. Paths are now resolved lazily on every access. - XDG_CONFIG_HOME handling put the config in $XDG_CONFIG_HOME/.config/stoke; per the XDG spec it now resolves to $XDG_CONFIG_HOME/stoke. - repo create: --auto-init defaulted to true with no way to disable it; added --no-auto-init. - repo import/import-batch: a GitHub token was required even for non-GitHub services (e.g. --service git), making those imports fail without gh/GITHUB_TOKEN. Tokens are now only auto-resolved for the github service; batch imports resolve per entry and memoize. - Branding leftovers: 'Run: forgejo auth login' hint and forgejo-cli/1.0.0 User-Agent now say stoke (UA tracks pkg.version). - Added request timeouts (30s default, 10m for migrations). - --limit and --team-id are validated as integers instead of silently misbehaving on garbage (NaN made -l show all results). New commands (per the repo's every-operation-becomes-a-command design): - stoke issue create (title/body/body-file/assignees) - stoke pr create (head/base/title/body/body-file) Tests and metadata: - New test suite on the built-in node:test runner (25 tests) covering config resolution/persistence, the API client with a mocked fetch, and end-to-end CLI behavior. npm test previously matched no files. - package.json: engines >=22.12.0 (required by commander@15 — the README claimed Node 18), repository, keywords, author; version 1.1.0. - README: corrected Node requirement, documented repo rename (was missing), issue create, pr create, logout options and revocation caveat, --no-auto-init, XDG behavior, import token rules, testing. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
68 lines
2 KiB
JavaScript
68 lines
2 KiB
JavaScript
const path = require('node:path');
|
|
const os = require('node:os');
|
|
const fs = require('node:fs');
|
|
|
|
const CONFIG_MODE = 0o600;
|
|
const DIR_MODE = 0o700;
|
|
|
|
// Paths are resolved lazily (on every call) so that the global `--config`
|
|
// flag — which sets STOKE_CONFIG_FILE from a preAction hook — takes effect
|
|
// even though this module is required before the CLI parses its arguments.
|
|
function getConfigDir() {
|
|
if (process.env.STOKE_CONFIG_DIR) return process.env.STOKE_CONFIG_DIR;
|
|
if (process.env.FORGEJO_CONFIG_DIR) return process.env.FORGEJO_CONFIG_DIR;
|
|
// Per the XDG spec, $XDG_CONFIG_HOME already points at the config root
|
|
// (it replaces ~/.config, it does not live inside it).
|
|
if (process.env.XDG_CONFIG_HOME) return path.join(process.env.XDG_CONFIG_HOME, 'stoke');
|
|
return path.join(os.homedir(), '.config', 'stoke');
|
|
}
|
|
|
|
function getConfigPath() {
|
|
return process.env.STOKE_CONFIG_FILE
|
|
|| process.env.FORGEJO_CONFIG_FILE
|
|
|| path.join(getConfigDir(), 'config.json');
|
|
}
|
|
|
|
function ensureConfigDir() {
|
|
fs.mkdirSync(path.dirname(getConfigPath()), { recursive: true, mode: DIR_MODE });
|
|
}
|
|
|
|
function loadConfig() {
|
|
const configPath = getConfigPath();
|
|
try {
|
|
const raw = fs.readFileSync(configPath, 'utf8');
|
|
return JSON.parse(raw);
|
|
} catch (err) {
|
|
if (err.code === 'ENOENT') return null;
|
|
throw new Error(`Failed to read config at ${configPath}: ${err.message}`);
|
|
}
|
|
}
|
|
|
|
function saveConfig(config) {
|
|
ensureConfigDir();
|
|
const configPath = getConfigPath();
|
|
const tmp = `${configPath}.tmp`;
|
|
fs.writeFileSync(tmp, JSON.stringify(config, null, 2), { mode: CONFIG_MODE });
|
|
fs.renameSync(tmp, configPath);
|
|
try {
|
|
fs.chmodSync(configPath, CONFIG_MODE);
|
|
} catch {
|
|
// ignore on platforms where chmod is unsupported
|
|
}
|
|
}
|
|
|
|
function clearConfig() {
|
|
try {
|
|
fs.unlinkSync(getConfigPath());
|
|
} catch (err) {
|
|
if (err.code !== 'ENOENT') throw err;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
getConfigDir,
|
|
getConfigPath,
|
|
loadConfig,
|
|
saveConfig,
|
|
clearConfig,
|
|
};
|