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

const fsx = require('../../api/fsx');
const logger = require('../../api/logger');
const confMgmt = require('../../api/conf-mgmt');
const utils = require('../../api/utils');

const INVALID_PATH_PATTERN = '((\\\\|/)\\.+(\\\\|/))|(^\\.{2,})|(\\.+$)';

function isPathValid(relativePath) {
	return relativePath.search(INVALID_PATH_PATTERN) < 0;
}


const CHILD_ITEM = [
	{
		label: 'Loading...',
		disabled: true
	}
];

function sortFilesFunc(a, b) {
	if (a.label.toUpperCase() > b.label.toUpperCase()) {
		return 1;
	}
	if (a.label.toUpperCase() < b.label.toUpperCase()) {
		return -1;
	}
	return 0;
}

function resolveFullPath(relativePath) {
	if (!isPathValid(relativePath)) {
		logger.log.error('The [%s] path is unacceptable', relativePath);
		return Promise.reject('Unacceptable path');
	}

	return Promise.resolve(confMgmt.CONF_FILES.SETTINGS).then(confMgmt.loadAsync)
		.then(
			(settings) => {
				const fullPath = path.join(settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR], relativePath || '');
				if (!isPathValid(fullPath)) {
					logger.log.error('The [%s] path is unacceptable', fullPath);
					return Promise.reject('Unacceptable path');
				}

				return Promise.resolve({
					fullPath: fullPath,
					settings: settings
				});
			}
		);
}

function makeDirItem(item, relativePath) {
	return {
		label: item,
		items: CHILD_ITEM.slice(),
		value: {
			relativePath: relativePath,
			label: item,
			mustLoadChildItems: true,
			isDir: true
		}
	}
}

function makeFileItem(item, relativePath) {
	return {
		label: item,
		value: {
			relativePath: relativePath,
			label: item,
			mustLoadChildItems: false,
			isDir: false
		}
	}
}

function assembleItemsPromised(relativePath, nexlSourcesDir, items) {
	return Promise.resolve().then(() => {
			let files = [];
			let dirs = [];
			const promises = [];

			// gathering promises
			for (let index in items) {
				let item = items[index];

				const itemRelativePath = path.join(relativePath, item);
				if (!isPathValid(itemRelativePath)) {
					logger.log.error('The [%s] path is unacceptable', relativePath);
					return Promise.reject('Unacceptable path');
				}

				const fullPath = path.join(nexlSourcesDir, itemRelativePath);
				const promise = fsx.stat(fullPath).then(
					(stats) => {
						if (stats.isDirectory()) {
							dirs.push(makeDirItem(item, itemRelativePath));
						}

						if (stats.isFile()) {
							files.push(makeFileItem(item, itemRelativePath));
						}

						return Promise.resolve();
					}
				);

				promises.push(promise);
			}

			// executing all promises
			return Promise.all(promises).then(
				() => {
					files = files.sort(sortFilesFunc);
					dirs = dirs.sort(sortFilesFunc);

					return Promise.resolve([].concat(dirs).concat(files));
				});
		}
	);
}

function loadNexlSource(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			const nexlSourcesRootDir = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR];
			const encoding = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_ENCODING];
			return fsx.readFile(stuff.fullPath, {encoding: encoding});
		}
	);
}

function saveNexlSource(relativePath, content) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			const nexlSourcesRootDir = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR];
			const encoding = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_ENCODING];
			return fsx.writeFile(stuff.fullPath, content, {encoding: encoding});
		}
	);
}

function listNexlSources(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			return fsx.exists(stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR]).then((isExists) => {
				if (!isExists) {
					logger.log.error('The [%s] nexl source dir doesn\'t exist', stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR]);
					return Promise.reject('nexl sources dir doesn\'t exist !');
				} else {
					return fsx.readdir(stuff.fullPath).then(
						(items) => assembleItemsPromised(relativePath, stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR], items));
				}
			});
		}
	);
}

function mkdir(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			return fsx.exists(stuff.fullPath).then((isExists) => {
				if (isExists) {
					return Promise.reject('Directory or file already exists');
				}

				return fsx.mkdir(stuff.fullPath);
			});
		});
}

function deleteItem(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			return fsx.deleteItem(stuff.fullPath);
		});
}

function rename(relativePath, newRelativePath) {
	return resolveFullPath(relativePath).then(
		(relativePathStuff) => {
			return resolveFullPath(newRelativePath).then(
				(newRelativePathStuff) => {
					return fsx.rename(relativePathStuff.fullPath, newRelativePathStuff.fullPath);
				}
			);
		}
	);
}

// --------------------------------------------------------------------------------
module.exports.listNexlSources = listNexlSources;
module.exports.loadNexlSource = loadNexlSource;
module.exports.saveNexlSource = saveNexlSource;
module.exports.mkdir = mkdir;
module.exports.deleteItem = deleteItem;
module.exports.rename = rename;<commit_msg>- working on items renaming<commit_after>const fs = require('fs');
const path = require('path');
const util = require('util');

const fsx = require('../../api/fsx');
const logger = require('../../api/logger');
const confMgmt = require('../../api/conf-mgmt');
const utils = require('../../api/utils');

const INVALID_PATH_PATTERN = '((\\\\|/)\\.+(\\\\|/))|(^\\.{2,})|(\\.+$)';

function isPathValid(relativePath) {
	return relativePath.search(INVALID_PATH_PATTERN) < 0;
}


const CHILD_ITEM = [
	{
		label: 'Loading...',
		disabled: true
	}
];

function sortFilesFunc(a, b) {
	if (a.label.toUpperCase() > b.label.toUpperCase()) {
		return 1;
	}
	if (a.label.toUpperCase() < b.label.toUpperCase()) {
		return -1;
	}
	return 0;
}

function resolveFullPath(relativePath) {
	if (!isPathValid(relativePath)) {
		logger.log.error('The [%s] path is unacceptable', relativePath);
		return Promise.reject('Unacceptable path');
	}

	return Promise.resolve(confMgmt.CONF_FILES.SETTINGS).then(confMgmt.loadAsync)
		.then(
			(settings) => {
				const fullPath = path.join(settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR], relativePath || '');
				if (!isPathValid(fullPath)) {
					logger.log.error('The [%s] path is unacceptable', fullPath);
					return Promise.reject('Unacceptable path');
				}

				return Promise.resolve({
					fullPath: fullPath,
					settings: settings
				});
			}
		);
}

function makeDirItem(item, relativePath) {
	return {
		label: item,
		items: CHILD_ITEM.slice(),
		value: {
			relativePath: relativePath,
			label: item,
			mustLoadChildItems: true,
			isDir: true
		}
	}
}

function makeFileItem(item, relativePath) {
	return {
		label: item,
		value: {
			relativePath: relativePath,
			label: item,
			mustLoadChildItems: false,
			isDir: false
		}
	}
}

function assembleItemsPromised(relativePath, nexlSourcesDir, items) {
	return Promise.resolve().then(() => {
			let files = [];
			let dirs = [];
			const promises = [];

			// gathering promises
			for (let index in items) {
				let item = items[index];

				const itemRelativePath = path.join(relativePath, item);
				if (!isPathValid(itemRelativePath)) {
					logger.log.error('The [%s] path is unacceptable', relativePath);
					return Promise.reject('Unacceptable path');
				}

				const fullPath = path.join(nexlSourcesDir, itemRelativePath);
				const promise = fsx.stat(fullPath).then(
					(stats) => {
						if (stats.isDirectory()) {
							dirs.push(makeDirItem(item, itemRelativePath));
						}

						if (stats.isFile()) {
							files.push(makeFileItem(item, itemRelativePath));
						}

						return Promise.resolve();
					}
				);

				promises.push(promise);
			}

			// executing all promises
			return Promise.all(promises).then(
				() => {
					files = files.sort(sortFilesFunc);
					dirs = dirs.sort(sortFilesFunc);

					return Promise.resolve([].concat(dirs).concat(files));
				});
		}
	);
}

function loadNexlSource(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			const nexlSourcesRootDir = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR];
			const encoding = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_ENCODING];
			return fsx.readFile(stuff.fullPath, {encoding: encoding});
		}
	);
}

function saveNexlSource(relativePath, content) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			const nexlSourcesRootDir = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR];
			const encoding = stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_ENCODING];
			return fsx.writeFile(stuff.fullPath, content, {encoding: encoding});
		}
	);
}

function listNexlSources(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			return fsx.exists(stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR]).then((isExists) => {
				if (!isExists) {
					logger.log.error('The [%s] nexl source dir doesn\'t exist', stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR]);
					return Promise.reject('nexl sources dir doesn\'t exist !');
				} else {
					return fsx.readdir(stuff.fullPath).then(
						(items) => assembleItemsPromised(relativePath, stuff.settings[confMgmt.SETTINGS.NEXL_SOURCES_DIR], items));
				}
			});
		}
	);
}

function mkdir(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			return fsx.exists(stuff.fullPath).then((isExists) => {
				if (isExists) {
					return Promise.reject('Directory or file already exists');
				}

				return fsx.mkdir(stuff.fullPath);
			});
		});
}

function deleteItem(relativePath) {
	return resolveFullPath(relativePath).then(
		(stuff) => {
			return fsx.deleteItem(stuff.fullPath);
		});
}

function rename(relativePath, newRelativePath) {
	return resolveFullPath(newRelativePath).then(
		(newRelativePathStuff) => {
			return fsx.exists(newRelativePathStuff.fullPath).then(
				(isExists) => {
					if (isExists) {
						logger.log.error('Cannot rename a [%s] to [%s] because the [%s] already exists', relativePath, newRelativePath, newRelativePath);
						return Promise.reject('Item with the same name already exists');
					}

					return resolveFullPath(relativePath).then(
						(relativePathStuff) => {
							return fsx.rename(relativePathStuff.fullPath, newRelativePathStuff.fullPath);
						}
					);

				}
			);
		}
	);
}

// --------------------------------------------------------------------------------
module.exports.listNexlSources = listNexlSources;
module.exports.loadNexlSource = loadNexlSource;
module.exports.saveNexlSource = saveNexlSource;
module.exports.mkdir = mkdir;
module.exports.deleteItem = deleteItem;
module.exports.rename = rename;
{'fraction_non_alphanumeric': 0.16193733247065348, 'fraction_numerical': 0.0009242998428690267, 'mean_word_length': 3.625908507909363, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, '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': '3822880', 'n_tokens_mistral': 4256, 'n_tokens_neox': 3552, 'n_words': 624}