Skip to content
Permalink
Browse files
fix(core): Path works on both Windows and *nix systems
  • Loading branch information
jan-molak committed Feb 5, 2019
1 parent 75bfe82 commit 5ebb30b0149c7dede566d8338c1c4deac9a7b2e7

Some generated files are not rendered by default. Learn more.

@@ -36,7 +36,8 @@
"mkdirp": "0.5.1",
"moment": "2.22.2",
"sanitize-filename": "1.6.1",
"tiny-types": "1.11.3"
"tiny-types": "1.11.3",
"upath": "^1.1.0"
},
"devDependencies": {
"@documentation/esdoc-template": "2.0.1-alpha.8",
@@ -1,5 +1,5 @@
import fs = require('fs');
import path = require('path');
import path = require('upath');
const { createFsFromVolume, Volume } = require('memfs'); // tslint:disable-line:no-var-requires Typings incorrectly assume the presence of "dom" lib

export interface DirectoryStructure {
@@ -7,6 +7,29 @@ import { expect } from '../expect';
/** @test {Path} */
describe ('Path', () => {

describe('when used across different operating systems', () => {
const linuxPath = new Path(`features/search/full-text.feature`),
windowsPath = new Path(`features\\search\\full-text.feature`);

it(`produces the same result no matter what path it's instantiated with`, () => {
expect(linuxPath).to.equal(windowsPath);
});

it(`exposes the normalised path string`, () => {
expect(linuxPath.value).to.equal(windowsPath.value);
expect(linuxPath.value).to.equal(`features/search/full-text.feature`);
});

it(`splits the same`, () => {
expect(linuxPath.split()).to.deep.equal([
'features',
'search',
'full-text.feature',
]);
expect(linuxPath.split()).to.deep.equal(windowsPath.split());
});
});

it('can be serialised and deserialised', () => {
const path = new Path('/home/jan/file.json');

@@ -1,8 +1,9 @@
import { ensure, isDefined, isGreaterThan, property, TinyType } from 'tiny-types';

import { posix } from 'path';
import path = require('upath');

export class Path extends TinyType {
private static readonly Separator = '/';
public readonly value: string;

static fromJSON = (v: string) => new Path(v);
@@ -11,22 +12,26 @@ export class Path extends TinyType {
super();
ensure(Path.name, value, isDefined(), property('length', isGreaterThan(0)));

this.value = posix.normalize(value);
this.value = path.normalize(value);
}

join(another: Path) {
return new Path(posix.join(this.value, another.value));
return new Path(path.join(this.value, another.value));
}

split(): string[] {
return this.value.split(Path.Separator);
}

resolve(another: Path) {
return new Path(posix.resolve(this.value, another.value));
return new Path(path.resolve(this.value, another.value));
}

directory() {
return new Path(posix.dirname(this.value));
return new Path(path.dirname(this.value));
}

basename(): string {
return posix.basename(this.value);
return path.basename(this.value);
}
}
@@ -109,8 +109,7 @@ export class Notifier {
const humanReadable = (text: string) => text.replace(/[_-]+/g, ' ');

const
separator = '/',
directories = notEmpty(feature.location.path.directory().value.split(separator)),
directories = notEmpty(feature.location.path.directory().split()),
featuresIndex = directories.indexOf('features'),
hierarchy = [ ...directories.slice(featuresIndex + 1), feature.name.value ] as string[];

0 comments on commit 5ebb30b

Please sign in to comment.