]> git.immae.eu Git - github/fretlink/purs-loader.git/blame - src/utils.js
Watch foreign modules on compilation error
[github/fretlink/purs-loader.git] / src / utils.js
CommitLineData
f9d5f2fa
CS
1const path = require('path');
2
e3de0f71
CS
3exports.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
15const repeat = (value, times) =>
16 times <= 0 ? [] : [value, ...repeat(value, times - 1)];
17const 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};
26exports.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
37exports.resolveForeignModule = pursModulePath =>
38 path.join(path.dirname(pursModulePath),
39 path.basename(pursModulePath, path.extname(pursModulePath)) + '.js');