diff --git a/.changeset/wise-fireants-check.md b/.changeset/wise-fireants-check.md new file mode 100644 index 000000000..5f8085bbc --- /dev/null +++ b/.changeset/wise-fireants-check.md @@ -0,0 +1,5 @@ +--- +"@nocturne-xyz/deposit-screener": patch +--- + +Fix bug in screener inspect CLI where output dir/files were not being correctly created before command run diff --git a/actors/deposit-screener/src/cli/commands/inspect/check.ts b/actors/deposit-screener/src/cli/commands/inspect/check.ts index 53734778f..a6582963c 100644 --- a/actors/deposit-screener/src/cli/commands/inspect/check.ts +++ b/actors/deposit-screener/src/cli/commands/inspect/check.ts @@ -1,14 +1,13 @@ import { makeLogger } from "@nocturne-xyz/offchain-utils"; import { Command } from "commander"; import fs from "fs"; -import { requireApiKeys } from "../../../utils"; import { RULESET_V1 } from "../../../screening/checks/v1/RULESET_V1"; import { isRejection } from "../../../screening/checks/RuleSet"; import { Logger } from "winston"; import { AddressDataSnapshot, dedupAddressesInOrder, - ensureDirectoriesExist, + ensureExists, formDepositInfo, getLocalRedis, populateRedisCache, @@ -62,10 +61,11 @@ function showReasonCounts( } async function main(options: any): Promise { - requireApiKeys(); - const { snapshotJsonPath, outputDir, logDir, stdoutLogLevel } = options; - ensureDirectoriesExist(snapshotJsonPath, outputDir); + ensureExists(snapshotJsonPath, { + path: outputDir, + type: "DIRECTORY", + }); const logger = makeLogger( logDir, diff --git a/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts b/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts index fd0031466..64a8dd1e5 100644 --- a/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts +++ b/actors/deposit-screener/src/cli/commands/inspect/snapshot.ts @@ -9,7 +9,7 @@ import * as JSON from "bigint-json-serialization"; import { CachedAddressData, dedupAddressesInOrder, - ensureDirectoriesExist, + ensureExists, formDepositInfo, getLocalRedis, } from "./utils"; @@ -66,7 +66,7 @@ async function main(options: any): Promise { requireApiKeys(); const { inputCsv, outputData, logDir, stdoutLogLevel, delayMs } = options; - ensureDirectoriesExist(inputCsv, outputData); + ensureExists(inputCsv, { path: outputData, type: "FILE" }); const logger = makeLogger( logDir, diff --git a/actors/deposit-screener/src/cli/commands/inspect/utils.ts b/actors/deposit-screener/src/cli/commands/inspect/utils.ts index ca60eb4ee..36dc60974 100644 --- a/actors/deposit-screener/src/cli/commands/inspect/utils.ts +++ b/actors/deposit-screener/src/cli/commands/inspect/utils.ts @@ -15,6 +15,11 @@ import { import fs from "fs"; import path from "path"; +export type OutputItem = { + path: string; + type: "FILE" | "DIRECTORY"; +}; + export type CachedAddressData = Partial< Record >; @@ -62,25 +67,41 @@ export function dedupAddressesInOrder(addresses: string[]): string[] { return dedupedAddresses; } -export function ensureDirectoriesExist( +export function ensureExists( inputPath: string, - outputPath: string + { path: outputPath, type }: OutputItem ): void { if (!fs.existsSync(inputPath)) { throw new Error(`Input file ${inputPath} does not exist`); } - // check that the dir where we are going to output to exists using the path library, if not, create it - const outputDir = path.dirname(outputPath); - if (!fs.existsSync(outputDir)) { - fs.mkdirSync(outputDir, { recursive: true }); - } + if (type === "DIRECTORY") { + // check that the dir where we are going to output to exists using the path library, if not, create it + if (!fs.existsSync(outputPath)) { + fs.mkdirSync(outputPath, { recursive: true }); + } - // check if we can write to the output directory - try { - fs.accessSync(outputDir, fs.constants.W_OK); - } catch (err) { - throw new Error(`Cannot write to output directory ${outputDir}`); + // check if we can write to the output directory + try { + fs.accessSync(outputPath, fs.constants.W_OK); + } catch (err) { + throw new Error(`Cannot write to output directory ${outputPath}`); + } + } else if (type === "FILE") { + // check that the directory where we are going to output to exists using the path library, if not, create it + const outputDir = path.dirname(outputPath); + if (!fs.existsSync(outputDir)) { + fs.mkdirSync(outputDir, { recursive: true }); + } + + // check if we can write to the output file + try { + fs.accessSync(outputDir, fs.constants.W_OK); + } catch (err) { + throw new Error(`Cannot write to output file ${outputPath}`); + } + } else { + throw new Error(`Invalid type parameter: ${type}`); } }