Tests. Autocomplete. Few Fixes. Mocks for Electrum Service. Template-to-Json parser. Fix global paths. Use IO Dependency injection for logging from cli. Additional commands in CLI.
This commit is contained in:
70
src/utils/paths.ts
Normal file
70
src/utils/paths.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Global XO CLI config layout (XDG-style: ~/.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 = 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 the last-used mnemonic reference for `-m` omission.
|
||||
*/
|
||||
export function getWalletConfigPath(): string {
|
||||
return join(getConfigDir(), ".wallet");
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a mnemonic reference to an absolute path.
|
||||
* Order: absolute path if it exists → path relative to cwd → ~/.config/xo-cli/mnemonics/<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.`,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user