forked from parcel-bundler/parcel
/
loadConfig.js
62 lines (54 loc) 路 1.7 KB
/
loadConfig.js
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
// @flow strict-local
import type {Config} from '@parcel/types';
import path from 'path';
import ThrowableDiagnostic from '@parcel/diagnostic';
import commandExists from 'command-exists';
import nullthrows from 'nullthrows';
async function load({config}: {|config: Config|}): Promise<null> {
const elmConfig = await config.getConfig(['elm.json']);
if (!elmConfig) {
elmBinaryPath(); // Check if elm is even installed
throw new ThrowableDiagnostic({
diagnostic: {
origin: '@parcel/elm-transformer',
message: "The 'elm.json' file is missing.",
hints: [
"Initialize your elm project by running 'elm init'",
"If you installed elm as project dependency then run 'yarn elm init' or 'npx elm init'",
],
},
});
}
return null;
}
function elmBinaryPath(): ?string {
let elmBinary = resolveLocalElmBinary();
if (elmBinary == null && !commandExists.sync('elm')) {
throw new ThrowableDiagnostic({
diagnostic: {
message: "Can't find 'elm' binary.",
hints: [
"You can add it as an dependency for your project by running 'yarn add -D elm' or 'npm add -D elm'",
'If you want to install it globally then follow instructions on https://elm-lang.org/',
],
origin: '@parcel/elm-transformer',
},
});
}
return elmBinary;
}
function resolveLocalElmBinary() {
try {
let result = require.resolve('elm/package.json');
// $FlowFixMe
let pkg = require('elm/package.json');
let bin = nullthrows(pkg.bin);
return path.join(
path.dirname(result),
typeof bin === 'string' ? bin : bin.elm,
);
} catch (_) {
return null;
}
}
export {load, elmBinaryPath};