forked from enketo/enketo-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
185 lines (179 loc) · 6.17 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
/**
* When using enketo-core in your own app, you'd want to replace
* this build file with one of your own in your project root.
*/
const nodeSass = require( 'node-sass' );
module.exports = grunt => {
// show elapsed time at the end
require( 'time-grunt' )( grunt );
// load all grunt tasks
require( 'load-grunt-tasks' )( grunt );
// Project configuration.
grunt.initConfig( {
pkg: grunt.file.readJSON( 'package.json' ),
concurrent: {
develop: {
tasks: [ 'shell:transformer', 'connect:server:keepalive', 'watch' ],
options: {
logConcurrentOutput: true
}
},
test: {
tasks: [ 'karma:watch', 'watch:transforms' ],
options: {
logConcurrentOutput: true,
},
},
},
connect: {
server: {
options: {
port: 8005,
base: [ 'test/forms', 'test/temp', 'build' ]
}
},
test: {
options: {
port: 8000
}
}
},
eslint: {
check: {
src: [ '*.js', 'src/**/*.js' ]
},
fix: {
options: {
fix: true,
},
src: [ '*.js', 'src/**/*.js' ]
}
},
watch: {
sass: {
files: [ 'grid/sass/**/*.scss', 'src/sass/**/*.scss', 'src/widget/**/*.scss' ],
tasks: [ 'css' ],
options: {
spawn: true,
livereload: true,
}
},
js: {
files: [ 'config.json', '*.js', 'src/**/*.js' ],
tasks: [ 'shell:build' ],
options: {
spawn: false,
livereload: true
}
},
transforms: {
files: 'test/forms/*.xml',
tasks: [ 'transforms' ],
options: {
spawn: true,
livereload: false,
},
},
},
karma: {
options: {
singleRun: true,
configFile: 'test/karma.conf.js',
customLaunchers: {
ChromeHeadlessNoSandbox: {
base: 'ChromeHeadless',
flags: [ '--no-sandbox' ]
},
ChromeHeadlessDebug: {
base: 'ChromeHeadless',
flags: [ '--no-sandbox', '--remote-debugging-port=9333' ],
},
},
},
headless: {
browsers: [ 'ChromeHeadlessNoSandbox' ]
},
browsers: {
browsers: [ 'Chrome', 'Firefox', 'Safari' ]
},
watch: {
browsers: [ 'ChromeHeadlessDebug' ],
options: {
autoWatch: true,
client: {
mocha: {
timeout: Number.MAX_SAFE_INTEGER,
},
},
reporters: [ 'dots' ],
singleRun: false,
}
},
},
sass: {
options: {
implementation: nodeSass,
sourceMap: false,
importer( url, prev, done ) {
// Fixes enketo-core submodule references.
// Those references are correct in apps that use enketo-core as a submodule.
url = ( /\.\.\/\.\.\/node_modules\//.test( url ) ) ? url.replace( '../../node_modules/', 'node_modules/' ) : url;
done( {
file: url
} );
}
},
compile: {
cwd: 'src/sass',
dest: 'build/css',
expand: true,
outputStyle: 'expanded',
src: '**/*.scss',
ext: '.css',
flatten: true,
extDot: 'last'
}
},
shell: {
transformer: {
command: 'node node_modules/enketo-transformer/app.js'
},
build: {
command: 'node ./scripts/build.js'
}
}
} );
grunt.loadNpmTasks( 'grunt-sass' );
grunt.registerTask( 'transforms', 'Creating forms.js', function() {
const forms = {};
const done = this.async();
const jsonStringify = require( 'json-pretty' );
const formsJsPath = 'test/mock/forms.js';
const xformsPaths = grunt.file.expand( {}, 'test/forms/*.xml' );
const transformer = require( 'enketo-transformer' );
grunt.log.write( 'Transforming XForms ' );
xformsPaths
.reduce( ( prevPromise, filePath ) => prevPromise.then( () => {
const xformStr = grunt.file.read( filePath );
grunt.log.write( '.' );
return transformer.transform( { xform: xformStr } )
.then( result => {
forms[filePath.substring( filePath.lastIndexOf( '/' ) + 1 )] = {
html_form: result.form,
xml_model: result.model
};
} );
} ), Promise.resolve() )
.then( () => {
grunt.file.write( formsJsPath, `export default ${jsonStringify( forms )};` );
done();
} );
} );
grunt.registerTask( 'compile', [ 'shell:build' ] );
grunt.registerTask( 'test', [ 'eslint:check', 'compile', 'transforms', 'karma:headless', 'css' ] );
grunt.registerTask( 'test:watch', [ 'transforms', 'concurrent:test' ] );
grunt.registerTask( 'css', [ 'sass' ] );
grunt.registerTask( 'server', [ 'connect:server:keepalive' ] );
grunt.registerTask( 'develop', [ 'css', 'compile', 'concurrent:develop' ] );
grunt.registerTask( 'default', [ 'css', 'compile' ] );
};