Repository: haoxins/gulp-file-include Branch: main Commit: d444c9bcf6e9 Files: 101 Total size: 63.5 KB Directory structure: gitextract_ccu95132/ ├── .editorconfig ├── .gitignore ├── .npmrc ├── .travis.yml ├── LICENSE ├── Readme.md ├── example/ │ ├── a/ │ │ ├── a1.txt │ │ └── a2.txt │ ├── b/ │ │ ├── b1.txt │ │ └── b2.txt │ ├── c/ │ │ └── c.txt │ ├── conditional.txt │ ├── gulpfile.js │ ├── index.txt │ └── result/ │ └── index.txt ├── lib/ │ ├── indent.js │ ├── index.js │ ├── replace-function.js │ ├── replace-operator.js │ └── replace-variable.js ├── package.json └── test/ ├── .editorconfig ├── edge-case.js ├── error.js ├── filters.js ├── fixtures/ │ ├── arr-result.html │ ├── arr.html │ ├── index-01.html │ ├── index-02.html │ ├── index-03.html │ ├── index-04.js │ ├── index-05.html │ ├── index-handler-options.html │ ├── index-markdown-rot13.html │ ├── index-markdown.html │ ├── result.html │ ├── result.js │ ├── sameprefix-result.html │ ├── sameprefix-var.html │ ├── sameprefix.html │ ├── var.html │ ├── var.rot13.html │ ├── view.html │ ├── view.md │ └── view.rot13.md ├── fixtures-edge-case/ │ ├── a.html │ ├── b.html │ ├── commented-inclusion-result.html │ ├── commented-inclusion.html │ ├── dangerous.txt │ ├── index.html │ ├── recursion.html │ ├── result.html │ ├── without-trailing-newline-result.txt │ └── without-trailing-newline.txt ├── fixtures-error/ │ ├── for.html │ └── if.html ├── fixtures-flatten/ │ ├── index.html │ └── result.html ├── fixtures-indent/ │ ├── index.html │ ├── result.html │ ├── sub-sub.html │ └── sub.html ├── fixtures-nested/ │ ├── index.html │ ├── result.html │ └── var.html ├── fixtures-nested-once/ │ ├── index-twice.html │ ├── index.html │ ├── result-twice.html │ ├── result.html │ └── var.html ├── fixtures-operator/ │ ├── index.html │ ├── result-index.html │ ├── result-suffix.html │ └── suffix.html ├── fixtures-recursion/ │ ├── index.txt │ ├── result.txt │ ├── sub/ │ │ ├── a.txt │ │ └── b.txt │ └── var.txt ├── fixtures-suffix/ │ ├── index.html │ └── var.html ├── fixtures-variable/ │ ├── index-suffix.html │ ├── index.html │ └── result.html ├── fixtures-webroot-variable/ │ ├── html-head.html │ ├── index.html │ ├── result.html │ └── sub/ │ ├── home-link.html │ ├── index.html │ └── result.html ├── flatten.js ├── indent.js ├── index.js ├── nested-once.js ├── nested.js ├── operator.js ├── plugin-indent.js ├── recursion.js ├── variable.js └── webroot-variable.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ root = true [*.{html,js}] indent_style = space indent_size = 2 end_of_line = lf charset = utf-8 trim_trailing_whitespace = true ================================================ FILE: .gitignore ================================================ package-lock.json npm-debug.log node_modules/ .DS_Store coverage/ ================================================ FILE: .npmrc ================================================ package-lock=false ================================================ FILE: .travis.yml ================================================ language: node_js node_js: - 16 script: "npm run test-travis" after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2020 Xin Hao Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: Readme.md ================================================ [![NPM version][npm-img]][npm-url] [![Build status][travis-img]][travis-url] [![Test coverage][coveralls-img]][coveralls-url] [![License][license-img]][license-url] [![Dependency status][david-img]][david-url] [![Gitter][gitter-img]][gitter-url] # gulp-file-include a [gulp](https://github.com/gulpjs/gulp) plugin for file includes ## Installation ```bash npm install --save-dev gulp-file-include ``` ## API ```js const fileinclude = require('gulp-file-include'); ``` ### fileinclude([prefix]) #### prefix Type: `string`
Default: `'@@'` ### fileinclude([options]) #### options Type: `object` ##### options.prefix Type: `string`
Default: `'@@'` ##### options.suffix Type: `string`
Default: `''` ##### options.basepath Type: `string`
Default: `'@file'` Possible values: - `'@file'`: include file relative to the dir in which `file` resides ([example](#include-options---type-json)) - `'@root'`: include file relative to the dir in which `gulp` is running - `path/to/dir`: include file relative to the basepath you provide ##### options.filters Type: `object`
Default: `false` Filters of include content. ##### options.context Type: `object` Default: `{}` Context of `if` statement. ##### options.indent Type: `boolean` Default: `false` ## Examples ### @@include options - type: `JSON` index.html ```html @@include('./view.html') @@include('./var.html', { "name": "haoxin", "age": 12345, "socials": { "fb": "facebook.com/include", "tw": "twitter.com/include" } }) ``` view.html ```html

view

``` var.html ```html @@socials.fb @@socials.tw ``` gulpfile.js ```js const fileinclude = require('gulp-file-include'); const gulp = require('gulp'); gulp.task('fileinclude', function() { gulp.src(['index.html']) .pipe(fileinclude({ prefix: '@@', basepath: '@file' })) .pipe(gulp.dest('./')); }); ``` result: ```html

view

facebook.com/include twitter.com/include ``` ### @@include_once options - type: `JSON` index.html ```html @@include_once('./view.html') @@include_once('./var.html', { "name": "haoxin", "age": 12345, "socials": { "fb": "facebook.com/include", "tw": "twitter.com/include" } }) @@include_once('./var.html', { "name": "haoxin", "age": 12345, "socials": { "fb": "facebook.com/include", "tw": "twitter.com/include" } }) ``` view.html ```html

view

``` var.html ```html @@socials.fb @@socials.tw ``` gulpfile.js ```js const fileinclude = require('gulp-file-include'); const gulp = require('gulp'); gulp.task('fileinclude', function() { gulp.src(['index.html']) .pipe(fileinclude({ prefix: '@@', basepath: '@file' })) .pipe(gulp.dest('./')); }); ``` result: ```html

view

facebook.com/include twitter.com/include ``` ### filters index.html ```html @@include(markdown('view.md')) @@include('./var.html', { "name": "haoxin", "age": 12345 }) ``` view.md ```html view ==== ``` gulpfile.js ```js const fileinclude = require('gulp-file-include'); const markdown = require('markdown'); const gulp = require('gulp'); gulp.task('fileinclude', function() { gulp.src(['index.html']) .pipe(fileinclude({ filters: { markdown: markdown.parse } })) .pipe(gulp.dest('./')); }); ``` ### `if` statement index.html ``` @@include('some.html', { "nav": true }) @@if (name === 'test' && nav === true) { @@include('test.html') } ``` gulpfile.js ```js fileinclude({ context: { name: 'test' } }); ``` ### `for` statement index.html ```html ``` gulpfile.js ```js fileinclude({ context: { arr: ['test1', 'test2'] } }); ``` ### `loop` statement index.html ```html @@loop('loop-article.html', [ { "title": "My post title", "text": "

lorem ipsum...

" }, { "title": "Another post", "text": "

lorem ipsum...

" }, { "title": "One more post", "text": "

lorem ipsum...

" } ]) ``` loop-article.html ```html

@@title

@@text
``` ### `loop` statement + `data.json` data.json ```js [ { "title": "My post title", "text": "

lorem ipsum...

" }, { "title": "Another post", "text": "

lorem ipsum...

" }, { "title": "One more post", "text": "

lorem ipsum...

" } ] ``` loop-article.html ```html @@loop("loop-article.html", "data.json") ``` ### `webRoot` built-in context variable The `webRoot` field of the context contains the relative path from the source document to the source root (unless the value is already set in the context options). support/contact/index.html ```html

Support Contact Info

``` result: ```html

Support Contact Info

``` ### License MIT [npm-img]: https://img.shields.io/npm/v/gulp-file-include.svg?style=flat-square [npm-url]: https://npmjs.org/package/gulp-file-include [travis-img]: https://img.shields.io/travis/haoxins/gulp-file-include.svg?style=flat-square [travis-url]: https://travis-ci.org/haoxins/gulp-file-include [coveralls-img]: https://img.shields.io/coveralls/coderhaoxin/gulp-file-include.svg?style=flat-square [coveralls-url]: https://coveralls.io/r/coderhaoxin/gulp-file-include?branch=master [license-img]: http://img.shields.io/badge/license-MIT-green.svg?style=flat-square [license-url]: http://opensource.org/licenses/MIT [david-img]: https://img.shields.io/david/coderhaoxin/gulp-file-include.svg?style=flat-square [david-url]: https://david-dm.org/coderhaoxin/gulp-file-include [gitter-img]: https://badges.gitter.im/Join%20Chat.svg [gitter-url]: https://gitter.im/coderhaoxin/gulp-file-include?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge ================================================ FILE: example/a/a1.txt ================================================ this is a1 // include a2 @@include('./a2.txt') ================================================ FILE: example/a/a2.txt ================================================ this is a2 // include b1 @@include('../b/b1.txt') ================================================ FILE: example/b/b1.txt ================================================ this is b1 // include b2 @@include('./b2.txt') ================================================ FILE: example/b/b2.txt ================================================ this is b2 ================================================ FILE: example/c/c.txt ================================================ this is loop @@number ================================================ FILE: example/conditional.txt ================================================ this is conditional Flatten object "@@obj" @@if(typeof context.obj === 'object') { Flatten variable "@@obj.obj_param" } Defined in parent "@@param" Context variable "@@name" ================================================ FILE: example/gulpfile.js ================================================ 'use strict' const fileinclude = require('..') const gulp = require('gulp') gulp.task('include', () => { gulp.src(['index.txt']) .pipe(fileinclude({ prefix: '@@', basepath: '@file', context: { name: 'example' } })) .pipe(gulp.dest('./result')) }) ================================================ FILE: example/index.txt ================================================ index @@include('a/a1.txt') @@name @@if (context.name === 'example') { @@include('conditional.txt', { "param": "success", "obj": { "obj_param": "flatten param" } }) @@include('conditional.txt', { "param": "success too" }) Defined in include @@param } @@loop('c/c.txt', [ { "number": 1 }, { "number": 2 }, { "number": 3 } ]) ================================================ FILE: example/result/index.txt ================================================ index this is a1 // include a2 this is a2 // include b1 this is b1 // include b2 this is b2 example this is conditional Flatten object "@@obj" Flatten variable "flatten param" Defined in parent "success" Context variable "example" this is conditional Flatten object "@@obj" Defined in parent "success too" Context variable "example" Defined in include @@param this is loop 1this is loop 2this is loop 3 ================================================ FILE: lib/indent.js ================================================ module.exports = function(src, index, dest) { var indent = '' var valid = false while (src[index -= 1] == 0) { // eslint-disable-line if (src[index] === '\n') { valid = true break } indent = src[index] + indent } if (valid) { dest = dest.split('\n').map(function(str, i) { return str == 0 || i === 0 ? str : (indent + str) // eslint-disable-line }).join('\n') } return dest } ================================================ FILE: lib/index.js ================================================ 'use strict' const replaceOperator = require('./replace-operator') const replaceFunction = require('./replace-function') const replaceVariable = require('./replace-variable') const concat = require('concat-stream') const setIndent = require('./indent') const through = require('through2') const Vinyl = require('vinyl') const PluginError = require('plugin-error') const extend = require('extend') const path = require('path') const fs = require('fs') const JSON5 = require('json5') module.exports = function(opts) { if (typeof opts === 'string') { opts = {prefix: opts} } opts = extend({}, { basepath: '@file', prefix: '@@', suffix: '', context: {}, filters: false, indent: false }, opts) if (opts.basepath !== '@file') { opts.basepath = opts.basepath === '@root' ? process.cwd() : path.resolve(opts.basepath) } var customWebRoot = !!opts.context.webRoot var includeOnceFiles = {}; function fileInclude(file, enc, cb) { if (!customWebRoot) { // built-in webRoot variable, example usage: opts.context.webRoot = path.relative(path.dirname(file.path), file.base).replace(/\\/g, '/') || '.' } if (file.isNull()) { cb(null, file) } else if (file.isStream()) { file.contents.pipe(concat(function(data) { try { data = include(file, String(data)) cb(null, data) } catch (e) { cb(new PluginError('gulp-file-include', e.message)) } })) } else if (file.isBuffer()) { try { file = include(file, String(file.contents)) cb(null, file) } catch (e) { cb(new PluginError('gulp-file-include', e.message)) } } } return through.obj(fileInclude) /** * utils */ function stripCommentedIncludes(content, opts) { // remove single line html comments that use the format: var regex = new RegExp('', 'g') return content.replace(regex, '') } function include(file, text, data, sourceFile = '') { var filebase = opts.basepath === '@file' ? path.dirname(file.path) : opts.basepath var currentFilename = path.resolve(file.base, file.path) data = extend(true, {}, opts.context, data || {}) data.content = text text = stripCommentedIncludes(text, opts) text = replaceOperator(text, { prefix: opts.prefix, suffix: opts.suffix, name: 'if', handler: conditionalHandler, sourceFile: sourceFile }) text = replaceOperator(text, { prefix: opts.prefix, suffix: opts.suffix, name: 'for', handler: forHandler, sourceFile: sourceFile }) text = replaceVariable(text, data, opts) text = replaceFunction(text, { prefix: opts.prefix, suffix: opts.suffix, name: 'include_once', handler: includeOnceHandler, sourceFile: sourceFile }) text = replaceFunction(text, { prefix: opts.prefix, suffix: opts.suffix, name: 'include', handler: includeHandler, sourceFile: sourceFile }) text = replaceFunction(text, { prefix: opts.prefix, suffix: opts.suffix, name: 'loop', handler: loopHandler, sourceFile: sourceFile }) function conditionalHandler(inst) { try { var condition = new Function('var context = this; with (context) { return ' + inst.args + '; }').call(data) // eslint-disable-line } catch (error) { throw new Error(error.message + ': ' + inst.args) } return condition ? inst.body : '' } function forHandler(inst) { var forLoop = 'for' + inst.args + ' { result+=`' + inst.body + '`; }' var condition = 'var context = this; with (context) { var result=""; ' + forLoop + ' return result; }' try { var result = new Function(condition).call(data) // eslint-disable-line } catch (error) { throw new Error(error.message + ': ' + forLoop) } return result } function includeOnceHandler(inst) { var args = /[^)"']*["']([^"']*)["'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args) if (args) { if (typeof includeOnceFiles[inst.sourceFile] === 'undefined') { includeOnceFiles[inst.sourceFile] = []; } if (includeOnceFiles[inst.sourceFile].indexOf(args[1]) === -1) { includeOnceFiles[inst.sourceFile].push(args[1]); return includeHandler(inst) } else { return ''; } } } function includeHandler(inst) { var args = /[^)"']*["']([^"']*)["'](,\s*({[\s\S]*})){0,1}\s*/.exec(inst.args) if (args) { var includePath = path.resolve(filebase, args[1]) // for checking if we are not including the current file again if (currentFilename.toLowerCase() === includePath.toLowerCase()) { throw new Error('recursion detected in file: ' + currentFilename) } var includeContent = fs.readFileSync(includePath, 'utf-8') if (opts.indent) { includeContent = setIndent(inst.before, inst.before.length, includeContent) } // need to double each `$` to escape it in the `replace` function // includeContent = includeContent.replace(/\$/gi, '$$$$'); // apply filters on include content if (typeof opts.filters === 'object') { includeContent = applyFilters(includeContent, args.input) } var recFile = new Vinyl({ cwd: process.cwd(), base: file.base, path: includePath, contents: Buffer.from(includeContent) }) recFile = include(recFile, includeContent, args[3] ? JSON5.parse(args[3]) : {}, inst.sourceFile != '' ? inst.sourceFile : currentFilename) return String(recFile.contents) } } function loopHandler(inst) { var args = /[^)"']*["']([^"']*)["'](,\s*([\s\S]*())){0,1}\s*/.exec(inst.args) var arr = [] if (args) { // loop array in the json file if (args[3].match(/^('|")[^']|[^"]('|")$/)) { // clean filename var and define path var jsonPath = args[3].replace(/^('|")/, '').replace(/('|")$/, '') var jsonfile = path.join(file.base, jsonPath) // check if json file exists if (fs.existsSync(jsonfile)) { // make sure we are getting the updated version of the json file delete require.cache[jsonfile] arr = require(jsonfile) } else { return console.error('JSON file not exists:', jsonfile) } } else { // loop array in the function try { arr = JSON5.parse(args[3]) } catch (err) { return console.error(err, args[3]) } } if (arr) { var includePath = path.resolve(filebase, args[1]) // for checking if we are not including the current file again if (currentFilename.toLowerCase() === includePath.toLowerCase()) { throw new Error('recursion detected in file: ' + currentFilename) } var includeContent = fs.readFileSync(includePath, 'utf-8') if (opts.indent) { includeContent = setIndent(inst.before, inst.before.length, includeContent) } // apply filters on include content if (typeof opts.filters === 'object') { includeContent = applyFilters(includeContent, args.input) } var recFile = new Vinyl({ cwd: process.cwd(), base: file.base, path: includePath, contents: Buffer.from(includeContent) }) var contents = '' for (var i in arr) { if (arr.hasOwnProperty(i)) { var context = arr[i] recFile = include(recFile, includeContent, args[3] ? context : {}, inst.sourceFile != '' ? inst.sourceFile : currentFilename) // why handler dont reconize underscore? // if (typeof context == 'object' && typeof context['_key'] == 'undefined') { // context['_key'] = i; // } contents += String(recFile.contents) } } } return contents } } file.contents = Buffer.from(text) return file } function applyFilters(includeContent, match) { if (!match.match(/\)+$/)) { // nothing to filter return unchanged return includeContent } // now get the ordered list of filters var filterlist = match.split('(').slice(0, -1) filterlist = filterlist.map(function(str) { return opts.filters[str.trim()] }) // compose them together into one function var filter = filterlist.reduce(compose) // check match for filter options object var options = match.match('{([^}]*)}') // and apply the composed function to the stringified content if (options) { options = JSON5.parse(options[0]) return filter(String(includeContent), options) } else { return filter(String(includeContent)) } } } function compose(f, g) { return function(x) { return f(g(x)) } } ================================================ FILE: lib/replace-function.js ================================================ 'use strict' const balanced = require('balanced-match') module.exports = function(content, opts) { var result = '' var reStart = new RegExp(opts.prefix + '[ ]*' + opts.name + '\\(') var reEnd = new RegExp('^[ ]*' + opts.suffix) var matchStart var matchArg var matchEnd var safeStart var before var replacement while (matchStart = reStart.exec(content)) { // eslint-disable-line safeStart = matchStart.index + matchStart[0].length - 1 matchArg = balanced('(', ')', content.slice(safeStart)) if (matchArg && matchArg.start === 0) { if (opts.suffix) { matchEnd = reEnd.exec(matchArg.post) } matchEnd = matchEnd ? matchEnd.index + matchEnd[0].length : 0 if (!opts.suffix || matchEnd) { before = content.slice(0, matchStart.index) replacement = opts.handler({ before: before, args: matchArg.body, sourceFile: opts.sourceFile }) if (replacement !== undefined) { result += before + replacement.toString() content = content.slice(safeStart + matchArg.end + 1 + matchEnd) continue } } } result += content.slice(0, safeStart) content = content.slice(safeStart) } result += content return result } ================================================ FILE: lib/replace-operator.js ================================================ 'use strict' const balanced = require('balanced-match') module.exports = function parse(content, opts) { var regexpStart = new RegExp(opts.prefix + '[ ]*' + opts.name + '([^{}]*)\\{') var regexpEnd = opts.suffix ? new RegExp('^\\s*' + opts.suffix) : false var replacement var result = '' var matchStart var matchBody var matchEnd var startEnd var before while (matchStart = regexpStart.exec(content)) { // eslint-disable-line startEnd = matchStart.index + matchStart[0].length matchBody = balanced('{', '}', content.slice(startEnd - 1)) if (matchBody && matchBody.start === 0) { matchEnd = regexpEnd ? regexpEnd.exec(matchBody.post) : true if (matchEnd) { before = content.slice(0, matchStart.index) matchEnd = regexpEnd ? matchEnd[0].length : 0 replacement = opts.handler({ before: before, args: matchStart[1], body: matchBody.body }) if (replacement !== undefined) { result += before + parse(replacement.toString(), opts) content = content.slice(startEnd + matchBody.end + matchEnd) continue } } } result += content.slice(0, startEnd) content = content.slice(startEnd) } result += content return result } ================================================ FILE: lib/replace-variable.js ================================================ 'use strict' const flatten = require('flatnest').flatten module.exports = function(content, data, opts) { var prefix = opts.prefix + '[ ]*' var suffix = opts.suffix ? '[ ]*' + opts.suffix : '' data = flatten(data) // sort keys by longest keys to iterate in that order var keys = Object.keys(data).sort() var i = keys.length - 1 var key for (; ~i; i -= 1) { key = keys[i] content = content.replace(new RegExp(prefix + key + suffix, 'g'), data[key]) } return content } ================================================ FILE: package.json ================================================ { "name": "gulp-file-include", "version": "2.3.0", "description": "A gulp plugin for file include", "main": "lib/index.js", "scripts": { "lint": "eslint lib test/*.js --fix", "test": "mocha -R spec -t 200 test/*.js", "test-cov": "istanbul cover node_modules/.bin/_mocha -- -R dot -t 200 test/*.js", "test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- -R dot -t 200 test/*.js" }, "repository": "haoxins/gulp-file-include", "keywords": [ "gulpplugin", "file", "include", "replace", "gulp", "plugin" ], "files": [ "lib" ], "author": "haoxin", "contributors": [ "Bogdan Chadkin ", "Arthur Araújo " ], "license": "MIT", "dependencies": { "balanced-match": "^1.0.0", "concat-stream": "^2.0.0", "extend": "^3.0.2", "flatnest": "^1.0.0", "json5": "^2.1.3", "plugin-error": "^1.0.1", "through2": "^4.0.2", "vinyl": "^2.2.1" }, "devDependencies": { "eslint-config-ok": "github:haoxins/eslint-config", "gulp": "^4.0.2", "istanbul": "^0.4.5", "markdown": "^0.5.0", "mocha": "^8.2.1", "should": "^13.2.3" }, "eslintConfig": { "extends": [ "ok" ] } } ================================================ FILE: test/.editorconfig ================================================ [*] insert_final_newline = false ================================================ FILE: test/edge-case.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## gulp-file-include', () => { describe('# edge cases', () => { it('should escape included content to avoid recursive includes', done => { var file = new Vinyl({ path: 'test/fixtures-edge-case/index.html', contents: fs.createReadStream('test/fixtures-edge-case/index.html') }) var expected = fs.readFileSync('test/fixtures-edge-case/result.html', 'utf8') var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(expected) done() }) stream.write(file) stream.end() }) it('should work without trailing newline', done => { var file = new Vinyl({ path: 'test/fixtures-edge-case/without-trailing-newline.txt', contents: fs.createReadStream('test/fixtures-edge-case/without-trailing-newline.txt') }) var expected = fs.readFileSync('test/fixtures-edge-case/without-trailing-newline-result.txt', 'utf8') var stream = fileIncludePlugin() stream.on('data', function(newFile) { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(expected) done() }) stream.write(file) stream.end() }) it('should skip commented includes', done => { var file = new Vinyl({ path: 'test/fixtures-edge-case/commented-inclusion.html', contents: fs.createReadStream('test/fixtures-edge-case/commented-inclusion.html') }) var expected = fs.readFileSync('test/fixtures-edge-case/commented-inclusion-result.html', 'utf8').replace(/\s/g, '') var stream = fileIncludePlugin() stream.on('data', newFile => { var inputString = String(newFile.contents).replace(/\s/g, '') inputString.should.equal(expected) done() }) stream.write(file) stream.end() }) it('should give an error on recursive includes', done => { var file = new Vinyl({ path: 'test/fixtures-edge-case/recursion.html', contents: fs.createReadStream('test/fixtures-edge-case/recursion.html') }) var stream = fileIncludePlugin() stream.on('error', err => { should.exist(err) done() }) stream.write(file) stream.end() }) // it('should give an error on circular recursive includes', function(done) { // var file = new Vinyl({ // path: 'test/fixtures-edge-case/a.html', // contents: fs.createReadStream('test/fixtures-edge-case/a.html') // }); // var stream = fileIncludePlugin(); // stream.on('error', function(err) { // should.exist(err); // done(); // }); // stream.write(file); // stream.end(); // }); }) }) ================================================ FILE: test/error.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const fs = require('fs') const os = require('os') require('should') describe('## error', () => { it('# if statement', done => { var file = new Vinyl({ path: 'test/fixtures-error/if.html', contents: fs.readFileSync('test/fixtures-error/if.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@root' }) stream.on('error', error => { error.message.should.equal('invalid is not defined: (invalid === true) ') done() }) stream.write(file) stream.end() }) it('# for statement', done => { var file = new Vinyl({ path: 'test/fixtures-error/if.html', contents: fs.readFileSync('test/fixtures-error/for.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@root' }) stream.on('error', error => { error.message.should.equal('invalid is not defined: for (var i = 0; i < invalid.length; i++) { result+=`' + os.EOL + ' ' + os.EOL + ' `; }') done() }) stream.write(file) stream.end() }) }) ================================================ FILE: test/filters.js ================================================ 'use strict' const fileIncludePlugin = require('..') const markdown = require('markdown') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## gulp-file-include', () => { var result = fs.readFileSync('test/fixtures/result.html', 'utf8') describe('# options - filters', () => { it('file - filters: markdown', done => { var file = new Vinyl({ path: 'test/fixtures/index-markdown.html', contents: fs.readFileSync('test/fixtures/index-markdown.html') }) var stream = fileIncludePlugin({ filters: { markdown: markdown.parse } }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - filters: markdown', done => { var file = new Vinyl({ path: 'test/fixtures/index-markdown.html', contents: fs.createReadStream('test/fixtures/index-markdown.html') }) var stream = fileIncludePlugin({ filters: { markdown: markdown.parse } }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('file - filters: markdown & rot13', done => { var file = new Vinyl({ path: 'test/fixtures/index-markdown-rot13.html', contents: fs.readFileSync('test/fixtures/index-markdown-rot13.html') }) var stream = fileIncludePlugin({ filters: { markdown: markdown.parse, rot13: rot13 } }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - filters: markdown & rot13', done => { var file = new Vinyl({ path: 'test/fixtures/index-markdown-rot13.html', contents: fs.createReadStream('test/fixtures/index-markdown-rot13.html') }) var stream = fileIncludePlugin({ filters: { markdown: markdown.parse, rot13: rot13 } }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('file - filters: custom filter handler options', done => { var file = new Vinyl({ path: 'test/fixtures/index-handler-options.html', contents: fs.createReadStream('test/fixtures/index-handler-options.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@file', filters: { handler: function(content, options) { should.exist(options) should.exist(options.age) should.exist(options.name) // other handler code or // template engine compiling here, i.e.: // var template = Handlebars.compile(content); // return template(options); return content } } }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) }) function rot13(str) { // original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) return (str + '').replace(/[a-z]/gi, s => { return String.fromCharCode( s.charCodeAt(0) + (s.toLowerCase() < 'n' ? 13 : -13) ) }) } ================================================ FILE: test/fixtures/arr-result.html ================================================

view

================================================ FILE: test/fixtures/arr.html ================================================ @@for (var i = 0; i < name.length; i++) { } ================================================ FILE: test/fixtures/index-01.html ================================================ @@include('view.html') @@ if(true) {@@include('./var.html', { "name": "haoxin", "age": 12345 })} ================================================ FILE: test/fixtures/index-02.html ================================================ @@include('fixtures/view.html') @@include('./fixtures/var.html', { "name": "haoxin", "age": 12345 }) ================================================ FILE: test/fixtures/index-03.html ================================================ @@include('./test/fixtures/view.html') @@include('test/fixtures/var.html', { "name": "haoxin", "age": 12345 }) ================================================ FILE: test/fixtures/index-04.js ================================================ (function(okanjo) { okanjo.mvc.registerCss('main', '@@include(jsStringEscape("./test/fixtures/view.html"))', { id: 'okanjo-test-main' }); okanjo.mvc.registerCss('main', '@@include(jsStringEscape("test/fixtures/var.html", { "name": "haoxin", "age": 12345 }))', { id: 'okanjo-test-main' }); })(okanjo); ================================================ FILE: test/fixtures/index-05.html ================================================ @@include('view.html') @@include('./arr.html', { "name": ["haoxin", 12345] }) ================================================ FILE: test/fixtures/index-handler-options.html ================================================ @@include('view.html') @@include(handler('var.html', { "name": "haoxin", "age": 12345 })) ================================================ FILE: test/fixtures/index-markdown-rot13.html ================================================ @@include(markdown(rot13('view.rot13.md'))) @@include(rot13('./var.rot13.html', { "name": "haoxin", "age": 12345 })) ================================================ FILE: test/fixtures/index-markdown.html ================================================ @@ include(markdown('view.md')) @@include('./var.html', { "name": "haoxin", "age": 12345 }) ================================================ FILE: test/fixtures/result.html ================================================

view

================================================ FILE: test/fixtures/result.js ================================================ (function(okanjo) { okanjo.mvc.registerCss('main', '

view

', { id: 'okanjo-test-main' }); okanjo.mvc.registerCss('main', ' ', { id: 'okanjo-test-main' }); })(okanjo); ================================================ FILE: test/fixtures/sameprefix-result.html ================================================ Cool Link ================================================ FILE: test/fixtures/sameprefix-var.html ================================================ @@title ================================================ FILE: test/fixtures/sameprefix.html ================================================ @@include('./sameprefix-var.html', { "title": "Cool Link", "title_link": "http://drewlustro.com" }) ================================================ FILE: test/fixtures/var.html ================================================ ================================================ FILE: test/fixtures/var.rot13.html ================================================ @@anzr @@ntr ================================================ FILE: test/fixtures/view.html ================================================

view

================================================ FILE: test/fixtures/view.md ================================================ view ==== ================================================ FILE: test/fixtures/view.rot13.md ================================================ ivrj ==== ================================================ FILE: test/fixtures-edge-case/a.html ================================================ @@include('b.html') ================================================ FILE: test/fixtures-edge-case/b.html ================================================ @@include('a.html') ================================================ FILE: test/fixtures-edge-case/commented-inclusion-result.html ================================================ ================================================ FILE: test/fixtures-edge-case/commented-inclusion.html ================================================ ================================================ FILE: test/fixtures-edge-case/dangerous.txt ================================================ This: $& should not trigger recursive includes. ================================================ FILE: test/fixtures-edge-case/index.html ================================================ @@include('dangerous.txt') ================================================ FILE: test/fixtures-edge-case/recursion.html ================================================ @@include('recursion.html') ================================================ FILE: test/fixtures-edge-case/result.html ================================================ This: $& should not trigger recursive includes. ================================================ FILE: test/fixtures-edge-case/without-trailing-newline-result.txt ================================================ hello world! This: $& should not trigger recursive includes. without trailing newline This: $& should not trigger recursive includes. !@ !!This: $& should not trigger recursive includes. !@ bye ================================================ FILE: test/fixtures-edge-case/without-trailing-newline.txt ================================================ hello world! @@include('dangerous.txt')without trailing newline @@include('dangerous.txt')!@ !!@@include('dangerous.txt')!@ bye ================================================ FILE: test/fixtures-error/for.html ================================================ @@for (var i = 0; i < invalid.length; i++) { } ================================================ FILE: test/fixtures-error/if.html ================================================ @@if (invalid === true) {

a

} ================================================ FILE: test/fixtures-flatten/index.html ================================================ @@obj.param1 @@obj.param2 ================================================ FILE: test/fixtures-flatten/result.html ================================================ value1 value2 ================================================ FILE: test/fixtures-indent/index.html ================================================

Hello

Sub //= include('sub.html')
================================================ FILE: test/fixtures-indent/result.html ================================================

Hello

Sub

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam in nisi blanditiis!

Lorem ipsum dolor sit amet, consectetur.

Am Dig Bo
================================================ FILE: test/fixtures-indent/sub-sub.html ================================================ Am Dig Bo ================================================ FILE: test/fixtures-indent/sub.html ================================================

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laboriosam in nisi blanditiis!

Lorem ipsum dolor sit amet, consectetur.

//= include('sub-sub.html')
================================================ FILE: test/fixtures-nested/index.html ================================================ @@include('var.html', { "name": "haoxin", "age": 12345, "socials": { "fb": "facebook.com/include", "tw": "twitter.com/include" } }) ================================================ FILE: test/fixtures-nested/result.html ================================================ facebook.com/include twitter.com/include ================================================ FILE: test/fixtures-nested/var.html ================================================ @@socials.fb @@socials.tw ================================================ FILE: test/fixtures-nested-once/index-twice.html ================================================ @@include_once('var.html', { "name": "haoxin", "age": 12345, "socials": { "fb": "facebook.com/include", "tw": "twitter.com/include" } }) @@include_once('var.html', { "name": "haoxin", "age": 12345, "socials": { "fb": "facebook.com/include", "tw": "twitter.com/include" } }) ================================================ FILE: test/fixtures-nested-once/index.html ================================================ @@include_once('var.html', { "name": "haoxin", "age": 12345, "socials": { "fb": "facebook.com/include", "tw": "twitter.com/include" } }) ================================================ FILE: test/fixtures-nested-once/result-twice.html ================================================ facebook.com/include twitter.com/include ================================================ FILE: test/fixtures-nested-once/result.html ================================================ facebook.com/include twitter.com/include ================================================ FILE: test/fixtures-nested-once/var.html ================================================ @@socials.fb @@socials.tw ================================================ FILE: test/fixtures-operator/index.html ================================================ @@if (content.indexOf('>a') > -1) {

a

} @@if(content.indexOf('>a') > -1){ {

a

} } @@if content.indexOf('>a') > -1 {

a

} @@if (content.length > 888) {

b

} @@if (name === 'c') { @@if (name === 'c') {

c

} } @@ if (name === 'c' && context.title === 'haoxin') {

c

} @@ if (name === 'c' && context.title !== 'haoxin') {

c

} @@if(name === 'c') { ================================================ FILE: test/fixtures-operator/result-index.html ================================================

a

{

a

}

a

c

c

@@if(name === 'c') { ================================================ FILE: test/fixtures-operator/result-suffix.html ================================================

a

{

a

}

a

c

c

@@if(name === 'c') { } @@if(name === 'c') { ================================================ FILE: test/fixtures-operator/suffix.html ================================================ @@if (content.indexOf('>a') > -1) {

a

}## @@if(content.indexOf('>a') > -1){ {

a

} } ## @@if content.indexOf('>a') > -1 {

a

} ## @@if (content.length > 888) {

b

} ## @@if (name === 'c') { @@if (name === 'c') {

c

}## } ## @@ if (name === 'c' && context.title === 'haoxin') {

c

}## @@ if (name === 'c' && context.title !== 'haoxin') {

c

}## @@if(name === 'c') { } @@if(name === 'c') { ================================================ FILE: test/fixtures-recursion/index.txt ================================================ this is index @@include('./sub/a.txt') @@include('./sub/b.txt') @@include('./var.txt', { "name": "from index.txt" }) ================================================ FILE: test/fixtures-recursion/result.txt ================================================ this is index this is a.txt this is b.txt name is: from b.txt this is b.txt name is: from b.txt name is: from index.txt ================================================ FILE: test/fixtures-recursion/sub/a.txt ================================================ this is a.txt @@include('./b.txt') ================================================ FILE: test/fixtures-recursion/sub/b.txt ================================================ this is b.txt @@include('../var.txt', { "name": "from b.txt" }) ================================================ FILE: test/fixtures-recursion/var.txt ================================================ name is: @@name ================================================ FILE: test/fixtures-suffix/index.html ================================================ @@include('../fixtures/view.html')@@ @@include('./var.html', { "name": "haoxin", "age": 12345 })@@ ================================================ FILE: test/fixtures-suffix/var.html ================================================ ================================================ FILE: test/fixtures-variable/index-suffix.html ================================================ ================================================ FILE: test/fixtures-variable/index.html ================================================ ================================================ FILE: test/fixtures-variable/result.html ================================================ ================================================ FILE: test/fixtures-webroot-variable/html-head.html ================================================ ================================================ FILE: test/fixtures-webroot-variable/index.html ================================================ @@include('html-head.html')

Page

About @@include('sub/home-link.html') ================================================ FILE: test/fixtures-webroot-variable/result.html ================================================

Page

About Home ================================================ FILE: test/fixtures-webroot-variable/sub/home-link.html ================================================ Home ================================================ FILE: test/fixtures-webroot-variable/sub/index.html ================================================ @@include('../html-head.html')

Sub Page

About @@include('home-link.html') ================================================ FILE: test/fixtures-webroot-variable/sub/result.html ================================================

Sub Page

About Home ================================================ FILE: test/flatten.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## gulp-file-include', () => { var result = fs.readFileSync('test/fixtures-flatten/result.html', 'utf8') describe('# flatten variables', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures-flatten/index.html', contents: fs.readFileSync('test/fixtures-flatten/index.html') }) var stream = fileIncludePlugin({ context: { obj: { 'param1': 'value1', 'param2': 'value2' } } }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures-flatten/index.html', contents: fs.createReadStream('test/fixtures-flatten/index.html') }) var stream = fileIncludePlugin({ context: { obj: { 'param1': 'value1', 'param2': 'value2' } } }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) }) ================================================ FILE: test/indent.js ================================================ 'use strict' const setIndent = require('../lib/indent') require('should') describe('## indent', () => { it('# basic', done => { setIndent( ' content \n\t start', 13, 'some other \n content').should.equal('some other \n\t content') done() }) it('# basic with \\r\\n newlines', done => { setIndent( ' content \r\n\t start', 14, '\r\n\r\nsome other \r\n content').should.equal('\r\n\r\n\t some other \r\n\t content') done() }) it('# with non-space char', done => { setIndent( ' content \n\t g start', 14, '\n\nsome other \n content').should.equal('\n\nsome other \n content') done() }) it('# with non-space char and \\r\\n newlines', done => { setIndent( ' content \r\n\t g start', 15, '\r\n\r\nsome other \r\n content').should.equal('\r\n\r\nsome other \r\n content') done() }) }) ================================================ FILE: test/index.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## gulp-file-include', () => { var result = fs.readFileSync('test/fixtures/result.html', 'utf8') var resultJS = fs.readFileSync('test/fixtures/result.js', 'utf8') var resultSamePrefix = fs.readFileSync('test/fixtures/sameprefix-result.html', 'utf8') var resultArr = fs.readFileSync('test/fixtures/arr-result.html', 'utf8') describe('# default', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures/index-01.html', contents: fs.readFileSync('test/fixtures/index-01.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures/index-01.html', contents: fs.createReadStream('test/fixtures/index-01.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) describe('# options - basepath', () => { it('file - basepath: @file', done => { var file = new Vinyl({ path: 'test/fixtures/index-01.html', contents: fs.readFileSync('test/fixtures/index-01.html') }) var stream = fileIncludePlugin({ basepath: '@file' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - basepath: @file', done => { var file = new Vinyl({ path: 'test/fixtures/index-01.html', contents: fs.createReadStream('test/fixtures/index-01.html') }) var stream = fileIncludePlugin({ basepath: '@file' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('file - basepath: @root', done => { var file = new Vinyl({ path: 'test/fixtures/index-03.html', contents: fs.readFileSync('test/fixtures/index-03.html') }) var stream = fileIncludePlugin({ basepath: '@root' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - basepath: @root', done => { var file = new Vinyl({ path: 'test/fixtures/index-03.html', contents: fs.createReadStream('test/fixtures/index-03.html') }) var stream = fileIncludePlugin({ basepath: '@root' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('file - basepath: dir', done => { var file = new Vinyl({ path: 'test/fixtures/index-02.html', contents: fs.readFileSync('test/fixtures/index-02.html') }) var stream = fileIncludePlugin({ basepath: __dirname }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - basepath: dir', done => { var file = new Vinyl({ path: 'test/fixtures/index-02.html', contents: fs.createReadStream('test/fixtures/index-02.html') }) var stream = fileIncludePlugin({ basepath: __dirname }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) describe('# options - prefix, basepath', () => { it('file - basepath: @file', done => { var file = new Vinyl({ path: 'test/fixtures/index-01.html', contents: fs.readFileSync('test/fixtures/index-01.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@file' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - basepath: @file', done => { var file = new Vinyl({ path: 'test/fixtures/index-01.html', contents: fs.createReadStream('test/fixtures/index-01.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@file' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('file - basepath: @root', done => { var file = new Vinyl({ path: 'test/fixtures/index-03.html', contents: fs.readFileSync('test/fixtures/index-03.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@root' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - basepath: @root', done => { var file = new Vinyl({ path: 'test/fixtures/index-03.html', contents: fs.createReadStream('test/fixtures/index-03.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@root' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('file - basepath: dir', done => { var file = new Vinyl({ path: 'test/fixtures/index-02.html', contents: fs.readFileSync('test/fixtures/index-02.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: __dirname }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream - basepath: dir', done => { var file = new Vinyl({ path: 'test/fixtures/index-02.html', contents: fs.createReadStream('test/fixtures/index-02.html') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: __dirname }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) describe('# options - suffix', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures-suffix/index.html', contents: fs.readFileSync('test/fixtures-suffix/index.html') }) var stream = fileIncludePlugin({ suffix: '@@' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents); // TODO (String(newFile.contents) === result).should.equal(true) // String(newFile.contents).should.equal(result); done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures-suffix/index.html', contents: fs.createReadStream('test/fixtures-suffix/index.html') }) var stream = fileIncludePlugin({ suffix: '@@' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents); // TODO (String(newFile.contents) === result).should.equal(true) // String(newFile.contents).should.equal(result); done() }) stream.write(file) stream.end() }) }) describe('# vars - same key prefix', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures/sameprefix.html', contents: fs.readFileSync('test/fixtures/sameprefix.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultSamePrefix) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures/sameprefix.html', contents: fs.createReadStream('test/fixtures/sameprefix.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultSamePrefix) done() }) stream.write(file) stream.end() }) }) describe('# aggressive regex', () => { it('file - basepath: @root', done => { var file = new Vinyl({ path: 'test/fixtures/index-04.js', contents: fs.readFileSync('test/fixtures/index-04.js') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@root' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultJS) done() }) stream.write(file) stream.end() }) it('stream - basepath: @root', done => { var file = new Vinyl({ path: 'test/fixtures/index-04.js', contents: fs.createReadStream('test/fixtures/index-04.js') }) var stream = fileIncludePlugin({ prefix: '@@', basepath: '@root' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultJS) done() }) stream.write(file) stream.end() }) }) describe('# for statement', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures/index-05.html', contents: fs.readFileSync('test/fixtures/index-05.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultArr) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures/index-05.html', contents: fs.createReadStream('test/fixtures/index-05.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultArr) done() }) stream.write(file) stream.end() }) }) }) ================================================ FILE: test/nested-once.js ================================================ 'use strict' const fileIncludePlugin = require('../lib') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## gulp-file-include', () => { var result = fs.readFileSync('test/fixtures-nested-once/result.html', 'utf8') var resultTwice = fs.readFileSync('test/fixtures-nested-once/result-twice.html', 'utf8') describe('# nested once arguments', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures-nested-once/index.html', contents: fs.readFileSync('test/fixtures-nested-once/index.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures-nested-once/index.html', contents: fs.createReadStream('test/fixtures-nested-once/index.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) describe('# nested once arguments twice', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures-nested-once/index-twice.html', contents: fs.readFileSync('test/fixtures-nested-once/index-twice.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultTwice) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures-nested-once/index-twice.html', contents: fs.createReadStream('test/fixtures-nested-once/index-twice.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(resultTwice) done() }) stream.write(file) stream.end() }) }) }) ================================================ FILE: test/nested.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## gulp-file-include', () => { var result = fs.readFileSync('test/fixtures-nested/result.html', 'utf8') describe('# nested arguments', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures-nested/index.html', contents: fs.readFileSync('test/fixtures-nested/index.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures-nested/index.html', contents: fs.createReadStream('test/fixtures-nested/index.html') }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) }) ================================================ FILE: test/operator.js ================================================ 'use strict' const parse = require('../lib/replace-operator') const fs = require('fs') require('should') describe('## operator', () => { it('# basic usage', done => { var result = fs.readFileSync('test/fixtures-operator/result-index.html', 'utf-8') var index = fs.readFileSync('test/fixtures-operator/index.html', 'utf-8') parse(index, { prefix: '@@', suffix: '', name: 'if', handler: inst => { var condition = new Function('var context = this; with (context) { return ' + inst.args + '; }').call({ // eslint-disable-line content: index, name: 'c' }) return condition ? inst.body : '' } }).should.equal(result) done() }) it('# with suffix', done => { var result = fs.readFileSync('test/fixtures-operator/result-suffix.html', 'utf-8') var index = fs.readFileSync('test/fixtures-operator/suffix.html', 'utf-8') parse(index, { name: 'if', prefix: '@@', suffix: '##', handler: inst => { var condition = new Function('var context = this; with (context) { return ' + inst.args + '; }').call({ // eslint-disable-line content: index, name: 'c' }) return condition ? inst.body : '' } }).should.equal(result) done() }) }) ================================================ FILE: test/plugin-indent.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## gulp-file-include', () => { var result = fs.readFileSync('test/fixtures-indent/result.html', 'utf-8') describe('# indent', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures-indent/index.html', contents: fs.readFileSync('test/fixtures-indent/index.html') }) var stream = fileIncludePlugin({ prefix: '//=', indent: true }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures-indent/index.html', contents: fs.createReadStream('test/fixtures-indent/index.html') }) var stream = fileIncludePlugin({ prefix: '//=', indent: true }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) }) ================================================ FILE: test/recursion.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## recursion include', () => { var result = fs.readFileSync('test/fixtures-recursion/result.txt', 'utf8') describe('# basepath: @file', () => { it('file', done => { var file = new Vinyl({ path: 'test/fixtures-recursion/index.txt', contents: fs.readFileSync('test/fixtures-recursion/index.txt') }) var stream = fileIncludePlugin({ basepath: '@file' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('stream', done => { var file = new Vinyl({ path: 'test/fixtures-recursion/index.txt', contents: fs.createReadStream('test/fixtures-recursion/index.txt') }) var stream = fileIncludePlugin({ basepath: '@file' }) stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) }) }) ================================================ FILE: test/variable.js ================================================ 'use strict' const replaceVariable = require('../lib/replace-variable') const fs = require('fs') require('should') describe('## variable', () => { var result = fs.readFileSync('test/fixtures-variable/result.html', 'utf-8') it('# basic', done => { var index = fs.readFileSync('test/fixtures-variable/index.html', 'utf-8') replaceVariable(index, { param1: 'value1', obj: { param1: 'o1', param2: 'o2' }, param2: 'value2' }, { prefix: '//=' }).should.equal(result) done() }) it('# with suffix', done => { var index = fs.readFileSync('test/fixtures-variable/index-suffix.html', 'utf-8') replaceVariable(index, { param1: 'value1', obj: { param1: 'o1', param2: 'o2' }, param2: 'value2' }, { prefix: '//=', suffix: '@@' }).should.equal(result) done() }) }) ================================================ FILE: test/webroot-variable.js ================================================ 'use strict' const fileIncludePlugin = require('..') const Vinyl = require('vinyl') const should = require('should') const fs = require('fs') describe('## built-in webRoot variable', () => { it('# regular usage and includes', done => { var result = fs.readFileSync('test/fixtures-webroot-variable/result.html', 'utf8') var path = 'test/fixtures-webroot-variable/index.html' var file = new Vinyl({ path: path, contents: fs.createReadStream(path) }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) it('# nested folder', done => { var result = fs.readFileSync('test/fixtures-webroot-variable/sub/result.html', 'utf8') var path = 'test/fixtures-webroot-variable/sub/index.html' var file = new Vinyl({ path: path, contents: fs.createReadStream(path) }) var stream = fileIncludePlugin() stream.on('data', newFile => { should.exist(newFile) should.exist(newFile.contents) String(newFile.contents).should.equal(result) done() }) stream.write(file) stream.end() }) })