venmjs
Version:
This is a tool ๐ง that can be installed in your terminal at any time โ๏ธ it was made for beginners and even for experts, for his utilities, and for a simple creation process ๐งจ. Every web developer knows how frustrating is to deal with the creation of a ne
133 lines (101 loc) โข 3.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.readFileContent = exports.dirOfChoice = exports.copyDirSync = exports.fetchProjectConfig = void 0;
var _fs = _interopRequireDefault(require("fs"));
var _inquirer = _interopRequireDefault(require("inquirer"));
var _path = _interopRequireDefault(require("path"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
var isWin = process.platform === 'win32';
/**
* Warns appropriately if the config file doesn't exist
* @returns {Void}
* Copy files
* @param {any} source - source directory path
* @param {any} target - path to the destination directory
* @returns {Void}
*/
var fetchProjectConfig = function fetchProjectConfig(genPath) {
var configFilePath = genPath ? _path["default"].join(genPath, '.venm') : '.venm';
if (!_fs["default"].existsSync(configFilePath)) {
return;
}
return JSON.parse(_fs["default"].readFileSync(configFilePath, 'utf8'));
};
exports.fetchProjectConfig = fetchProjectConfig;
var copyFileSync = function copyFileSync(source, target) {
var targetFile = target; // if target is a directory a new file with the same name will be created
if (_fs["default"].existsSync(target)) {
if (_fs["default"].lstatSync(target).isDirectory()) {
targetFile = _path["default"].join(target, _path["default"].basename(source));
}
}
_fs["default"].writeFileSync(targetFile, _fs["default"].readFileSync(source));
};
/**
* Copy directory content recursively
* @param {any} source - source directory path
* @param {any} target - path to the destination directory
* @returns {Void}
*/
var copyDirSync = function copyDirSync(source, target) {
// check if folder needs to be created or integrated
var targetFolder = _path["default"].join(target, _path["default"].basename(source));
if (!_fs["default"].existsSync(targetFolder)) {
_fs["default"].mkdirSync(targetFolder);
} // copy
if (_fs["default"].lstatSync(source).isDirectory()) {
_fs["default"].readdirSync(source).forEach(function (file) {
var curSource = _path["default"].join(source, file);
if (_fs["default"].lstatSync(curSource).isDirectory()) {
copyDirSync(curSource, targetFolder);
} else {
copyFileSync(curSource, targetFolder);
}
});
}
};
/**
* Choose between client and server directories
*
* @returns {Promise<object>}
*/
exports.copyDirSync = copyDirSync;
var dirOfChoice = function dirOfChoice() {
return _inquirer["default"].prompt({
name: 'dir',
type: 'list',
message: 'Choose from below ๐',
choices: ['client', 'server']
});
};
/**
* Strips \r character from the file content if the host OS is windows
*
* @param {String[]} fileContent - The file content
* @returns {String[]}
*/
exports.dirOfChoice = dirOfChoice;
var stripChar = function stripChar(fileContent) {
return fileContent.map(function (content) {
return content.includes('\r') ? content.substr(0, content.indexOf('\r')) : content;
});
};
/**
* Returns the file content as an arrray
*
* @param {String} filePath - The file path
* @returns {String[]}
*/
var readFileContent = function readFileContent(filePath) {
if (!filePath) {
return;
}
var fileContent = _fs["default"].readFileSync(filePath, 'utf8').split('\n'); // Check if the host OS is windows
if (isWin) {
return stripChar(fileContent);
}
return fileContent;
};
exports.readFileContent = readFileContent;