Skip to content

Commit cf6270d

Browse files
committedJan 19, 2021
fix: Accept absolute paths in CliRunner
1 parent 36761e8 commit cf6270d

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed
 

‎src/init/CliRunner.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { IComponentsManagerBuilderOptions, LogLevel } from 'componentsjs';
55
import { ComponentsManager } from 'componentsjs';
66
import yargs from 'yargs';
77
import { getLoggerFor } from '../logging/LogUtil';
8-
import { joinFilePath, ensureTrailingSlash } from '../util/PathUtil';
8+
import { joinFilePath, ensureTrailingSlash, absoluteFilePath } from '../util/PathUtil';
99
import type { Initializer } from './Initializer';
1010

1111
export class CliRunner {
@@ -71,7 +71,7 @@ export class CliRunner {
7171
*/
7272
protected resolveFilePath(cwdPath?: string | null, modulePath = ''): string {
7373
return typeof cwdPath === 'string' ?
74-
joinFilePath(process.cwd(), cwdPath) :
74+
absoluteFilePath(cwdPath) :
7575
joinFilePath(__dirname, '../../', modulePath);
7676
}
7777

‎src/util/PathUtil.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { posix } from 'path';
1+
import { posix, win32 } from 'path';
22
import type { ResourceIdentifier } from '../ldp/representation/ResourceIdentifier';
33

44
/**
@@ -13,7 +13,7 @@ function windowsToPosixPath(path: string): string {
1313
}
1414

1515
/**
16-
* Resolves relative segments in the path/
16+
* Resolves relative segments in the path.
1717
*
1818
* @param path - Path to check (POSIX or Windows).
1919
*
@@ -35,6 +35,26 @@ export function joinFilePath(basePath: string, ...paths: string[]): string {
3535
return posix.join(windowsToPosixPath(basePath), ...paths);
3636
}
3737

38+
/**
39+
* Resolves a path to its absolute form.
40+
* Absolute inputs will not be changed (except changing Windows to POSIX).
41+
* Relative inputs will be interpreted relative to process.cwd().
42+
*
43+
* @param path - Path to check (POSIX or Windows).
44+
*
45+
* @returns The potentially changed path (POSIX).
46+
*/
47+
export function absoluteFilePath(path: string): string {
48+
if (posix.isAbsolute(path)) {
49+
return path;
50+
}
51+
if (win32.isAbsolute(path)) {
52+
return windowsToPosixPath(path);
53+
}
54+
55+
return joinFilePath(process.cwd(), path);
56+
}
57+
3858
/**
3959
* Makes sure the input path has exactly 1 slash at the end.
4060
* Multiple slashes will get merged into one.

‎test/unit/init/CliRunner.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ describe('CliRunner', (): void => {
104104
'urn:solid-server:default:variable:loggingLevel': 'debug',
105105
'urn:solid-server:default:variable:podTemplateFolder': '/var/cwd/templates',
106106
'urn:solid-server:default:variable:port': 4000,
107-
'urn:solid-server:default:variable:rootFilePath': '/var/cwd/root',
107+
'urn:solid-server:default:variable:rootFilePath': '/root',
108108
'urn:solid-server:default:variable:sparqlEndpoint': 'http://localhost:5000/sparql',
109109
},
110110
},

‎test/unit/util/PathUtil.test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
absoluteFilePath,
23
decodeUriPathComponents,
34
encodeUriPathComponents,
45
ensureTrailingSlash,
@@ -8,7 +9,7 @@ import {
89
} from '../../../src/util/PathUtil';
910

1011
describe('PathUtil', (): void => {
11-
describe('normalizeFilePath', (): void => {
12+
describe('#normalizeFilePath', (): void => {
1213
it('normalizes POSIX paths.', async(): Promise<void> => {
1314
expect(normalizeFilePath('/foo/bar/../baz')).toEqual('/foo/baz');
1415
});
@@ -18,7 +19,7 @@ describe('PathUtil', (): void => {
1819
});
1920
});
2021

21-
describe('joinFilePath', (): void => {
22+
describe('#joinFilePath', (): void => {
2223
it('joins POSIX paths.', async(): Promise<void> => {
2324
expect(joinFilePath('/foo/bar/', '..', '/baz')).toEqual('/foo/baz');
2425
});
@@ -28,6 +29,20 @@ describe('PathUtil', (): void => {
2829
});
2930
});
3031

32+
describe('#absoluteFilePath', (): void => {
33+
it('does not change absolute posix paths.', async(): Promise<void> => {
34+
expect(absoluteFilePath('/foo/bar/')).toEqual('/foo/bar/');
35+
});
36+
37+
it('converts absolute win32 paths to posix paths.', async(): Promise<void> => {
38+
expect(absoluteFilePath('C:\\foo\\bar')).toEqual('C:/foo/bar');
39+
});
40+
41+
it('makes relative paths absolute.', async(): Promise<void> => {
42+
expect(absoluteFilePath('foo/bar/')).toEqual(joinFilePath(process.cwd(), 'foo/bar/'));
43+
});
44+
});
45+
3146
describe('#ensureTrailingSlash', (): void => {
3247
it('makes sure there is always exactly 1 slash.', async(): Promise<void> => {
3348
expect(ensureTrailingSlash('http://test.com')).toEqual('http://test.com/');

0 commit comments

Comments
 (0)
Please sign in to comment.