79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
/**
|
|
* Global XO CLI config layout (`XO_CONFIG_DIR` or ~/.config/xo-cli/).
|
|
* User-provided paths (templates, invitation JSON) stay relative to cwd.
|
|
*/
|
|
|
|
import { existsSync, mkdirSync } from "node:fs";
|
|
import { homedir } from "node:os";
|
|
import { basename, isAbsolute, join, resolve } from "node:path";
|
|
|
|
/**
|
|
* Base config directory. Created on first access.
|
|
*/
|
|
export function getConfigDir(): string {
|
|
const dir =
|
|
process.env["XO_CONFIG_DIR"] || join(homedir(), ".config", "xo-cli");
|
|
mkdirSync(dir, { recursive: true });
|
|
return dir;
|
|
}
|
|
|
|
/**
|
|
* Directory for mnemonic wallet files (mnemonic-*).
|
|
*/
|
|
export function getMnemonicsDir(): string {
|
|
const dir = join(getConfigDir(), "mnemonics");
|
|
mkdirSync(dir, { recursive: true });
|
|
return dir;
|
|
}
|
|
|
|
/**
|
|
* Directory for engine DB and invitation storage SQLite files.
|
|
*/
|
|
export function getDataDir(): string {
|
|
const dir = join(getConfigDir(), "data");
|
|
mkdirSync(dir, { recursive: true });
|
|
return dir;
|
|
}
|
|
|
|
/**
|
|
* File storing CLI settings (JSON), including the last-used mnemonic reference.
|
|
*/
|
|
export function getSettingsPath(): string {
|
|
return join(getConfigDir(), ".wallet");
|
|
}
|
|
|
|
/**
|
|
* @deprecated Prefer {@link getSettingsPath}.
|
|
*/
|
|
export function getWalletConfigPath(): string {
|
|
return getSettingsPath();
|
|
}
|
|
|
|
/**
|
|
* Resolves a mnemonic reference to an absolute path.
|
|
* Order: absolute path if it exists → path relative to cwd → config mnemonics directory/<basename>.
|
|
*
|
|
* @param mnemonicRef - Path or basename (e.g. `mnemonic-nuclear`)
|
|
* @returns Absolute path to the mnemonic file
|
|
* @throws If no matching file exists
|
|
*/
|
|
export function resolveMnemonicFilePath(mnemonicRef: string): string {
|
|
if (isAbsolute(mnemonicRef) && existsSync(mnemonicRef)) {
|
|
return mnemonicRef;
|
|
}
|
|
|
|
const relativeToCwd = resolve(process.cwd(), mnemonicRef);
|
|
if (existsSync(relativeToCwd)) {
|
|
return relativeToCwd;
|
|
}
|
|
|
|
const inMnemonics = join(getMnemonicsDir(), basename(mnemonicRef));
|
|
if (existsSync(inMnemonics)) {
|
|
return inMnemonics;
|
|
}
|
|
|
|
throw new Error(
|
|
`Mnemonic file not found: ${mnemonicRef}. Run "xo-cli mnemonic list" to see available files.`,
|
|
);
|
|
}
|