docsforadobe / aequery

MIT License
41 stars 8 forks source link

Implement/adapt recursiveGetFiles into aeq.file #34

Closed runegan closed 6 years ago

runegan commented 7 years ago

Original report by Zack Lovatt (Bitbucket: zlovatt, GitHub: zlovatt).


Something like this? Or as a flag to the existing aeq.file.getFiles() function

    /**
     * Recursively scan folder for all files matching extension
     *
     * @param {Folder} folder Root folder to serach in
     * @param {string} extension File extension to search
     * @returns {File[]} Array of found files
     */
    function recursiveGetFiles (folder, extension) {
        var foundItems = aeq.arrayEx();

        var folderObject = aeq.file.getFolder(folder);
        if (aeq.isNullOrUndefined(folderObject))
            return foundItems;

        var items = aeq.file.getFiles(folderObject);
        if (aeq.isNullOrUndefined(items))
            return foundItems;

        items.forEach(function (item) {
            if (aeq.isFolder(item)) {
                foundItems = foundItems.concat(recursiveGetFiles(item, extension));
            } else if (aeq.file.getExtension(item) === extension){
                foundItems.push(item);
            }
        });

        return foundItems;
    }
runegan commented 7 years ago

Original comment by Rune Gangsø (Bitbucket: runegan, GitHub: runegan).


I would like it to use a filter string or function, like file.getFiles does, instead of giving it an extension.

runegan commented 7 years ago

Original comment by Zack Lovatt (Bitbucket: zlovatt, GitHub: zlovatt).


Here's a version w/ filter--

    /**
     * Recursively scan folder for all files matching filter
     *
     * @param {Folder|String}   folder - Folder or path to get files from
     * @param {String|Function} filter - Filter string or function
     * @returns {File[]}               - Array of found files
     */
    function recursiveGetFiles (folder, filter) {
        var foundItems = aeq.arrayEx();

        var folderObject = aeq.file.getFolder(folder);
        if (aeq.isNullOrUndefined(folderObject))
            return foundItems;

        // Find all folders within folderObject, and recursively append anything it finds to foundItems
        aeq.file.getFiles(folderObject).filter(function (item) {
            return aeq.isFolder(item);
        }).forEach(function (folderItem) {
            foundItems = foundItems.concat(recursiveGetFiles(folderItem, filter));
        });

        var filesInFolder = aeq.file.getFiles(folderObject, filter);
        if (!aeq.isNullOrUndefined(filesInFolder))
            foundItems = foundItems.concat(filesInFolder);

        return foundItems;
    }
runegan commented 6 years ago

Original comment by Zack Lovatt (Bitbucket: zlovatt, GitHub: zlovatt).


Adds getFilesRecursive to aeq.file (resolve #34)