<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forex Software — Loading collections from any /collections subdirectory]]></title>
	<link rel="self" href="https://forexsb.com/forum/feed/atom/topic/9885/" />
	<updated>2024-11-19T23:45:00Z</updated>
	<generator>PunBB</generator>
	<id>https://forexsb.com/forum/topic/9885/loading-collections-from-any-collections-subdirectory/</id>
		<entry>
			<title type="html"><![CDATA[Re: Loading collections from any /collections subdirectory]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/81913/#p81913" />
			<content type="html"><![CDATA[<p>Thank you :-)</p>]]></content>
			<author>
				<name><![CDATA[geektrader]]></name>
				<uri>https://forexsb.com/forum/user/1841/</uri>
			</author>
			<updated>2024-11-19T23:45:00Z</updated>
			<id>https://forexsb.com/forum/post/81913/#p81913</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Loading collections from any /collections subdirectory]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/81883/#p81883" />
			<content type="html"><![CDATA[<p>&gt; It&#039;s super helpful for organizing strategies across different projects. <br />It is a good idea!</p><p>This can be better implemented with additional options.</p><p>Something like: <br /> <strong>--input-dir</strong> /path/to/my/collections<br /> <strong>--input-pattern</strong> [SERVER]-[SYMBOL]<br /> <strong>--input-recursive</strong> true</p><p>I may think of it.</p>]]></content>
			<author>
				<name><![CDATA[Popov]]></name>
				<uri>https://forexsb.com/forum/user/2/</uri>
			</author>
			<updated>2024-11-17T10:07:21Z</updated>
			<id>https://forexsb.com/forum/post/81883/#p81883</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Loading collections from any /collections subdirectory]]></title>
			<link rel="alternate" href="https://forexsb.com/forum/post/81872/#p81872" />
			<content type="html"><![CDATA[<p>Hi again Mr. Popov,</p><p>another quick suggestion for the next EG release regarding collection file handling.</p><p>As I´ve mentioned a while ago, I&#039;ve been patching collection.js to allow loading collections from any /collections subdirectory relative to the working directory. It&#039;s super helpful for organizing strategies across different projects. Here are the exact changes needed:</p><p>1. Add &#039;resolve&#039; to the path imports:</p><div class="codebox"><pre><code>const {join, parse, isAbsolute, resolve} = require(&quot;path&quot;);</code></pre></div><p>2. Modify the loadCollection function to handle paths relative to the current working directory:</p><div class="codebox"><pre><code>function loadCollection(inputPath, inputKeys, dataId, dataEndTime, suppressInputError) {
    if (typeof inputPath === &quot;string&quot; &amp;&amp; inputPath.length &gt; 0) {
        try {
            let collectionPath = replacePlaceholders(inputPath, dataId, dataEndTime);

            // If path is not absolute, prepend the collections directory
            if (!isAbsolute(collectionPath) &amp;&amp; 
                (parse(collectionPath).dir === &quot;&quot; || !collectionPath.includes(&quot;collections&quot;))) {
                collectionPath = join(&quot;.&quot;, &quot;collections&quot;, 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(&quot;.json&quot;)) {
                collectionPath += &quot;.json&quot;;
                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) &amp;&amp; inputPath.length &gt; 0) {
        return inputPath.flatMap(file =&gt; 
            loadCollection(file, inputKeys, dataId, dataEndTime, suppressInputError)
        );
    }

    return [];
}</code></pre></div><p>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.</p><p>Would you consider adding this to the next release? Would save me from patching after each update :-)</p><p>Thanks as always!</p>]]></content>
			<author>
				<name><![CDATA[geektrader]]></name>
				<uri>https://forexsb.com/forum/user/1841/</uri>
			</author>
			<updated>2024-11-15T11:57:05Z</updated>
			<id>https://forexsb.com/forum/post/81872/#p81872</id>
		</entry>
</feed>
