All files / src/shared/lib/assets parseAssetPath.ts

87.5% Statements 14/16
66.67% Branches 4/6
100% Functions 2/2
87.5% Lines 14/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 35 36 37 38 39 40 41 42 43 4441x 41x   41x 4x       4x   4x       4x       4x     41x         6x 6x     6x         6x             41x  
import { relative, sep } from "path";
import { pathToPosix } from "shared/lib/helpers/path";
 
const extractPluginPath = (relativePath: string, assetFolder: string) => {
  Iif (!relativePath.startsWith("plugins")) {
    return undefined;
  }
 
  const assetFolderIndex = relativePath.lastIndexOf(assetFolder);
 
  Iif (assetFolderIndex === -1) {
    return undefined;
  }
 
  const extractedPath = relativePath.substring(
    "plugins".length + 1,
    assetFolderIndex - 1
  );
  return extractedPath.split(sep).join("/");
};
 
const parseAssetPath = (
  filename: string,
  projectRoot: string,
  assetFolder: string
) => {
  const relativePath = relative(projectRoot, filename);
  const plugin = relativePath.startsWith("plugins")
    ? extractPluginPath(relativePath, assetFolder)
    : undefined;
  const file = pathToPosix(
    plugin
      ? relative(`plugins/${plugin}/${assetFolder}/`, relativePath)
      : relative(`assets/${assetFolder}/`, relativePath)
  );
  return {
    relativePath,
    plugin,
    file,
  };
};
 
export default parseAssetPath;