<commit_before>const fs = require( "fs" );
const AWS = require( "aws-sdk" );

const utils = require( "./utils" );

function getLambdaEventFile () {

	const path = `${process.env.COLLY__PROJECT_DIR}/${process.env.COLLY__LAMBDA_EVENT_FILE}`;

	if ( fs.existsSync( path ) ) {
		return JSON.parse( fs.readFileSync( path, "utf8" ) );
    } else {
    	console.log( "WARNING: Event file not found." );
    	return {};
    }

}

function getLambdaContextFile () {

	const defaultContext = {
		"done": () => {}
	};

	const path = `${process.env.COLLY__PROJECT_DIR}/${process.env.COLLY__LAMBDA_CONTEXT_FILE}`;

	if ( fs.existsSync( path ) ) {
		return require( path );
	} else {
		return defaultContext;
	}

}

function runningLocally () {
	return JSON.parse( process.env.COLLY__RUN_LAMBDA_LOCAL );
}

function runLocally () {

	return new Promise( ( resolve, reject ) => {

		const projectConfig = utils.getProjectConfig();

        utils.addLambdaEnvironmentVariablesToProcess( projectConfig.environmentVariables );

		const config = utils.getLambdaConfigFile();

		const lambdaHandler = getLambdaHandler();

		// console.log( "11", getLambdaContextFile().done.toString(), "11" );

		lambdaHandler(
			getLambdaEventFile(),
			getLambdaContextFile(),
			function callback( err, data ) {
				if ( err ) {
					reject( err );
				}
				resolve( data );
			}
		);

	});

}

function runDeployedLamda () {
	
	utils.setAwsRegion();
	const config = utils.getLambdaConfigFile();
	const lambda = new AWS.Lambda();
	const params = {
		"FunctionName": utils.getLambdaName(),
		"InvocationType": "RequestResponse",
		"Payload": JSON.stringify( getLambdaEventFile() ),
		"LogType": "Tail"
	};

	lambda.invoke(params, function(err, data) {
		if ( err ) {
			console.log( err );
		} else {
			if ( data.LogResult ) {
				console.log( "Status code: " + data.StatusCode );
				console.log( "Payload: " + data.Payload );
				console.log( Buffer.from( data.LogResult, "base64" ).toString() );
			}
			// console.log( data );
		}
	});

}

function getLambdaHandler() {
	try {
		return require( utils.getLambdaFilePath() )[ utils.getLambdaHandlerName() ];
	}
	catch( err ) {
		console.log( "Error: Cannot find the lambda you are trying to run. Check the `--name` or the handler property in the lambda's function file.");
		throw err;
	}
}

function init () {

	return new Promise( ( resolve, reject ) => {

		if ( runningLocally() ) {
			runLocally()
				.then( ( data ) => {
					console.log( data );
					resolve( data );
				})
				.catch( reject );
		} else {
			utils
				.authenticate()
				.then( runDeployedLamda )<commit_msg>Small improvement to feedback when running lambda remotely<commit_after>const fs = require( "fs" );
const AWS = require( "aws-sdk" );

const utils = require( "./utils" );

function getLambdaEventFile () {

	const path = `${process.env.COLLY__PROJECT_DIR}/${process.env.COLLY__LAMBDA_EVENT_FILE}`;

	if ( fs.existsSync( path ) ) {
		return JSON.parse( fs.readFileSync( path, "utf8" ) );
    } else {
    	console.log( "WARNING: Event file not found." );
    	return {};
    }

}

function getLambdaContextFile () {

	const defaultContext = {
		"done": () => {}
	};

	const path = `${process.env.COLLY__PROJECT_DIR}/${process.env.COLLY__LAMBDA_CONTEXT_FILE}`;

	if ( fs.existsSync( path ) ) {
		return require( path );
	} else {
		return defaultContext;
	}

}

function runningLocally () {
	return JSON.parse( process.env.COLLY__RUN_LAMBDA_LOCAL );
}

function runLocally () {

	return new Promise( ( resolve, reject ) => {

		const projectConfig = utils.getProjectConfig();

        utils.addLambdaEnvironmentVariablesToProcess( projectConfig.environmentVariables );

		const config = utils.getLambdaConfigFile();

		const lambdaHandler = getLambdaHandler();

		// console.log( "11", getLambdaContextFile().done.toString(), "11" );

		lambdaHandler(
			getLambdaEventFile(),
			getLambdaContextFile(),
			function callback( err, data ) {
				if ( err ) {
					reject( err );
				}
				resolve( data );
			}
		);

	});

}

function runDeployedLamda () {
	
	utils.setAwsRegion();
	const config = utils.getLambdaConfigFile();
	const lambda = new AWS.Lambda();
	const params = {
		"FunctionName": utils.getLambdaName(),
		"InvocationType": "RequestResponse",
		"Payload": JSON.stringify( getLambdaEventFile() ),
		"LogType": "Tail"
	};

	lambda.invoke(params, function(err, data) {
		if ( err ) {
			console.log( err );
		} else {
			if ( data.LogResult ) {
				console.log( "Status code: ")
				console.log( data.StatusCode );
				console.log( "Payload: " )
				console.log( JSON.parse( data.Payload ) );
				console.log( "Lambda log:" );
				console.log( Buffer.from( data.LogResult, "base64" ).toString() );
			}
		}
	});

}

function getLambdaHandler() {
	try {
		return require( utils.getLambdaFilePath() )[ utils.getLambdaHandlerName() ];
	}
	catch( err ) {
		console.log( "Error: Cannot find the lambda you are trying to run. Check the `--name` or the handler property in the lambda's function file.");
		throw err;
	}
}

function init () {

	return new Promise( ( resolve, reject ) => {

		if ( runningLocally() ) {
			runLocally()
				.then( ( data ) => {
					console.log( data );
					resolve( data );
				})
				.catch( reject );
		} else {
			utils
				.authenticate()
				.then( runDeployedLamda )
{'fraction_non_alphanumeric': 0.1505740636175419, 'fraction_numerical': 0.002635046113306983, 'mean_word_length': 3.4099585062240663, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, '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': '2049203', 'n_tokens_mistral': 1956, 'n_tokens_neox': 1741, 'n_words': 397}