From 5177da5037522809590d81f422a022c5a49a816a Mon Sep 17 00:00:00 2001 From: Mirko Kruschke Date: Fri, 17 Jan 2025 23:43:37 +0100 Subject: [PATCH] feat(yaml): ignore paths for packages (#46) --- README.md | 1 + package.json | 2 +- src/update-ts-references.js | 13 ++- .../ts-options-yaml/update-ts-references.yaml | 2 + test-scenarios/ts-paths-ignore/package.json | 14 +++ test-scenarios/ts-paths-ignore/tsconfig.json | 8 ++ .../ts-paths-ignore/update-ts-references.yaml | 6 ++ .../utils/foos/foo-a/package.json | 7 ++ .../utils/foos/foo-a/tsconfig.json | 6 ++ .../utils/foos/foo-b/package.json | 4 + .../utils/foos/foo-b/tsconfig.json | 6 ++ .../utils/foos/foo-c/package.json | 6 ++ .../ts-paths-ignore/workspace-a/package.json | 10 ++ .../ts-paths-ignore/workspace-a/tsconfig.json | 15 +++ .../ts-paths-ignore/workspace-b/package.json | 10 ++ .../ts-paths-ignore/workspace-b/tsconfig.json | 6 ++ tests/update-ts-references.test.js | 95 +++++++++++++++++++ 17 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 test-scenarios/ts-paths-ignore/package.json create mode 100644 test-scenarios/ts-paths-ignore/tsconfig.json create mode 100644 test-scenarios/ts-paths-ignore/update-ts-references.yaml create mode 100644 test-scenarios/ts-paths-ignore/utils/foos/foo-a/package.json create mode 100644 test-scenarios/ts-paths-ignore/utils/foos/foo-a/tsconfig.json create mode 100644 test-scenarios/ts-paths-ignore/utils/foos/foo-b/package.json create mode 100644 test-scenarios/ts-paths-ignore/utils/foos/foo-b/tsconfig.json create mode 100644 test-scenarios/ts-paths-ignore/utils/foos/foo-c/package.json create mode 100644 test-scenarios/ts-paths-ignore/workspace-a/package.json create mode 100644 test-scenarios/ts-paths-ignore/workspace-a/tsconfig.json create mode 100644 test-scenarios/ts-paths-ignore/workspace-b/package.json create mode 100644 test-scenarios/ts-paths-ignore/workspace-b/tsconfig.json diff --git a/README.md b/README.md index df8eac0..59db145 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Additional to that you can configure also the following options: - rootConfigName (default: tsconfig.json) - createPathMappings (default: false) - withoutRootConfig (default: false) +- ignorePathMappings (default: []) Example configuration see [here](./test-scenarios/ts-options-yaml/update-ts-references.yaml) diff --git a/package.json b/package.json index b2d176e..741b94b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "update-ts-references", - "version": "3.5.0", + "version": "3.6.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 33286ff..aa3ef0c 100644 --- a/src/update-ts-references.js +++ b/src/update-ts-references.js @@ -225,8 +225,10 @@ const updateTsConfig = ( } }; -function getPathsFromReferences(references, tsconfigMap) { +function getPathsFromReferences(references, tsconfigMap, ignorePathMappings + = []) { return references.reduce((paths, ref) => { + if(ignorePathMappings.includes(ref.name)) return paths const rootFolder= tsconfigMap[ref.name]?.compilerOptions?.rootDir ?? 'src' return { ...paths, @@ -271,6 +273,8 @@ const execute = async ({ withoutRootConfig } = configurable + let ignorePathMappings = [] + if (fs.existsSync(path.join(cwd, usecase))) { const yamlConfig = yaml.load( fs.readFileSync(path.join(cwd, usecase)) @@ -280,12 +284,15 @@ const execute = async ({ createPathMappings = yamlConfig.createPathMappings ?? createPathMappings withoutRootConfig = yamlConfig.withoutRootConfig ?? withoutRootConfig workspaces = [...(yamlConfig.packages ? yamlConfig.packages : []), ...(workspaces ? workspaces : [])]; + ignorePathMappings = yamlConfig.ignorePathMappings ?? [] if (verbose) { console.log(`configName ${configName}`); console.log(`rootConfigName ${rootConfigName}`); console.log(`createPathMappings ${createPathMappings}`) console.log('joined workspaces', workspaces); + console.log('ignorePathMappings', ignorePathMappings +); } } @@ -341,7 +348,7 @@ const execute = async ({ verbose ) || []).map(ensurePosixPathStyle); - const paths = getPathsFromReferences(references, tsconfigMap) + const paths = getPathsFromReferences(references, tsconfigMap, ignorePathMappings) if (verbose) { console.log(`references of ${packageName}`, references); @@ -369,7 +376,7 @@ const execute = async ({ }); rootReferences = (rootReferences || []).map(ensurePosixPathStyle); - rootPaths = getPathsFromReferences((rootReferences || []).map(ensurePosixPathStyle), tsconfigMap) + rootPaths = getPathsFromReferences((rootReferences || []).map(ensurePosixPathStyle), tsconfigMap, ignorePathMappings) if (verbose) { console.log('rootReferences', rootReferences); diff --git a/test-scenarios/ts-options-yaml/update-ts-references.yaml b/test-scenarios/ts-options-yaml/update-ts-references.yaml index 338df0d..6752e3f 100644 --- a/test-scenarios/ts-options-yaml/update-ts-references.yaml +++ b/test-scenarios/ts-options-yaml/update-ts-references.yaml @@ -1,6 +1,8 @@ configName: 'tsconfig.dev.json' rootConfigName: 'tsconfig.root.json' createPathMappings: true +ignorePathMappings: + - name-of-the-package packages: # all packages in subdirs of packages/ and components/ - 'workspace-a' diff --git a/test-scenarios/ts-paths-ignore/package.json b/test-scenarios/ts-paths-ignore/package.json new file mode 100644 index 0000000..e29972f --- /dev/null +++ b/test-scenarios/ts-paths-ignore/package.json @@ -0,0 +1,14 @@ +{ + "name": "ts-ref-yaml-workspace", + "version": "0.0.1", + "private": true, + "workspaces": [ + "workspace-a", + "workspace-b", + "shared/*", + "utils/**/" + ], + "devDependencies": { + "typescript": "latest" + } +} diff --git a/test-scenarios/ts-paths-ignore/tsconfig.json b/test-scenarios/ts-paths-ignore/tsconfig.json new file mode 100644 index 0000000..23b6225 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/tsconfig.json @@ -0,0 +1,8 @@ +{ + "files": [], + "compilerOptions": { + /* Basic Options */ + // "allowJs": true, + "composite": true + } +} diff --git a/test-scenarios/ts-paths-ignore/update-ts-references.yaml b/test-scenarios/ts-paths-ignore/update-ts-references.yaml new file mode 100644 index 0000000..3069507 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/update-ts-references.yaml @@ -0,0 +1,6 @@ +configName: 'tsconfig.json' +rootConfigName: 'tsconfig.json' +createPathMappings: true +ignorePathMappings: + - workspace-a + diff --git a/test-scenarios/ts-paths-ignore/utils/foos/foo-a/package.json b/test-scenarios/ts-paths-ignore/utils/foos/foo-a/package.json new file mode 100644 index 0000000..699ec9c --- /dev/null +++ b/test-scenarios/ts-paths-ignore/utils/foos/foo-a/package.json @@ -0,0 +1,7 @@ +{ + "name": "foo-a", + "version": "1.0.0", + "dependencies": { + "foo-b": "1.0.0" + } +} diff --git a/test-scenarios/ts-paths-ignore/utils/foos/foo-a/tsconfig.json b/test-scenarios/ts-paths-ignore/utils/foos/foo-a/tsconfig.json new file mode 100644 index 0000000..1dd8e13 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/utils/foos/foo-a/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + } +} diff --git a/test-scenarios/ts-paths-ignore/utils/foos/foo-b/package.json b/test-scenarios/ts-paths-ignore/utils/foos/foo-b/package.json new file mode 100644 index 0000000..2c997b8 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/utils/foos/foo-b/package.json @@ -0,0 +1,4 @@ +{ + "name": "foo-b", + "version": "1.0.0" +} diff --git a/test-scenarios/ts-paths-ignore/utils/foos/foo-b/tsconfig.json b/test-scenarios/ts-paths-ignore/utils/foos/foo-b/tsconfig.json new file mode 100644 index 0000000..1dd8e13 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/utils/foos/foo-b/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src" + } +} diff --git a/test-scenarios/ts-paths-ignore/utils/foos/foo-c/package.json b/test-scenarios/ts-paths-ignore/utils/foos/foo-c/package.json new file mode 100644 index 0000000..d751623 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/utils/foos/foo-c/package.json @@ -0,0 +1,6 @@ +{ + "name": "foo-c", + "version": "1.0.0", + "dependencies": { + } +} diff --git a/test-scenarios/ts-paths-ignore/workspace-a/package.json b/test-scenarios/ts-paths-ignore/workspace-a/package.json new file mode 100644 index 0000000..f35aa29 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/workspace-a/package.json @@ -0,0 +1,10 @@ +{ + "name": "workspace-a", + "version": "1.0.0", + "dependencies": { + "workspace-b": "1.0.0" + }, + "devDependencies": { + "foo-a": "1.0.0" + } +} diff --git a/test-scenarios/ts-paths-ignore/workspace-a/tsconfig.json b/test-scenarios/ts-paths-ignore/workspace-a/tsconfig.json new file mode 100644 index 0000000..40169b3 --- /dev/null +++ b/test-scenarios/ts-paths-ignore/workspace-a/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "src", + "paths": { "replace-me": ["../utils/old-one/src"]}, + }, + "references": [ + { + "path": "../utils/foos/foo-a" + }, + { + "path": "../workspace-b" + } + ] +} diff --git a/test-scenarios/ts-paths-ignore/workspace-b/package.json b/test-scenarios/ts-paths-ignore/workspace-b/package.json new file mode 100644 index 0000000..552cf0e --- /dev/null +++ b/test-scenarios/ts-paths-ignore/workspace-b/package.json @@ -0,0 +1,10 @@ +{ + "name": "workspace-b", + "version": "1.0.0", + "dependencies": { + "cross-env": "5.0.5" + }, + "devDependencies": { + "foo-b": "1.0.0" + } +} diff --git a/test-scenarios/ts-paths-ignore/workspace-b/tsconfig.json b/test-scenarios/ts-paths-ignore/workspace-b/tsconfig.json new file mode 100644 index 0000000..57ff9da --- /dev/null +++ b/test-scenarios/ts-paths-ignore/workspace-b/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "outDir": "dist", + "rootDir": "." + } +} diff --git a/tests/update-ts-references.test.js b/tests/update-ts-references.test.js index f7c76a8..db96ce1 100644 --- a/tests/update-ts-references.test.js +++ b/tests/update-ts-references.test.js @@ -18,6 +18,7 @@ const rootFolderYarnCreate = path.join( ); const rootFolderPnpm = path.join(process.cwd(), 'test-run', 'pnpm'); const rootFolderTsPaths = path.join(process.cwd(), 'test-run', 'ts-paths'); +const rootFolderTsPathsIgnore = path.join(process.cwd(), 'test-run', 'ts-paths-ignore'); const rootFolderLerna = path.join(process.cwd(), 'test-run', 'lerna'); const rootFolderConfigName = path.join( @@ -313,6 +314,100 @@ test('create paths mappings ', async () => { }); +test('create paths mappings with ignorePathMappings', async () => { + await setup(rootFolderTsPathsIgnore, undefined, undefined, undefined, true); + + + const rootTsConfig = [ + '.', + { + compilerOptions: { + composite: true, + paths: { "foo-a": ["utils/foos/foo-a/src"] ,"foo-b": ["utils/foos/foo-b/src"] ,"workspace-b": ["workspace-b"] } + }, + files: [], + references: [ + { + path: 'workspace-a', + }, + { + path: 'workspace-b', + }, + { + path: 'utils/foos/foo-a', + }, + { + path: 'utils/foos/foo-b', + }, + ], + }, + ]; + + const wsATsConfig = [ + './workspace-a', + { + compilerOptions: { + ...compilerOptions, + paths: {"foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b"]} + }, + references: [ + { + path: '../utils/foos/foo-a', + }, + { + path: '../workspace-b', + }, + ], + }, + ]; + + const wsBTsConfig = [ + './workspace-b', + { + compilerOptions: {...compilerOptions,rootDir: '.', paths: {"foo-b": ["../utils/foos/foo-b/src"]}}, + references: [ + { + path: '../utils/foos/foo-b', + }, + ], + }, + ]; + + const fooATsConfig = [ + './utils/foos/foo-a', + { + compilerOptions: { + ...compilerOptions, + rootDir: "src", + paths: { + "foo-b": ["../foo-b/src"] + }, + }, + references: [ + { + path: '../foo-b', + }, + ], + }, + ]; + + const fooBTsConfig = [ + './utils/foos/foo-b', + { + compilerOptions, + references: undefined, + }, + ]; + [rootTsConfig, wsATsConfig, wsBTsConfig, fooATsConfig, fooBTsConfig].forEach((tsconfig) => { + const [configPath, config] = tsconfig; + + expect( + parse(fs.readFileSync(path.join(rootFolderTsPathsIgnore, configPath, 'tsconfig.json')).toString()) + ).toEqual(config); + }); + +}); + test('Test create tsconfig', async () => { await setup(rootFolderYarnCreate, undefined, undefined, true); const r = [