Skip to content

Commit

Permalink
fix: perform recursive workdir detection in web cli too
Browse files Browse the repository at this point in the history
  • Loading branch information
thomvaill committed Dec 14, 2024
1 parent 9446079 commit 98c1046
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 42 deletions.
5 changes: 0 additions & 5 deletions packages/cli-common/src/Log4brainsConfigNotFound.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/cli-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./AppConsole";
export * from "./ConsoleCapturer";
export * from "./FailureExit";
export * from "./Log4brainsConfigNotFound";
22 changes: 2 additions & 20 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import fs from "fs";
import path from "path";
import commander from "commander";
import terminalLink from "terminal-link";
import { Log4brains, Log4brainsError } from "@log4brains/core";
import {
AppConsole,
FailureExit,
Log4brainsConfigNotFound
} from "@log4brains/cli-common";
import { AppConsole, FailureExit } from "@log4brains/cli-common";

import {
ListCommand,
Expand All @@ -19,22 +13,10 @@ import {
const templateExampleUrl =
"https://raw.githubusercontent.com/thomvaill/log4brains/stable/packages/init/assets/template.md";

function findRootFolder(cwd: string): string {
if (fs.existsSync(path.join(cwd, ".log4brains.yml"))) {
return cwd;
}
if (path.resolve(cwd) === "/") {
throw new Log4brainsConfigNotFound();
}
return findRootFolder(path.join(cwd, ".."));
}

let l4bInstance: Log4brains;
function getL4bInstance(): Log4brains {
if (!l4bInstance) {
l4bInstance = Log4brains.create(
findRootFolder(process.env.LOG4BRAINS_CWD || ".")
);
l4bInstance = Log4brains.createFromCwd(process.env.LOG4BRAINS_CWD || ".");
}
return l4bInstance;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ yarn add @log4brains/core
```typescript
import { Log4brains } from "@log4brains/core";

const l4b = Log4brains.create(process.cwd());
const l4b = Log4brains.createFromCwd(process.cwd());

// See the TypeDoc documentation (TODO: to deploy on GitHub pages) to see available API methods
```
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ import "./polyfills";
export * from "./infrastructure/api";
export * from "./infrastructure/file-watcher";
export { Log4brainsError } from "./domain";
export { Log4brainsConfig } from "./infrastructure/config";
export {
Log4brainsConfig,
Log4brainsConfigNotFoundError
} from "./infrastructure/config";
28 changes: 26 additions & 2 deletions packages/core/src/infrastructure/api/Log4brains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
GetAdrBySlugQuery
} from "@src/adr/application";
import { Log4brainsError } from "@src/domain";
import { buildConfigFromWorkdir, Log4brainsConfig } from "../config";
import {
buildConfigFromWorkdir,
Log4brainsConfig,
findWorkdirRecursive
} from "../config";
import { AdrDto, AdrDtoStatus } from "./types";
import { adrToDto } from "./transformers";
import { CommandBus, QueryBus } from "../buses";
Expand Down Expand Up @@ -205,10 +209,30 @@ export class Log4brains {
*
* @param workdir Path to working directory (ie. where ".log4brains.yml" is located)
*
* @throws {@link Log4brainsConfigNotFoundError}
* In case of missing config file.
*
* @throws {@link Log4brainsError}
* In case of missing or invalid config file.
* In case of invalid config file or other domain error.
*/
static create(workdir = "."): Log4brains {
return new Log4brains(buildConfigFromWorkdir(workdir), workdir);
}

/**
* Creates an instance of the Log4brains API from a working directory.
* The real working directory (ie. where ".log4brains.yml" is located) will be guessed looking in the parent directories.
*
* @param cwd Current working directory
*
* @throws {@link Log4brainsConfigNotFoundError}
* In case of missing config file.
*
* @throws {@link Log4brainsError}
* In case of invalid config file or other domain error.
*/
static createFromCwd(cwd = "."): Log4brains {
const workdir = findWorkdirRecursive(cwd);
return Log4brains.create(workdir);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Log4brainsError } from "@src/domain";

export class Log4brainsConfigNotFoundError extends Log4brainsError {
constructor() {
super("Impossible to find the .log4brains.yml config file");
}
}
22 changes: 18 additions & 4 deletions packages/core/src/infrastructure/config/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { deepFreeze } from "@src/utils";
import { Log4brainsError } from "@src/domain";
import { Log4brainsConfig, schema } from "./schema";
import { guessGitRepositoryConfig } from "./guessGitRepositoryConfig";
import { Log4brainsConfigNotFoundError } from "./Log4brainsConfigNotFoundError";

const configFilename = ".log4brains.yml";

Expand Down Expand Up @@ -56,10 +57,7 @@ export function buildConfigFromWorkdir(workdir = "."): Log4brainsConfig {
const workdirAbsolute = path.resolve(workdir);
const configPath = path.join(workdirAbsolute, configFilename);
if (!fs.existsSync(configPath)) {
throw new Log4brainsError(
`Impossible to find the ${configFilename} config file`,
`workdir: ${workdirAbsolute}`
);
throw new Log4brainsConfigNotFoundError();
}

try {
Expand All @@ -83,3 +81,19 @@ export function buildConfigFromWorkdir(workdir = "."): Log4brainsConfig {
);
}
}

export function findWorkdirRecursive(cwd = "."): string {
const cwdAbsolute = path.resolve(cwd);

if (fs.existsSync(path.join(cwdAbsolute, configFilename))) {
return cwdAbsolute;
}

const parsedPath = path.parse(cwdAbsolute);
if (parsedPath.dir === parsedPath.root) {
// we are at the filesystem root -> stop recursion
throw new Log4brainsConfigNotFoundError();
}

return findWorkdirRecursive(path.join(cwd, ".."));
}
1 change: 1 addition & 0 deletions packages/core/src/infrastructure/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./builders";
export { Log4brainsConfig, GitProvider, GitRepositoryConfig } from "./schema";
export * from "./Log4brainsConfigNotFoundError";
1 change: 1 addition & 0 deletions packages/global-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"dependencies": {
"@log4brains/cli": "1.0.2-alpha.4",
"@log4brains/cli-common": "1.0.2-alpha.4",
"@log4brains/core": "1.0.2-alpha.4",
"@log4brains/init": "1.0.2-alpha.4",
"@log4brains/web": "1.0.2-alpha.4",
"chalk": "^4.1.0",
Expand Down
9 changes: 3 additions & 6 deletions packages/global-cli/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import chalk from "chalk";
import {
AppConsole,
FailureExit,
Log4brainsConfigNotFound
} from "@log4brains/cli-common";
import { AppConsole, FailureExit } from "@log4brains/cli-common";
import { Log4brainsConfigNotFoundError } from "@log4brains/core";
import { createGlobalCli } from "./cli";

const debug = !!process.env.DEBUG;
Expand All @@ -19,7 +16,7 @@ function handleError(err: unknown): void {
process.exit(1);
}

if (err instanceof Log4brainsConfigNotFound) {
if (err instanceof Log4brainsConfigNotFoundError) {
appConsole.fatal(`Cannot find ${chalk.bold(".log4brains.yml")}`);
appConsole.printlnErr(
chalk.red(
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/lib/core-api/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export function getLog4brainsInstance(): Log4brains {
if (!instance) {
if (process.env.LOG4BRAINS_PHASE === "initial-build") {
// Noop instance during "next build" phase
instance = Log4brains.create(
instance = Log4brains.createFromCwd(
path.join(
getConfig().serverRuntimeConfig.PROJECT_ROOT,
"lib/core-api/noop"
)
);
} else {
instance = Log4brains.create(process.env.LOG4BRAINS_CWD || ".");
instance = Log4brains.createFromCwd(process.env.LOG4BRAINS_CWD || ".");
}
}
return instance;
Expand Down

0 comments on commit 98c1046

Please sign in to comment.