# all files / red/runtime/nodes/registry/ localfilesystem.js

94.41% Statements 152/161
87.5% Branches 63/72
94.12% Functions 16/17
95% Lines 152/160
1 branch Ignored

    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
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312

    1×
    1×
    1×

    1×
    1×
    1×

    1×
    1×

    1×
    21×
    21×
    21×
    21×

    1×
    63×
    8×
    8×
    2×

    55×

    6×

    1×
    57×
    8×
    8×
    2×

    55×

    1×
    52×
    8×

    44×
    44×
    18×

    26×

    1×
    49×

    49×
    49×
    49×
    49×

    49×
    49×
    127×
    127×
    77×
    52×
    52×
    18×

    50×

    50×
    37×
    13×
    3×

    49×

    1×
    4×
    4×
    4×
    4×
    11×
    11×

    11×
    5×
    5×
    5×
    4×
    4×
    4×

    1×

    5×
    2×

    4×

    1×
    4×
    4×
    4×

    4×

    4×
    4×
    4×
    4×
    4×
    4×

    4×
    4×

    4×

    1×

    4×
    4×

    4×
    4×
    4×

    4×

    8×
    8×
    8×

    8×
    8×
    4×
    4×
    4×
    4×

    4×
    4×
    4×

    4×

    1×
    8×

    8×

    8×
    8×
    8×
    8×

    8×
    1×
    1×

    8×
    3×
    3×
    2×

    3×
    3×

    8×

    8×
    18×

    8×
    1×
    1×
    2×
    2×

    2×
    1×

    2×
    4×
    4×

    2×

    7×

    8×

    1×
    3×

    3×
    3×
    1×

    2×
    2×
    2×

    2×
    1×

    2×
    4×
    4×

    2×

    1×

     /**
     * Copyright JS Foundation and other contributors, http://js.foundation
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     **/

    var when = require("when");
    var fs = require("fs");
    var path = require("path");

    var events;
    var log;
    var i18n;

    var settings;
    var disableNodePathScan = false;

    function init(runtime) {
        settings = runtime.settings;
        events = runtime.events;
        log = runtime.log;
        i18n = runtime.i18n;
    }

    function isIncluded(name) {
        if (settings.nodesIncludes) {
            for (var i=0;i<settings.nodesIncludes.length;i++) {
                if (settings.nodesIncludes[i] == name) {
                    return true;
                }
            }
        } else {
            return true;
        }
        return false;
    }

    function isExcluded(name) {
         if (settings.nodesExcludes) {
            for (var i=0;i<settings.nodesExcludes.length;i++) {
                if (settings.nodesExcludes[i] == name) {
                    return true;
                }
            }
        }
        return false;
    }
    function getLocalFile(file) {
        if (!isIncluded(path.basename(file)) || isExcluded(path.basename(file))) {
            return null;
        }
        try {
            fs.statSync(file.replace(/\.js$/,".html"));
            return {
                file:    file,
                module:  "node-red",
                name:    path.basename(file).replace(/^\d+-/,"").replace(/\.js$/,""),
                version: settings.version
            };
        } catch(err) {
            return null;
        }
    }

    /**
     * Synchronously walks the directory looking for node files.
     * Emits 'node-icon-dir' events for an icon dirs found
     * @param dir the directory to search
     * @return an array of fully-qualified paths to .js files
     */
    function getLocalNodeFiles(dir) {
        dir = path.resolve(dir);

        var result = [];
        var files = [];
        try {
            files = fs.readdirSync(dir);
        } catch(err) {
            return result;
        }
        files.sort();
        files.forEach(function(fn) {
            var stats = fs.statSync(path.join(dir,fn));
            if (stats.isFile()) {
                if (/\.js$/.test(fn)) {
                    var info = getLocalFile(path.join(dir,fn));
                    if (info) {
                        result.push(info);
                    }
                }
            } else Eif (stats.isDirectory()) {
                // Ignore /.dirs/, /lib/ /node_modules/
                if (!/^(\..*|lib|icons|node_modules|test|locales)$/.test(fn)) {
                    result = result.concat(getLocalNodeFiles(path.join(dir,fn)));
                } else if (fn === "icons") {
                    events.emit("node-icon-dir",{name:'node-red',path:path.join(dir,fn)});
                }
            }
        });
        return result;
    }

    function scanDirForNodesModules(dir,moduleName) {
        var results = [];
        try {
            var files = fs.readdirSync(dir);
            for (var i=0;i<files.length;i++) {
                var fn = files[i];
                Iif (/^@/.test(fn)) {
                    results = results.concat(scanDirForNodesModules(path.join(dir,fn),moduleName));
                } else {
                    if (isIncluded(fn) && !isExcluded(fn) && (!moduleName || fn == moduleName)) {
                        var pkgfn = path.join(dir,fn,"package.json");
                        try {
                            var pkg = require(pkgfn);
                            Eif (pkg['node-red']) {
                                var moduleDir = path.join(dir,fn);
                                results.push({dir:moduleDir,package:pkg});
                            }
                        } catch(err) {
                            Iif (err.code != "MODULE_NOT_FOUND") {
                                // TODO: handle unexpected error
                            }
                        }
                        if (fn == moduleName) {
                            break;
                        }
                    }
                }
            }
        } catch(err) {
        }
        return results;
    }

    /**
     * Scans the node_modules path for nodes
     * @param moduleName the name of the module to be found
     * @return a list of node modules: {dir,package}
     */
    function scanTreeForNodesModules(moduleName) {
        var dir = settings.coreNodesDir;
        var results = [];
        var userDir;

        Iif (settings.userDir) {
            userDir = path.join(settings.userDir,"node_modules");
            results = scanDirForNodesModules(userDir,moduleName);
            results.forEach(function(r) { r.local = true; });
        }

        Eif (dir) {
            var up = path.resolve(path.join(dir,".."));
            while (up !== dir) {
                var pm = path.join(dir,"node_modules");
                Eif (pm != userDir) {
                    results = results.concat(scanDirForNodesModules(pm,moduleName));
                }
                dir = up;
                up = path.resolve(path.join(dir,".."));
            }
        }
        return results;
    }

    function getModuleNodeFiles(module) {

        var moduleDir = module.dir;
        var pkg = module.package;

        var nodes = pkg['node-red'].nodes||{};
        var results = [];
        var iconDirs = [];

        for (var n in nodes) {
            /* istanbul ignore else */
            Eif (nodes.hasOwnProperty(n)) {
                var file = path.join(moduleDir,nodes[n]);
                results.push({
                    file:    file,
                    module:  pkg.name,
                    name:    n,
                    version: pkg.version
                });
                var iconDir = path.join(moduleDir,path.dirname(nodes[n]),"icons");
                if (iconDirs.indexOf(iconDir) == -1) {
                    try {
                        fs.statSync(iconDir);
                        events.emit("node-icon-dir",{name:pkg.name,path:iconDir});
                        iconDirs.push(iconDir);
                    } catch(err) {
                    }
                }
            }
        }
        var examplesDir = path.join(moduleDir,"examples");
        try {
            fs.statSync(examplesDir)
            events.emit("node-examples-dir",{name:pkg.name,path:examplesDir});
        } catch(err) {
        }
        return results;
    }

    function getNodeFiles(disableNodePathScan) {
        var dir;
        // Find all of the nodes to load
        var nodeFiles = [];

        Eif (settings.coreNodesDir) {
            nodeFiles = getLocalNodeFiles(path.resolve(settings.coreNodesDir));
            var defaultLocalesPath = path.join(settings.coreNodesDir,"core","locales");
            i18n.registerMessageCatalog("node-red",defaultLocalesPath,"messages.json");
        }

        if (settings.userDir) {
            dir = path.join(settings.userDir,"nodes");
            nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir));
        }
        if (settings.nodesDir) {
            dir = settings.nodesDir;
            if (typeof settings.nodesDir == "string") {
                dir = [dir];
            }
            for (var i=0;i<dir.length;i++) {
                nodeFiles = nodeFiles.concat(getLocalNodeFiles(dir[i]));
            }
        }

        var nodeList = {
            "node-red": {
                name: "node-red",
                version: settings.version,
                nodes: {}
            }
        }
        nodeFiles.forEach(function(node) {
            nodeList["node-red"].nodes[node.name] = node;
        });

        if (!disableNodePathScan) {
            var moduleFiles = scanTreeForNodesModules();
            moduleFiles.forEach(function(moduleFile) {
                var nodeModuleFiles = getModuleNodeFiles(moduleFile);
                nodeList[moduleFile.package.name] = {
                    name: moduleFile.package.name,
                    version: moduleFile.package.version,
                    local: moduleFile.local||false,
                    nodes: {}
                };
                if (moduleFile.package['node-red'].version) {
                    nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
                }
                nodeModuleFiles.forEach(function(node) {
                    node.local = moduleFile.local||false;
                    nodeList[moduleFile.package.name].nodes[node.name] = node;
                });
                nodeFiles = nodeFiles.concat(nodeModuleFiles);
            });
        } else {
            console.log("node path scan disabled");
        }
        return nodeList;
    }

    function getModuleFiles(module) {
        var nodeList = {};

        var moduleFiles = scanTreeForNodesModules(module);
        if (moduleFiles.length === 0) {
            var err = new Error(log._("nodes.registry.localfilesystem.module-not-found", {module:module}));
            err.code = 'MODULE_NOT_FOUND';
            throw err;
        }

        moduleFiles.forEach(function(moduleFile) {
            var nodeModuleFiles = getModuleNodeFiles(moduleFile);
            nodeList[moduleFile.package.name] = {
                name: moduleFile.package.name,
                version: moduleFile.package.version,
                nodes: {}
            };
            if (moduleFile.package['node-red'].version) {
                nodeList[moduleFile.package.name].redVersion = moduleFile.package['node-red'].version;
            }
            nodeModuleFiles.forEach(function(node) {
                nodeList[moduleFile.package.name].nodes[node.name] = node;
                nodeList[moduleFile.package.name].nodes[node.name].local = moduleFile.local || false;
            });
        });
        return nodeList;
    }

    module.exports = {
        init: init,
        getNodeFiles: getNodeFiles,
        getLocalFile: getLocalFile,
        getModuleFiles: getModuleFiles
    }