diff --git a/README.md b/README.md index e6b4f7c..55c89d4 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ npx update-ts-references --help --check Checks if updates would be necessary (without applying them) --help Show help --createTsConfig Create default TS configs for packages where the main entry in the package.json have a ts|tsx extension (Note: respects the --configName parameter) - -createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src" + --createPathMappings Create paths mappings under compilerOptions for a better IDE support. It respects the rootDir if no rootDir available it falls back to "src" --cwd Set working directory. Default: /Users/john-doe/projects/my-project --verbose Show verbose output. Default: false @@ -68,7 +68,7 @@ The output for the created file looks like the following ``` ## using --createPathMappings -will create path mappings under `compilerOptions` for a better IDE support. It respects the `rootDir` if no `rootDir` available it falls back to `src` +will create path mappings under `compilerOptions` for a better IDE support. It assumes the source files are under `src`. ```json { diff --git a/package.json b/package.json index 3b4c78c..21f9607 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "update-ts-references", - "version": "3.1.0", + "version": "3.2.0", "description": "Updates TypeScript references automatically while using workspaces", "bin": "src/index.js", "scripts": { diff --git a/src/update-ts-references.js b/src/update-ts-references.js index da9c0fb..58fa022 100644 --- a/src/update-ts-references.js +++ b/src/update-ts-references.js @@ -161,17 +161,17 @@ const getReferencesFromDependencies = ( const ensurePosixPathStyle = (reference) => ({ ...reference, path: reference.path.split(path.sep).join(path.posix.sep), - folder: reference.folder.split(path.sep).join(path.posix.sep), + folder: reference.folder?.split(path.sep).join(path.posix.sep), }); const updateTsConfig = ( configName, - win32OrPosixReferences, + references, + paths, check, createPathMappings = false, {packageDir} = {packageDir: process.cwd()}, ) => { - const references = (win32OrPosixReferences || []).map(ensurePosixPathStyle); const tsconfigFilePath = path.join(packageDir, configName); try { @@ -195,12 +195,9 @@ const updateTsConfig = ( if (check === false) { const compilerOptions = config?.compilerOptions ?? {}; - if (createPathMappings) + if (createPathMappings && paths && Object.keys(paths).length > 0) assign(compilerOptions, { - paths: references.reduce((paths, ref) => ({ - ...paths, - [`${ref.name}`]: [`${ref.folder}/${config.compilerOptions?.rootDir ?? 'src'}`] - }), {}) + paths }) const newTsConfig = assign(config, @@ -219,6 +216,13 @@ const updateTsConfig = ( } }; +function getPathsFromReferences(references) { + return references.reduce((paths, ref) => ({ + ...paths, + [`${ref.name}`]: [`${ref.folder}/src`] + }), {}); +} + const execute = async ({ cwd, createTsConfig, verbose, @@ -286,8 +290,8 @@ const execute = async ({ console.log('packagesMap', packagesMap); } - const rootReferences = []; - + let rootReferences = []; + let rootPaths = []; packagesMap.forEach((packageEntry, packageName) => { const detectedConfig = detectTSConfig(packageEntry.packageDir, configName, packageEntry.hasTsEntry && createTsConfig, cwd) @@ -297,19 +301,25 @@ const execute = async ({ path: path.join(path.relative(cwd, packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''), folder: path.relative(cwd, packageEntry.packageDir), }); - const references = getReferencesFromDependencies( + const references = (getReferencesFromDependencies( configName, packageEntry, packageName, packagesMap, verbose - ); + ) || []).map(ensurePosixPathStyle); + + const paths = getPathsFromReferences(references) + if (verbose) { console.log(`references of ${packageName}`, references); + console.log(`paths of ${packageName}`, paths); } + changesCount += updateTsConfig( detectedConfig, references, + paths, check, createPathMappings, packageEntry @@ -317,16 +327,26 @@ const execute = async ({ } else { // eslint-disable-next-line no-console console.log(`NO ${configName === TSCONFIG_JSON ? configName : `${configName} nor ${TSCONFIG_JSON}`} for ${packageName}`); + rootPaths.push({ + name: packageName, + path: path.relative(cwd, packageEntry.packageDir), + folder: path.relative(cwd, packageEntry.packageDir), + }); } }); + rootReferences = (rootReferences || []).map(ensurePosixPathStyle); + rootPaths = getPathsFromReferences((rootPaths || []).map(ensurePosixPathStyle)) + if (verbose) { console.log('rootReferences', rootReferences); + console.log('rootPaths', rootPaths); } changesCount += updateTsConfig( rootConfigName, rootReferences, - check, false, {packageDir: cwd} + rootPaths, + check, createPathMappings, {packageDir: cwd} ); if (verbose) { diff --git a/test-scenarios/ts-paths/utils/foos/foo-a/tsconfig.json b/test-scenarios/ts-paths/utils/foos/foo-a/tsconfig.json index aa3d6e5..1dd8e13 100644 --- a/test-scenarios/ts-paths/utils/foos/foo-a/tsconfig.json +++ b/test-scenarios/ts-paths/utils/foos/foo-a/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { "outDir": "dist", - "rootDir": "src2" + "rootDir": "src" } } diff --git a/test-scenarios/ts-paths/utils/foos/foo-c/package.json b/test-scenarios/ts-paths/utils/foos/foo-c/package.json new file mode 100644 index 0000000..d751623 --- /dev/null +++ b/test-scenarios/ts-paths/utils/foos/foo-c/package.json @@ -0,0 +1,6 @@ +{ + "name": "foo-c", + "version": "1.0.0", + "dependencies": { + } +} diff --git a/tests/update-ts-references.test.js b/tests/update-ts-references.test.js index 39cfa0b..a16119f 100644 --- a/tests/update-ts-references.test.js +++ b/tests/update-ts-references.test.js @@ -237,6 +237,7 @@ test('create paths mappings ', async () => { { compilerOptions: { composite: true, + paths: { "foo-c": ["utils/foos/foo-c/src"] } }, files: [], references: [ @@ -291,9 +292,9 @@ test('create paths mappings ', async () => { { compilerOptions: { ...compilerOptions, - rootDir: "src2", + rootDir: "src", paths: { - "foo-b": ["../foo-b/src2"] + "foo-b": ["../foo-b/src"] }, }, references: [