Skip to content

Commit

Permalink
fix: only write tsconfig when references changes
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Mar 30, 2021
1 parent 9207b05 commit ca1987b
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions packages/plugin-typescript/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,36 @@ const plugin: Plugin<EssentialsHooks & PackHooks & CoreHooks> = {
return ppath.relative(workspace.cwd, dependingWorkspace.cwd);
});

let configChanged = false;

let tsconfig: {references?: Array<{path: string}>} = {};
try {
tsconfig = await xfs.readJsonPromise(ppath.join(workspace.cwd, `tsconfig.json` as Filename));
} catch { }

if (referencedWorkspaces.length === 0)
tsconfig.references = undefined;
else
tsconfig.references = referencedWorkspaces.map(relativePath => ({path: relativePath}));

await xfs.writeJsonPromise(ppath.join(workspace.cwd, `tsconfig.json` as Filename), tsconfig);
} catch {
configChanged = true;
}

if (referencedWorkspaces.length === 0) {
if (typeof tsconfig.references !== `undefined`) {
configChanged = true;
tsconfig.references = undefined;
}
} else {
const oldReferences = new Set(tsconfig.references?.map(ref => ref.path));

tsconfig.references = [];

for (const relativePath of referencedWorkspaces) {
if (!oldReferences.has(relativePath))
configChanged = true;

tsconfig.references.push({path: relativePath});
}
}

if (configChanged) {
await xfs.writeJsonPromise(ppath.join(workspace.cwd, `tsconfig.json` as Filename), tsconfig);
}
}
},
afterWorkspaceDependencyAddition,
Expand Down

0 comments on commit ca1987b

Please sign in to comment.