Skip to content

Commit e065e3e

Browse files
committed
refactor(lerna): move typings to /typings folder and provide manual dts file for getDependencies to make it work in both js/ts modules
1 parent 7970429 commit e065e3e

File tree

7 files changed

+96
-81
lines changed

7 files changed

+96
-81
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Why is this manual type declaration needed?
2+
// - modules(projects) written in TS not having enabled `checkJS:true` wont infer types from .js files, thus the API will have `any` type.
3+
// This errors in strict ts check mode.
4+
5+
/// <reference path="../../../typings/lerna/index.d.ts" />
6+
export declare function getDependencies(packageName: string | string[]): Promise<{
7+
dependencies: string[];
8+
devDependencies: string[];
9+
all: string[];
10+
projectGraph: import('lerna/utils').ProjectGraphWithPackages;
11+
}>;

scripts/monorepo/src/getDependencies.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const { utils: lernaUtils } = require('./lerna-utils');
1+
const lernaUtils = require('lerna/utils');
22

33
/**
44
*
55
* @param {string[]} packageNames
6-
* @param {import('./lerna-utils').ProjectGraphWithPackages} projectGraph
6+
* @param {lernaUtils.ProjectGraphWithPackages} projectGraph
77
* @param {{dependenciesOnly?:boolean}} options
88
* @param {string[]} _packagesList
99
* @returns

scripts/monorepo/src/lerna-utils/index.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

scripts/monorepo/src/lerna-utils/types.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

typings/lerna/index.d.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Type definitions for lerna 7.0.0
2+
3+
// NOTE: types taken from @see https://github.com/lerna/lerna/blob/main/libs/core/src/lib/project-graph-with-packages.ts
4+
// ISSUE: https://github.com/lerna/lerna/issues/3851
5+
6+
declare module 'lerna/utils' {
7+
import type { ProjectFileMap, ProjectGraph, ProjectGraphDependency, ProjectGraphProjectNode } from '@nx/devkit';
8+
9+
interface RawManifest {
10+
name: string;
11+
version: string;
12+
description?: string;
13+
private?: boolean;
14+
bin?: Record<string, string> | string;
15+
scripts?: Record<string, string>;
16+
dependencies?: Record<string, string>;
17+
devDependencies?: Record<string, string>;
18+
optionalDependencies?: Record<string, string>;
19+
peerDependencies?: Record<string, string>;
20+
publishConfig?: Record<'directory' | 'registry' | 'tag', string>;
21+
workspaces?: string[];
22+
nx?: Record<string, unknown>;
23+
gitHead?: string;
24+
lerna?: RawManifestLernaConfig;
25+
}
26+
27+
interface RawManifestLernaConfig {
28+
command?: {
29+
publish?: {
30+
directory?: string;
31+
assets?: AssetDefinition[];
32+
};
33+
};
34+
}
35+
type AssetDefinition = string | { from: string; to: string };
36+
37+
interface Package extends Omit<RawManifest, 'lerna' | 'nx' | 'gitHead' | 'workspaces'> {
38+
/**
39+
* Map-like retrieval of arbitrary values from package.json
40+
*/
41+
get(key: string): unknown;
42+
binLocation: string;
43+
44+
resolved: Record<string, unknown>;
45+
/**
46+
* path to package.json
47+
*/
48+
manifestLocation: string;
49+
location: string;
50+
lernaConfig: RawManifest['lerna'] | undefined;
51+
}
52+
53+
type ExtendedNpaResult = {
54+
workspaceSpec?: string;
55+
workspaceAlias?: string;
56+
};
57+
58+
interface ProjectGraphProjectNodeWithPackage extends ProjectGraphProjectNode {
59+
package: Package | null;
60+
}
61+
interface ProjectGraphWorkspacePackageDependency extends ProjectGraphDependency {
62+
targetVersionMatchesDependencyRequirement: boolean;
63+
targetResolvedNpaResult: ExtendedNpaResult;
64+
dependencyCollection: 'dependencies' | 'devDependencies' | 'optionalDependencies'; // lerna doesn't manage peer dependencies
65+
}
66+
export interface ProjectGraphWithPackages extends ProjectGraph {
67+
nodes: Record<string, ProjectGraphProjectNodeWithPackage>;
68+
localPackageDependencies: Record<string, ProjectGraphWorkspacePackageDependency[]>;
69+
}
70+
71+
export function detectProjects(): Promise<{
72+
projectGraph: ProjectGraphWithPackages;
73+
projectFileMap: ProjectFileMap;
74+
}>;
75+
}

typings/lerna/tsconfig.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {},
4+
"include": ["*.d.ts"]
5+
}

typings/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
},
3131
{
3232
"path": "./find-free-port/tsconfig.json"
33+
},
34+
{
35+
"path": "./lerna/tsconfig.json"
3336
}
3437
]
3538
}

0 commit comments

Comments
 (0)