Skip to content

Commit

Permalink
screener CLI inspect fixes (#529)
Browse files Browse the repository at this point in the history
* ensure dir and files exist fix

* changeset
  • Loading branch information
luketchang authored Oct 20, 2023
1 parent d115535 commit 026718d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-fireants-check.md
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions actors/deposit-screener/src/cli/commands/inspect/check.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -62,10 +61,11 @@ function showReasonCounts(
}

async function main(options: any): Promise<void> {
requireApiKeys();

const { snapshotJsonPath, outputDir, logDir, stdoutLogLevel } = options;
ensureDirectoriesExist(snapshotJsonPath, outputDir);
ensureExists(snapshotJsonPath, {
path: outputDir,
type: "DIRECTORY",
});

const logger = makeLogger(
logDir,
Expand Down
4 changes: 2 additions & 2 deletions actors/deposit-screener/src/cli/commands/inspect/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as JSON from "bigint-json-serialization";
import {
CachedAddressData,
dedupAddressesInOrder,
ensureDirectoriesExist,
ensureExists,
formDepositInfo,
getLocalRedis,
} from "./utils";
Expand Down Expand Up @@ -66,7 +66,7 @@ async function main(options: any): Promise<void> {
requireApiKeys();

const { inputCsv, outputData, logDir, stdoutLogLevel, delayMs } = options;
ensureDirectoriesExist(inputCsv, outputData);
ensureExists(inputCsv, { path: outputData, type: "FILE" });

const logger = makeLogger(
logDir,
Expand Down
45 changes: 33 additions & 12 deletions actors/deposit-screener/src/cli/commands/inspect/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiCallNames, ApiCallReturnData>
>;
Expand Down Expand Up @@ -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}`);
}
}

Expand Down

0 comments on commit 026718d

Please sign in to comment.