fix: Accept absolute paths in CliRunner

This commit is contained in:
Joachim Van Herwegen
2021-01-19 10:22:21 +01:00
parent 36761e8124
commit cf6270d161
4 changed files with 42 additions and 7 deletions

View File

@@ -5,7 +5,7 @@ import type { IComponentsManagerBuilderOptions, LogLevel } from 'componentsjs';
import { ComponentsManager } from 'componentsjs';
import yargs from 'yargs';
import { getLoggerFor } from '../logging/LogUtil';
import { joinFilePath, ensureTrailingSlash } from '../util/PathUtil';
import { joinFilePath, ensureTrailingSlash, absoluteFilePath } from '../util/PathUtil';
import type { Initializer } from './Initializer';
export class CliRunner {
@@ -71,7 +71,7 @@ export class CliRunner {
*/
protected resolveFilePath(cwdPath?: string | null, modulePath = ''): string {
return typeof cwdPath === 'string' ?
joinFilePath(process.cwd(), cwdPath) :
absoluteFilePath(cwdPath) :
joinFilePath(__dirname, '../../', modulePath);
}

View File

@@ -1,4 +1,4 @@
import { posix } from 'path';
import { posix, win32 } from 'path';
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
/**
@@ -13,7 +13,7 @@ function windowsToPosixPath(path: string): string {
}
/**
* Resolves relative segments in the path/
* Resolves relative segments in the path.
*
* @param path - Path to check (POSIX or Windows).
*
@@ -35,6 +35,26 @@ export function joinFilePath(basePath: string, ...paths: string[]): string {
return posix.join(windowsToPosixPath(basePath), ...paths);
}
/**
* Resolves a path to its absolute form.
* Absolute inputs will not be changed (except changing Windows to POSIX).
* Relative inputs will be interpreted relative to process.cwd().
*
* @param path - Path to check (POSIX or Windows).
*
* @returns The potentially changed path (POSIX).
*/
export function absoluteFilePath(path: string): string {
if (posix.isAbsolute(path)) {
return path;
}
if (win32.isAbsolute(path)) {
return windowsToPosixPath(path);
}
return joinFilePath(process.cwd(), path);
}
/**
* Makes sure the input path has exactly 1 slash at the end.
* Multiple slashes will get merged into one.