]>
git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/utils.js
b6ccf8193749d2e2e263994c3b0b6649aeb8e065
1 const path
= require('path');
3 exports
.PscError
= class PscError
extends Error
{
4 constructor(message
, modules
) {
6 this.modules
= modules
;
7 this.isPscError
= true;
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
);
26 exports
.resolvePursModule
= ({ baseModulePath
, baseModuleName
, targetModuleName
}) => {
27 const parts
= diffPursModuleNames(
28 baseModuleName
.split('.'),
29 targetModuleName
.split('.'),
32 ? path
.resolve(baseModulePath
,
33 `${path.join(...parts)}.purs`)