aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils.js
blob: 671b58006ee37272f64e22fed79456a60da8b367 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const path = require('path');

exports.PscError = class PscError extends Error {
  constructor(message, modules) {
    super(message);
    this.modules = modules;
  }

  static get name() {
    return 'PscError';
  }
};

const repeat = (value, times) =>
  times <= 0 ? [] : [value, ...repeat(value, times - 1)];
const diffPursModuleNames = (from, target, parts) => {
  if (!from.length) return parts.concat(target);
  if (!target.length) return parts.concat(repeat('..', from.length));
  const [head_from, ...tail_from] = from;
  const [head_target, ...tail_target] = target;
  return head_from === head_target
    ? diffPursModuleNames(tail_from, tail_target, parts)
    : parts.concat(repeat('..', from.length), target);
};
exports.resolvePursModule = ({ baseModulePath, baseModuleName, targetModuleName }) => {
  const parts = diffPursModuleNames(
    baseModuleName.split('.'),
    targetModuleName.split('.'),
    []);
  return parts.length
    ? path.resolve(baseModulePath,
      `${path.join(...parts)}.purs`)
    : baseModulePath;
};