{"size": 2099, "ext": "js", "max_stars_repo_path": "src/Running/index.js", "max_stars_repo_name": "Rexrn/cpp-build", "max_stars_repo_stars_event_min_datetime": "2020-05-19T13:26:03.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-19T13:26:03.000Z", "max_issues_repo_path": "src/Running/index.js", "max_issues_repo_name": "Rexrn/cpp-build", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/Running/index.js", "max_forks_repo_name": "Rexrn/cpp-build", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.4069767442, "max_line_length": 83, "alphanum_fraction": 0.6412577418}
const { build } = require("../Building"); 

const path = require("path");
const fs = require("fs");

function ensureDirectoryExistence(filePath) {
	if (fs.existsSync(filePath)) {
		return true;
	}
	fs.mkdirSync(filePath);
}

function runBuild(args)
{
	try
	{
		// Resolve path and use it to build file.
		console.log("# cpp-build is running build process...");

		build( path.resolve(process.cwd(), args.target) );


		console.log(`# cpp-build process finished (success).`);
	}
	catch(exc)
	{
		console.error(`Error: Details:\n${exc}`);
		console.error("Stack trace:\n", exc.stack);
		console.error("# cpp-build process finished (failure)");
	}
}

function runDiscover(args)
{
	//try
	{
		// Resolve path and use it to build file.
		console.log("# cpp-build is running discover process...");

		ensureDirectoryExistence('meta');
		let projects = [];
		const discovered = require(path.resolve(process.cwd(), args.target));
		if (Array.isArray(discovered))
			projects = discovered;
		else if (typeof discovered === "object")
			projects.push(discovered);
		else
			throw `executing build script resulted with invalid type: ${typeof discovered}`;

		fs.writeFileSync("meta/discover.json",
				JSON.stringify(projects.map(p => ({ name: p.name })), null, "\t"),
				{ flag: 'w'}
			);

		console.error(`# cpp-build process finished (success).`);
	}
	// catch(exc)
	// {
	// 	console.error(`# cpp-build process finished (failure, reason: "${exc}")`);
	// }
}

module.exports = {
	run()
	{
		const targetOption = {
				type: 		"string",
				describe: 	"path to the targets build script/json"
			};

		const argv = 
			require("yargs")
			.usage("Usage: $0 (build|discover) [target]")
			.command('build [target]', 'build target script',
					(yargs)	=> { yargs.positional('target', targetOption ) },
					(args)	=> runBuild(args)
				)
			.command('discover [target]', 'discover projects from target script',
					(yargs)	=> { yargs.positional('target', targetOption) },
					(args)	=> runDiscover(args)
				)
			.demandCommand(1, "Error: no command specified!")
			.demandOption("target")
			.argv;
	}
};