soupette dbfcfa211d Created custom plugin to switch from ee to ce at runtime
Signed-off-by: soupette <cyril.lpz@gmail.com>
2021-07-01 14:19:50 +02:00

70 lines
2.0 KiB
JavaScript

'use strict';
const path = require('path');
const normalizeOptions = require('./normalizeOptions');
const mapToRelative = require('./mapToRelative');
const { nodeResolvePath, replaceExtension, toLocalPath, toPosixPath } = require('./utils');
function getRelativePath(sourcePath, currentFile, absFileInRoot, opts) {
const realSourceFileExtension = path.extname(absFileInRoot);
const sourceFileExtension = path.extname(sourcePath);
let relativePath = mapToRelative(opts.cwd, currentFile, absFileInRoot);
if (realSourceFileExtension !== sourceFileExtension) {
relativePath = replaceExtension(relativePath, opts);
}
return toLocalPath(toPosixPath(relativePath));
}
function findPathInRoots(sourcePath, { extensions, roots }) {
// Search the source path inside every custom root directory
const resolvedEESourceFile = nodeResolvePath(
`./${sourcePath.replace('ee_else_ce', '')}`,
roots.eeRoot,
extensions
);
const resolvedCESourceFile = nodeResolvePath(
`./${sourcePath.replace('ee_else_ce', '')}`,
roots.ceRoot,
extensions
);
return { resolvedEESourceFile, resolvedCESourceFile };
}
function resolvePathFromRootConfig(sourcePath, currentFile, opts) {
const absFileInRoot = findPathInRoots(sourcePath, opts);
const relativeEEPath = getRelativePath(
sourcePath,
currentFile,
absFileInRoot.resolvedEESourceFile,
opts
);
const relativeCEPath = getRelativePath(
sourcePath,
currentFile,
absFileInRoot.resolvedCESourceFile,
opts
);
return { relativeCEPath, relativeEEPath };
}
function resolvePath(sourcePath, currentFile, opts) {
const normalizedOpts = normalizeOptions(currentFile, opts);
// File param is a relative path from the environment current working directory
// (not from cwd param)
const absoluteCurrentFile = path.resolve(currentFile);
const resolvedPaths = resolvePathFromRootConfig(sourcePath, absoluteCurrentFile, normalizedOpts);
return resolvedPaths;
}
module.exports = resolvePath;