forked from web-infra-dev/rslib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.ts
More file actions
129 lines (114 loc) · 2.69 KB
/
Copy pathhelper.ts
File metadata and controls
129 lines (114 loc) · 2.69 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import fs from 'node:fs';
import fsP from 'node:fs/promises';
import path from 'node:path';
import color from 'picocolors';
import type { PackageJson } from '../types';
import { logger } from './logger';
/**
* Node.js built-in modules.
* Copied from https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L12-L72
*/
export const nodeBuiltInModules: Array<string | RegExp> = [
'assert',
'assert/strict',
'async_hooks',
'buffer',
'child_process',
'cluster',
'console',
'constants',
'crypto',
'dgram',
'diagnostics_channel',
'dns',
'dns/promises',
'domain',
'events',
'fs',
'fs/promises',
'http',
'http2',
'https',
'inspector',
'inspector/promises',
'module',
'net',
'os',
'path',
'path/posix',
'path/win32',
'perf_hooks',
'process',
'punycode',
'querystring',
'readline',
'readline/promises',
'repl',
'stream',
'stream/consumers',
'stream/promises',
'stream/web',
'string_decoder',
'sys',
'timers',
'timers/promises',
'tls',
'trace_events',
'tty',
'url',
'util',
'util/types',
'v8',
'vm',
'wasi',
'worker_threads',
'zlib',
/^node:/,
// cspell:word pnpapi
// Yarn PnP adds pnpapi as "builtin"
'pnpapi',
];
export async function calcLongestCommonPath(
absPaths: string[],
): Promise<string | null> {
if (absPaths.length === 0) {
return null;
}
// we support two cases
// 1. /packages-a/src/index.ts
// 2. D:/packages-a/src/index.ts
const sep = path.posix.sep as '/';
const splitPaths = absPaths.map((p) => p.split(sep));
let lcaFragments = splitPaths[0]!;
for (let i = 1; i < splitPaths.length; i++) {
const currentPath = splitPaths[i]!;
const minLength = Math.min(lcaFragments.length, currentPath.length);
let j = 0;
while (j < minLength && lcaFragments[j] === currentPath[j]) {
j++;
}
lcaFragments = lcaFragments.slice(0, j);
}
let lca = lcaFragments.length > 0 ? lcaFragments.join(sep) : sep;
const stats = await fsP.stat(lca);
if (stats?.isFile()) {
lca = path.dirname(lca);
}
return lca;
}
export const readPackageJson = (rootPath: string): undefined | PackageJson => {
const pkgJsonPath = path.join(rootPath, './package.json');
if (!fs.existsSync(pkgJsonPath)) {
logger.warn(`package.json does not exist in the ${rootPath} directory`);
return;
}
try {
return JSON.parse(fs.readFileSync(pkgJsonPath, 'utf8'));
} catch (err) {
logger.warn(`Failed to parse ${pkgJsonPath}, it might not be valid JSON`);
return;
}
};
export const isObject = (obj: unknown): obj is Record<string, any> =>
Object.prototype.toString.call(obj) === '[object Object]';
export { color };