UNPKG

tprompter

Version:
76 lines (75 loc) 2.73 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; export function getFilePathsFromLine(data) { data = data.trim(); if (!data || data.startsWith('//') || data.startsWith('/*')) { return []; } const startsLikePath = data.startsWith('/') || data.startsWith("'/") || data.startsWith('"/'); if (!startsLikePath) { return []; } const chars = data.trim().split(''); let currentFilePath = ''; let isInsideQuotes = false; let isInsideQuotesChar = ''; const filepaths = []; for (let i = 0; i < chars.length; i++) { const char = chars[i]; if (char === ' ' && !isInsideQuotes) { if (currentFilePath) { filepaths.push(currentFilePath); currentFilePath = ''; } } else if (char === "'" || char === '"') { if (!isInsideQuotes) { isInsideQuotes = true; isInsideQuotesChar = char; } else if (isInsideQuotesChar === char) { isInsideQuotes = false; isInsideQuotesChar = ''; } } else if (char === '\\') { currentFilePath += chars[++i]; } else { currentFilePath += char; } } if (currentFilePath) { filepaths.push(currentFilePath); } return filepaths; } /** * This finction finds all the filepaths in the given text data and replaces them with the file content. */ export function enrichTextData(data, fileResolver) { return __awaiter(this, void 0, void 0, function* () { const lines = data.split('\n'); const result = []; for (const line of lines) { const paths = getFilePathsFromLine(line); if (paths.length) { for (const path of paths) { const content = yield fileResolver(path); result.push(content); } } else { result.push(line); } } return result.join('\n'); }); }