diff options
Diffstat (limited to 'src/utils.js')
-rw-r--r-- | src/utils.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..671b580 --- /dev/null +++ b/src/utils.js | |||
@@ -0,0 +1,34 @@ | |||
1 | const path = require('path'); | ||
2 | |||
3 | exports.PscError = class PscError extends Error { | ||
4 | constructor(message, modules) { | ||
5 | super(message); | ||
6 | this.modules = modules; | ||
7 | } | ||
8 | |||
9 | static get name() { | ||
10 | return 'PscError'; | ||
11 | } | ||
12 | }; | ||
13 | |||
14 | const repeat = (value, times) => | ||
15 | times <= 0 ? [] : [value, ...repeat(value, times - 1)]; | ||
16 | const diffPursModuleNames = (from, target, parts) => { | ||
17 | if (!from.length) return parts.concat(target); | ||
18 | if (!target.length) return parts.concat(repeat('..', from.length)); | ||
19 | const [head_from, ...tail_from] = from; | ||
20 | const [head_target, ...tail_target] = target; | ||
21 | return head_from === head_target | ||
22 | ? diffPursModuleNames(tail_from, tail_target, parts) | ||
23 | : parts.concat(repeat('..', from.length), target); | ||
24 | }; | ||
25 | exports.resolvePursModule = ({ baseModulePath, baseModuleName, targetModuleName }) => { | ||
26 | const parts = diffPursModuleNames( | ||
27 | baseModuleName.split('.'), | ||
28 | targetModuleName.split('.'), | ||
29 | []); | ||
30 | return parts.length | ||
31 | ? path.resolve(baseModulePath, | ||
32 | `${path.join(...parts)}.purs`) | ||
33 | : baseModulePath; | ||
34 | }; | ||