]>
Commit | Line | Data |
---|---|---|
f9d5f2fa CS |
1 | const path = require('path'); |
2 | ||
e3de0f71 CS |
3 | exports.PscError = class PscError extends Error { |
4 | constructor(message, modules) { | |
5 | super(message); | |
6 | this.modules = modules; | |
40d1d0a5 | 7 | this.isPscError = true; |
e3de0f71 CS |
8 | } |
9 | ||
10 | static get name() { | |
11 | return 'PscError'; | |
12 | } | |
13 | }; | |
14 | ||
f9d5f2fa CS |
15 | const repeat = (value, times) => |
16 | times <= 0 ? [] : [value, ...repeat(value, times - 1)]; | |
17 | const diffPursModuleNames = (from, target, parts) => { | |
18 | if (!from.length) return parts.concat(target); | |
19 | if (!target.length) return parts.concat(repeat('..', from.length)); | |
20 | const [head_from, ...tail_from] = from; | |
21 | const [head_target, ...tail_target] = target; | |
22 | return head_from === head_target | |
23 | ? diffPursModuleNames(tail_from, tail_target, parts) | |
24 | : parts.concat(repeat('..', from.length), target); | |
25 | }; | |
26 | exports.resolvePursModule = ({ baseModulePath, baseModuleName, targetModuleName }) => { | |
27 | const parts = diffPursModuleNames( | |
28 | baseModuleName.split('.'), | |
29 | targetModuleName.split('.'), | |
30 | []); | |
31 | return parts.length | |
32 | ? path.resolve(baseModulePath, | |
33 | `${path.join(...parts)}.purs`) | |
34 | : baseModulePath; | |
35 | }; | |
c69d78d9 CS |
36 | |
37 | exports.resolveForeignModule = pursModulePath => | |
38 | path.join(path.dirname(pursModulePath), | |
39 | path.basename(pursModulePath, path.extname(pursModulePath)) + '.js'); |