<commit_before>/* moleculer
 * Copyright (c) 2019 MoleculerJS (https://github.com/moleculerjs/moleculer)
 * MIT Licensed
 */

"use strict";

const ServiceBroker	= require("./service-broker");
const utils			= require("./utils");
const fs 			= require("fs");
const path 			= require("path");
const glob 			= require("glob").sync;
const _ 			= require("lodash");
const Args 			= require("args");
const os			= require("os");
const cluster		= require("cluster");
const kleur			= require("kleur");

const stopSignals = [
	"SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT",
	"SIGBUS", "SIGFPE", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGTERM"
];

/* eslint-disable no-console */

/**
 * Logger helper
 *
 */
const logger = {
	info(message) {
		console.log(kleur.grey("[Runner]"), kleur.green().bold(message));
	},
	error(err) {
		if (err instanceof Error)
			console.error(kleur.grey("[Runner]"), kleur.red().bold(err.message), err);
		else
			console.error(kleur.grey("[Runner]"), kleur.red().bold(err));
	}
};

class MoleculerRunner {

	constructor() {
		this.watchFolders = [];

		this.flags = null;
		this.configFile = null;
		this.config = null;
		this.servicePaths = null;
		this.broker = null;
		this.worker = null;
	}

	/**
	 * Process command line arguments
	 *
	 * Available options:
		-c, --config     Load the configuration from a file
		-e, --env        Load .env file from the current directory
		-E, --envfile    Load a specified .env file
		-h, --help       Output usage information
		-H, --hot        Hot reload services if changed (disabled by default)
		-i, --instances  Launch [number] instances node (load balanced)
		-m, --mask       Filemask for service loading
		-r, --repl       Start REPL mode (disabled by default)
		-s, --silent     Silent mode. No logger (disabled by default)
		-v, --version    Output the version number
	*/
	processFlags(procArgs) {
		Args
			.option("config", "Load the configuration from a file")
			.option("repl", "Start REPL mode", false)
			.option(["H", "hot"], "Hot reload services if changed", false)
			.option("silent", "Silent mode. No logger", false)
			.option("env", "Load .env file from the current directory")
			.option("envfile", "Load a specified .env file")
			.option("instances", "Launch [number] instances node (load balanced)")
			.option("mask", "Filemask for service loading");

		this.flags = Args.parse(procArgs, {
			mri: {
				alias: {
					c: "config",
					r: "repl",
					H: "hot",
					s: "silent",
					e: "env",
					E: "envfile",
					i: "instances",
					m: "mask"
				},
				boolean: ["repl", "silent", "hot", "env"],
				string: ["config", "envfile", "mask"]
			}
		});

		this.servicePaths = Args.sub;
	}

	/**
	 * Load environment variables from '.env' file
	 */
	loadEnvFile() {
		if (this.flags.env || this.flags.envfile) {
			try {
				const dotenv = require("dotenv");

				if (this.flags.envfile)
					dotenv.config({ path: this.flags.envfile });
				else
					dotenv.config();
			} catch(err) {
				throw new Error("The 'dotenv' package is missing! Please install it with 'npm install dotenv --save' command.");
			}
		}
	}

	/**
	 * Load configuration file
	 *
	 * Try to load a configuration file in order to:
	 *
	 * 		- load file which is defined in CLI option with --config
	 * 		- try to load the `moleculer.config.js` file if exist in the cwd
	 * 		- try to load the `moleculer.config.json` file if exist in the cwd
	 */
	loadConfigFile() {
		let filePath;
		if (this.flags.config) {
			filePath = path.isAbsolute(this.flags.config) ? this.flags.config : path.resolve(process.cwd(), this.flags.config);
		}
		if (!filePath && fs.existsSync(path.resolve(process.cwd(), "moleculer.config.js"))) {
			filePath = path.resolve(process.cwd(), "moleculer.config.js");
		}
		if (!filePath && fs.existsSync(path.resolve(process.cwd(), "moleculer.config.json"))) {
			filePath = path.resolve(process.cwd(), "moleculer.config.json");
		}

		if (filePath) {
			if (!fs.existsSync(filePath))
				return Promise.reject(new Error(`Config file not found: ${filePath}`));

			const ext = path.extname(filePath);
			switch (ext) {
				case ".json":
				case ".js":
				case ".ts": {
					const content = require(filePath);
					return Promise.resolve()
						.then(() => {
							if (_.isFunction(content))
								return content.call(this);
							else
								return content;
						})
						.then(res => this.configFile = res);
				}<commit_msg>fix: use config file with "export default"<commit_after>/* moleculer
 * Copyright (c) 2019 MoleculerJS (https://github.com/moleculerjs/moleculer)
 * MIT Licensed
 */

"use strict";

const ServiceBroker	= require("./service-broker");
const utils			= require("./utils");
const fs 			= require("fs");
const path 			= require("path");
const glob 			= require("glob").sync;
const _ 			= require("lodash");
const Args 			= require("args");
const os			= require("os");
const cluster		= require("cluster");
const kleur			= require("kleur");

const stopSignals = [
	"SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT",
	"SIGBUS", "SIGFPE", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGTERM"
];

/* eslint-disable no-console */

/**
 * Logger helper
 *
 */
const logger = {
	info(message) {
		console.log(kleur.grey("[Runner]"), kleur.green().bold(message));
	},
	error(err) {
		if (err instanceof Error)
			console.error(kleur.grey("[Runner]"), kleur.red().bold(err.message), err);
		else
			console.error(kleur.grey("[Runner]"), kleur.red().bold(err));
	}
};

class MoleculerRunner {

	constructor() {
		this.watchFolders = [];

		this.flags = null;
		this.configFile = null;
		this.config = null;
		this.servicePaths = null;
		this.broker = null;
		this.worker = null;
	}

	/**
	 * Process command line arguments
	 *
	 * Available options:
		-c, --config     Load the configuration from a file
		-e, --env        Load .env file from the current directory
		-E, --envfile    Load a specified .env file
		-h, --help       Output usage information
		-H, --hot        Hot reload services if changed (disabled by default)
		-i, --instances  Launch [number] instances node (load balanced)
		-m, --mask       Filemask for service loading
		-r, --repl       Start REPL mode (disabled by default)
		-s, --silent     Silent mode. No logger (disabled by default)
		-v, --version    Output the version number
	*/
	processFlags(procArgs) {
		Args
			.option("config", "Load the configuration from a file")
			.option("repl", "Start REPL mode", false)
			.option(["H", "hot"], "Hot reload services if changed", false)
			.option("silent", "Silent mode. No logger", false)
			.option("env", "Load .env file from the current directory")
			.option("envfile", "Load a specified .env file")
			.option("instances", "Launch [number] instances node (load balanced)")
			.option("mask", "Filemask for service loading");

		this.flags = Args.parse(procArgs, {
			mri: {
				alias: {
					c: "config",
					r: "repl",
					H: "hot",
					s: "silent",
					e: "env",
					E: "envfile",
					i: "instances",
					m: "mask"
				},
				boolean: ["repl", "silent", "hot", "env"],
				string: ["config", "envfile", "mask"]
			}
		});

		this.servicePaths = Args.sub;
	}

	/**
	 * Load environment variables from '.env' file
	 */
	loadEnvFile() {
		if (this.flags.env || this.flags.envfile) {
			try {
				const dotenv = require("dotenv");

				if (this.flags.envfile)
					dotenv.config({ path: this.flags.envfile });
				else
					dotenv.config();
			} catch(err) {
				throw new Error("The 'dotenv' package is missing! Please install it with 'npm install dotenv --save' command.");
			}
		}
	}

	/**
	 * Load configuration file
	 *
	 * Try to load a configuration file in order to:
	 *
	 * 		- load file which is defined in CLI option with --config
	 * 		- try to load the `moleculer.config.js` file if exist in the cwd
	 * 		- try to load the `moleculer.config.json` file if exist in the cwd
	 */
	loadConfigFile() {
		let filePath;
		if (this.flags.config) {
			filePath = path.isAbsolute(this.flags.config) ? this.flags.config : path.resolve(process.cwd(), this.flags.config);
		}
		if (!filePath && fs.existsSync(path.resolve(process.cwd(), "moleculer.config.js"))) {
			filePath = path.resolve(process.cwd(), "moleculer.config.js");
		}
		if (!filePath && fs.existsSync(path.resolve(process.cwd(), "moleculer.config.json"))) {
			filePath = path.resolve(process.cwd(), "moleculer.config.json");
		}

		if (filePath) {
			if (!fs.existsSync(filePath))
				return Promise.reject(new Error(`Config file not found: ${filePath}`));

			const ext = path.extname(filePath);
			switch (ext) {
				case ".json":
				case ".js":
				case ".ts": {
					const content = require(filePath);
					return Promise.resolve()
						.then(() => {
							if (_.isFunction(content))
								return content.call(this);
							else
								return content;
						})
						.then(res => this.configFile = res.default != null && res.__esModule ? res.default : res);
				}
{'fraction_non_alphanumeric': 0.164040674935747, 'fraction_numerical': 0.0013409319477036541, 'mean_word_length': 3.0534420289855073, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4460409', 'n_tokens_mistral': 3499, 'n_tokens_neox': 3025, 'n_words': 812}