-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntfile.js
121 lines (101 loc) · 3.62 KB
/
Gruntfile.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
var fs = require("fs");
var mkdirp = require('mkdirp');
module.exports = function(grunt) {
var env;
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
template: {
files: ['src/**/*'],
tasks: ['build'],
options: {
interrupt: true,
spawn: true,
debounceDelay: 250
}
}
},
replace: {
dist: {
options: {
patterns: [
{
match: /\[\%ClickThroughUrl\%\]/g,
replacement: 'http://www.google.de'
},
{
match: /%%CLICK_URL_ESC%%/g,
replacement: ''
},
]
},
files: [
{expand: true, src: ['**/*'], dest: 'build/', cwd:'src'}
]
}
},
folder_list: {
options: {
// Default options, you dont need these they are just to highlight the options available.
files: true,
folders: false
},
files: {
src : ['src/**'],
dest: 'temp/folderlist.json'
}
},
clean: {
temp: {
src: ['temp','build']
}
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-replace');
grunt.loadNpmTasks('grunt-folder-list');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.registerTask('replacePH', 'Set a config property.', function() {
var folderJSON = grunt.file.readJSON('temp/folderlist.json');
for (var i = 0; i < folderJSON.length; i++) {
if(folderJSON[i].filetype === "html" || folderJSON[i].filetype === "js"){
var dir = folderJSON[i].location;
//read replace config
var config;
try{
config = undefined;
config = grunt.file.readJSON(dir.replace(folderJSON[i].filename, 'replace.json'));
}
catch(e){
//console.info("no replace config found for",folderJSON[i].filename);
}
if(config){
var done = this.async();
var file = grunt.file.read(folderJSON[i].location);
for(var j in config){
var tj = j.replace('[','\\[').replace(']','\\]').replace('&','\\&').replace('?','\\?');
var reg = new RegExp(tj,"g");
file = file.replace(reg, config[j]);
}
(function(done,dir,json,file){
console.log(dir.replace(json.filename,'').replace('src','build'))
mkdirp(dir.replace(json.filename,'').replace('src','build'), function (err) {
fs.writeFile(dir.replace('src','build'), file, function(err) {
if(err) {
return console.log(err);
}
done();
});
});
})(done,dir,folderJSON[i],file)
}
}
}
});
grunt.registerTask('build',[
'clean:temp',
'folder_list',
'replacePH'
]);
};