This repository has been archived by the owner on Apr 8, 2020. It is now read-only.
forked from enketo/enketo-express
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Gruntfile.js
269 lines (263 loc) · 10 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
module.exports = grunt => {
const JS_INCLUDE = [
'**/*.js',
'!**/offline-app-worker-partial.js',
'!**/node_modules/**',
'!test/**/*.spec.js',
'!public/js/build/*',
'!test/client/config/karma.conf.js',
'!docs/**',
'!test-coverage/**',
'!**/redirect-IE.js'
];
const path = require( 'path' );
const nodeSass = require( 'node-sass' );
const bundles = require( './buildFiles' ).bundles;
require( 'time-grunt' )( grunt );
require( 'load-grunt-tasks' )( grunt );
grunt.config.init( {
concurrent: {
develop: {
tasks: [ 'nodemon', 'watch' ],
options: {
logConcurrentOutput: true
}
}
},
nodemon: {
dev: {
script: 'app.js',
options: {
watch: [ 'app', 'config' ],
//nodeArgs: [ '--debug' ],
env: {
NODE_ENV: 'development',
DEBUG: '*, -express:*, -send, -compression, -body-parser:*, -puppeteer:*'
}
}
}
},
sass: {
options: {
implementation: nodeSass
},
compile: {
cwd: 'app/views/styles',
dest: 'public/css',
expand: true,
outputStyle: 'compressed',
src: '**/*.scss',
ext: '.css',
flatten: true,
extDot: 'last'
}
},
watch: {
config: {
files: [ 'config/*.json' ],
tasks: [ 'client-config-file:create' ]
},
sass: {
files: [ 'app/views/styles/**/*.scss', 'widget/**/*.scss', '!app/views/styles/component/_system_variables.scss' ],
tasks: [ 'sass' ],
options: {
spawn: false,
livereload: true
}
},
jade: {
files: [ 'app/views/**/*.pug' ],
options: {
spawn: false,
livereload: true
}
},
language: {
files: [ 'app/views/**/*.pug', 'app/controllers/**/*.js', 'app/models/**/*.js', 'public/js/src/**/*.js' ],
tasks: [ 'shell:translation', 'i18next' ]
},
js: {
files: [ 'public/js/src/**/*.js', 'widget/**/*.js' ],
tasks: [ 'js-dev' ],
options: {
spawn: false,
livereload: true
}
}
},
shell: {
buildReadmeBadge: {
command: 'node ./tools/update-readme-with-shield-badge.js'
},
'clean-css': {
command: 'rm -f public/css/*'
},
'clean-locales': {
command: 'find locales -name "translation-combined.json" -delete && rm -fr locales/??'
},
'clean-js': {
command: 'rm -f public/js/build/* && rm -f public/js/*.js && rm -f public/temp-client-config.json'
},
translation: {
command: 'echo "No automatic translation key generation at the moment."'
// Does not work correctly yet for TError() calls and probably not for pug files either.
// npx i18next -c ./i18next-parser.config.js
},
rollup: {
command: 'npx rollup --config'
}
},
jsbeautifier: {
test: {
src: JS_INCLUDE,
options: {
config: './.jsbeautifyrc',
mode: 'VERIFY_ONLY'
}
},
fix: {
src: JS_INCLUDE,
options: {
config: './.jsbeautifyrc'
}
}
},
eslint: {
all: JS_INCLUDE,
},
// test server JS
mochaTest: {
all: {
options: {
reporter: 'dot'
},
src: [ 'test/server/**/*.spec.js' ]
},
account: {
src: [ 'test/server/account-*.spec.js' ]
}
},
// test client JS
karma: {
options: {
singleRun: true,
configFile: 'test/client/config/karma.conf.js'
},
headless: {
browsers: [ 'ChromeHeadless' ]
},
browsers: {
browsers: [ 'Chrome', 'ChromeCanary', 'Firefox', 'Opera' /*,'Safari'*/ ],
}
},
nyc: {
cover: {
options: {
reporter: [
'html',
'text-summary',
'json'
],
reportDir: './test-coverage/server',
include: [ 'app/**/*.js' ],
},
cmd: false,
args: [ 'grunt', 'mochaTest:all' ]
}
},
terser: {
options: {
// https://github.com/enketo/enketo-express/issues/72
keep_classnames: true,
},
all: {
files: bundles
.map( bundle => [ bundle.replace( '.js', '.min.js' ), [ bundle ] ] )
.reduce( ( o, [ key, value ] ) => {
o[ key ] = value;
return o;
}, {} )
},
},
env: {
develop: {
NODE_ENV: 'develop'
},
test: {
NODE_ENV: 'test'
},
production: {
NODE_ENV: 'production'
}
},
i18next: {
locales: {
cwd: 'locales/src/',
expand: true,
src: [ '*/' ],
include: [ '**/translation.json', '**/translation-additions.json' ],
rename( dest, src ) {
return `${dest + src}translation-combined.json`;
},
dest: 'locales/build/'
}
}
} );
grunt.registerTask( 'client-config-file', 'Temporary client-config file', task => {
const CLIENT_CONFIG_PATH = 'public/js/build/client-config.js';
if ( task === 'create' ) {
// https://github.com/enketo/enketo-express/issues/102
// The require cache may contain stale configuration from another task. Purge it.
delete require.cache[ require.resolve( './app/models/config-model' ) ];
const config = require( './app/models/config-model' );
grunt.file.write( CLIENT_CONFIG_PATH, `export default ${JSON.stringify( config.client )};` );
grunt.log.writeln( `File ${CLIENT_CONFIG_PATH} created` );
} else if ( task === 'remove' ) {
grunt.file.delete( CLIENT_CONFIG_PATH );
grunt.log.writeln( `File ${CLIENT_CONFIG_PATH} removed` );
}
} );
grunt.registerTask( 'system-sass-variables', 'Creating _system_variables.scss', () => {
const SYSTEM_SASS_VARIABLES_PATH = 'app/views/styles/component/_system_variables.scss';
const config = require( './app/models/config-model' );
grunt.file.write( SYSTEM_SASS_VARIABLES_PATH, `$base-path: "${config.server[ 'base path' ]}";` );
grunt.log.writeln( `File ${SYSTEM_SASS_VARIABLES_PATH} created` );
} );
grunt.registerTask( 'widgets', 'generate widget reference files', () => {
const WIDGETS_JS_LOC = 'public/js/build/';
const WIDGETS_JS = `${WIDGETS_JS_LOC}widgets.js`;
const WIDGETS_SASS_LOC = 'app/views/styles/component/';
const WIDGETS_SASS = `${WIDGETS_SASS_LOC}_widgets.scss`;
const PRE = '// This file is automatically generated with `grunt widgets`\n\n';
const widgets = require( './app/models/config-model' ).server.widgets;
const coreWidgets = require( './public/js/src/module/core-widgets' );
const paths = Object.keys( widgets ).map( key => coreWidgets[ widgets[ key ] ] || widgets[ key ] );
let num = 0;
let content = PRE + paths.map( p => {
if ( grunt.file.exists( WIDGETS_JS_LOC, `${p}.js` ) ) {
num++;
return `import w${num} from '${p}';`;
} else {
return `//${p} not found`;
}
} ).join( '\n' ) + `\n\nexport default [${[...Array(num).keys()].map(n => 'w'+(n+1)).join(', ')}];`;
grunt.file.write( WIDGETS_JS, content );
grunt.log.writeln( `File ${WIDGETS_JS} created` );
content = `${PRE +
paths.map( p => {
p = path.join( '../', p );
return grunt.file.exists( WIDGETS_SASS_LOC, `${p}.scss` ) ? `@import "${p}"` : `//${p} not found`;
} ).join( ';\n' )};`;
grunt.file.write( WIDGETS_SASS, content );
grunt.log.writeln( `File ${WIDGETS_SASS} created` );
} );
grunt.registerTask( 'default', [ 'locales', 'widgets', 'css', 'js', 'terser' ] );
grunt.registerTask( 'locales', [ 'shell:clean-locales', 'i18next' ] );
grunt.registerTask( 'js', [ 'shell:clean-js', 'client-config-file:create', 'widgets', 'shell:rollup' ] );
grunt.registerTask( 'js-dev', [ 'js' ] );
grunt.registerTask( 'css', [ 'shell:clean-css', 'system-sass-variables:create', 'sass' ] );
grunt.registerTask( 'test', [ 'env:test', 'js', 'css', 'nyc:cover', 'karma:headless', 'shell:buildReadmeBadge', 'jsbeautifier:test', 'eslint' ] );
grunt.registerTask( 'test-browser', [ 'env:test', 'css', 'client-config-file:create', 'karma:browsers' ] );
grunt.registerTask( 'develop', [ 'env:develop', 'i18next', 'js-dev', 'css', 'concurrent:develop' ] );
grunt.registerTask( 'test-and-build', [ 'env:test', 'mochaTest:all', 'karma:headless', 'env:production', 'default' ] );
};