Skip to content

Commit

Permalink
feat(typescript): automatically setup typescript references
Browse files Browse the repository at this point in the history
  • Loading branch information
merceyz committed Apr 7, 2021
1 parent fc2fc2a commit c42a7b8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/plugin-typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"license": "BSD-2-Clause",
"main": "./sources/index.ts",
"dependencies": {
"@yarnpkg/fslib": "workspace:^2.4.0",
"@yarnpkg/plugin-pack": "workspace:^2.2.3",
"algoliasearch": "^4.2.0",
"semver": "^7.1.2",
Expand Down
30 changes: 27 additions & 3 deletions packages/plugin-typescript/sources/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {structUtils, ThrowReport, miscUtils, Hooks as CoreHooks} from '@yarnpkg/core';
import {Descriptor, Plugin, Workspace, ResolveOptions, Manifest, AllDependencies, DescriptorHash, Package} from '@yarnpkg/core';
import {structUtils, ThrowReport, miscUtils} from '@yarnpkg/core';
import {Hooks as EssentialsHooks} from '@yarnpkg/plugin-essentials';
import {xfs, ppath, Filename} from "@yarnpkg/fslib";
import {suggestUtils} from '@yarnpkg/plugin-essentials';
import {Hooks as EssentialsHooks} from '@yarnpkg/plugin-essentials';
import {Hooks as PackHooks} from '@yarnpkg/plugin-pack';
import semver from 'semver';

Expand Down Expand Up @@ -128,8 +129,31 @@ const beforeWorkspacePacking = (workspace: Workspace, rawManifest: any) => {
}
};

const plugin: Plugin<EssentialsHooks & PackHooks> = {
const plugin: Plugin<EssentialsHooks & PackHooks & CoreHooks> = {
hooks: {
async afterAllInstalled(project) {
for (const workspace of project.workspaces) {
const referencedWorkspaces = miscUtils.mapAndFilter(workspace.dependencies, ([identHash, descriptor]) => {
const dependingWorkspace = project.tryWorkspaceByDescriptor(descriptor);
if (!dependingWorkspace || dependingWorkspace === workspace)
return miscUtils.mapAndFilter.skip;

return ppath.relative(workspace.cwd, dependingWorkspace.cwd);
});

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);
}
},
afterWorkspaceDependencyAddition,
afterWorkspaceDependencyRemoval,
beforeWorkspacePacking,
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6191,6 +6191,7 @@ __metadata:
"@yarnpkg/builder": "workspace:^2.1.3"
"@yarnpkg/cli": "workspace:^2.4.0"
"@yarnpkg/core": "workspace:^2.4.0"
"@yarnpkg/fslib": "workspace:^2.4.0"
"@yarnpkg/plugin-essentials": "workspace:^2.4.0"
"@yarnpkg/plugin-pack": "workspace:^2.2.3"
algoliasearch: ^4.2.0
Expand Down

0 comments on commit c42a7b8

Please sign in to comment.