forked from moment/moment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembed_languages.js
49 lines (36 loc) · 1.52 KB
/
embed_languages.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
module.exports = function (grunt) {
grunt.registerTask('embed_languages', function () {
var config = grunt.config('embed_languages');
var files = grunt.file.expand(config.targetLangs);
var embeddedContents = determineEmbeddedContent(files);
var momentContents = grunt.file.read(config.moment);
var modifiedContents = momentContents.replace('/* EMBED_LANGUAGES */', function () {
// If we don't do this, $ symbols in lang files may get interpreted in
// the regex replacement
return embeddedContents;
});
grunt.file.write(config.dest, modifiedContents);
});
var languageReset = 'moment.lang(\'en\');';
function determineEmbeddedContent(files) {
var embeddedContent = '';
files.forEach(function (file) {
embeddedContent += transformFile(file);
});
embeddedContent += '\n ' + languageReset + '\n';
return embeddedContent;
}
var reTransform = /function \(factory\) \{[^]*\}(?=\(function \(moment\) \{)/gm;
var replaceWith =
'function (factory) {\n' +
' factory(moment);\n' +
'}';
function transformFile(file) {
var fileContents = grunt.file.read(file);
if (!fileContents.match(reTransform)) {
grunt.warn('Warning: all language files must use the common UMD wrapper pattern. Failed language file: ' + file);
return '';
}
return fileContents.replace(reTransform, replaceWith);
}
};