From 23152c37c8c29c89d899b7a0455a57a60854ccc2 Mon Sep 17 00:00:00 2001 From: Pete Bacon Darwin Date: Sun, 28 Apr 2019 20:47:57 +0100 Subject: [PATCH] feat(ivy): add helper methods to AbsoluteFsPath (#29643) PR Close #29643 --- .../compiler-cli/src/ngtsc/path/src/types.ts | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/compiler-cli/src/ngtsc/path/src/types.ts b/packages/compiler-cli/src/ngtsc/path/src/types.ts index 0e2188bf0e..6561d7e2fe 100644 --- a/packages/compiler-cli/src/ngtsc/path/src/types.ts +++ b/packages/compiler-cli/src/ngtsc/path/src/types.ts @@ -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;} };