This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
forked from AppVentus/av-gulp-injector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
160 lines (131 loc) · 4.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var finder = require('fs-finder');
var fs = require('fs');
var ginjector = {};
/**
* Transform path aliases to theirs real path
*/
ginjector.compileAliases = function(str, aliases) {
for (var alias in aliases) {
str = str.replace(new RegExp(alias, 'g'), aliases[alias]);
}
return str;
};
/**
* Reinplement path aliases for real path
*/
ginjector.decompileAliases = function(str, aliases) {
for (var alias in aliases) {
str = str.replace(new RegExp(aliases[alias], 'g'), alias);
}
return str;
};
/**
* Merge a single injector to the injectorsAssemble
*/
ginjector.mergeInjectors = function (injectorsCollection, injector) {
for (var tag in injector) {
if (injector.hasOwnProperty(tag)) {
var paths = injector[tag];
/**
* Foreach values {tag: paths}
*/
var previousTagValues = [];
if (typeof(injectorsCollection[tag]) !== 'undefined') {
previousTagValues = injectorsCollection[tag];
}
injectorsCollection[tag] = previousTagValues.concat(paths);
}
}
return injectorsCollection;
};
ginjector.isJson = function (str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
};
/**
* Collect all injector.json and group them into the injectorsCollection
*/
ginjector.injectorsAssemble = function (aliases) {
// All tags and all paths to inject
var injectorsCollection = {};
// get all injector.json locations
var injectors = finder.from('./').findFiles('injector.json');
for (var index in injectors) {
var injector = injectors[index];
var data = fs.readFileSync(injector, 'utf8');
if (typeof(aliases) !== 'undefined') {
data = ginjector.compileAliases(data, aliases);
}
if (this.isJson(data)) {
injectorsCollection = ginjector.mergeInjectors(injectorsCollection, JSON.parse(data));
}
}
return injectorsCollection;
};
/**
* Generates a pipe with the injection for one tag
*/
ginjector.tagInjector = function(source, tag, path, relativeInjection, aliases) {
var sources = gulp.src(path, {read: false});
var injection = $.inject(
sources, {
name: tag,
ignorePath: 'web/',
relative: relativeInjection,
/**
* Transform inject with asset() function
*/
transform: function(filepath) {
if (filepath.slice(-3) === '.js') {
return '<script src="{{ asset(\'' + filepath + '\') }}"></script>';
}
if (filepath.slice(-4) === '.css') {
return '<link rel="stylesheet" href="{{ asset(\'' + filepath + '\') }}">';
}
if (filepath.slice(-5) === '.scss') {
return '@import \'' + filepath + '\';';
}
if (filepath.slice(-5) === '.twig') {
// aliases are needed with symfony twig path includes
filepath = ginjector.decompileAliases(filepath, aliases);
// substring is made to remove the "/" at the start of the
// path
filepath = filepath.substring(1);
return '{% include "' + filepath + '" %}';
}
}
}
);
source.pipe(injection);
};
/**
* Loop over all the tags to inject everything
*/
ginjector.injector = function(src, aliases, relativeInjection) {
var injectorsCollection = this.injectorsAssemble(aliases);
if (typeof(relativeInjection) === 'undefined') {
relativeInjection = false;
}
/**
* Injection for each tags
*/
for (var tag in injectorsCollection){
if (injectorsCollection.hasOwnProperty(tag)) {
this.tagInjector(src, tag, injectorsCollection[tag], relativeInjection, aliases);
}
}
/**
* Same dest as gulp.src
*/
src.pipe(gulp.dest(function(file) {
return file.base;
}));
};
module.exports = ginjector;