Skip to content

Commit

Permalink
fix(core): do not use joinPathFragments for generating files
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Aug 22, 2023
1 parent b309d83 commit b4c121b
Show file tree
Hide file tree
Showing 21 changed files with 42 additions and 24 deletions.
3 changes: 2 additions & 1 deletion docs/generated/devkit/joinPathFragments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion docs/generated/devkit/normalizePath.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/shared/recipes/generators/creating-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand Down
3 changes: 2 additions & 1 deletion packages/angular/src/generators/host/lib/add-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
Expand Down Expand Up @@ -274,7 +275,7 @@ export async function createWorkspaceFiles(tree: Tree): Promise<void> {

await jsInitGenerator(tree, { skipFormat: true });

generateFiles(tree, joinPathFragments(__dirname, '../files/root'), '.', {
generateFiles(tree, join(__dirname, '../files/root'), '.', {
tmpl: '',
dot: '.',
rootTsConfigPath: getRootTsConfigPathInTree(tree),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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: '',
});
Expand Down
3 changes: 2 additions & 1 deletion packages/expo/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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: '',
});
Expand Down
3 changes: 2 additions & 1 deletion packages/js/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
typescriptVersion,
} from '../../utils/versions';
import { InitSchema } from './schema';
import { join } from 'path';

async function getInstalledTypescriptVersion(
tree: Tree
Expand Down Expand Up @@ -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',
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/next/src/generators/custom-server/custom-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
updateProjectConfiguration,
} from '@nx/devkit';
import { CustomServerSchema } from './schema';
import { join } from 'path';

export async function customServerGenerator(
host: Tree,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/generators/setup-docker/setup-docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from '@nx/devkit';

import { SetUpDockerOptions } from './schema';
import { join } from 'path';

function normalizeOptions(
tree: Tree,
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions packages/nx/src/utils/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion packages/react-native/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: '',
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/generators/component/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/generators/hook/hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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: '',
});
Expand Down
3 changes: 2 additions & 1 deletion packages/react/src/generators/setup-ssr/setup-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,7 +37,7 @@ export async function setupTailwindGenerator(
return;
}

generateFiles(tree, joinPathFragments(__dirname, './files'), project.root, {
generateFiles(tree, join(__dirname, './files'), project.root, {
tmpl: '',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/generators/vitest/vitest-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from '../../utils/versions';

import { addTsLibDependencies } from '@nx/js';
import { join } from 'path';

export async function vitestGenerator(
tree: Tree,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion packages/workspace/src/generators/ci-workflow/ci-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
writeJson,
} from '@nx/devkit';
import { deduceDefaultBase } from '../../utilities/default-base';
import { join } from 'path';

export interface Schema {
name: string;
Expand All @@ -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);
}

Expand Down

0 comments on commit b4c121b

Please sign in to comment.