feat(ivy): add helper methods to AbsoluteFsPath (#29643)

PR Close #29643
This commit is contained in:
Pete Bacon Darwin 2019-04-28 20:47:57 +01:00 committed by Andrew Kushnir
parent 4a2405929c
commit 23152c37c8
1 changed files with 28 additions and 1 deletions

View File

@ -5,7 +5,7 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as path from 'path';
import * as ts from 'typescript';
import {isAbsolutePath, normalizeSeparators} from './util';
@ -62,6 +62,24 @@ export const AbsoluteFsPath = {
// ts.SourceFile paths are always absolute.
return sf.fileName as AbsoluteFsPath;
},
/**
* Wrapper around `path.dirname` that returns an absolute path.
*/
dirname: function(file: AbsoluteFsPath):
AbsoluteFsPath { return AbsoluteFsPath.fromUnchecked(path.dirname(file));},
/**
* Wrapper around `path.join` that returns an absolute path.
*/
join: function(basePath: AbsoluteFsPath, ...paths: string[]):
AbsoluteFsPath { return AbsoluteFsPath.fromUnchecked(path.posix.join(basePath, ...paths));},
/**
* Wrapper around `path.resolve` that returns an absolute paths.
*/
resolve: function(basePath: string, ...paths: string[]):
AbsoluteFsPath { return AbsoluteFsPath.from(path.resolve(basePath, ...paths));},
};
/**
@ -83,4 +101,13 @@ export const PathSegment = {
* Convert the path `str` to a `PathSegment`, while assuming that `str` is already normalized.
*/
fromUnchecked: function(str: string): PathSegment { return str as PathSegment;},
/**
* Wrapper around `path.relative` that returns a `PathSegment`.
*/
relative: function(from: AbsoluteFsPath, to: AbsoluteFsPath):
PathSegment { return PathSegment.fromFsPath(path.relative(from, to));},
basename: function(filePath: string, extension?: string):
PathSegment { return path.basename(filePath, extension) as PathSegment;}
};