From 75ef20ce6ed62fd4c7b83ac1d8d4eca0e864dd19 Mon Sep 17 00:00:00 2001 From: Colum Ferry Date: Fri, 16 Dec 2022 10:48:11 +0000 Subject: [PATCH] fix(core): move generator should work if there are comments in tsconfig #13740 --- packages/workspace/package.json | 1 + .../move/lib/update-imports.spec.ts | 24 +++++++++++++++++++ .../src/generators/move/lib/update-imports.ts | 11 ++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/packages/workspace/package.json b/packages/workspace/package.json index a287487e6dc70..50c959940b7b8 100644 --- a/packages/workspace/package.json +++ b/packages/workspace/package.json @@ -82,6 +82,7 @@ "fs-extra": "^10.1.0", "glob": "7.1.4", "ignore": "^5.0.4", + "jsonc-parser": "3.2.0", "minimatch": "3.0.5", "npm-run-path": "^4.0.1", "open": "^8.4.0", diff --git a/packages/workspace/src/generators/move/lib/update-imports.spec.ts b/packages/workspace/src/generators/move/lib/update-imports.spec.ts index 1ec416eec5d05..18ec4ce6aaf5d 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.spec.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.spec.ts @@ -340,6 +340,30 @@ export MyExtendedClass extends MyClass {};` }); }); + it('should update project ref in the root tsconfig.json when there is a comment', async () => { + tree.rename('tsconfig.base.json', 'tsconfig.json'); + tree.write( + 'tsconfig.json', + `// A comment\n${tree.read('tsconfig.json', 'utf-8')}` + ); + await libraryGenerator(tree, { + name: 'my-source', + standaloneConfig: false, + }); + const projectConfig = readProjectConfiguration(tree, 'my-source'); + + updateImports( + tree, + normalizeSchema(tree, schema, projectConfig), + projectConfig + ); + + const tsConfig = readJson(tree, '/tsconfig.json'); + expect(tsConfig.compilerOptions.paths).toEqual({ + '@proj/my-destination': ['libs/my-destination/src/index.ts'], + }); + }); + it('should only update the project ref paths in the tsconfig file when --updateImportPath=false', async () => { await libraryGenerator(tree, { name: 'my-source', diff --git a/packages/workspace/src/generators/move/lib/update-imports.ts b/packages/workspace/src/generators/move/lib/update-imports.ts index eb935048ef082..b815a643fdbba 100644 --- a/packages/workspace/src/generators/move/lib/update-imports.ts +++ b/packages/workspace/src/generators/move/lib/update-imports.ts @@ -17,6 +17,7 @@ import { findNodes } from 'nx/src/utils/typescript'; import { NormalizedSchema } from '../schema'; import { normalizeSlashes } from './utils'; import { relative } from 'path'; +import { parse } from 'jsonc-parser'; /** * Updates all the imports in the workspace and modifies the tsconfig appropriately. @@ -42,7 +43,15 @@ export function updateImports( let tsConfig: any; let fromPath: string; if (tree.exists(tsConfigPath)) { - tsConfig = JSON.parse(tree.read(tsConfigPath).toString('utf-8')); + const tsConfigRawContents = tree.read(tsConfigPath).toString('utf-8'); + tsConfig = (() => { + try { + return JSON.parse(tsConfigRawContents); + } catch { + return parse(tsConfigRawContents); + } + })(); + fromPath = Object.keys(tsConfig.compilerOptions.paths).find((path) => tsConfig.compilerOptions.paths[path].some((x) => x.startsWith(project.sourceRoot)