@cuba-platform/front-generator
Version:
CUBA Platform front-end clients generator
84 lines • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.throwError = exports.normalizeRelativePath = exports.isBlank = exports.getEntityModulePath = exports.fqnToName = exports.convertToUnixPath = exports.splitByCapitalLetter = exports.unCapitalizeFirst = exports.capitalizeFirst = exports.elementNameToClass = void 0;
const path = require("path");
/**
* Remove illegal symbols and normalize class name by standard js notation.
*
* @param {string} elementName 'my-app-custom' | 'myAppCustom' | 'my app custom'
* @returns {string} class name MyAppCustom
*/
const elementNameToClass = (elementName) => {
if (elementName.match(/$\s*^/))
throw new Error('Could not generate class name from empty string');
return elementName
.trim().replace(/\s{2,}/g, ' ') // extra spaces
.replace(/[^\w\d\s_\-$]/g, '') // remove symbols not allowed in class name
.split(/[-\s]/) // split by separate words
.map(exports.capitalizeFirst)
.join('');
};
exports.elementNameToClass = elementNameToClass;
const capitalizeFirst = (part) => part[0].toUpperCase() + part.slice(1);
exports.capitalizeFirst = capitalizeFirst;
const unCapitalizeFirst = (part) => part[0].toLowerCase() + part.slice(1);
exports.unCapitalizeFirst = unCapitalizeFirst;
const splitByCapitalLetter = (word) => word.replace(/([^A-Z])([A-Z])/g, '$1 $2');
exports.splitByCapitalLetter = splitByCapitalLetter;
function convertToUnixPath(input) {
const isExtendedLengthPath = /^\\\\\?\\/.test(input);
const hasNonAscii = /[^\u0000-\u0080]+/.test(input); // eslint-disable-line no-control-regex
if (isExtendedLengthPath || hasNonAscii) {
return input;
}
return input.replace(/\\/g, '/');
}
exports.convertToUnixPath = convertToUnixPath;
/**
* Convert java class fully qualified name to compilable TS class name
*
* @param fqn java class fqn
*/
function fqnToName(fqn) {
return fqn.replace(/\./g, '_');
}
exports.fqnToName = fqnToName;
function getEntityModulePath(entity, prefix = '') {
const modulePath = entity.name ? entity.name : entity.className;
return path.posix.join(prefix, modulePath);
}
exports.getEntityModulePath = getEntityModulePath;
function isBlank(str) {
return (str == null || /^\s*$/.test(str));
}
exports.isBlank = isBlank;
/**
* Ensure that the path contains only forward slashes and no backward slashes, and has a trailing forward slash.
*
* @param relPath
*/
function normalizeRelativePath(relPath) {
if (relPath == null || isBlank(relPath)) {
return '';
}
const relPathPosix = relPath.replace(/\\/g, '/');
return path.posix.join(relPathPosix, '/');
}
exports.normalizeRelativePath = normalizeRelativePath;
/**
* The preferable method of throwing an error in Yeoman is using Environment.error() method.
* It will prevent execution of the subsequent code, however, TypeScript compiler doesn't know about that.
* Therefore, using Environment.error() in a function that returns a value will require e.g. throwing an error manually
* or returning an empty value.
* In such cases this helper function can be used. It returns `never`, which means that compiler won't consider
* the code after this function reachable.
*
* @param generator
* @param error
*/
function throwError(generator, error) {
generator.env.error(Error(error)); // This will throw an error and the subsequent code will never be reached
throw error; // This code will never be reached and exists only to allow to return `never`
}
exports.throwError = throwError;
//# sourceMappingURL=utils.js.map