UNPKG

hades-cli

Version:
69 lines (68 loc) 3.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayDriver = void 0; const ts_morph_1 = require("ts-morph"); class ArrayDriver { /** * Create items in array * * @param sourceFile * @param item item to add to array * @param array name array or Array literal where will be added the item */ static createArrayItems(sourceFile, items, array) { let arrayLiteralExpression; if (typeof array === 'string') { const routesDeclaration = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getVariableDeclaration(array); arrayLiteralExpression = routesDeclaration === null || routesDeclaration === void 0 ? void 0 : routesDeclaration.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression); // if array not exist return void if (!arrayLiteralExpression) return; } else { arrayLiteralExpression = array; } const itemsToImport = ArrayDriver.getUniqueArrayItems(arrayLiteralExpression, items); if (itemsToImport.length > 0) arrayLiteralExpression === null || arrayLiteralExpression === void 0 ? void 0 : arrayLiteralExpression.addElements(itemsToImport, { useNewLines: true }); } /** * Create item in array * * @param sourceFile * @param item item to add to array * @param array name array or Array literal where will be added the item */ static createArrayItem(sourceFile, item, array) { let arrayLiteralExpression; if (typeof array === 'string') { const routesDeclaration = sourceFile === null || sourceFile === void 0 ? void 0 : sourceFile.getVariableDeclaration(array); arrayLiteralExpression = routesDeclaration === null || routesDeclaration === void 0 ? void 0 : routesDeclaration.getInitializerIfKindOrThrow(ts_morph_1.SyntaxKind.ArrayLiteralExpression); } else { arrayLiteralExpression = array; } if (!ArrayDriver.isDuplicateArrayValue(arrayLiteralExpression, item)) arrayLiteralExpression === null || arrayLiteralExpression === void 0 ? void 0 : arrayLiteralExpression.addElement(item, { useNewLines: true }); } static isDuplicateArrayValue(array, item) { // format string to avoid break spaces and extra white spaces const arrayItems = array === null || array === void 0 ? void 0 : array.getElements().map(i => i.getText()).map(j => j.replace(/(\r\n|\n|\r|\s)/gm, '')); if (Array.isArray(arrayItems)) return arrayItems.includes(item.replace(/(\r\n|\n|\r|\s)/gm, '')); return false; } /** * From the items array, only non-repeating items are returned. * * @param sourceFile * @param moduleNames */ static getUniqueArrayItems(array, items) { // format string to avoid break spaces and extra white spaces const arrayItems = array === null || array === void 0 ? void 0 : array.getElements().map(i => i.getText()).map(j => j.replace(/(\r\n|\n|\r|\s)/gm, '')); // filter only items where is not in array return items.filter(item => !arrayItems.includes(item)); } } exports.ArrayDriver = ArrayDriver;