Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use rootDir for paths resolution (incl. fix for updating the root config) #45

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "update-ts-references",
"version": "3.4.0",
"version": "3.5.0",
"description": "Updates TypeScript references automatically while using workspaces",
"bin": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -39,6 +39,7 @@
"license": "MIT",
"keywords": [
"typescript",
"intellisense",
"project references",
"tool",
"util",
Expand All @@ -49,7 +50,8 @@
"monorepo",
"packages",
"npm",
"automatic"
"automatic",
"update"
],
"repository": {
"type": "git",
Expand Down
31 changes: 25 additions & 6 deletions src/update-ts-references.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,13 @@ const updateTsConfig = (
}
};

function getPathsFromReferences(references) {
return references.reduce((paths, ref) => ({
function getPathsFromReferences(references, tsconfigMap) {
return references.reduce((paths, ref) => {
const rootFolder= tsconfigMap[ref.name]?.compilerOptions?.rootDir ?? 'src'
return {
...paths,
[`${ref.name}`]: [`${ref.folder}/src`]
}), {});
[`${ref.name}`]: [`${ref.folder}${rootFolder === '.' ? '' : `/${rootFolder}`}`],
}}, {});
}

const execute = async ({
Expand Down Expand Up @@ -305,9 +307,26 @@ const execute = async ({

let rootReferences = [];
let rootPaths = [];
let tsconfigMap = {}
packagesMap.forEach((packageEntry, packageName) => {
const detectedConfig = detectTSConfig(packageEntry.packageDir, configName, packageEntry.hasTsEntry && createTsConfig, cwd)

if (detectedConfig) {
const compilerOptions = parse(fs.readFileSync(path.join(packageEntry.packageDir, detectedConfig)).toString()).compilerOptions

tsconfigMap = {
...tsconfigMap,
[packageName]: {
detectedConfig,
compilerOptions
}
}
}
});

packagesMap.forEach((packageEntry, packageName) => {
const detectedConfig = tsconfigMap[packageName]?.detectedConfig

if (detectedConfig) {
rootReferences.push({
name: packageName,
Expand All @@ -322,7 +341,7 @@ const execute = async ({
verbose
) || []).map(ensurePosixPathStyle);

const paths = getPathsFromReferences(references)
const paths = getPathsFromReferences(references, tsconfigMap)

if (verbose) {
console.log(`references of ${packageName}`, references);
Expand Down Expand Up @@ -350,7 +369,7 @@ const execute = async ({
});

rootReferences = (rootReferences || []).map(ensurePosixPathStyle);
rootPaths = getPathsFromReferences((rootPaths || []).map(ensurePosixPathStyle))
rootPaths = getPathsFromReferences((rootReferences || []).map(ensurePosixPathStyle), tsconfigMap)

if (verbose) {
console.log('rootReferences', rootReferences);
Expand Down
2 changes: 1 addition & 1 deletion test-scenarios/ts-paths/workspace-b/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"outDir": "dist",
"rootDir": "src"
"rootDir": "."
}
}
6 changes: 3 additions & 3 deletions tests/update-ts-references.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ test('create paths mappings ', async () => {
{
compilerOptions: {
composite: true,
paths: { "foo-c": ["utils/foos/foo-c/src"] }
paths: { "foo-a": ["utils/foos/foo-a/src"] ,"foo-b": ["utils/foos/foo-b/src"] ,"workspace-a": ["workspace-a/src"],"workspace-b": ["workspace-b"] }
},
files: [],
references: [
Expand All @@ -253,7 +253,7 @@ test('create paths mappings ', async () => {
{
compilerOptions: {
...compilerOptions,
paths: {"foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b/src"]}
paths: {"foo-a": ["../utils/foos/foo-a/src"], "workspace-b": ["../workspace-b"]}
},
references: [
{
Expand All @@ -269,7 +269,7 @@ test('create paths mappings ', async () => {
const wsBTsConfig = [
'./workspace-b',
{
compilerOptions: {...compilerOptions, paths: {"foo-b": ["../utils/foos/foo-b/src"]}},
compilerOptions: {...compilerOptions,rootDir: '.', paths: {"foo-b": ["../utils/foos/foo-b/src"]}},
references: [
{
path: '../utils/foos/foo-b',
Expand Down
4 changes: 3 additions & 1 deletion tests/update-ts-references.yaml.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ test('receive options via the config', async () => {
{
compilerOptions: {
composite: true,
},
paths:{"workspace-a": ["workspace-a/src"],"workspace-b": ["workspace-b/src"]}
},
files: [],
references: [
{
Expand Down Expand Up @@ -240,6 +241,7 @@ test('receive options for a different usecase', async () => {
{
compilerOptions: {
composite: true,
paths:{"workspace-a": ["workspace-a/src"],"workspace-b": ["workspace-b/src"]}
},
files: [],
references: [
Expand Down
Loading