UNPKG

php-htx-cli

Version:

A cli to convert .htx (different components) files to .php

140 lines (139 loc) 5.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleScopes = exports.checkForExistingScopes = void 0; const fs_1 = __importDefault(require("fs")); const regex_1 = __importDefault(require("./regex")); const path_1 = __importDefault(require("path")); const tagDetection_1 = require("./tagDetection"); function checkForExistingScopes(css, js) { let scopes = []; css.forEach(pathStr => { if (fs_1.default.existsSync(pathStr)) { scopes.push({ type: "css", remote: false, path: pathStr }); } }); js.forEach(pathStr => { if (fs_1.default.existsSync(pathStr)) { scopes.push({ type: "js", remote: false, path: pathStr }); } }); return scopes; } exports.checkForExistingScopes = checkForExistingScopes; function isValidScopeType(type) { return type == "\"css\"" || type == "\"js\""; } // Error messages function noScopeTypeError() { return new Error(`No scope type defined in the <SCOPES /> component. Please add a type property with either "css" or "js"`); } function unknownScopeTypeError(unknownType) { return new Error(`Unknown type ${unknownType} in <SCOPES /> component. Please use either "css" or "js"`); } function generateScopeCode(data, scopes, isDev) { if (!("type" in data.attr)) throw noScopeTypeError(); if (!isValidScopeType(data.attr.type)) throw unknownScopeTypeError(data.attr.type); const scopeType = data.attr.type.slice(1, -1); let scopesUsed = scopes.filter(scope => scope.type == scopeType); let remoteScopes = []; let contents = []; scopesUsed.forEach(scopeUsed => { if (!scopeUsed.remote) { contents.push({ content: fs_1.default.readFileSync(scopeUsed.path, 'utf8'), path: scopeUsed.path }); } else { remoteScopes.push(scopeUsed.path); } }); const joinedContents = contents.map(content => { if (!isDev) return content.content; return `\n/* DEBUG: Start scoped ${scopeType} for file: ${path_1.default.relative(process.cwd(), content.path)} */\n` + content.content; }).join(isDev ? "\n" : ""); return scopeType == "css" ? `${remoteScopes.map(remoteScope => { if ((remoteScope.startsWith("\"") || remoteScope.startsWith("'")) || (remoteScope.endsWith("\"") || remoteScope.endsWith("'"))) { remoteScope = remoteScope.slice(1, -1); } return `<link rel="stylesheet" href="${remoteScope}">`; }).join("")}<style>${joinedContents + (isDev ? "\n" : "")}\n</style>` : `${remoteScopes.map(remoteScope => { if ((remoteScope.startsWith("\"") || remoteScope.startsWith("'")) || (remoteScope.endsWith("\"") || remoteScope.endsWith("'"))) { remoteScope = remoteScope.slice(1, -1); } return `<script src="${remoteScope}"></script>`; }).join("")}<script>${joinedContents + (isDev ? "\n" : "")}</script>`; } function handleScopes(code, scopes, isDev) { let output = ""; let isInPhp = false; let isInString = false; let skipNext = false; for (let charIndex = 0; charIndex < code.length; charIndex++) { let match; if ((match = code.slice(charIndex).match(regex_1.default.php.open)) != null && isInString == false) { isInPhp = true; output += match[0]; charIndex += match[0].length - 1; continue; } if ((match = code.slice(charIndex).match(regex_1.default.php.close)) != null && isInString == false) { isInPhp = false; output += match[0]; charIndex += match[0].length - 1; continue; } if (isInPhp && isInString == false && code[charIndex] == "\"" && !skipNext) { isInString = "\""; output += "\""; continue; } if (isInPhp && isInString == "\"" && code[charIndex] == "\"" && !skipNext) { isInString = false; output += "\""; continue; } if (isInPhp && isInString == false && code[charIndex] == "'" && !skipNext) { isInString = "'"; output += "'"; continue; } if (isInPhp && isInString == "'" && code[charIndex] == "'" && !skipNext) { isInString = false; output += "'"; continue; } if (code[charIndex] == "\\" && isInPhp && isInString != false) { skipNext = true; output += "\\"; continue; } else { skipNext = false; } if ((match = (0, tagDetection_1.isTag)("/>", code, charIndex)) != false && match.tagname == "SCOPES" && !isInPhp) { output += generateScopeCode(match, scopes, isDev); charIndex += match.size - 1; continue; } output += code[charIndex]; } return output; } exports.handleScopes = handleScopes;