UNPKG

4.92 kBJavaScriptView Raw
1'use strict';
2
3var fs = require("fs");
4
5// Inject coffeescript
6require("coffee-script/register");
7
8/**
9 * Used to autoload all .js or .coffee files from a given subfolder.
10 * Expects an object argument like this:
11 * {
12 * path: <string> [optional] The path where your gulp scripts are saved
13 * moduleConfig: <object> [optional] An object that is passed to your gulp scripts
14 * debug: <bool> [optional] Enables debug logging
15 * }
16 * @param config
17 */
18function autoloader(config) {
19 var logger = getLogger(config);
20
21 logger.log("Initializing");
22
23 // Apply a fallback in case the user did not provide a path
24 logger.logPrefixed(["var", "config::path"], "Checking if var is defined");
25 if (typeof config["path"] === typeof undefined) {
26 logger.logPrefixed(["var", "config::path"], "Var is undefined. Applying fallback");
27 config.path = "./gulp.d";
28 }
29 logger.logPrefixed(["var", "config::path"], "Value present");
30
31 // Check if config.path is a folder
32 logger.logPrefixed(["fs"], "Checking if " + config.path + " is an actual folder");
33 var pathStats = fs.statSync(config.path);
34 if (!pathStats.isDirectory()) {
35 logger.logPrefixed(["fs"], config.path + " is not a folder!");
36 return 1;
37 }
38 logger.logPrefixed(["fs"], "Check successful.");
39
40 // Open folder
41 logger.logPrefixed(["fs"], "Opening " + config.path + "...");
42
43 var files;
44 try {
45 files = fs.readdirSync(config.path);
46 } catch(e) {
47 logger.logPrefixed(["fs"], "Error: It seems like " + config.path + " is not a folder!");
48 return 1;
49 }
50
51 // Load modules
52 logger.logPrefixed(["fs"], "Walking " + config.path + "...");
53 for(var i in files) {
54 //noinspection JSUnfilteredForInLoop
55 var file = files[i];
56 var realpath = process.cwd() + "/" + config.path + '/' + file;
57
58 logger.logPrefixed(["fs", file], "Evaluating file...");
59
60 // Check if the file is JS/CS
61 if(file.indexOf(".js") === -1 && file.indexOf(".coffee") === -1) {
62 logger.logPrefixed(["fs", file], "Skipping! This is neither javascript nor coffee-script.");
63 return 2;
64 }
65
66 // Require the module
67 logger.logPrefixed(["fs", file], "Calling require()...");
68
69 var module;
70 try {
71 module = require(realpath);
72 } catch(e) {
73 logger.logPrefixed(["mod", file], "Error during require() call:");
74 console.log(e);
75 console.log();
76 return 3;
77 }
78
79 // If the module is not returning a function we're done
80 logger.logPrefixed(["fs", file], "Checking if require() returned a function...");
81 if(typeof module !== "function") {
82 logger.logPrefixed(["fs", file], "require() did not return a function. Assuming the presence of side-effects.");
83 logger.logPrefixed(["fs", file, "+O"], "Module loaded.");
84 continue;
85 }
86
87 // Else call the function and apply the moduleConfig
88 //noinspection JSUnresolvedVariable
89 logger.logPrefixed(["fs", file], "require() returned a function! Calling it...");
90 module(config.moduleConfig);
91 logger.logPrefixed(["fs", file, "+F"], "Module loaded.");
92 }
93
94 return 0;
95}
96
97/**
98 * Small getter
99 * @param config
100 * @returns {Function}
101 */
102function getLogger(config) {
103 return {
104 /**
105 * Log a message to the console.
106 * Prefixes msg with [AutoLoader].
107 *
108 * @param msg
109 * @param spacing
110 */
111 log: function(msg, spacing) {
112 if(config["debug"] !== true) {
113 return;
114 }
115
116 if(typeof spacing === typeof undefined) {
117 spacing = false;
118 }
119
120 console.log("[AutoLoader]" + (spacing ? " " : "") + msg)
121 },
122
123 /**
124 * Log a message to the console.
125 * Works like log() but allows more prefixes
126 *
127 * @param prefixes
128 * @param msg
129 */
130 logPrefixed: function(prefixes, msg) {
131 if(config["debug"] !== true) {
132 return;
133 }
134
135 if(typeof prefixes !== typeof []) {
136 prefixes = [];
137 }
138
139 var s = "[AutoLoader]";
140 for(var i = 0; i < prefixes.length; i++) {
141 s += "[" + prefixes[i] + "]";
142 }
143
144 s += " " + msg;
145
146 console.log(s);
147 }
148 }
149}
150
151/**
152 * Export module to nodejs
153 * @param config
154 */
155module.exports = function(config) {
156 var exitCode = autoloader(config);
157
158 if(typeof exitCode !== "number" || exitCode > 0) {
159 var logger = getLogger(config);
160
161 logger.log("Error. Autoloader exited with statuscode " + exitCode);
162 logger.log("This should *not* happen!");
163 logger.log("Please check your configuration.");
164 }
165};
\No newline at end of file