diff --git a/modules/effects/schematics-core/utility/find-module.ts b/modules/effects/schematics-core/utility/find-module.ts index ffb09be271..bdc442332d 100644 --- a/modules/effects/schematics-core/utility/find-module.ts +++ b/modules/effects/schematics-core/utility/find-module.ts @@ -5,7 +5,16 @@ * 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 { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + Path, + join, + normalize, + relative, + strings, + basename, + extname, + dirname, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; export interface ModuleOptions { @@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path { * Build a relative path from one file path to another file path. */ export function buildRelativePath(from: string, to: string): string { - from = normalize(from); - to = normalize(to); - - // Convert to arrays. - const fromParts = from.split('/'); - const toParts = to.split('/'); - - // Remove file names (preserving destination) - fromParts.pop(); - const toFileName = convertToTypeScriptFileName(toParts.pop()); - - const relativePath = relative( - normalize(fromParts.join('/')), - normalize(toParts.join('/')) - ); - let pathPrefix = ''; - - // Set the path prefix for same dir or child dir, parent dir starts with `..` - if (!relativePath) { - pathPrefix = '.'; - } else if (!relativePath.startsWith('.')) { - pathPrefix = `./`; - } - if (pathPrefix && !pathPrefix.endsWith('/')) { - pathPrefix += '/'; - } - - return toFileName - ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName - : pathPrefix + relativePath; + const { + path: fromPath, + filename: fromFileName, + directory: fromDirectory, + } = parsePath(from); + const { + path: toPath, + filename: toFileName, + directory: toDirectory, + } = parsePath(to); + const relativePath = relative(fromDirectory, toDirectory); + const fixedRelativePath = relativePath.startsWith('.') + ? relativePath + : `./${relativePath}`; + + return !toFileName || toFileName === 'index.ts' + ? fixedRelativePath + : `${ + fixedRelativePath.endsWith('/') + ? fixedRelativePath + : fixedRelativePath + '/' + }${convertToTypeScriptFileName(toFileName)}`; } +function parsePath(path: string) { + const pathNormalized = normalize(path) as Path; + const filename = extname(pathNormalized) ? basename(pathNormalized) : ''; + const directory = filename ? dirname(pathNormalized) : pathNormalized; + return { + path: pathNormalized, + filename, + directory, + }; +} /** * Strips the typescript extension and clears index filenames * foo.ts -> foo diff --git a/modules/entity/schematics-core/utility/find-module.ts b/modules/entity/schematics-core/utility/find-module.ts index ffb09be271..bdc442332d 100644 --- a/modules/entity/schematics-core/utility/find-module.ts +++ b/modules/entity/schematics-core/utility/find-module.ts @@ -5,7 +5,16 @@ * 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 { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + Path, + join, + normalize, + relative, + strings, + basename, + extname, + dirname, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; export interface ModuleOptions { @@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path { * Build a relative path from one file path to another file path. */ export function buildRelativePath(from: string, to: string): string { - from = normalize(from); - to = normalize(to); - - // Convert to arrays. - const fromParts = from.split('/'); - const toParts = to.split('/'); - - // Remove file names (preserving destination) - fromParts.pop(); - const toFileName = convertToTypeScriptFileName(toParts.pop()); - - const relativePath = relative( - normalize(fromParts.join('/')), - normalize(toParts.join('/')) - ); - let pathPrefix = ''; - - // Set the path prefix for same dir or child dir, parent dir starts with `..` - if (!relativePath) { - pathPrefix = '.'; - } else if (!relativePath.startsWith('.')) { - pathPrefix = `./`; - } - if (pathPrefix && !pathPrefix.endsWith('/')) { - pathPrefix += '/'; - } - - return toFileName - ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName - : pathPrefix + relativePath; + const { + path: fromPath, + filename: fromFileName, + directory: fromDirectory, + } = parsePath(from); + const { + path: toPath, + filename: toFileName, + directory: toDirectory, + } = parsePath(to); + const relativePath = relative(fromDirectory, toDirectory); + const fixedRelativePath = relativePath.startsWith('.') + ? relativePath + : `./${relativePath}`; + + return !toFileName || toFileName === 'index.ts' + ? fixedRelativePath + : `${ + fixedRelativePath.endsWith('/') + ? fixedRelativePath + : fixedRelativePath + '/' + }${convertToTypeScriptFileName(toFileName)}`; } +function parsePath(path: string) { + const pathNormalized = normalize(path) as Path; + const filename = extname(pathNormalized) ? basename(pathNormalized) : ''; + const directory = filename ? dirname(pathNormalized) : pathNormalized; + return { + path: pathNormalized, + filename, + directory, + }; +} /** * Strips the typescript extension and clears index filenames * foo.ts -> foo diff --git a/modules/router-store/schematics-core/utility/find-module.ts b/modules/router-store/schematics-core/utility/find-module.ts index ffb09be271..bdc442332d 100644 --- a/modules/router-store/schematics-core/utility/find-module.ts +++ b/modules/router-store/schematics-core/utility/find-module.ts @@ -5,7 +5,16 @@ * 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 { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + Path, + join, + normalize, + relative, + strings, + basename, + extname, + dirname, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; export interface ModuleOptions { @@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path { * Build a relative path from one file path to another file path. */ export function buildRelativePath(from: string, to: string): string { - from = normalize(from); - to = normalize(to); - - // Convert to arrays. - const fromParts = from.split('/'); - const toParts = to.split('/'); - - // Remove file names (preserving destination) - fromParts.pop(); - const toFileName = convertToTypeScriptFileName(toParts.pop()); - - const relativePath = relative( - normalize(fromParts.join('/')), - normalize(toParts.join('/')) - ); - let pathPrefix = ''; - - // Set the path prefix for same dir or child dir, parent dir starts with `..` - if (!relativePath) { - pathPrefix = '.'; - } else if (!relativePath.startsWith('.')) { - pathPrefix = `./`; - } - if (pathPrefix && !pathPrefix.endsWith('/')) { - pathPrefix += '/'; - } - - return toFileName - ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName - : pathPrefix + relativePath; + const { + path: fromPath, + filename: fromFileName, + directory: fromDirectory, + } = parsePath(from); + const { + path: toPath, + filename: toFileName, + directory: toDirectory, + } = parsePath(to); + const relativePath = relative(fromDirectory, toDirectory); + const fixedRelativePath = relativePath.startsWith('.') + ? relativePath + : `./${relativePath}`; + + return !toFileName || toFileName === 'index.ts' + ? fixedRelativePath + : `${ + fixedRelativePath.endsWith('/') + ? fixedRelativePath + : fixedRelativePath + '/' + }${convertToTypeScriptFileName(toFileName)}`; } +function parsePath(path: string) { + const pathNormalized = normalize(path) as Path; + const filename = extname(pathNormalized) ? basename(pathNormalized) : ''; + const directory = filename ? dirname(pathNormalized) : pathNormalized; + return { + path: pathNormalized, + filename, + directory, + }; +} /** * Strips the typescript extension and clears index filenames * foo.ts -> foo diff --git a/modules/schematics-core/utility/find-module.ts b/modules/schematics-core/utility/find-module.ts index ffb09be271..bdc442332d 100644 --- a/modules/schematics-core/utility/find-module.ts +++ b/modules/schematics-core/utility/find-module.ts @@ -5,7 +5,16 @@ * 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 { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + Path, + join, + normalize, + relative, + strings, + basename, + extname, + dirname, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; export interface ModuleOptions { @@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path { * Build a relative path from one file path to another file path. */ export function buildRelativePath(from: string, to: string): string { - from = normalize(from); - to = normalize(to); - - // Convert to arrays. - const fromParts = from.split('/'); - const toParts = to.split('/'); - - // Remove file names (preserving destination) - fromParts.pop(); - const toFileName = convertToTypeScriptFileName(toParts.pop()); - - const relativePath = relative( - normalize(fromParts.join('/')), - normalize(toParts.join('/')) - ); - let pathPrefix = ''; - - // Set the path prefix for same dir or child dir, parent dir starts with `..` - if (!relativePath) { - pathPrefix = '.'; - } else if (!relativePath.startsWith('.')) { - pathPrefix = `./`; - } - if (pathPrefix && !pathPrefix.endsWith('/')) { - pathPrefix += '/'; - } - - return toFileName - ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName - : pathPrefix + relativePath; + const { + path: fromPath, + filename: fromFileName, + directory: fromDirectory, + } = parsePath(from); + const { + path: toPath, + filename: toFileName, + directory: toDirectory, + } = parsePath(to); + const relativePath = relative(fromDirectory, toDirectory); + const fixedRelativePath = relativePath.startsWith('.') + ? relativePath + : `./${relativePath}`; + + return !toFileName || toFileName === 'index.ts' + ? fixedRelativePath + : `${ + fixedRelativePath.endsWith('/') + ? fixedRelativePath + : fixedRelativePath + '/' + }${convertToTypeScriptFileName(toFileName)}`; } +function parsePath(path: string) { + const pathNormalized = normalize(path) as Path; + const filename = extname(pathNormalized) ? basename(pathNormalized) : ''; + const directory = filename ? dirname(pathNormalized) : pathNormalized; + return { + path: pathNormalized, + filename, + directory, + }; +} /** * Strips the typescript extension and clears index filenames * foo.ts -> foo diff --git a/modules/schematics/schematics-core/utility/find-module.ts b/modules/schematics/schematics-core/utility/find-module.ts index ffb09be271..bdc442332d 100644 --- a/modules/schematics/schematics-core/utility/find-module.ts +++ b/modules/schematics/schematics-core/utility/find-module.ts @@ -5,7 +5,16 @@ * 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 { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + Path, + join, + normalize, + relative, + strings, + basename, + extname, + dirname, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; export interface ModuleOptions { @@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path { * Build a relative path from one file path to another file path. */ export function buildRelativePath(from: string, to: string): string { - from = normalize(from); - to = normalize(to); - - // Convert to arrays. - const fromParts = from.split('/'); - const toParts = to.split('/'); - - // Remove file names (preserving destination) - fromParts.pop(); - const toFileName = convertToTypeScriptFileName(toParts.pop()); - - const relativePath = relative( - normalize(fromParts.join('/')), - normalize(toParts.join('/')) - ); - let pathPrefix = ''; - - // Set the path prefix for same dir or child dir, parent dir starts with `..` - if (!relativePath) { - pathPrefix = '.'; - } else if (!relativePath.startsWith('.')) { - pathPrefix = `./`; - } - if (pathPrefix && !pathPrefix.endsWith('/')) { - pathPrefix += '/'; - } - - return toFileName - ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName - : pathPrefix + relativePath; + const { + path: fromPath, + filename: fromFileName, + directory: fromDirectory, + } = parsePath(from); + const { + path: toPath, + filename: toFileName, + directory: toDirectory, + } = parsePath(to); + const relativePath = relative(fromDirectory, toDirectory); + const fixedRelativePath = relativePath.startsWith('.') + ? relativePath + : `./${relativePath}`; + + return !toFileName || toFileName === 'index.ts' + ? fixedRelativePath + : `${ + fixedRelativePath.endsWith('/') + ? fixedRelativePath + : fixedRelativePath + '/' + }${convertToTypeScriptFileName(toFileName)}`; } +function parsePath(path: string) { + const pathNormalized = normalize(path) as Path; + const filename = extname(pathNormalized) ? basename(pathNormalized) : ''; + const directory = filename ? dirname(pathNormalized) : pathNormalized; + return { + path: pathNormalized, + filename, + directory, + }; +} /** * Strips the typescript extension and clears index filenames * foo.ts -> foo diff --git a/modules/schematics/src/store/index.spec.ts b/modules/schematics/src/store/index.spec.ts index b00713bb18..cac2ffdfa3 100644 --- a/modules/schematics/src/store/index.spec.ts +++ b/modules/schematics/src/store/index.spec.ts @@ -63,6 +63,18 @@ describe('Store Schematic', () => { ); }); + it('should import the environments correctly', () => { + const options = { ...defaultOptions, module: 'app.module.ts' }; + + const tree = schematicRunner.runSchematic('store', options, appTree); + const content = tree.readContent( + `${projectPath}/src/app/reducers/index.ts` + ); + expect(content).toMatch( + /import { environment } from '..\/..\/environments\/environment';/ + ); + }); + it('should fail if specified module does not exist', () => { const options = { ...defaultOptions, module: '/src/app/app.moduleXXX.ts' }; let thrownError: Error | null = null; diff --git a/modules/schematics/src/store/index.ts b/modules/schematics/src/store/index.ts index f7a2ffdded..9f547f39e7 100644 --- a/modules/schematics/src/store/index.ts +++ b/modules/schematics/src/store/index.ts @@ -12,6 +12,7 @@ import { template, url, } from '@angular-devkit/schematics'; +import { Path, dirname } from '@angular-devkit/core'; import * as ts from 'typescript'; import { stringUtils, @@ -53,9 +54,10 @@ function addImportToNgModule(options: StoreOptions): Rule { const statePath = `${options.path}/${options.statePath}`; const relativePath = buildRelativePath(modulePath, statePath); + const srcPath = dirname(options.path as Path); const environmentsPath = buildRelativePath( statePath, - `/${options.path}/environments/environment` + `${srcPath}/environments/environment` ); const storeNgModuleImport = addImportToModule( @@ -135,9 +137,10 @@ export default function(options: StoreOptions): Rule { options.path = parsedPath.path; const statePath = `/${options.path}/${options.statePath}/index.ts`; + const srcPath = dirname(options.path as Path); const environmentsPath = buildRelativePath( statePath, - `/${options.path}/environments/environment` + `${srcPath}/environments/environment` ); if (options.module) { diff --git a/modules/store-devtools/schematics-core/utility/find-module.ts b/modules/store-devtools/schematics-core/utility/find-module.ts index ffb09be271..bdc442332d 100644 --- a/modules/store-devtools/schematics-core/utility/find-module.ts +++ b/modules/store-devtools/schematics-core/utility/find-module.ts @@ -5,7 +5,16 @@ * 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 { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + Path, + join, + normalize, + relative, + strings, + basename, + extname, + dirname, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; export interface ModuleOptions { @@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path { * Build a relative path from one file path to another file path. */ export function buildRelativePath(from: string, to: string): string { - from = normalize(from); - to = normalize(to); - - // Convert to arrays. - const fromParts = from.split('/'); - const toParts = to.split('/'); - - // Remove file names (preserving destination) - fromParts.pop(); - const toFileName = convertToTypeScriptFileName(toParts.pop()); - - const relativePath = relative( - normalize(fromParts.join('/')), - normalize(toParts.join('/')) - ); - let pathPrefix = ''; - - // Set the path prefix for same dir or child dir, parent dir starts with `..` - if (!relativePath) { - pathPrefix = '.'; - } else if (!relativePath.startsWith('.')) { - pathPrefix = `./`; - } - if (pathPrefix && !pathPrefix.endsWith('/')) { - pathPrefix += '/'; - } - - return toFileName - ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName - : pathPrefix + relativePath; + const { + path: fromPath, + filename: fromFileName, + directory: fromDirectory, + } = parsePath(from); + const { + path: toPath, + filename: toFileName, + directory: toDirectory, + } = parsePath(to); + const relativePath = relative(fromDirectory, toDirectory); + const fixedRelativePath = relativePath.startsWith('.') + ? relativePath + : `./${relativePath}`; + + return !toFileName || toFileName === 'index.ts' + ? fixedRelativePath + : `${ + fixedRelativePath.endsWith('/') + ? fixedRelativePath + : fixedRelativePath + '/' + }${convertToTypeScriptFileName(toFileName)}`; } +function parsePath(path: string) { + const pathNormalized = normalize(path) as Path; + const filename = extname(pathNormalized) ? basename(pathNormalized) : ''; + const directory = filename ? dirname(pathNormalized) : pathNormalized; + return { + path: pathNormalized, + filename, + directory, + }; +} /** * Strips the typescript extension and clears index filenames * foo.ts -> foo diff --git a/modules/store/schematics-core/utility/find-module.ts b/modules/store/schematics-core/utility/find-module.ts index ffb09be271..bdc442332d 100644 --- a/modules/store/schematics-core/utility/find-module.ts +++ b/modules/store/schematics-core/utility/find-module.ts @@ -5,7 +5,16 @@ * 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 { Path, join, normalize, relative, strings } from '@angular-devkit/core'; +import { + Path, + join, + normalize, + relative, + strings, + basename, + extname, + dirname, +} from '@angular-devkit/core'; import { DirEntry, Tree } from '@angular-devkit/schematics'; export interface ModuleOptions { @@ -89,38 +98,40 @@ export function findModule(host: Tree, generateDir: string): Path { * Build a relative path from one file path to another file path. */ export function buildRelativePath(from: string, to: string): string { - from = normalize(from); - to = normalize(to); - - // Convert to arrays. - const fromParts = from.split('/'); - const toParts = to.split('/'); - - // Remove file names (preserving destination) - fromParts.pop(); - const toFileName = convertToTypeScriptFileName(toParts.pop()); - - const relativePath = relative( - normalize(fromParts.join('/')), - normalize(toParts.join('/')) - ); - let pathPrefix = ''; - - // Set the path prefix for same dir or child dir, parent dir starts with `..` - if (!relativePath) { - pathPrefix = '.'; - } else if (!relativePath.startsWith('.')) { - pathPrefix = `./`; - } - if (pathPrefix && !pathPrefix.endsWith('/')) { - pathPrefix += '/'; - } - - return toFileName - ? pathPrefix + (relativePath ? relativePath + '/' : '') + toFileName - : pathPrefix + relativePath; + const { + path: fromPath, + filename: fromFileName, + directory: fromDirectory, + } = parsePath(from); + const { + path: toPath, + filename: toFileName, + directory: toDirectory, + } = parsePath(to); + const relativePath = relative(fromDirectory, toDirectory); + const fixedRelativePath = relativePath.startsWith('.') + ? relativePath + : `./${relativePath}`; + + return !toFileName || toFileName === 'index.ts' + ? fixedRelativePath + : `${ + fixedRelativePath.endsWith('/') + ? fixedRelativePath + : fixedRelativePath + '/' + }${convertToTypeScriptFileName(toFileName)}`; } +function parsePath(path: string) { + const pathNormalized = normalize(path) as Path; + const filename = extname(pathNormalized) ? basename(pathNormalized) : ''; + const directory = filename ? dirname(pathNormalized) : pathNormalized; + return { + path: pathNormalized, + filename, + directory, + }; +} /** * Strips the typescript extension and clears index filenames * foo.ts -> foo