UNPKG

include-me

Version:

[![Build Status](https://travis-ci.com/Emre-Kul/include-me.svg?branch=master)](https://travis-ci.com/Emre-Kul/include-me) ![](https://img.shields.io/github/license/Emre-Kul/include-me.svg)

39 lines 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class TemplateEngine { constructor(fileSelector = 'path', includeRegex = /\${include\(\'(.*)\'\)}/g) { this.fileSelector = fileSelector; this.includeRegex = includeRegex; } compile(files) { let fileIndex = 0; while (fileIndex < files.length) { this.processIncludes(files, fileIndex); if (!this.includeRegex.test(files[fileIndex].content)) { fileIndex++; } } } processIncludes(files, fileIndex) { const file = files[fileIndex]; const processedBefore = {}; let matches = []; do { matches = this.includeRegex.exec(file.content); if (matches) { const targetFile = files.find(f => f[this.fileSelector] === matches[1]); if (!targetFile) { throw new Error(`"${matches[1]}" couldn't find, check out + '${file.name}' ('${file.path}')`); } if (processedBefore[targetFile.name]) { throw new Error(`Cycle problem at '${file.name}' file`); } processedBefore[targetFile.name] = true; file.content = file.content.replace(matches[0], targetFile.content); } } while (matches); } } exports.default = TemplateEngine; //# sourceMappingURL=template-engine.js.map