mirror of
https://github.com/Kaelio/ktx.git
synced 2026-06-22 08:38:08 +02:00
Initial open-source release
This commit is contained in:
commit
1a42152e6f
1199 changed files with 257054 additions and 0 deletions
56
packages/cli/src/project-resolver.ts
Normal file
56
packages/cli/src/project-resolver.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import { existsSync } from 'node:fs';
|
||||
import { dirname, join, resolve } from 'node:path';
|
||||
|
||||
export interface KloProjectResolverOptions {
|
||||
explicitProjectDir?: string;
|
||||
env?: Partial<Pick<NodeJS.ProcessEnv, 'KLO_PROJECT_DIR'>>;
|
||||
cwd?: string;
|
||||
}
|
||||
|
||||
function nonEmptyValue(value: string | undefined): string | undefined {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? value : undefined;
|
||||
}
|
||||
|
||||
export function findNearestKloProjectDir(startDir = process.cwd()): string | undefined {
|
||||
let current = resolve(startDir);
|
||||
|
||||
while (true) {
|
||||
if (existsSync(join(current, 'klo.yaml'))) {
|
||||
return current;
|
||||
}
|
||||
|
||||
const parent = dirname(current);
|
||||
if (parent === current) {
|
||||
return undefined;
|
||||
}
|
||||
current = parent;
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveKloProjectDir(options: KloProjectResolverOptions = {}): string {
|
||||
const cwd = options.cwd ?? process.cwd();
|
||||
|
||||
if (options.explicitProjectDir !== undefined) {
|
||||
const explicit = nonEmptyValue(options.explicitProjectDir);
|
||||
if (!explicit) {
|
||||
throw new Error('--project-dir requires a value');
|
||||
}
|
||||
return resolve(cwd, explicit);
|
||||
}
|
||||
|
||||
const rawEnvProjectDir = options.env ? options.env.KLO_PROJECT_DIR : process.env.KLO_PROJECT_DIR;
|
||||
const envProjectDir = nonEmptyValue(rawEnvProjectDir);
|
||||
if (rawEnvProjectDir !== undefined && envProjectDir === undefined) {
|
||||
throw new Error('KLO_PROJECT_DIR must not be empty');
|
||||
}
|
||||
if (envProjectDir !== undefined) {
|
||||
return resolve(cwd, envProjectDir);
|
||||
}
|
||||
|
||||
const resolvedCwd = resolve(cwd);
|
||||
return findNearestKloProjectDir(resolvedCwd) ?? resolvedCwd;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue