1 | import * as path from 'path';
|
2 | import * as tsm from 'ts-morph';
|
3 |
|
4 | export function getEntryPoint({
|
5 | fileSystem,
|
6 | name,
|
7 | source,
|
8 | types,
|
9 | typings,
|
10 | }: {
|
11 | fileSystem: tsm.FileSystemHost;
|
12 | name?: string;
|
13 | source?: string;
|
14 | types?: string;
|
15 | typings?: string;
|
16 | }): string | undefined {
|
17 | const filenames = [
|
18 |
|
19 | ...getCandidatesForSource({ source }),
|
20 | ...getCandidatesForTypes({ types, typings }),
|
21 |
|
22 | 'public-package-api.ts',
|
23 | ...getCandidatesForName({ name, extension: '.ts' }),
|
24 | 'index.ts',
|
25 | 'main.ts',
|
26 |
|
27 | 'public-package-api.d.ts',
|
28 | ...getCandidatesForName({ name, extension: '.d.ts' }),
|
29 | 'index.d.ts',
|
30 | 'main.d.ts',
|
31 | ];
|
32 |
|
33 | for (const filename of filenames) {
|
34 | const found = fileSystem.globSync([
|
35 | `**/${filename}`,
|
36 | '!node_modules/*',
|
37 | ]);
|
38 |
|
39 | const [first] = found.sort((a, b) => {
|
40 | const shortestPath = a.split('/').length - b.split('/').length;
|
41 | const alphabeticalOrder = a.localeCompare(b);
|
42 | return shortestPath || alphabeticalOrder;
|
43 | });
|
44 | if (first) {
|
45 | return first;
|
46 | }
|
47 | }
|
48 |
|
49 | return undefined;
|
50 | }
|
51 |
|
52 | function getCandidatesForSource({ source }: { source?: string }): string[] {
|
53 | if (!source?.endsWith('.ts')) {
|
54 | return [];
|
55 | }
|
56 |
|
57 | return [path.normalize(source)];
|
58 | }
|
59 |
|
60 | function getCandidatesForName({
|
61 | name,
|
62 | extension,
|
63 | }: {
|
64 | name?: string;
|
65 | extension: string;
|
66 | }): string[] {
|
67 | if (!name) {
|
68 | return [];
|
69 | }
|
70 |
|
71 | const normalizedName = name
|
72 | .replace('.d.ts', '')
|
73 | .replace('.ts', '')
|
74 | .replace('.js', '')
|
75 | .replace('@', '')
|
76 | .replace('/', '__');
|
77 |
|
78 | return [`${name}${extension}`, `${normalizedName}${extension}`];
|
79 | }
|
80 |
|
81 | function getCandidatesForTypes({
|
82 | types: aliasTypes,
|
83 | typings: aliasTypings,
|
84 | }: {
|
85 | types?: string;
|
86 | typings?: string;
|
87 | }): string[] {
|
88 | const types = aliasTypes || aliasTypings;
|
89 | if (!types?.endsWith('.d.ts')) {
|
90 | return [];
|
91 | }
|
92 |
|
93 | return [path.normalize(types)];
|
94 | }
|