const path = require('path');
exports.PscError = class PscError extends Error {
constructor(message, modules) {
super(message);
this.modules = modules;
this.isPscError = true;
}
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);
};
const normalizeRewriteRuleDest = ({ dest, moduleName }) =>
typeof dest === 'function' ? dest(moduleName) : dest;
const rewrite = ({ rules, moduleName }) => {
const moduleNameParts = moduleName.split('.')
for (const [rule, dest] of Object.entries(rules)) {
const ruleParts = rule.split('.');
const matched = ruleParts.every((part, i) =>
part === '*' || part === moduleNameParts[i]);
if (!matched) continue;
const rest = moduleNameParts.slice(ruleParts.length);
const base = normalizeRewriteRuleDest({ dest, moduleName });
return `${path.join(base, ...rest)}.purs`;
}
};
exports.resolvePursModule = ({ baseModulePath, baseModuleName, rewriteRules, targetModuleName }) => {
const rewrittenModulePath = rewrite({ rules: rewriteRules, moduleName: targetModuleName });
if (rewrittenModulePath) return rewrittenModulePath;
const parts = diffPursModuleNames(
baseModuleName.split('.'),
targetModuleName.split('.'),
[]);
return parts.length
? path.resolve(baseModulePath,
`${path.join(...parts)}.purs`)
: baseModulePath;
};
exports.resolveForeignModule = pursModulePath =>
path.join(path.dirname(pursModulePath),
path.basename(pursModulePath, path.extname(pursModulePath)) + '.js');