Skip to content

Commit

Permalink
fix error messages for formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
eeue56 committed Nov 25, 2023
1 parent 17f8ff2 commit 69a7d7a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 26 deletions.
22 changes: 2 additions & 20 deletions src/cli/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
string,
variableList,
} from "@eeue56/baner";
import { Err, Ok, Result } from "@eeue56/ts-core/build/main/lib/result";
import { Ok, Result } from "@eeue56/ts-core/build/main/lib/result";
import { spawnSync } from "child_process";
import * as chokidar from "chokidar";
import { promises } from "fs";
Expand All @@ -25,6 +25,7 @@ import {
ensureDirectoryExists,
fileExists,
getDerwFiles,
getFlatFiles,
suggestFileNames,
} from "./utils";

Expand Down Expand Up @@ -123,25 +124,6 @@ function filterBodyForName(module: ContextModule, name: string): Block[] {

export type ProcessedFiles = Record<string, ContextModule>;

async function getFlatFiles(
files: string[]
): Promise<Result<string, string[]>> {
const nestedFiles = await Promise.all(
files.map(async (file) => await getDerwFiles(file))
);
let returnedFiles: string[] = [ ];

for (const innerFiles of nestedFiles) {
if (innerFiles.kind === "Err") {
return Err(`Failed to find the file ${innerFiles.error}`);
} else {
returnedFiles = returnedFiles.concat(innerFiles.value);
}
}

return Ok(returnedFiles);
}

export async function compileFiles(
isInPackageDirectory: boolean,
argv: string[]
Expand Down
62 changes: 56 additions & 6 deletions src/cli/format.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { bothFlag, empty, help, longFlag, parse, parser } from "@eeue56/baner";
import { Ok, Result } from "@eeue56/ts-core/build/main/lib/result";
import * as chokidar from "chokidar";
import { readFile, writeFile } from "fs/promises";
import path from "path";
import { generate } from "../Generator";
import { parseWithContext } from "../parser";
import { contextModuleToModule } from "../types";
import { getDerwFiles } from "./utils";
import { getDerwFiles, getFlatFiles, suggestFileNames } from "./utils";

const formatParser = parser([
longFlag("watch", "Watch Derw files for changes", empty()),
Expand Down Expand Up @@ -51,10 +52,34 @@ export async function format(
return;
}

const isPackageDirectoryAndNoFilesPassed =
isInPackageDirectory && !program.flags.files.isPresent;

if (program.flags.watch.isPresent) {
console.log("Watching src...");

const files = await getDerwFiles("./src");
const maybeFiles: Result<string, string[]> =
isPackageDirectoryAndNoFilesPassed
? await getDerwFiles("./src")
: await getFlatFiles(
(program.flags.files.arguments as Ok<string[]>).value
);

if (maybeFiles.kind === "Err") {
const filesToFind = isPackageDirectoryAndNoFilesPassed
? [ "./src" ]
: (program.flags.files.arguments as Ok<string[]>).value;

for (const file of filesToFind) {
const suggestion = await suggestFileNames(file);
if (suggestion !== file) {
console.error(suggestion);
}
}
process.exit(1);
}

const files = maybeFiles.value;

for (const file of files) {
await formatFile(file);
Expand All @@ -79,15 +104,40 @@ export async function format(
}, 50);
}
} else if (event === "addDir") {
const files = await getDerwFiles(path);
for (const file of files) {
await formatFile(file);
const maybeFiles = await getDerwFiles(path);

if (maybeFiles.kind === "Ok") {
const files = maybeFiles.value;
for (const file of files) {
await formatFile(file);
}
}
}
}
);
} else {
const files = await getDerwFiles("./src");
const maybeFiles: Result<string, string[]> =
isPackageDirectoryAndNoFilesPassed
? await getDerwFiles("./src")
: await getFlatFiles(
(program.flags.files.arguments as Ok<string[]>).value
);

if (maybeFiles.kind === "Err") {
const filesToFind = isPackageDirectoryAndNoFilesPassed
? [ "./src" ]
: (program.flags.files.arguments as Ok<string[]>).value;

for (const file of filesToFind) {
const suggestion = await suggestFileNames(file);
if (suggestion !== file) {
console.error(suggestion);
}
}
process.exit(1);
}

const files = maybeFiles.value;

for (const file of files) {
await formatFile(file);
Expand Down
19 changes: 19 additions & 0 deletions src/cli/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ export async function getDerwFiles(
return Ok(files);
}

export async function getFlatFiles(
files: string[]
): Promise<Result<string, string[]>> {
const nestedFiles = await Promise.all(
files.map(async (file) => await getDerwFiles(file))
);
let returnedFiles: string[] = [ ];

for (const innerFiles of nestedFiles) {
if (innerFiles.kind === "Err") {
return Err(`Failed to find the file ${innerFiles.error}`);
} else {
returnedFiles = returnedFiles.concat(innerFiles.value);
}
}

return Ok(returnedFiles);
}

export async function suggestFileNames(fullPath: string): Promise<string> {
const dir = path.dirname(fullPath);
const files = await getDerwFiles(dir);
Expand Down

0 comments on commit 69a7d7a

Please sign in to comment.