fix(core): do not use joinPathFragments for generating files (#18753)

This commit is contained in:
Jonathan Cammisuli 2023-08-23 12:23:03 -04:00 committed by GitHub
parent 7ea37e3bd1
commit bbae14b9a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 42 additions and 24 deletions

View File

@ -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

View File

@ -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

View File

@ -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,
});

View File

@ -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: '',

View File

@ -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<void> {
await jsInitGenerator(tree, { skipFormat: true });
generateFiles(tree, joinPathFragments(__dirname, '../files/root'), '.', {
generateFiles(tree, join(__dirname, '../files/root'), '.', {
tmpl: '',
dot: '.',
rootTsConfigPath: getRootTsConfigPathInTree(tree),

View File

@ -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: '',
});

View File

@ -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: '',
});

View File

@ -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',
});
}

View File

@ -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,

View File

@ -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,

View File

@ -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));

View File

@ -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: '',
});

View File

@ -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

View File

@ -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),

View File

@ -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: '',
});

View File

@ -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

View File

@ -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: '',
});

View File

@ -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: '',
});
}

View File

@ -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

View File

@ -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,

View File

@ -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);
}