]> git.immae.eu Git - github/fretlink/purs-loader.git/blob - src/utils.js
b6ccf8193749d2e2e263994c3b0b6649aeb8e065
[github/fretlink/purs-loader.git] / src / utils.js
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 this.isPscError = true;
8 }
9
10 static get name() {
11 return 'PscError';
12 }
13 };
14
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 };