This repository has been archived by the owner on Jun 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Gruntfile.js
204 lines (194 loc) · 9.42 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*jshint node:true*/
"use strict";
module.exports = function( grunt ) {
// show elapsed time at the end
require( 'time-grunt' )( grunt );
// load all grunt tasks
require( 'load-grunt-tasks' )( grunt );
grunt.initConfig( {
pkg: grunt.file.readJSON( "package.json" ),
jsbeautifier: {
test: {
src: [ "*.js", "src/js/**/*.js" ],
options: {
config: "./.jsbeautifyrc",
mode: "VERIFY_ONLY"
}
},
fix: {
src: [ "*.js", "src/js/**/*.js" ],
options: {
config: "./.jsbeautifyrc"
}
}
},
jshint: {
options: {
jshintrc: ".jshintrc"
},
all: [ "*.js", "src/js/**/*.js", "!src/js/extern.js" ]
},
prepWidgetSass: {
writePath: "public/src/sass/component/_widgets.scss",
widgetConfigPath: "public/src/js/config.json"
},
watch: {
sass: {
files: [ 'public/src/js/config.json', 'public/src/sass/**/*.scss', 'public/lib/enketo-core/src/**/*.scss', 'src-private/sass/**/*.scss' ],
tasks: [ 'style' ],
options: {
spawn: false
}
},
js: {
files: [ '*.js', 'src/js/*/**.js', 'public/enketo-core/**/*.js' ],
tasks: [ 'compile' ],
options: {
spawn: false
}
}
},
sass: {
pub: {
files: {
'public/build/css/webform_formhub.css': 'public/src/sass/webform_formhub.scss',
'public/build/css/webform_print_formhub.css': 'public/src/sass/webform_print_formhub.scss',
'public/build/css/empty-front.css': 'public/src/sass/empty-front.scss',
'public/build/css/forms.css': 'public/src/sass/forms.scss',
'public/build/css/formtester.css': 'public/src/sass/formtester.scss',
'public/build/css/webform_grid.css': 'public/src/sass/webform_grid.scss',
'public/build/css/webform_print_grid.css': 'public/src/sass/webform_print_grid.scss'
}
},
pri: {
files: {
'public/build/css/openrosa.css': 'src-private/sass/private/openrosa.scss',
'public/build/css/front.css': 'src-private/sass/private/front.scss'
}
}
},
jasmine: {
test: {
src: [ "public/src/js/module/connection.js", "public/src/js/module/store.js" ],
options: {
keepRunner: true,
specs: "test/spec/*.spec.js",
helpers: [],
template: require( "grunt-template-jasmine-requirejs" ),
templateOptions: {
//requireConfigFile: "src/js/require-config.js",
requireConfig: {
baseUrl: "public/src/js/module",
paths: {
"gui": "../../../../test/mock/gui.mock",
"lib": "../../../lib",
"enketo-js": "../../../lib/enketo-core/src/js",
"enketo-js/Form": "../../../../test/mock/Form.mock",
"enketo-js/FormModel": "../../../../test/mock/FormModel.mock",
"enketo-widget": "../../../../test/mock/empty.mock",
"enketo-config": "../config.json", //should move elsewhere
"text": "../../../lib/enketo-core/lib/text/text",
"xpath": "../../../../test/mock/empty.mock",
"file-manager": "../../../../test/mock/empty.mock",
"jquery.xpath": "../../../../test/mock/empty.mock",
"support": "../../../../test/mock/empty.mock",
"bootstrap": "../../../lib/enketo-core/lib/bootstrap",
"jquery": "../../../lib/bower-components/jquery/dist/jquery",
"file-saver": "../../../../test/mock/file-saver.mock",
"Blob": "../../../../test/mock/Blob.mock",
"vkbeautify": "../../../../test/mock/vkbeautify.mock"
},
}
}
}
}
},
// this compiles all javascript to a single minified file
requirejs: {
options: {
//generateSourceMaps: true,
preserveLicenseComments: false,
baseUrl: "public/src/js/module",
findNestedDependencies: true,
include: [ '../../../lib/bower-components/requirejs/require' ],
mainConfigFile: "public/src/js/require-config.js",
optimize: "uglify2",
done: function( done, output ) {
var duplicates = require( 'rjs-build-analysis' ).duplicates( output );
if ( duplicates.length > 0 ) {
grunt.log.subhead( 'Duplicates found in requirejs build:' );
grunt.log.warn( duplicates );
done( new Error( 'r.js built duplicate modules, please check the excludes option.' ) );
} else {
grunt.log.writeln( 'Checked for duplicates. All seems OK!' );
}
done();
}
},
"webform": getWebformCompileOptions(),
"webform-edit": getWebformCompileOptions( 'edit' ),
"webform-preview": getWebformCompileOptions( 'preview', false ),
"webform-single": getWebformCompileOptions( 'single' ),
"webform-tester": getWebformCompileOptions( 'tester' ),
"front": {
options: {
name: "../main-front",
out: "public/build/js/front-combined.min.js"
}
},
"formlist": {
options: {
name: "../main-formlist",
out: "public/build/js/formlist-combined.min.js"
}
}
}
} );
function getWebformCompileOptions( type, offline ) {
//add widgets js and widget config.json files
var widgets = grunt.file.readJSON( 'public/src/js/config.json' ).widgets;
offline = ( typeof offline === 'undefined' ) ? true : offline;
widgets.forEach( function( widget, index, arr ) {
arr.push( 'text!' + widget.substr( 0, widget.lastIndexOf( '/' ) + 1 ) + 'config.json' );
} );
type = ( type ) ? '-' + type : '';
return {
options: {
name: "../main-webform" + type,
mainConfigFile: ( offline ) ? "public/src/js/require-config.js" : [ "public/src/js/require-config.js", "public/src/js/require-online-config.js" ],
out: "public/build/js/webform" + type + "-combined.min.js",
include: [ '../../../lib/bower-components/requirejs/require' ].concat( widgets )
}
};
}
//maybe this can be turned into a npm module?
grunt.registerTask( "prepWidgetSass", "Preparing _widgets.scss dynamically", function() {
var widgetConfig, widgetFolderPath, widgetSassPath, widgetConfigPath,
config = grunt.config( "prepWidgetSass" ),
widgets = grunt.file.readJSON( config.widgetConfigPath ).widgets,
content = "// Dynamically created list of widget stylesheets to import\r\n" +
"// based on the content of config.json\r\n\r\n";
widgets.forEach( function( widget ) {
if ( widget.indexOf( "enketo-widget/" ) === 0 ) {
//strip require.js module name
widgetFolderPath = widget.substr( 0, widget.lastIndexOf( "/" ) + 1 );
//replace widget require.js path shortcut with proper path relative to src/js
widgetSassPath = widgetFolderPath.replace( /^enketo-widget\//, "public/lib/enketo-core/src/widget/" );
//create path to widget config file
widgetConfigPath = widgetFolderPath.replace( /^enketo-widget\//, "public/lib/enketo-core/src/widget/" ) + "config.json";
grunt.log.writeln( "widget config path: " + widgetConfigPath );
//create path to widget stylesheet file
widgetSassPath += grunt.file.readJSON( widgetConfigPath ).stylesheet;
} else {
grunt.log.error( [ "Expected widget path " + widget + " in config.json to be preceded by \"widget/\"." ] );
}
//replace this by a function that parses config.json in each widget folder to get the "stylesheet" variable
content += "@import \"" + widgetSassPath + "\";\r\n";
} );
grunt.file.write( config.writePath, content );
} );
grunt.registerTask( "test", [ "jsbeautifier:test", "jshint", "jasmine" ] );
grunt.registerTask( "style", [ "prepWidgetSass", "sass" ] );
grunt.registerTask( "compile", [ "requirejs" ] );
grunt.registerTask( "default", [ "style", "compile" ] );
};