/** * Copyright 2014 IBM Corp. * * 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 util = require("util"); var when = require("when"); var whenNode = require('when/node'); var fs = require("fs"); var path = require("path"); var cheerio = require("cheerio"); var UglifyJS = require("uglify-js"); var events = require("../events"); var Node; var settings; var node_types = {}; var node_configs = []; var node_scripts = []; /** * 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 getNodeFiles(dir) { var result = []; var files = fs.readdirSync(dir); files.sort(); files.forEach(function(fn) { var stats = fs.statSync(path.join(dir,fn)); if (stats.isFile()) { if (/\.js$/.test(fn)) { result.push(path.join(dir,fn)); } } else if (stats.isDirectory()) { // Ignore /.dirs/, /lib/ /node_modules/ if (!/^(\..*|lib|icons|node_modules|test)$/.test(fn)) { result = result.concat(getNodeFiles(path.join(dir,fn))); } else if (fn === "icons") { events.emit("node-icon-dir",path.join(dir,fn)); } } }); return result; } /** * Scans the node_modules path for nodes * @return a list of node modules: {dir,package} */ function scanTreeForNodesModules() { var dir = __dirname+"/../../nodes"; var results = []; var up = path.resolve(path.join(dir,"..")); while (up !== dir) { var pm = path.join(dir,"node_modules"); try { var files = fs.readdirSync(pm); files.forEach(function(fn) { var pkgfn = path.join(pm,fn,"package.json"); try { var pkg = require(pkgfn); if (pkg['node-red']) { var moduleDir = path.join(pm,fn); results.push({dir:moduleDir,package:pkg}); } } catch(err) { if (err.code != "MODULE_NOT_FOUND") { // TODO: handle unexpected error } } }); } catch(err) { } dir = up; up = path.resolve(path.join(dir,"..")); } return results; } /** * Loads the specified node into the registry. * @param nodeDir the directory containing the node * @param nodeFn the node file * @param nodeLabel the name of the node (npm nodes only) * @return a promise that resolves to either {fn,path,err} */ function loadNode(nodeDir, nodeFn, nodeLabel) { if (settings.nodesExcludes) { for (var i=0;i"; if (el.attribs) { for (var j in el.attribs) { if (el.attribs.hasOwnProperty(j)) { openTag += " "+j+'="'+el.attribs[j]+'"'; } } } openTag += ">"; template += openTag+$(el).text()+closeTag; } }); node_configs.push(template); } /** * Gets all of the node template configs * @return all of the node templates in a single string */ function getNodeConfigs() { var result = ""; for (var i=0;i