From b4c121bb0a3652bd0ccb281218627da875662a13 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Mon, 21 Aug 2023 17:06:46 -0400 Subject: [PATCH] fix(core): do not use joinPathFragments for generating files --- docs/generated/devkit/joinPathFragments.md | 3 ++- docs/generated/devkit/normalizePath.md | 3 ++- docs/shared/recipes/generators/creating-files.md | 4 ++-- packages/angular/src/generators/host/lib/add-ssr.ts | 3 ++- .../angular/src/generators/ng-add/utilities/workspace.ts | 3 ++- .../angular/src/generators/setup-ssr/lib/generate-files.ts | 3 ++- packages/expo/src/generators/component/component.ts | 3 ++- packages/js/src/generators/init/init.ts | 3 ++- packages/next/src/generators/custom-server/custom-server.ts | 3 ++- packages/node/src/generators/setup-docker/setup-docker.ts | 3 ++- packages/nx/src/utils/path.ts | 6 ++++-- packages/react-native/src/generators/component/component.ts | 3 ++- .../react/src/generators/component-test/component-test.ts | 4 ++-- packages/react/src/generators/component/component.ts | 3 ++- packages/react/src/generators/hook/hook.ts | 3 ++- packages/react/src/generators/setup-ssr/setup-ssr.ts | 3 ++- .../react/src/generators/setup-tailwind/setup-tailwind.ts | 3 ++- .../src/generators/cypress-project/cypress-project.ts | 2 +- .../storybook/src/generators/migrate-7/helper-functions.ts | 2 +- packages/vite/src/generators/vitest/vitest-generator.ts | 3 ++- .../workspace/src/generators/ci-workflow/ci-workflow.ts | 3 ++- 21 files changed, 42 insertions(+), 24 deletions(-) diff --git a/docs/generated/devkit/joinPathFragments.md b/docs/generated/devkit/joinPathFragments.md index b4a849bb9c4d76..2539946f8a0ec7 100644 --- a/docs/generated/devkit/joinPathFragments.md +++ b/docs/generated/devkit/joinPathFragments.md @@ -2,7 +2,8 @@ ▸ **joinPathFragments**(`...fragments`): `string` -Normalized path fragments and joins them +Normalized path fragments and joins them. Use this when writing paths to config files. +This should not be used to read files on disk because of the removal of Windows drive letters. #### Parameters diff --git a/docs/generated/devkit/normalizePath.md b/docs/generated/devkit/normalizePath.md index 5625dc2e200ded..ef08d17d4dc0a9 100644 --- a/docs/generated/devkit/normalizePath.md +++ b/docs/generated/devkit/normalizePath.md @@ -2,7 +2,8 @@ ▸ **normalizePath**(`osSpecificPath`): `string` -Coverts an os specific path to a unix style path +Coverts an os specific path to a unix style path. Use this when writing paths to config files. +This should not be used to read files on disk because of the removal of Windows drive letters. #### Parameters diff --git a/docs/shared/recipes/generators/creating-files.md b/docs/shared/recipes/generators/creating-files.md index 151459a5597516..6bf24d45b29a08 100644 --- a/docs/shared/recipes/generators/creating-files.md +++ b/docs/shared/recipes/generators/creating-files.md @@ -120,7 +120,7 @@ function uppercase(val: string) { // later -generateFiles(tree, joinPathFragments(__dirname, './files'), libraryRoot, { +generateFiles(tree, join(__dirname, './files'), libraryRoot, { uppercase, name: schema.name, }); @@ -141,7 +141,7 @@ This is the short version. ```typescript // typescript file -generateFiles(tree, joinPathFragments(__dirname, './files'), libraryRoot, { +generateFiles(tree, join(__dirname, './files'), libraryRoot, { shortVersion: false, numRepetitions: 3, }); diff --git a/packages/angular/src/generators/host/lib/add-ssr.ts b/packages/angular/src/generators/host/lib/add-ssr.ts index 0fc06118ac358b..85c519cba85a9c 100644 --- a/packages/angular/src/generators/host/lib/add-ssr.ts +++ b/packages/angular/src/generators/host/lib/add-ssr.ts @@ -16,6 +16,7 @@ import { typesCorsVersion, typesExpressVersion, } from '../../../utils/versions'; +import { join } from 'path'; export async function addSsr(tree: Tree, options: Schema, appName: string) { let project = readProjectConfiguration(tree, appName); @@ -34,7 +35,7 @@ export async function addSsr(tree: Tree, options: Schema, appName: string) { "import('./src/main.server');" ); - generateFiles(tree, joinPathFragments(__dirname, '../files'), project.root, { + generateFiles(tree, join(__dirname, '../files'), project.root, { appName, standalone: options.standalone, tmpl: '', diff --git a/packages/angular/src/generators/ng-add/utilities/workspace.ts b/packages/angular/src/generators/ng-add/utilities/workspace.ts index 69a6985c2d4cb7..2c7e875dc7ebff 100644 --- a/packages/angular/src/generators/ng-add/utilities/workspace.ts +++ b/packages/angular/src/generators/ng-add/utilities/workspace.ts @@ -21,6 +21,7 @@ import { angularDevkitVersion, nxVersion } from '../../../utils/versions'; import type { ProjectMigrator } from '../migrators'; import type { GeneratorOptions } from '../schema'; import type { WorkspaceRootFileTypesInfo } from './types'; +import { join } from 'path'; export function validateWorkspace(tree: Tree): void { const errors: string[] = []; @@ -274,7 +275,7 @@ export async function createWorkspaceFiles(tree: Tree): Promise { await jsInitGenerator(tree, { skipFormat: true }); - generateFiles(tree, joinPathFragments(__dirname, '../files/root'), '.', { + generateFiles(tree, join(__dirname, '../files/root'), '.', { tmpl: '', dot: '.', rootTsConfigPath: getRootTsConfigPathInTree(tree), diff --git a/packages/angular/src/generators/setup-ssr/lib/generate-files.ts b/packages/angular/src/generators/setup-ssr/lib/generate-files.ts index f604094a898598..9aada2b97fcdbd 100644 --- a/packages/angular/src/generators/setup-ssr/lib/generate-files.ts +++ b/packages/angular/src/generators/setup-ssr/lib/generate-files.ts @@ -7,6 +7,7 @@ import { import { lt } from 'semver'; import { getInstalledAngularVersionInfo } from '../../utils/version-utils'; import type { Schema } from '../schema'; +import { join } from 'path'; export function generateSSRFiles(tree: Tree, schema: Schema) { const projectConfig = readProjectConfiguration(tree, schema.project); @@ -16,7 +17,7 @@ export function generateSSRFiles(tree: Tree, schema: Schema) { const pathToFiles = joinPathFragments(__dirname, '..', 'files'); - generateFiles(tree, joinPathFragments(pathToFiles, 'base'), projectRoot, { + generateFiles(tree, join(pathToFiles, 'base'), projectRoot, { ...schema, tpl: '', }); diff --git a/packages/expo/src/generators/component/component.ts b/packages/expo/src/generators/component/component.ts index fec25e59760e31..c7496a5a15caff 100644 --- a/packages/expo/src/generators/component/component.ts +++ b/packages/expo/src/generators/component/component.ts @@ -12,6 +12,7 @@ import { } from '@nx/devkit'; import { NormalizedSchema, normalizeOptions } from './lib/normalize-options'; import { addImport } from './lib/add-import'; +import {join} from 'path'; export async function expoComponentGenerator(host: Tree, schema: Schema) { const options = await normalizeOptions(host, schema); @@ -30,7 +31,7 @@ function createComponentFiles(host: Tree, options: NormalizedSchema) { options.directory ); - generateFiles(host, joinPathFragments(__dirname, './files'), componentDir, { + generateFiles(host, join(__dirname, './files'), componentDir, { ...options, tmpl: '', }); diff --git a/packages/js/src/generators/init/init.ts b/packages/js/src/generators/init/init.ts index cdac8d1d578275..b1287559a3d49c 100644 --- a/packages/js/src/generators/init/init.ts +++ b/packages/js/src/generators/init/init.ts @@ -23,6 +23,7 @@ import { typescriptVersion, } from '../../utils/versions'; import { InitSchema } from './schema'; +import { join } from 'path'; async function getInstalledTypescriptVersion( tree: Tree @@ -65,7 +66,7 @@ export async function initGenerator( const tasks: GeneratorCallback[] = []; // add tsconfig.base.json if (!getRootTsConfigFileName(tree)) { - generateFiles(tree, joinPathFragments(__dirname, './files'), '.', { + generateFiles(tree, join(__dirname, './files'), '.', { fileName: schema.tsConfigName ?? 'tsconfig.base.json', }); } diff --git a/packages/next/src/generators/custom-server/custom-server.ts b/packages/next/src/generators/custom-server/custom-server.ts index ec8fa015587e84..aa1fc1f3e71754 100644 --- a/packages/next/src/generators/custom-server/custom-server.ts +++ b/packages/next/src/generators/custom-server/custom-server.ts @@ -10,6 +10,7 @@ import { updateProjectConfiguration, } from '@nx/devkit'; import { CustomServerSchema } from './schema'; +import { join } from 'path'; export async function customServerGenerator( host: Tree, @@ -52,7 +53,7 @@ export async function customServerGenerator( return; } - generateFiles(host, joinPathFragments(__dirname, 'files'), project.root, { + generateFiles(host, join(__dirname, 'files'), project.root, { ...options, offsetFromRoot: offsetFromRoot(project.root), projectRoot: project.root, diff --git a/packages/node/src/generators/setup-docker/setup-docker.ts b/packages/node/src/generators/setup-docker/setup-docker.ts index cb99209198c6a7..d15d355437793f 100644 --- a/packages/node/src/generators/setup-docker/setup-docker.ts +++ b/packages/node/src/generators/setup-docker/setup-docker.ts @@ -13,6 +13,7 @@ import { } from '@nx/devkit'; import { SetUpDockerOptions } from './schema'; +import { join } from 'path'; function normalizeOptions( tree: Tree, @@ -39,7 +40,7 @@ function addDocker(tree: Tree, options: SetUpDockerOptions) { } else { const outputPath = project.targets[`${options.buildTarget}`]?.options.outputPath; - generateFiles(tree, joinPathFragments(__dirname, './files'), project.root, { + generateFiles(tree, join(__dirname, './files'), project.root, { tmpl: '', app: project.sourceRoot, buildLocation: outputPath, diff --git a/packages/nx/src/utils/path.ts b/packages/nx/src/utils/path.ts index df6ab83a7b22a6..1251c9fedf1885 100644 --- a/packages/nx/src/utils/path.ts +++ b/packages/nx/src/utils/path.ts @@ -5,14 +5,16 @@ function removeWindowsDriveLetter(osSpecificPath: string): string { } /** - * Coverts an os specific path to a unix style path + * Coverts an os specific path to a unix style path. Use this when writing paths to config files. + * This should not be used to read files on disk because of the removal of Windows drive letters. */ export function normalizePath(osSpecificPath: string): string { return removeWindowsDriveLetter(osSpecificPath).split('\\').join('/'); } /** - * Normalized path fragments and joins them + * Normalized path fragments and joins them. Use this when writing paths to config files. + * This should not be used to read files on disk because of the removal of Windows drive letters. */ export function joinPathFragments(...fragments: string[]): string { return normalizePath(path.join(...fragments)); diff --git a/packages/react-native/src/generators/component/component.ts b/packages/react-native/src/generators/component/component.ts index 2bfa9eb0cfc0c3..0ca810744f3461 100644 --- a/packages/react-native/src/generators/component/component.ts +++ b/packages/react-native/src/generators/component/component.ts @@ -12,6 +12,7 @@ import { import { NormalizedSchema, normalizeOptions } from './lib/normalize-options'; import { addImport } from './lib/add-import'; import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; +import { join } from 'path'; export async function reactNativeComponentGenerator( host: Tree, @@ -31,7 +32,7 @@ function createComponentFiles(host: Tree, options: NormalizedSchema) { options.directory ); - generateFiles(host, joinPathFragments(__dirname, './files'), componentDir, { + generateFiles(host, join(__dirname, './files'), componentDir, { ...options, tmpl: '', }); diff --git a/packages/react/src/generators/component-test/component-test.ts b/packages/react/src/generators/component-test/component-test.ts index ae783e94b63787..a7e462517b34b5 100644 --- a/packages/react/src/generators/component-test/component-test.ts +++ b/packages/react/src/generators/component-test/component-test.ts @@ -5,7 +5,7 @@ import { readProjectConfiguration, Tree, } from '@nx/devkit'; -import { basename, dirname, extname, relative } from 'path'; +import { basename, dirname, extname, join, relative } from 'path'; import { findExportDeclarationsForJsx, getComponentNode, @@ -100,7 +100,7 @@ function generateSpecsForComponents(tree: Tree, filePath: string) { const namedImportStatement = namedImports.length > 0 ? `, { ${namedImports} }` : ''; - generateFiles(tree, joinPathFragments(__dirname, 'files'), componentDir, { + generateFiles(tree, join(__dirname, 'files'), componentDir, { fileName, components, importStatement: defaultExport diff --git a/packages/react/src/generators/component/component.ts b/packages/react/src/generators/component/component.ts index aa4f203378c03c..48e14cb01eabd3 100644 --- a/packages/react/src/generators/component/component.ts +++ b/packages/react/src/generators/component/component.ts @@ -23,6 +23,7 @@ import { getComponentTests } from './get-component-tests'; import { NormalizedSchema } from './noramlized-schema'; import { Schema } from './schema'; import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; +import { join } from 'path'; export async function componentGenerator(host: Tree, schema: Schema) { const options = await normalizeOptions(host, schema); @@ -58,7 +59,7 @@ function createComponentFiles(host: Tree, options: NormalizedSchema) { ); const componentTests = getComponentTests(options); - generateFiles(host, joinPathFragments(__dirname, './files'), componentDir, { + generateFiles(host, join(__dirname, './files'), componentDir, { ...options, componentTests, inSourceVitestTests: getInSourceVitestTestsTemplate(componentTests), diff --git a/packages/react/src/generators/hook/hook.ts b/packages/react/src/generators/hook/hook.ts index 07110aface77b8..dff84d2fbe00c7 100644 --- a/packages/react/src/generators/hook/hook.ts +++ b/packages/react/src/generators/hook/hook.ts @@ -15,6 +15,7 @@ import { import { Schema } from './schema'; import { addImport } from '../../utils/ast-utils'; import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; +import { join } from 'path'; interface NormalizedSchema extends Schema { projectSourceRoot: string; @@ -38,7 +39,7 @@ function createFiles(host: Tree, options: NormalizedSchema) { options.directory ); - generateFiles(host, joinPathFragments(__dirname, './files'), hookDir, { + generateFiles(host, join(__dirname, './files'), hookDir, { ...options, tmpl: '', }); diff --git a/packages/react/src/generators/setup-ssr/setup-ssr.ts b/packages/react/src/generators/setup-ssr/setup-ssr.ts index 8c68eaa17133b5..655ca2734fa450 100644 --- a/packages/react/src/generators/setup-ssr/setup-ssr.ts +++ b/packages/react/src/generators/setup-ssr/setup-ssr.ts @@ -24,6 +24,7 @@ import { } from '../../utils/versions'; import { addStaticRouter } from '../../utils/ast-utils'; import { ensureTypescript } from '@nx/js/src/utils/typescript/ensure-typescript'; +import { join } from 'path'; let tsModule: typeof import('typescript'); @@ -192,7 +193,7 @@ export async function setupSsrGenerator(tree: Tree, options: Schema) { ]; } - generateFiles(tree, joinPathFragments(__dirname, 'files'), projectRoot, { + generateFiles(tree, join(__dirname, 'files'), projectRoot, { tmpl: '', extraInclude: options.extraInclude?.length > 0 diff --git a/packages/react/src/generators/setup-tailwind/setup-tailwind.ts b/packages/react/src/generators/setup-tailwind/setup-tailwind.ts index c27731e37ab77a..d7ee99fa75c0af 100644 --- a/packages/react/src/generators/setup-tailwind/setup-tailwind.ts +++ b/packages/react/src/generators/setup-tailwind/setup-tailwind.ts @@ -18,6 +18,7 @@ import { import type { SetupTailwindOptions } from './schema'; import { addTailwindStyleImports } from './lib/add-tailwind-style-imports'; import { updateProject } from './lib/update-project'; +import { join } from 'path'; export async function setupTailwindGenerator( tree: Tree, @@ -36,7 +37,7 @@ export async function setupTailwindGenerator( return; } - generateFiles(tree, joinPathFragments(__dirname, './files'), project.root, { + generateFiles(tree, join(__dirname, './files'), project.root, { tmpl: '', }); diff --git a/packages/storybook/src/generators/cypress-project/cypress-project.ts b/packages/storybook/src/generators/cypress-project/cypress-project.ts index 2e1861c6e6d098..40b2bacc57d9eb 100644 --- a/packages/storybook/src/generators/cypress-project/cypress-project.ts +++ b/packages/storybook/src/generators/cypress-project/cypress-project.ts @@ -108,7 +108,7 @@ function addBaseUrlToCypressConfig(tree: Tree, projectName: string) { } else if (tree.exists(cypressTs)) { // cypress >= v10 tree.delete(cypressTs); - generateFiles(tree, joinPathFragments(__dirname, 'files'), projectRoot, { + generateFiles(tree, join(__dirname, 'files'), projectRoot, { tpl: '', }); } diff --git a/packages/storybook/src/generators/migrate-7/helper-functions.ts b/packages/storybook/src/generators/migrate-7/helper-functions.ts index 42b8118a5df1d1..d8b8f21000efaa 100644 --- a/packages/storybook/src/generators/migrate-7/helper-functions.ts +++ b/packages/storybook/src/generators/migrate-7/helper-functions.ts @@ -686,7 +686,7 @@ export function logResult( color: 'green', }); - generateFiles(tree, joinPathFragments(__dirname, 'files'), '.', { + generateFiles(tree, join(__dirname, 'files'), '.', { tmpl: '', successfulProjects: Object.entries( migrationSummary?.successfulProjects diff --git a/packages/vite/src/generators/vitest/vitest-generator.ts b/packages/vite/src/generators/vitest/vitest-generator.ts index 0ff7d26f4d8f8a..92388d8d0aef6d 100644 --- a/packages/vite/src/generators/vitest/vitest-generator.ts +++ b/packages/vite/src/generators/vitest/vitest-generator.ts @@ -26,6 +26,7 @@ import { } from '../../utils/versions'; import { addTsLibDependencies } from '@nx/js'; +import { join } from 'path'; export async function vitestGenerator( tree: Tree, @@ -141,7 +142,7 @@ function createFiles( options: VitestGeneratorSchema, projectRoot: string ) { - generateFiles(tree, joinPathFragments(__dirname, 'files'), projectRoot, { + generateFiles(tree, join(__dirname, 'files'), projectRoot, { tmpl: '', ...options, projectRoot, diff --git a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts index 64617432a3fdb2..f492cd072a0db9 100644 --- a/packages/workspace/src/generators/ci-workflow/ci-workflow.ts +++ b/packages/workspace/src/generators/ci-workflow/ci-workflow.ts @@ -10,6 +10,7 @@ import { writeJson, } from '@nx/devkit'; import { deduceDefaultBase } from '../../utilities/default-base'; +import { join } from 'path'; export interface Schema { name: string; @@ -32,7 +33,7 @@ export async function ciWorkflowGenerator(host: Tree, schema: Schema) { writeJson(host, 'nx.json', appendOriginPrefix(nxJson)); } - generateFiles(host, joinPathFragments(__dirname, 'files', ci), '', options); + generateFiles(host, join(__dirname, 'files', ci), '', options); await formatFiles(host); }