UNPKG

@capawesome/cli

Version:

The Capawesome Cloud Command Line Interface (CLI) to manage Live Updates and more.

40 lines (39 loc) 1.32 kB
import { getCodeFromUnknownError, UserError } from '../utils/error.js'; import { homedir } from 'node:os'; import { resolve } from 'node:path'; import { readUser, writeUser } from 'rc9'; class UserConfigImpl { file = '.capawesome'; read() { try { return readUser({ name: this.file }); } catch (error) { this.rethrow(error); } } write(config) { try { writeUser(config, { name: this.file }); } catch (error) { this.rethrow(error); } } rethrow(error) { const code = getCodeFromUnknownError(error); if (code === 'EACCES' || code === 'EPERM') { const path = error instanceof Error && 'path' in error && typeof error.path === 'string' ? error.path : this.getResolvedPath(); throw new UserError(`Permission denied accessing ${path}. The file may be owned by another user (e.g. from a previous run with sudo). ` + `Try running \`sudo chown $USER ${path}\` or \`rm ${path}\`, then retry.`); } throw error; } getResolvedPath() { return resolve(process.env.XDG_CONFIG_HOME || homedir(), this.file); } } const userConfig = new UserConfigImpl(); export default userConfig;