-
Notifications
You must be signed in to change notification settings - Fork 288
/
Gruntfile.js
345 lines (321 loc) · 11 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// LICENCE https://github.com/adaptlearning/adapt_authoring/blob/master/LICENSE
module.exports = function(grunt) {
require('matchdep').filterAll('grunt-*').forEach(grunt.loadNpmTasks);
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
'generate-lang-json': {
options: {
langFileExt: '.json',
src: {
backend: 'routes/lang',
frontend: 'frontend/src/**/lang'
},
dest: 'temp/lang'
}
},
copy: {
main: {
files: [
{
expand: true,
flatten: true,
src: [
'frontend/src/core/**/assets/**',
'frontend/src/modules/**/assets/**',
'frontend/src/plugins/**/assets/**',
'frontend/src/libraries/**/assets/**'
],
dest: 'frontend/build/css/assets/',
filter: 'isFile'
},
{
expand: true,
flatten: true,
src: ['frontend/src/libraries/ace/*'],
dest: 'frontend/build/js/ace'
}
]
}
},
less: {
dev: {
options: {
baseUrl: 'frontend/src',
src: [
'frontend/src/core/**/*.less',
'frontend/src/modules/**/*.less',
'frontend/src/plugins/**/*.less',
'frontend/src/libraries/**/*.less'
],
paths: 'frontend/src/core/less',
generateSourceMaps: true,
compress: false,
dest: 'frontend/build/css',
cssFilename: 'adapt.css',
mapFilename: 'adapt.css.map'
}
},
compile: {
options: {
baseUrl: 'frontend/src',
src: [
'frontend/src/core/**/*.less',
'frontend/src/less/**/*.less',
'frontend/src/modules/**/*.less',
'frontend/src/plugins/**/*.less',
'frontend/src/libraries/**/*.less'
],
paths: 'frontend/src/core/less',
generateSourceMaps: false,
compress: true,
dest: 'frontend/build/css',
cssFilename: 'adapt.css',
mapFilename: 'adapt.css.map'
}
}
},
handlebars: {
compile: {
options: {
amd: true,
namespace:"Handlebars.templates",
processName: function(filePath) {
var newFilePath = filePath.split("/");
newFilePath = newFilePath[newFilePath.length - 1].replace(/\.[^/.]+$/, "");
return newFilePath;
},
partialRegex: /^part_/,
partialsPathRegex: /\/partials\//
},
files: [
{
follow: true,
src: [
'frontend/src/core/**/*.hbs',
'frontend/src/modules/**/*.hbs',
'frontend/src/plugins/**/*.hbs'
],
dest: 'frontend/src/templates/templates.js'
}
]
}
},
requirejs: {
dev: {
options: {
baseUrl: 'frontend/src',
name: 'core/app',
mainConfigFile: "frontend/src/core/config.js",
out: "frontend/build/js/origin.js",
preserveLicenseComments: true,
optimize: "none"
}
},
compile: {
options: {
baseUrl: 'frontend/src',
name: 'core/app',
mainConfigFile: "frontend/src/core/config.js",
out: "frontend/build/js/origin.js",
optimize: "none"
}
}
},
babel: {
dev: {
options: {
compact: false,
retainLines: true,
presets: [ [ '@babel/preset-env', { targets: { ie: '11' } } ] ],
sourceType: 'script'
},
src: 'frontend/build/js/origin.js',
dest: 'frontend/build/js/origin.js'
},
compile: {
options: {
comments: false,
minified: true,
presets: [ [ '@babel/preset-env', { targets: { ie: '11' } } ] ],
sourceType: 'script'
},
src: 'frontend/build/js/origin.js',
dest: 'frontend/build/js/origin.js'
}
},
mochaTest: {
src: ['test/*.js'],
options: {
reporter: 'spec',
timeout: 3500
}
},
requireBundle: {
modules: {
src: 'frontend/src/modules/*',
dest: 'frontend/src/modules/modules.js'
},
plugins: {
src: 'frontend/src/plugins/*',
dest: 'frontend/src/plugins/plugins.js'
}
}
});
grunt.registerTask('migration-conf', 'Creating migration Conf', function() {
var mongoUri = require('mongodb-uri');
var config = grunt.file.readJSON('conf/config.json');
var connectionString = '';
if (config.dbConnectionUri) {
connectionString = config.dbConnectionUri;
var dbConnectionUriParsed = mongoUri.parse(connectionString);
dbConnectionUriParsed.database = config.dbName;
connectionString = mongoUri.format(dbConnectionUriParsed);
} else {
// Construct the authentication part of the connection string.
var authenticationString = config.dbUser && config.dbPass ? config.dbUser + ':' + config.dbPass + '@' : '';
// Check if a MongoDB replicaset array has been specified.
if (config.dbReplicaset && Array.isArray(config.dbReplicaset) && config.dbReplicaset.length !== 0) {
// The replicaset should contain an array of hosts and ports
connectionString = 'mongodb://' + authenticationString + config.dbReplicaset.join(',') + '/' + config.dbName
} else {
// Get the host and port number from the configuration.
var portString = config.dbPort ? ':' + config.dbPort : '';
connectionString = 'mongodb://' + authenticationString + config.dbHost + portString + '/' + config.dbName;
}
if (typeof config.dbAuthSource === 'string' && config.dbAuthSource !== '' ) {
connectionString += '?authSource=' + config.dbAuthSource
}
}
var migrateConf = {
migrationsDir : 'migrations/lib',
es6 : false,
dbConnectionUri: connectionString
};
grunt.file.write('conf/migrate.json', JSON.stringify(migrateConf, null, 2));
});
// Compiles frontend plugins
grunt.registerMultiTask('requireBundle', 'Generates a .js file with a bunch of imports for the path files', function() {
var modulePaths = '';
// Go through each subfolder in the plugins directory
var foldersArray = grunt.file.expand({ filter: "isDirectory" }, this.data.src);
// Check if any plugins are available
if (foldersArray.length === 0) {
modulePaths += "'";
}
foldersArray.forEach(function(path, index, folders) {
// Strip off front of path to make relative path to config file
var relativePath = path.replace(grunt.config.get('requirejs').dev.options.baseUrl, '').slice(1);
var splitter = "','";
if (index === folders.length - 1) splitter = "'";
modulePaths += relativePath + '/index' + splitter;
});
grunt.file.write(this.data.dest, "define(['" + modulePaths +"], function() {});");
});
grunt.registerMultiTask('less', 'Compile Less files to CSS', function() {
var path = require('path');
var less = require('less');
var options = this.options({});
var shouldGenerateSourceMaps = options.generateSourceMaps;
var destination = options.dest;
var mapFilename = options.mapFilename;
var imports = getImports();
var lessOptions = getLessOptions();
var sourceMapPath = path.join(destination, mapFilename);
var importsPath = sourceMapPath + '.imports';
var done = this.async();
if (!shouldGenerateSourceMaps) removeSourceMaps();
less.render(imports, lessOptions, complete);
function getImports() {
var src = options.src;
var ret = '';
for (var i = 0, l = src.length; i < l; i++) {
grunt.file.expand({
filter: options.filter,
follow: true
}, src[i]).forEach(function(lessPath) {
ret += '@import \'' + path.normalize(lessPath) + '\';\n';
});
}
return ret;
}
function getLessOptions() {
var ret = {
compress: options.compress,
paths: options.paths
};
if (shouldGenerateSourceMaps) {
ret.sourceMap = {
'sourceMapFileInline': false,
'outputSourceFiles': true,
'sourceMapBasepath': 'src',
'sourceMapURL': mapFilename
};
}
return ret;
}
function removeSourceMaps() {
if (grunt.file.exists(sourceMapPath)) {
grunt.file.delete(sourceMapPath, { force: true });
}
if (grunt.file.exists(importsPath)) {
grunt.file.delete(importsPath, { force: true });
}
}
function complete(error, output) {
if (error) return grunt.fail.fatal(JSON.stringify(error, false, ' '));
var outputMap = output.map;
if (outputMap) {
grunt.file.write(sourceMapPath, outputMap);
grunt.file.write(importsPath, imports);
}
grunt.file.write(path.join(destination, options.cssFilename), output.css);
done();
}
});
grunt.registerTask('generate-lang-json', function() {
const fs = require('fs-extra');
const path = require('path');
const options = this.options();
const backendGlob = path.join(options.src.backend, `*${options.langFileExt}`);
const dest = options.dest;
// load each route lang file
/**
* NOTE there must be a file in routes/lang for the language to be loaded,
* won't work if you've only got lang files in frontend
*/
grunt.file.expand({}, path.join(backendGlob)).forEach(backendPath => {
const basename = path.basename(backendPath);
const frontendGlob = path.join(options.src.frontend, basename);
let data = { ...fs.readJSONSync(backendPath) };
// load all matching frontend lang files
grunt.file.expand({}, frontendGlob).forEach(frontendPath => {
data = { ...data, ...fs.readJSONSync(frontendPath) };
});
fs.ensureDirSync(dest);
fs.writeJSONSync(path.join(dest, basename), data, { spaces: 2 });
});
});
grunt.registerTask('default', ['build:dev']);
grunt.registerTask('test', ['mochaTest']);
/**
* Accepts 'build' and 'prod' params
* e.g. grunt build:prod
*/
grunt.registerTask('build', 'Running build', function(mode) {
grunt.log.subhead(`Building application in ${mode === 'prod' ? 'production' : 'dev'} mode`);
var isProduction = mode === 'prod' ? true : false;
var compilation = isProduction ? 'compile' : 'dev';
try {
// add flag to config
var configFile = 'conf/config.json';
var config = grunt.file.readJSON(configFile);
config.isProduction = isProduction;
grunt.file.write(configFile, JSON.stringify(config, null, 2));
// run the task
grunt.task.run(['migration-conf', 'requireBundle', 'generate-lang-json', 'copy', 'less:' + compilation, 'handlebars', 'requirejs:'+ compilation, `babel:${compilation}`]);
} catch(e) {
grunt.task.run(['requireBundle', 'copy', 'less:' + compilation, 'handlebars', 'requirejs:' + compilation, `babel:${compilation}`]);
}
});
};