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

@ -18,7 +18,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options ## Options
| Option | Type | Description | | Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. | | `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). | | `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. | | `--exclude` | string | Exclude certain projects from being processed. |
@ -27,6 +27,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
| `--help` | boolean | Show help. | | `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. | | `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). | | `--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. | | `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. | | `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. | | `--version` | boolean | Show version number. |

View File

@ -18,7 +18,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options ## Options
| Option | Type | Description | | Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. | | `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). | | `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. | | `--exclude` | string | Exclude certain projects from being processed. |
@ -27,6 +27,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
| `--help` | boolean | Show help. | | `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. | | `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). | | `--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. | | `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. | | `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. | | `--version` | boolean | Show version number. |

View File

@ -1,14 +1,16 @@
# Function: formatFiles # Function: formatFiles
**formatFiles**(`tree`): `Promise`\<`void`\> **formatFiles**(`tree`, `options?`): `Promise`\<`void`\>
Formats all the created or updated files using Prettier Formats all the created or updated files using Prettier
#### Parameters #### Parameters
| Name | Type | Description | | Name | Type | Description |
| :----- | :------------------------------------ | :------------------- | | :------------------------------ | :------------------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `tree` | [`Tree`](../../devkit/documents/Tree) | the file system tree | | `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 #### Returns

View File

@ -18,7 +18,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options ## Options
| Option | Type | Description | | Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. | | `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). | | `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. | | `--exclude` | string | Exclude certain projects from being processed. |
@ -27,6 +27,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
| `--help` | boolean | Show help. | | `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. | | `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). | | `--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. | | `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. | | `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. | | `--version` | boolean | Show version number. |

View File

@ -18,7 +18,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
## Options ## Options
| Option | Type | Description | | Option | Type | Description |
| ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------- | | ---------------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--all` | boolean | Format all projects. | | `--all` | boolean | Format all projects. |
| `--base` | string | Base of the current branch (usually main). | | `--base` | string | Base of the current branch (usually main). |
| `--exclude` | string | Exclude certain projects from being processed. | | `--exclude` | string | Exclude certain projects from being processed. |
@ -27,6 +27,7 @@ Install `nx` globally to invoke the command directly using `nx`, or use `npx nx`
| `--help` | boolean | Show help. | | `--help` | boolean | Show help. |
| `--libs-and-apps` | boolean | Format only libraries and applications files. | | `--libs-and-apps` | boolean | Format only libraries and applications files. |
| `--projects` | string | Projects to format (comma/space delimited). | | `--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. | | `--uncommitted` | boolean | Uncommitted changes. |
| `--untracked` | boolean | Untracked changes. | | `--untracked` | boolean | Untracked changes. |
| `--version` | boolean | Show version number. | | `--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 * Formats all the created or updated files using Prettier
* @param tree - the file system tree * @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; let prettier: typeof Prettier;
try { try {
prettier = await import('prettier'); prettier = await import('prettier');
} catch {} } catch {}
if (options.sortRootTsconfigPaths) {
sortTsConfig(tree); sortTsConfig(tree);
}
if (!prettier) return; if (!prettier) return;

View File

@ -39,6 +39,15 @@ function withFormatOptions(yargs: Argv): Argv {
type: 'string', type: 'string',
coerce: parseCSV, 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', { .option('all', {
describe: 'Format all projects.', describe: 'Format all projects.',
type: 'boolean', type: 'boolean',

View File

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

View File

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