feat(core): allow disabling of tsconfig path sorting in format:write and formatFiles() (#28517)

This commit is contained in:
James Henry 2024-10-18 20:12:44 +04:00 committed by GitHub
parent 768e59e144
commit 59bb1c6b89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 88 additions and 59 deletions

View File

@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options
| Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |
| Option | Type | Description |
| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |

View File

@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options
| Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |
| Option | Type | Description |
| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |

View File

@ -1,14 +1,16 @@
# Function: formatFiles
**formatFiles**(`tree`): `Promise`\<`void`\>
**formatFiles**(`tree`, `options?`): `Promise`\<`void`\>
Formats all the created or updated files using Prettier
#### Parameters
| Name | Type | Description |
| :----- | :------------------------------------ | :------------------- |
| `tree` | [`Tree`](../../devkit/documents/Tree) | the file system tree |
| Name | Type | Description |
| :------------------------------ | :------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/documents/Tree) | the file system tree |
| `options?` | `Object` | - |
| `options.sortRootTsconfigPaths` | `boolean` | TODO(v21): Stop sorting tsconfig paths by default, paths are now less common/important in Nx workspace setups, and the sorting causes comments to be lost. |
#### Returns

View File

@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options
| Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |
| Option | Type | Description |
| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |

View File

@ -17,16 +17,17 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options
| Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |
| Option | Type | Description |
| ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. |
| `--files` | string | Change the way Nx is calculating the affected command by providing directly changed files, list of files delimited by commas or spaces. |
| `--head` | string | Latest commit of the current branch (usually HEAD). |
| `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). |
| `--sort-root-tsconfig-paths` | boolean | Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost. (Default: `true`) |
| `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. |

View File

@ -8,13 +8,24 @@ import { sortObjectByKeys } from 'nx/src/devkit-internals';
* Formats all the created or updated files using Prettier
* @param tree - the file system tree
*/
export async function formatFiles(tree: Tree): Promise<void> {
export async function formatFiles(
tree: Tree,
options = {
/**
* TODO(v21): Stop sorting tsconfig paths by default, paths are now less common/important
* in Nx workspace setups, and the sorting causes comments to be lost.
*/
sortRootTsconfigPaths: true,
}
): Promise<void> {
let prettier: typeof Prettier;
try {
prettier = await import('prettier');
} catch {}
sortTsConfig(tree);
if (options.sortRootTsconfigPaths) {
sortTsConfig(tree);
}
if (!prettier) return;

View File

@ -39,6 +39,15 @@ function withFormatOptions(yargs: Argv): Argv {
type: 'string',
coerce: parseCSV,
})
.option('sort-root-tsconfig-paths', {
describe: `Ensure the workspace's tsconfig compilerOptions.paths are sorted. Warning: This will cause comments in the tsconfig to be lost.`,
type: 'boolean',
/**
* TODO(v21): Stop sorting tsconfig paths by default, paths are now less common/important
* in Nx workspace setups, and the sorting causes comments to be lost.
*/
default: true,
})
.option('all', {
describe: 'Format all projects.',
type: 'boolean',

View File

@ -60,7 +60,9 @@ export async function format(
switch (command) {
case 'write':
sortTsConfig();
if (nxArgs.sortRootTsconfigPaths) {
sortTsConfig();
}
addRootConfigFiles(chunkList, nxArgs);
chunkList.forEach((chunk) => write(chunk));
break;

View File

@ -41,6 +41,7 @@ export interface NxArgs {
batch?: boolean;
excludeTaskDependencies?: boolean;
skipSync?: boolean;
sortRootTsconfigPaths?: boolean;
}
export function createOverrides(__overrides_unparsed__: string[] = []) {