-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathutils.js
More file actions
92 lines (82 loc) · 2.23 KB
/
Copy pathutils.js
File metadata and controls
92 lines (82 loc) · 2.23 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
// @flow strict-local
import type {ModuleRequest} from './types';
import type {FilePath} from '@parcel/types';
import type {FileSystem} from '@parcel/fs';
import invariant from 'assert';
import ThrowableDiagnostic from '@parcel/diagnostic';
import {resolveConfig} from '@parcel/utils';
import {exec as _exec} from 'child_process';
import {promisify} from 'util';
export const exec: (
command: string,
options?: child_process$execOpts,
) => Promise<{|stdout: string | Buffer, stderr: string | Buffer|}> =
promisify(_exec);
export function npmSpecifierFromModuleRequest(
moduleRequest: ModuleRequest,
): string {
return moduleRequest.range != null
? [moduleRequest.name, moduleRequest.range].join('@')
: moduleRequest.name;
}
export function moduleRequestsFromDependencyMap(dependencyMap: {|
[string]: string,
|}): Array<ModuleRequest> {
return Object.entries(dependencyMap).map(([name, range]) => {
invariant(typeof range === 'string');
return {
name,
range,
};
});
}
export async function getConflictingLocalDependencies(
fs: FileSystem,
name: string,
local: FilePath,
projectRoot: FilePath,
): Promise<?{|json: string, filePath: FilePath, fields: Array<string>|}> {
let pkgPath = await resolveConfig(fs, local, ['package.json'], projectRoot);
if (pkgPath == null) {
return;
}
let pkgStr = await fs.readFile(pkgPath, 'utf8');
let pkg;
try {
pkg = JSON.parse(pkgStr);
} catch (e) {
// TODO: codeframe
throw new ThrowableDiagnostic({
diagnostic: {
message: 'Failed to parse package.json',
origin: '@parcel/package-manager',
},
});
}
if (typeof pkg !== 'object' || pkg == null) {
// TODO: codeframe
throw new ThrowableDiagnostic({
diagnostic: {
message: 'Expected package.json contents to be an object.',
origin: '@parcel/package-manager',
},
});
}
let fields = [];
for (let field of ['dependencies', 'devDependencies', 'peerDependencies']) {
if (
typeof pkg[field] === 'object' &&
pkg[field] != null &&
pkg[field][name] != null
) {
fields.push(field);
}
}
if (fields.length > 0) {
return {
filePath: pkgPath,
json: pkgStr,
fields,
};
}
}