@ou-imdt/utils
Version:
Utility library for interactive media development
28 lines (26 loc) • 1.2 kB
JavaScript
import getUrlParam from '../getUrlParam.js';
import splitPath from '../splitPath.js';
/**
* Retrieves the contents of a folder from localhost, initially via 'listing.json' at the root
* (see node/createDirTreeJSON.js) falling back to a script at '[name]/[name].folder.js'
* (a module that default exports an array of files contained within the folder).
* @param {string} name - The name of the folder.
* @returns {Promise<any>} A promise that resolves with the folder contents, or rejects on error.
*/
export default async function getDevFolder(name = 'assets') {
const { origin, pathname } = location;
const { dirname, filename } = splitPath(getUrlParam(name));
const path = `${dirname}${filename}`;
try {
const base = `${origin}${pathname.slice(0, pathname.lastIndexOf('/'))}`;
const url = new URL(`${path}/${filename}.folder.js`, base);
const { default: files } = await import(/* @vite-ignore */ url);
const contents = files.map(value => {
if (typeof value === 'object') return value;
return { path: value, url: `${origin}/${path}/${value}` };
});
return contents;
} catch(msg) {
console.error(`Unable to load folder ${name}`, msg);
}
};