diff --git a/src/cli/compile.ts b/src/cli/compile.ts index 71492de..d5b7678 100644 --- a/src/cli/compile.ts +++ b/src/cli/compile.ts @@ -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"; @@ -25,6 +25,7 @@ import { ensureDirectoryExists, fileExists, getDerwFiles, + getFlatFiles, suggestFileNames, } from "./utils"; @@ -123,25 +124,6 @@ function filterBodyForName(module: ContextModule, name: string): Block[] { export type ProcessedFiles = Record; -async function getFlatFiles( - files: string[] -): Promise> { - 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[] diff --git a/src/cli/format.ts b/src/cli/format.ts index 716b4aa..7c59887 100644 --- a/src/cli/format.ts +++ b/src/cli/format.ts @@ -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()), @@ -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 = + isPackageDirectoryAndNoFilesPassed + ? await getDerwFiles("./src") + : await getFlatFiles( + (program.flags.files.arguments as Ok).value + ); + + if (maybeFiles.kind === "Err") { + const filesToFind = isPackageDirectoryAndNoFilesPassed + ? [ "./src" ] + : (program.flags.files.arguments as Ok).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); @@ -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 = + isPackageDirectoryAndNoFilesPassed + ? await getDerwFiles("./src") + : await getFlatFiles( + (program.flags.files.arguments as Ok).value + ); + + if (maybeFiles.kind === "Err") { + const filesToFind = isPackageDirectoryAndNoFilesPassed + ? [ "./src" ] + : (program.flags.files.arguments as Ok).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); diff --git a/src/cli/utils.ts b/src/cli/utils.ts index 382e567..8967274 100644 --- a/src/cli/utils.ts +++ b/src/cli/utils.ts @@ -59,6 +59,25 @@ export async function getDerwFiles( return Ok(files); } +export async function getFlatFiles( + files: string[] +): Promise> { + 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 { const dir = path.dirname(fullPath); const files = await getDerwFiles(dir);