scripts/generator.js
function generateCodeHandlerJs (config) {

	// Read the handler script
	var handler_js = null;
	try {
		// Read from current script
		handler_js = fs.readFileSync(HANDLER_FILENAME).toString();
	} catch (e) {
		// Read from template script
		handler_js = fs.readFileSync(__dirname + '/../templates/' + HANDLER_FILENAME + '.tmpl').toString();
	}

	// Iterate the each commands
	for (var cmd_name in config.commands) {

		if (!cmd_name.match(/^[a-zA-Z][a-zA-Z0-9]+$/)) {
			throw new Error('Defined method name is invalid format: ' + cmd_name);
		}

		if (handler_js.indexOf(cmd_name) != -1) { // handler for this command was written
			continue; // skip
		}

		// Make a handler for this command
		var cmd = config.commands[cmd_name];
		var js_func = '\n\n\
/**\n\
* %cmdDescription%\n\
* @param  {Object} args         Arguments of the received command\n\
* @param  {Function} cb_runner  Callback runner for response\n\
* @return {Boolean} if returns true, handler indicates won\'t use the callback \n\
*/\n\
CommandsHandler.prototype.' + cmd_name + ' = function (args, cb_runner) {\n\
\t\n';

		// To align the line length of argument examples
		var cmd_args = cmd.arguments || cmd.args || [];
		var longest_length_of_arg_name = 0;
		for (var name_ in cmd_args) {
			if (longest_length_of_arg_name < name_.length) longest_length_of_arg_name = name_.length;
		}

		// Generate the argument examples
		for (var name in cmd_args) {

			// Generate an example
			var line = '\tthis.logger.log(args.' + name + ');';

			// Generate a comment
			var comment_spaces = new String(' ');
			for (var i = 0, l = longest_length_of_arg_name - name.length; i < l; i++) {
				comment_spaces += ' ';
			}
			var comment = '// ' + cmd_args[name];

			js_func += line + comment_spaces + comment + '\n';

		}

		if (0 < Object.keys(cmd_args).length) {
			js_func += '\t\n';
		}

		js_func += '\tcb_runner.send(null, \'OKAY\');\n\t\n\};\n';

		// Replace the command placeholders of the commands handler script
		js_func = js_func.replace(/\%cmdName\%/g, cmd_name);
		if (cmd.description) {
			js_func = js_func.replace(/\%cmdDescription\%/g, cmd.description);
		}

		// Done
		handler_js += js_func;

	}

	// Replace the placeholders (e.g. %deviceType%) of whole of commands handler script
	for (var config_key in config) {
		handler_js = handler_js.replace(new RegExp('\\%' + config_key + '\\%', 'g'), config[config_key]);
	}

	// Append the module.exports
	if (!handler_js.match(/module\.exports = CommandsHandler;/)) {
		handler_js += '\n\nmodule.exports = CommandsHandler;\n';
	}

	return handler_js;

}
function generateCodeHandlerJs (config) {

	// Read the handler script
	var handler_js = null;
	try {
		// Read from current script
		handler_js = fs.readFileSync(HANDLER_FILENAME).toString();
	} catch (e) {
		// Read from template script
		handler_js = fs.readFileSync(__dirname + '/../templates/' + HANDLER_FILENAME + '.tmpl').toString();
	}

	// Iterate the each commands
	for (var cmd_name in config.commands) {

		if (!cmd_name.match(/^[a-zA-Z][a-zA-Z0-9]+$/)) {
			throw new Error('Defined method name is invalid format: ' + cmd_name);
		}

		if (handler_js.indexOf(cmd_name) != -1) { // handler for this command was written
			continue; // skip
		}

		// Make a handler for this command
		var cmd = config.commands[cmd_name];
		var js_func = '\n\n\
/**\n\
* %cmdDescription%\n\
* @param  {Object} args         Arguments of the received command\n\
* @param  {Function} cb_runner  Callback runner for response\n\
* @return {Boolean} if returns true, handler indicates won\'t use the callback \n\
*/\n\
CommandsHandler.prototype.' + cmd_name + ' = function (args, cb_runner) {\n\
\t\n';

		// To align the line length of argument examples
		var cmd_args = cmd.arguments || cmd.args || [];
		var longest_length_of_arg_name = 0;
		for (var name_ in cmd_args) {
			if (longest_length_of_arg_name < name_.length) longest_length_of_arg_name = name_.length;
		}

		// Generate the argument examples
		for (var name in cmd_args) {

			// Generate an example
			var line = '\tthis.logger.log(args.' + name + ');';

			// Generate a comment
			var comment_spaces = new String(' ');
			for (var i = 0, l = longest_length_of_arg_name - name.length; i < l; i++) {
				comment_spaces += ' ';
			}
			var comment = '// ' + cmd_args[name];

			js_func += line + comment_spaces + comment + '\n';

		}

		if (0 < Object.keys(cmd_args).length) {
			js_func += '\t\n';
		}

		js_func += '\tcb_runner.send(null, \'OKAY\');\n\t\n\};\n';

		// Replace the command placeholders of the commands handler script
		js_func = js_func.replace(/\%cmdName\%/g, cmd_name);
		if (cmd.description) {
			js_func = js_func.replace(/\%cmdDescription\%/g, cmd.description);
		}

		// Done
		handler_js += js_func;

	}

	// Replace the placeholders (e.g. %deviceType%) of whole of commands handler script
	for (var config_key in config) {
		handler_js = handler_js.replace(new RegExp('\\%' + config_key + '\\%', 'g'), config[config_key]);
	}

	// Append the module.exports
	if (!handler_js.match(/module\.exports = CommandsHandler;/)) {
		handler_js += '\n\nmodule.exports = CommandsHandler;\n';
	}

	return handler_js;

}
function generateCodeHandlerJs ( config ) { // Read the handler script var handler_js = null ; try { // Read from current script handler_js = fs . readFileSync ( HANDLER_FILENAME ) . toString ( ) ; } catch ( e ) { // Read from template script handler_js = fs . readFileSync ( __dirname + '/../templates/' + HANDLER_FILENAME + '.tmpl' ) . toString ( ) ; } // Iterate the each commands for ( var cmd_name in config . commands ) { if ( ! cmd_name . match ( / ^[a-zA-Z][a-zA-Z0-9]+$ / ) ) { throw new Error ( 'Defined method name is invalid format: ' + cmd_name ) ; } if ( handler_js . indexOf ( cmd_name ) != - 1 ) { // handler for this command was written continue ; // skip } // Make a handler for this command var cmd = config . commands [ cmd_name ] ; var js_func = '\n\n\
/**\n\
* %cmdDescription%\n\
* @param  {Object} args         Arguments of the received command\n\
* @param  {Function} cb_runner  Callback runner for response\n\
* @return {Boolean} if returns true, handler indicates won\'t use the callback \n\
*/\n\
CommandsHandler.prototype.' + cmd_name + ' = function (args, cb_runner) {\n\
\t\n' ; // To align the line length of argument examples var cmd_args = cmd . arguments || cmd . args || [ ] ; var longest_length_of_arg_name = 0 ; for ( var name_ in cmd_args ) { if ( longest_length_of_arg_name < name_ . length ) longest_length_of_arg_name = name_ . length ; } // Generate the argument examples for ( var name in cmd_args ) { // Generate an example var line = '\tthis.logger.log(args.' + name + ');' ; // Generate a comment var comment_spaces = new String ( ' ' ) ; for ( var i = 0 , l = longest_length_of_arg_name - name . length ; i < l ; i ++ ) { comment_spaces += ' ' ; } var comment = '// ' + cmd_args [ name ] ; js_func += line + comment_spaces + comment + '\n' ; } if ( 0 < Object . keys ( cmd_args ) . length ) { js_func += '\t\n' ; } js_func += '\tcb_runner.send(null, \'OKAY\');\n\t\n\};\n' ; // Replace the command placeholders of the commands handler script js_func = js_func . replace ( / \%cmdName\% / g , cmd_name ) ; if ( cmd . description ) { js_func = js_func . replace ( / \%cmdDescription\% / g , cmd . description ) ; } // Done handler_js += js_func ; } // Replace the placeholders (e.g. %deviceType%) of whole of commands handler script for ( var config_key in config ) { handler_js = handler_js . replace ( new RegExp ( '\\%' + config_key + '\\%' , 'g' ) , config [ config_key ] ) ; } // Append the module.exports if ( ! handler_js . match ( / module\.exports = CommandsHandler; / ) ) { handler_js += '\n\nmodule.exports = CommandsHandler;\n' ; } return handler_js ; }
Generate the source code for commands handler
@param  {Object} config Device configuration
@return {String}        Source code
Generate the source code for commands handler