All files / src/lib/project loadMusicData.ts

56% Statements 14/25
0% Branches 0/2
0% Functions 0/3
56% Lines 14/25

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 44 45 46 47 48 49 50 51 52 53 541x 1x 1x 1x 1x 1x 1x 1x   1x 1x     1x                                           1x                               1x 1x  
import glob from "glob";
import { promisify } from "util";
import uuidv4 from "uuid/v4";
import { stat } from "fs";
import parseAssetPath from "shared/lib/assets/parseAssetPath";
import { toValidSymbol } from "shared/lib/helpers/symbols";
import { MusicResource, MusicResourceAsset } from "shared/lib/resources/types";
import { getAssetResource } from "./assets";
 
const globAsync = promisify(glob);
const statAsync = promisify(stat);
 
const loadMusicData =
  (projectRoot: string) =>
  async (filename: string): Promise<MusicResourceAsset> => {
    const { file, plugin } = parseAssetPath(filename, projectRoot, "music");
    const resource = await getAssetResource(MusicResource, filename);
    const fileStat = await statAsync(filename, { bigint: true });
    const inode = fileStat.ino.toString();
    const name = file.replace(/(.mod|.uge)$/i, "");
    return {
      _resourceType: "music",
      id: uuidv4(),
      plugin,
      name,
      symbol: toValidSymbol(`song_${name}`),
      settings: {},
      _v: Date.now(),
      ...resource,
      filename: file,
      type: file.endsWith(".uge") ? "uge" : "mod",
      inode,
    };
  };
 
const loadAllMusicData = async (projectRoot: string) => {
  const musicPaths = await globAsync(
    `${projectRoot}/assets/music/**/@(*.mod|*.MOD|*.uge|*.UGE)`
  );
  const pluginPaths = await globAsync(
    `${projectRoot}/plugins/*/**/music/**/@(*.mod|*.MOD|*.uge|*.UGE)`
  );
  const musicData = await Promise.all(
    ([] as Promise<MusicResourceAsset>[]).concat(
      musicPaths.map(loadMusicData(projectRoot)),
      pluginPaths.map(loadMusicData(projectRoot))
    )
  );
  return musicData;
};
 
export default loadAllMusicData;
export { loadMusicData };