From da328aa9b706579d23cbacb447c4893de03b5e1d Mon Sep 17 00:00:00 2001 From: Chapman Pendery <35637443+cpendery@users.noreply.github.com> Date: Wed, 13 Dec 2023 12:29:29 -0800 Subject: [PATCH] fix: enable logging via the --verbose flag (#115) Signed-off-by: Chapman Pendery --- src/commands/root.ts | 4 ++++ src/index.ts | 1 + src/utils/log.ts | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/commands/root.ts b/src/commands/root.ts index d9adc45..41c4e01 100644 --- a/src/commands/root.ts +++ b/src/commands/root.ts @@ -6,14 +6,18 @@ import { Shell, supportedShells as shells, setupZshDotfiles } from "../utils/she import { inferShell } from "../utils/shell.js"; import { loadConfig } from "../utils/config.js"; import { Command } from "commander"; +import log from "../utils/log.js"; export const supportedShells = shells.join(", "); type RootCommandOptions = { shell: Shell | undefined; + verbose: boolean | undefined; }; export const action = (program: Command) => async (options: RootCommandOptions) => { + if (options.verbose) await log.enable(); + await loadConfig(program); const shell = options.shell ?? ((await inferShell()) as unknown as Shell | undefined); diff --git a/src/index.ts b/src/index.ts index 5c2389e..e8ee11a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,6 +20,7 @@ program .version(await getVersion(), "-v, --version", "output the current version") .action(action(program)) .option("-s, --shell ", `shell to use for command execution, supported shells: ${supportedShells}`) + .option("-V, --verbose", `enable verbose logging`) .showHelpAfterError("(add --help for additional information)"); program.addCommand(complete); diff --git a/src/utils/log.ts b/src/utils/log.ts index 5f7d5f5..ec32b4f 100644 --- a/src/utils/log.ts +++ b/src/utils/log.ts @@ -6,10 +6,14 @@ import path from "node:path"; import fs from "node:fs"; import fsAsync from "node:fs/promises"; -const logTarget = path.join(os.homedir(), ".inshellisense", "inshellisense.log"); -const logEnabled = false; +const logFolder = path.join(os.homedir(), ".inshellisense"); +const logTarget = path.join(logFolder, "inshellisense.log"); +let logEnabled = false; const reset = async () => { + if (!fs.existsSync(logTarget)) { + await fsAsync.mkdir(logFolder, { recursive: true }); + } await fsAsync.writeFile(logTarget, ""); }; @@ -24,4 +28,9 @@ const debug = (content: object) => { }); }; -export default { reset, debug }; +export const enable = async () => { + await reset(); + logEnabled = true; +}; + +export default { reset, debug, enable };