-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
54 lines (47 loc) · 1.82 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { Dirent } from 'fs';
import { readdirSync, readFileSync } from 'fs';
import * as path from 'path';
import WebpackChain from 'webpack-chain';
export const getDirectoryNames = (source: string) =>
readdirSync(source, { withFileTypes: true })
.filter((dirent: Dirent) => dirent.isDirectory())
.map((dirent: Dirent) => dirent.name);
export const getPackageDocPaths = (source: string) =>
getDirectoryNames(source).map(
(packageDir: string) => `packages/${packageDir}/docs`,
);
export const getPackageNames = (packageDirNames: string[]) => {
return packageDirNames.map((packageDir) => {
const content = readFileSync(path.join(packageDir, 'package.json'), 'utf8');
const packageJson = JSON.parse(content);
return packageJson.name;
});
};
export const getPackageAlias = (source: string) => {
const packageDirNames = getDirectoryNames(source);
const packageDirPaths = packageDirNames.map((packageDirName: string) =>
path.join(source, packageDirName),
);
const packageNames = getPackageNames(packageDirPaths);
return packageNames.reduce(
(acc: { [key: string]: string }, packageName: string, index) => {
const alias = {
[`${packageName}/dist/esm`]: path.join(packageDirPaths[index], 'src'),
[`${packageName}/dist/cjs`]: path.join(packageDirPaths[index], 'src'),
[`${packageName}`]: path.join(packageDirPaths[index], 'src'),
};
return { ...acc, ...alias };
},
{},
);
};
export const removePackageAlias = (memo: WebpackChain, source: string) => {
const packageDirNames = getDirectoryNames(source);
const packageDirPaths = packageDirNames.map((packageDirName: string) =>
path.join(source, packageDirName),
);
const packageNames = getPackageNames(packageDirPaths);
packageNames.forEach((name) => {
memo.resolve.alias.delete(name);
});
};