<commit_before>const express = require('express');
const fs = require('fs');
const path = require('path');

const logger = require('../../logger');
const { version } = require('../../../package.json');

const router = express.Router();

router.get('/version', (req, res, next) => {
	if (!process.env.SHOW_VERSION) {
		return res.sendStatus(403);
	}
	let sha = false;
	try {
		sha = fs.readFileSync(path.join(__dirname, '../../../', 'version'), 'utf8').trim();
	} catch (error) {
		logger.error('version missing', error);
	}
	return res.json({ sha, version });
});

module.exports = router;
<commit_msg>separate version into single parameters<commit_after>const express = require('express');
const fs = require('fs');
const path = require('path');

const logger = require('../../logger');
const { version } = require('../../../package.json');

const router = express.Router();


const getLine = (stringArr, i) => {
	if (stringArr && stringArr.length > i && i >= 0) {
		return stringArr[i].trim();
	}
	return null;
};

const getLines = (stringArr, start, end) => {
	if (!end) {
	// response with single line
		return getLine(stringArr, start);
	}
	// response with multiline
	const retValue = [];
	for (let i = start; i <= end; i += 1) {
		const line = getLine(stringArr, i);
		if (line) { retValue.push(line); }
	}
	return retValue.join('\n');
};

router.get('/version', (req, res, next) => {
	if (!process.env.SHOW_VERSION) {
		return res.sendStatus(403);
	}
	let sha = false;
	let branch = false;
	let message = false;
	try {
		const versionFileLines = fs.readFileSync(path.join(__dirname, '../../../', 'version'), 'utf8').split('\n');
		sha = getLines(versionFileLines, 0);
		branch = getLines(versionFileLines, 1);
		message = getLines(versionFileLines, 2, versionFileLines.length);
	} catch (error) {
		logger.error('version file missing', error);
	}
	return res.json({
		sha, version, branch, message,
	});
});

module.exports = router;

{'fraction_non_alphanumeric': 0.19927909371781669, 'fraction_numerical': 0.006694129763130793, 'mean_word_length': 3.931472081218274, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, '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': '641251', 'n_tokens_mistral': 659, 'n_tokens_neox': 637, 'n_words': 164}