diff --git a/package.json b/package.json index ab8acd8..54e7aa9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "update-ts-references", - "version": "2.3.0", + "version": "2.4.0", "bin": "src/index.js", "scripts": { "lint": "eslint src tests", diff --git a/src/update-ts-references.js b/src/update-ts-references.js index 9151021..daad7df 100644 --- a/src/update-ts-references.js +++ b/src/update-ts-references.js @@ -8,6 +8,7 @@ const readlineSync = require('readline-sync'); const assert = require('assert').strict; const PACKAGE_JSON = 'package.json'; +const TSCONFIG_JSON = 'tsconfig.json' const defaultOptions = { configName: 'tsconfig.json', @@ -62,6 +63,15 @@ const getAllPackageJsons = async (workspaces) => { ); }; +const detectTSConfig = (directory, configName) => { + let detectedConfig = fs.existsSync(path.join(directory, configName)) ? configName : null + if (configName !== TSCONFIG_JSON && detectedConfig === null) { + detectedConfig = fs.existsSync(path.join(directory, TSCONFIG_JSON)) ? TSCONFIG_JSON : null + } + return detectedConfig +} + + const getPackageNamesAndPackageDir = (packageFilePaths) => packageFilePaths.reduce((map, packageFilePath) => { const fullPackageFilePath = path.join(process.cwd(), packageFilePath); @@ -103,13 +113,13 @@ const getReferencesFromDependencies = ( .reduce((referenceArray, dependency) => { if (packagesMap.has(dependency)) { const { packageDir: dependencyDir } = packagesMap.get(dependency); - if (fs.existsSync(path.join(dependencyDir, configName))) { - const relativePath = path.relative(packageDir, dependencyDir); - + const relativePath = path.relative(packageDir, dependencyDir); + const detectedConfig = detectTSConfig(dependencyDir, configName) + if (detectedConfig !== null) { return [ ...referenceArray, { - path: relativePath, + path: detectedConfig !== TSCONFIG_JSON ? path.join(relativePath, detectedConfig) : relativePath, }, ]; } @@ -149,8 +159,8 @@ const updateTsConfig = ( if (check === false && pureJson === false && discardComments === false) { if ( !readlineSync.keyInYN( - `Found comments in the tsconfig. ${tsconfigFilePath} -Do you want to discard them and proceed?` + `Found comments in the tsconfig.${tsconfigFilePath} +Do you want to discard them and proceed ? ` ) ) { process.exit(0); @@ -239,10 +249,11 @@ const execute = async ({ const rootReferences = []; packagesMap.forEach((packageEntry, packageName) => { - const tsconfigFilePath = path.join(packageEntry.packageDir, configName); - if (fs.existsSync(tsconfigFilePath)) { + const detectedConfig = detectTSConfig(packageEntry.packageDir, configName) + + if (detectedConfig) { rootReferences.push({ - path: path.relative(process.cwd(), packageEntry.packageDir), + path: path.join(path.relative(process.cwd(), packageEntry.packageDir), detectedConfig !== TSCONFIG_JSON ? detectedConfig : ''), }); const references = getReferencesFromDependencies( configName, @@ -255,7 +266,7 @@ const execute = async ({ console.log(`references of ${packageName}`, references); } changesCount += updateTsConfig( - configName, + detectedConfig, references, discardComments, check, @@ -263,7 +274,7 @@ const execute = async ({ ); } else { // eslint-disable-next-line no-console - console.log(`NO ${configName} for ${packageName}`); + console.log(`NO ${configName === TSCONFIG_JSON ? configName : `${configName} nor ${TSCONFIG_JSON}`} for ${packageName}`); } }); diff --git a/test-scenarios/yarn-ws-custom-tsconfig-names/utils/foos/foo-b/tsconfig.dev.json b/test-scenarios/yarn-ws-custom-tsconfig-names/utils/foos/foo-b/tsconfig.dev.json deleted file mode 100644 index 1dd8e13..0000000 --- a/test-scenarios/yarn-ws-custom-tsconfig-names/utils/foos/foo-b/tsconfig.dev.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "compilerOptions": { - "outDir": "dist", - "rootDir": "src" - } -} diff --git a/test-scenarios/yarn-ws-custom-tsconfig-names/utils/foos/foo-b/tsconfig.json b/test-scenarios/yarn-ws-custom-tsconfig-names/utils/foos/foo-b/tsconfig.json new file mode 100644 index 0000000..3b1f7df --- /dev/null +++ b/test-scenarios/yarn-ws-custom-tsconfig-names/utils/foos/foo-b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + },"references": [ + + { + "path": "../some/old/ref" + } + ] +} diff --git a/test-scenarios/yarn-ws-custom-tsconfig-names/workspace-a/tsconfig.dev.json b/test-scenarios/yarn-ws-custom-tsconfig-names/workspace-a/tsconfig.dev.json index 9d06489..9a1c4cc 100644 --- a/test-scenarios/yarn-ws-custom-tsconfig-names/workspace-a/tsconfig.dev.json +++ b/test-scenarios/yarn-ws-custom-tsconfig-names/workspace-a/tsconfig.dev.json @@ -5,7 +5,7 @@ }, "references": [ { - "path": "../utils/foos/foo-a", + "path": "../utils/foos/foo-a/tsconfig.dev.json", "prepend": false }, { diff --git a/tests/update-ts-references.test.js b/tests/update-ts-references.test.js index 99d3b3f..ea78342 100644 --- a/tests/update-ts-references.test.js +++ b/tests/update-ts-references.test.js @@ -36,10 +36,9 @@ const setup = async (rootFolder, configName) => { try { await execSh( `npx update-ts-references --discardComments${ - configName ? ` --configName ${configName}` : '' + configName ? ` --configName ${configName}` : '' }`, { - stdio: null, cwd: rootFolder, } ); @@ -292,32 +291,92 @@ test('Support custom tsconfig names', async () => { await setup(rootFolder, configName); const tsconfigs = [ - rootTsConfig, + [ + '.', + { + compilerOptions: { + composite: true, + }, + files: [], + references: [ + { + path: 'workspace-a/tsconfig.dev.json', + }, + { + path: 'workspace-b/tsconfig.dev.json', + }, + { + path: 'shared/workspace-c/tsconfig.dev.json', + }, + { + path: 'shared/workspace-d/tsconfig.dev.json', + }, + { + path: 'utils/foos/foo-a/tsconfig.dev.json', + }, + { + path: 'utils/foos/foo-b', + }, + ], + }, + ], [ './workspace-a', { compilerOptions, references: [ { - path: '../utils/foos/foo-a', + path: '../utils/foos/foo-a/tsconfig.dev.json', prepend: false, }, { - path: '../workspace-b', + path: '../workspace-b/tsconfig.dev.json', }, ], }, ], wsBTsConfig, - wsCTsConfig, - wsDTsConfig, - fooATsConfig, - fooBTsConfig, + [ + './shared/workspace-c', + { + compilerOptions, + + references: [ + { + path: '../../utils/foos/foo-a/tsconfig.dev.json', + }, + ], + }, + ], + [ + './shared/workspace-d', + { + compilerOptions, + + references: [ + { + path: '../workspace-c/tsconfig.dev.json', + }, + ], + }, + ], + [ + './utils/foos/foo-a', + { + compilerOptions, + references: [ + { + path: '../foo-b', + }, + ], + }, + ], + [...fooBTsConfig, 'tsconfig.json'], ]; tsconfigs.forEach((tsconfig) => { - const [configPath, config] = tsconfig; - expect(require(path.join(rootFolder, configPath, configName))).toEqual( + const [configPath, config, configNameOverride] = tsconfig; + expect(require(path.join(rootFolder, configPath, configNameOverride || configName))).toEqual( config ); });