scripts/generator.js
function generateCode (config, is_all_yes) {

	console.log(colors.bold('--------------- Code Generator ---------------\n'));

	// Check the configuration
	if (!config.commands) {
		throw new Error('commands undefined in your config.js');
	}

	// Start the processes
	var entry_js = null, handler_js = null, package_json = null, is_modules_installed = false;
	Promise.resolve()
	.then(function () {

		// Generate the entry point script
		entry_js = generateCodeEntryJs(config);

		// Preview of the entry point script
		console.log(entry_js);

		// To skip mode
		if (is_all_yes) return {isSaveEntryJs: true};

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isSaveEntryJs',
			message: 'The entry point was generated. Would you write it to ' + ENTRY_POINT_FILENAME + ' ?'
		}]);

	}).then(function (answer) {

		if (!answer.isSaveEntryJs) {
			entry_js = null;
			return false;
		}

		// Save the entry point script
		console.log('\nWriting to ' + process.cwd() + '/' + ENTRY_POINT_FILENAME);
		fs.writeFileSync(ENTRY_POINT_FILENAME, entry_js);
		console.log('Writing has been completed.\n\n');

		return true;

	}).then(function (result) {

		// Generate the commands handler script
		handler_js = generateCodeHandlerJs(config);

		// Preview of the commands handler script
		console.log(handler_js);

		// To skip mode
		if (is_all_yes) return {isSaveHandlerJs: true};

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isSaveHandlerJs',
			message: 'The commands handler was generated. Would you write it to ' + HANDLER_FILENAME + ' ?'
		}]);

	}).then(function (answer) {

		if (!answer.isSaveHandlerJs) {
			handler_js = null;
			return false;
		}

		// Save the commands handler script
		console.log('\nWriting to ' + process.cwd() + '/' + HANDLER_FILENAME);
		fs.writeFileSync(HANDLER_FILENAME, handler_js);
		console.log('Writing has been completed.\n\n');

		return true;

	}).then(function (result) {

		// Generate the package.json
		package_json = generatePackageJson(config);
		if (package_json == null) { // If user's package.json exists and invalid
			// Skip
			return { answer: { isSavePackageJson: false} };
		}

		// Preview of the package.json
		console.log(package_json + '\n');

		// To skip mode
		if (is_all_yes) return {isSavePackageJson: true};

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isSavePackageJson',
			message: 'The package.json was generated. Would you write it to package.json ?'
		}]);

	}).then(function (answer) {

		if (!answer.isSavePackageJson) {
			package_json = null;
			return false;
		}

		// Save the commands handler script
		console.log('\nWriting to ' + process.cwd() + '/package.json');
		fs.writeFileSync('package.json', package_json);
		console.log('Writing has been completed.\n\n');
		return true;

	}).then(function (result) {

		// Confirm to whether the dependency modules are installed
		var dir_list = [];
		try {
			dir_list = fs.readdirSync('./node_modules/');
		} catch (e) {
			dir_list = [];
		}

		is_modules_installed = false;
		dir_list.forEach(function (name, i) {
			if (name == 'denhub-device') {
				is_modules_installed = true;
			}
		});

		if (is_modules_installed || is_all_yes) {
			return {isDependencyInstall: false};
		}

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isDependencyInstall',
			message: 'The required modules are not installed. Would you install it now ?'
		}]);

	}).then(function (answer) {

		if (!answer.isDependencyInstall) {
			return false;
		}

		// Install the dependency modules
		is_modules_installed = true;
		return execDependencyInstall();

	}).then(function (result) {

		console.log(colors.bold.green('All was completed.'));

		if (!entry_js && !handler_js) {
			console.log('');
			process.exit(0);
			return;
		}

		// Show the guide
		console.log(colors.bold('Enjoy :)\n'));
		if (!is_modules_installed) console.log('$ npm install');
		console.log('$ npm start -- --dev\n');
		console.log('For details, please refer to https://github.com/odentools/denhub-device/\n');

	});

}
function generateCode (config, is_all_yes) {

	console.log(colors.bold('--------------- Code Generator ---------------\n'));

	// Check the configuration
	if (!config.commands) {
		throw new Error('commands undefined in your config.js');
	}

	// Start the processes
	var entry_js = null, handler_js = null, package_json = null, is_modules_installed = false;
	Promise.resolve()
	.then(function () {

		// Generate the entry point script
		entry_js = generateCodeEntryJs(config);

		// Preview of the entry point script
		console.log(entry_js);

		// To skip mode
		if (is_all_yes) return {isSaveEntryJs: true};

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isSaveEntryJs',
			message: 'The entry point was generated. Would you write it to ' + ENTRY_POINT_FILENAME + ' ?'
		}]);

	}).then(function (answer) {

		if (!answer.isSaveEntryJs) {
			entry_js = null;
			return false;
		}

		// Save the entry point script
		console.log('\nWriting to ' + process.cwd() + '/' + ENTRY_POINT_FILENAME);
		fs.writeFileSync(ENTRY_POINT_FILENAME, entry_js);
		console.log('Writing has been completed.\n\n');

		return true;

	}).then(function (result) {

		// Generate the commands handler script
		handler_js = generateCodeHandlerJs(config);

		// Preview of the commands handler script
		console.log(handler_js);

		// To skip mode
		if (is_all_yes) return {isSaveHandlerJs: true};

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isSaveHandlerJs',
			message: 'The commands handler was generated. Would you write it to ' + HANDLER_FILENAME + ' ?'
		}]);

	}).then(function (answer) {

		if (!answer.isSaveHandlerJs) {
			handler_js = null;
			return false;
		}

		// Save the commands handler script
		console.log('\nWriting to ' + process.cwd() + '/' + HANDLER_FILENAME);
		fs.writeFileSync(HANDLER_FILENAME, handler_js);
		console.log('Writing has been completed.\n\n');

		return true;

	}).then(function (result) {

		// Generate the package.json
		package_json = generatePackageJson(config);
		if (package_json == null) { // If user's package.json exists and invalid
			// Skip
			return { answer: { isSavePackageJson: false} };
		}

		// Preview of the package.json
		console.log(package_json + '\n');

		// To skip mode
		if (is_all_yes) return {isSavePackageJson: true};

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isSavePackageJson',
			message: 'The package.json was generated. Would you write it to package.json ?'
		}]);

	}).then(function (answer) {

		if (!answer.isSavePackageJson) {
			package_json = null;
			return false;
		}

		// Save the commands handler script
		console.log('\nWriting to ' + process.cwd() + '/package.json');
		fs.writeFileSync('package.json', package_json);
		console.log('Writing has been completed.\n\n');
		return true;

	}).then(function (result) {

		// Confirm to whether the dependency modules are installed
		var dir_list = [];
		try {
			dir_list = fs.readdirSync('./node_modules/');
		} catch (e) {
			dir_list = [];
		}

		is_modules_installed = false;
		dir_list.forEach(function (name, i) {
			if (name == 'denhub-device') {
				is_modules_installed = true;
			}
		});

		if (is_modules_installed || is_all_yes) {
			return {isDependencyInstall: false};
		}

		// Confirm to user
		return inquirer.prompt([{
			type: 'confirm',
			name: 'isDependencyInstall',
			message: 'The required modules are not installed. Would you install it now ?'
		}]);

	}).then(function (answer) {

		if (!answer.isDependencyInstall) {
			return false;
		}

		// Install the dependency modules
		is_modules_installed = true;
		return execDependencyInstall();

	}).then(function (result) {

		console.log(colors.bold.green('All was completed.'));

		if (!entry_js && !handler_js) {
			console.log('');
			process.exit(0);
			return;
		}

		// Show the guide
		console.log(colors.bold('Enjoy :)\n'));
		if (!is_modules_installed) console.log('$ npm install');
		console.log('$ npm start -- --dev\n');
		console.log('For details, please refer to https://github.com/odentools/denhub-device/\n');

	});

}
function generateCode ( config , is_all_yes ) { console . log ( colors . bold ( '--------------- Code Generator ---------------\n' ) ) ; // Check the configuration if ( ! config . commands ) { throw new Error ( 'commands undefined in your config.js' ) ; } // Start the processes var entry_js = null , handler_js = null , package_json = null , is_modules_installed = false ; Promise . resolve ( ) . then ( function ( ) { // Generate the entry point script entry_js = generateCodeEntryJs ( config ) ; // Preview of the entry point script console . log ( entry_js ) ; // To skip mode if ( is_all_yes ) return { isSaveEntryJs : true } ; // Confirm to user return inquirer . prompt ( [ { type : 'confirm' , name : 'isSaveEntryJs' , message : 'The entry point was generated. Would you write it to ' + ENTRY_POINT_FILENAME + ' ?' } ] ) ; } ) . then ( function ( answer ) { if ( ! answer . isSaveEntryJs ) { entry_js = null ; return false ; } // Save the entry point script console . log ( '\nWriting to ' + process . cwd ( ) + '/' + ENTRY_POINT_FILENAME ) ; fs . writeFileSync ( ENTRY_POINT_FILENAME , entry_js ) ; console . log ( 'Writing has been completed.\n\n' ) ; return true ; } ) . then ( function ( result ) { // Generate the commands handler script handler_js = generateCodeHandlerJs ( config ) ; // Preview of the commands handler script console . log ( handler_js ) ; // To skip mode if ( is_all_yes ) return { isSaveHandlerJs : true } ; // Confirm to user return inquirer . prompt ( [ { type : 'confirm' , name : 'isSaveHandlerJs' , message : 'The commands handler was generated. Would you write it to ' + HANDLER_FILENAME + ' ?' } ] ) ; } ) . then ( function ( answer ) { if ( ! answer . isSaveHandlerJs ) { handler_js = null ; return false ; } // Save the commands handler script console . log ( '\nWriting to ' + process . cwd ( ) + '/' + HANDLER_FILENAME ) ; fs . writeFileSync ( HANDLER_FILENAME , handler_js ) ; console . log ( 'Writing has been completed.\n\n' ) ; return true ; } ) . then ( function ( result ) { // Generate the package.json package_json = generatePackageJson ( config ) ; if ( package_json == null ) { // If user's package.json exists and invalid // Skip return { answer : { isSavePackageJson : false } } ; } // Preview of the package.json console . log ( package_json + '\n' ) ; // To skip mode if ( is_all_yes ) return { isSavePackageJson : true } ; // Confirm to user return inquirer . prompt ( [ { type : 'confirm' , name : 'isSavePackageJson' , message : 'The package.json was generated. Would you write it to package.json ?' } ] ) ; } ) . then ( function ( answer ) { if ( ! answer . isSavePackageJson ) { package_json = null ; return false ; } // Save the commands handler script console . log ( '\nWriting to ' + process . cwd ( ) + '/package.json' ) ; fs . writeFileSync ( 'package.json' , package_json ) ; console . log ( 'Writing has been completed.\n\n' ) ; return true ; } ) . then ( function ( result ) { // Confirm to whether the dependency modules are installed var dir_list = [ ] ; try { dir_list = fs . readdirSync ( './node_modules/' ) ; } catch ( e ) { dir_list = [ ] ; } is_modules_installed = false ; dir_list . forEach ( function ( name , i ) { if ( name == 'denhub-device' ) { is_modules_installed = true ; } } ) ; if ( is_modules_installed || is_all_yes ) { return { isDependencyInstall : false } ; } // Confirm to user return inquirer . prompt ( [ { type : 'confirm' , name : 'isDependencyInstall' , message : 'The required modules are not installed. Would you install it now ?' } ] ) ; } ) . then ( function ( answer ) { if ( ! answer . isDependencyInstall ) { return false ; } // Install the dependency modules is_modules_installed = true ; return execDependencyInstall ( ) ; } ) . then ( function ( result ) { console . log ( colors . bold . green ( 'All was completed.' ) ) ; if ( ! entry_js && ! handler_js ) { console . log ( '' ) ; process . exit ( 0 ) ; return ; } // Show the guide console . log ( colors . bold ( 'Enjoy :)\n' ) ) ; if ( ! is_modules_installed ) console . log ( '$ npm install' ) ; console . log ( '$ npm start -- --dev\n' ) ; console . log ( 'For details, please refer to https://github.com/odentools/denhub-device/\n' ) ; } ) ; }
Generate the source code for device daemon
@param  {Object} config      Device configuration
@param  {Boolea} is_all_yes  Whether the response will choose yes automatically
Generate the source code for device daemon