Topic: Loading collections from any /collections subdirectory
Hi again Mr. Popov,
another quick suggestion for the next EG release regarding collection file handling.
As I´ve mentioned a while ago, I've been patching collection.js to allow loading collections from any /collections subdirectory relative to the working directory. It's super helpful for organizing strategies across different projects. Here are the exact changes needed:
1. Add 'resolve' to the path imports:
const {join, parse, isAbsolute, resolve} = require("path");
2. Modify the loadCollection function to handle paths relative to the current working directory:
function loadCollection(inputPath, inputKeys, dataId, dataEndTime, suppressInputError) {
if (typeof inputPath === "string" && inputPath.length > 0) {
try {
let collectionPath = replacePlaceholders(inputPath, dataId, dataEndTime);
// If path is not absolute, prepend the collections directory
if (!isAbsolute(collectionPath) &&
(parse(collectionPath).dir === "" || !collectionPath.includes("collections"))) {
collectionPath = join(".", "collections", collectionPath);
}
// Resolve the path relative to the current working directory
collectionPath = resolve(process.cwd(), collectionPath);
// Check if the path exists and is a directory or file
if (existsSync(collectionPath)) {
const stat = statSync(collectionPath);
if (stat.isDirectory()) {
return readCollectionDir(collectionPath, inputKeys);
} else if (stat.isFile()) {
return readCollectionFile(collectionPath, inputKeys);
}
}
// If we reach here, try adding .json extension if missing
if (!collectionPath.toLowerCase().endsWith(".json")) {
collectionPath += ".json";
if (existsSync(collectionPath)) {
return readCollectionFile(collectionPath, inputKeys);
}
}
if (!suppressInputError) {
console.error(`${color.Red}Cannot open input file: ${color.Bright}${collectionPath}${color.Reset}`);
}
return [];
} catch (e) {
console.error(`${color.Red}${color.Bright}${e.message}${color.Reset}`);
return [];
}
} else if (Array.isArray(inputPath) && inputPath.length > 0) {
return inputPath.flatMap(file =>
loadCollection(file, inputKeys, dataId, dataEndTime, suppressInputError)
);
}
return [];
}
This replaces the current path resolution logic in loadCollection and makes it much more flexible. The changes maintain backward compatibility while allowing users to organize their collections in subdirectories.
Would you consider adding this to the next release? Would save me from patching after each update :-)
Thanks as always!