{"size": 1571, "ext": "js", "max_stars_repo_path": "lib/fsSafe/index.js", "max_stars_repo_name": "BarchartKP/production-forecast-doc", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/fsSafe/index.js", "max_issues_repo_name": "BarchartKP/production-forecast-doc", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/fsSafe/index.js", "max_forks_repo_name": "BarchartKP/production-forecast-doc", "max_forks_repo_forks_event_min_datetime": "2021-04-15T13:49:31.000Z", "max_forks_repo_forks_event_max_datetime": "2021-04-15T13:49:31.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 17.6516853933, "max_line_length": 74, "alphanum_fraction": 0.6562698918}
const fs = require('fs');
const path = require('path');

function writeFileSync(filePath, data, options) {
	try {
		const pathSplit = filePath.split('/');
		const dir = path.join('/', ...pathSplit.slice(0, pathSplit.length - 1));
		const isDirExist = existsSync(dir);
		if (!isDirExist) {
			fs.mkdirSync(dir, { recursive: true });
		}

		fs.writeFileSync(filePath, data, options);
	} catch (e) {
		console.error(e);
	}
}

function readFileSync(filePath, defaultValue = '', options = '') {
	let output;

	try {
		const isFileExist = existsSync(filePath);
		if (!isFileExist) {
			writeFileSync(filePath, '', options);
		}

		output = fs.readFileSync(filePath, options);
	} catch (e) {
		console.error(e);
	}

	return output;
}

function readdirSync(path, options) {
	let dir = [];

	try {
		const isDirExist = existsSync(path);
		if (!isDirExist) {
			mkdirSync(path, { recursive: true });
		}

		dir = fs.readdirSync(path, options);
	} catch (e) {
		console.error(e);
	}

	return dir;
}

function mkdirSync(path, options) {
	try {
		fs.mkdirSync(path, options);
	} catch (e) {
		console.error(e);
	}
}

function copyFileSync(src, dest, flags) {
	let destDir = dest.split('/');
	destDir.pop();
	destDir = destDir.join('/');
	if (!existsSync(destDir)) {
		mkdirSync(destDir, { recursive: true });
	}

	fs.copyFileSync(src, dest, flags);
}

function existsSync(path) {
	return fs.existsSync(path);
}

function unlinkSync(path) {
	fs.unlinkSync(path);
}

module.exports = {
	copyFileSync,
	existsSync,
	mkdirSync,
	readFileSync,
	readdirSync,
	writeFileSync,
	unlinkSync
};
