-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGruntfile.js
265 lines (256 loc) · 8.85 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
"use strict"
module.exports = function (grunt) {
grunt.initConfig({
// Define our Pattern Library and Application Assets paths
applicationAssetsPath: "app/assets",
patternLibraryPath: "../sf-dahlia-pattern-library",
// Delete the old toolkit.css
clean: {
css: ["<%= applicationAssetsPath %>/stylesheets/toolkit.css"],
},
// Copy the latest compiled toolkit.css file from the pattern library into our app
copy: {
main: {
files: [
{
src: "<%= patternLibraryPath %>/dist/toolkit/styles/toolkit.css",
dest: "<%= applicationAssetsPath %>/stylesheets/toolkit.scss",
},
],
},
// TODO: remove this when angular is deleted
removeLanguageKey: {
files: [
{
src: "app/assets/json/translations/locale-en.json",
dest: "app/assets/json/translations/locale-en.json",
},
{
src: "app/assets/json/translations/locale-es.json",
dest: "app/assets/json/translations/locale-es.json",
},
{
src: "app/assets/json/translations/locale-tl.json",
dest: "app/assets/json/translations/locale-tl.json",
},
{
src: "app/assets/json/translations/locale-zh.json",
dest: "app/assets/json/translations/locale-zh.json",
},
],
options: {
// the locale files are prefixed with a key indicating the language but we remove this prefixed key
// via the process function below so that it can run through i18nextract translation process
// e.g. {"es": {key: value}} just returns { key: value }
process: function (content, srcpath) {
const locale = srcpath.split("-")[1].split(".")[0]
return JSON.stringify(JSON.parse(content)[locale], null, 4)
},
},
},
// TODO: remove this when angular is deleted
addBackLanguageKey: {
files: [
{
src: "app/assets/json/translations/locale-en.json",
dest: "app/assets/json/translations/locale-en.json",
},
{
src: "app/assets/json/translations/locale-es.json",
dest: "app/assets/json/translations/locale-es.json",
},
{
src: "app/assets/json/translations/locale-tl.json",
dest: "app/assets/json/translations/locale-tl.json",
},
{
src: "app/assets/json/translations/locale-zh.json",
dest: "app/assets/json/translations/locale-zh.json",
},
],
options: {
// Add back the prefixed key indicating the language
// e.g. { key: value } returns {"es": { key: value }}
process: function (content, srcpath) {
const locale = srcpath.split("-")[1].split(".")[0]
const body = {}
body[locale] = JSON.parse(content)
return JSON.stringify(body, null, 4)
},
},
},
},
// TODO: remove this when angular is deleted
// Make any string replacements that are needed when transferring assets to app.
replace: {
dist: {
options: {
patterns: [
{
match: "http://fonts.googleapis.com",
replacement: "//fonts.googleapis.com",
},
{
match: /\.\.\/images\/([a-zA-Z0-9\-_@]*\.(png|jpg|svg))/g,
replacement: "asset-path('$1')",
},
],
usePrefix: false,
},
files: [
{
expand: true,
flatten: true,
src: ["<%= applicationAssetsPath %>/stylesheets/toolkit.scss"],
dest: "<%= applicationAssetsPath %>/stylesheets/",
},
],
},
},
// Parse react code for t() translation calls
i18next: {
dev: {
src: "app/javascript/**/*.{js,jsx,ts,tsx}",
dest: "app/assets/json/translations/react",
options: {
sort: true,
func: {
list: ["t"],
extensions: [".js", ".jsx", ".ts", ".tsx"],
},
lngs: ["en", "es", "tl", "zh"],
defaultLng: "en",
defaultValue: "",
removeUnusedKeys: false,
resource: {
loadPath: "app/assets/json/translations/react/{{lng}}.json",
savePath: "{{lng}}.json",
jsonIndent: 2,
lineEnding: "\n",
},
nsSeparator: false, // namespace separator
keySeparator: false,
},
},
},
// TODO: remove this when angular is deleted
i18nextract: {
default_options: {
src: [
"app/assets/javascripts/**/*.js",
"app/assets/javascripts/**/*.js.coffee",
"app/assets/javascripts/**/*.html",
"app/assets/javascripts/**/*.html.slim",
"app/views/layouts/application-angular.html.slim",
"app/views/layouts/application-react.html.slim",
"app/views/devise/mailer/*.html.slim",
"app/views/emailer/*.html.slim",
"app/views/layouts/email.html.slim",
"app/mailers/**/*.rb",
],
customRegex: [
"{{\\s*(?:::)?'((?:\\\\.|[^'\\\\])*)'\\s*\\|\\s*translate(:.*?)?\\s*(?:\\s*\\|\\s*[a-zA-Z]*)?}}",
"=\"'((?:\\\\.|[^'\\\\])*)'\\s*\\|\\s*translate\"",
'translate="([A-Z0-9.-_]*)"',
'translated-error="([A-Z.-_]*)"',
'translated-description="([A-Z.-_]*)"',
'translated-short-description="([A-Z.-_]*)"',
"translatedLabel: '([A-Z.-_]*)'",
// email template regexes below
" t '([A-Z.-_]*)'",
" t\\('([A-Z.-_]*)'",
"\\(t\\('([A-Z.-_]*)'",
"#{t\\('([A-Z.-_]*)'",
"I18n.translate\\(\n?[ ]*'([A-Z.-_]*)'", // emailer.rb usages
"flagForI18n.'([A-Z0-9.-_]*)'", // search for flagForI18n([translation string]
],
namespace: true,
lang: ["locale-en", "locale-es", "locale-tl", "locale-zh"],
dest: "app/assets/json/translations",
},
},
json_remove_fields: {
locale_es: {
src: "app/assets/json/translations/locale-es.json",
},
locale_tl: {
src: "app/assets/json/translations/locale-tl.json",
},
locale_zh: {
src: "app/assets/json/translations/locale-zh.json",
},
es: {
src: "app/assets/json/translations/react/es.json",
},
tl: {
src: "app/assets/json/translations/react/tl.json",
},
zh: {
src: "app/assets/json/translations/react/zh.json",
},
},
// TODO: remove this when angular is deleted
sortJSON: {
src: [
"app/assets/json/translations/locale-en.json",
"app/assets/json/translations/locale-es.json",
"app/assets/json/translations/locale-tl.json",
"app/assets/json/translations/locale-zh.json",
],
},
exec: {
phrasePull: {
cmd: function () {
// If token is present, pass to phrase, otherwise phrase will look for
// the PHRASE_ACCESS_TOKEN env var.
const token = grunt.option("phraseAccessToken")
return token ? `phrase pull --access_token ${token}` : "phrase pull"
},
},
phrasePush: {
cmd: function () {
// If token is present, pass to phrase, otherwise phrase will look for
// the PHRASE_ACCESS_TOKEN env var.
const token = grunt.option("phraseAccessToken")
return token ? `phrase push --access_token ${token}` : "phrase push"
},
},
},
})
// load tasks
grunt.loadNpmTasks("grunt-contrib-clean")
grunt.loadNpmTasks("grunt-contrib-copy")
grunt.loadNpmTasks("grunt-replace-regex")
grunt.loadNpmTasks("grunt-angular-translate")
grunt.loadNpmTasks("grunt-sort-json")
grunt.loadNpmTasks("grunt-json-remove-fields")
grunt.loadNpmTasks("grunt-exec")
grunt.loadNpmTasks("i18next-scanner")
// Set this variable by appending --phraseAccessToken=[token] to any grunt command.
// var phraseAccessToken = grunt.option("phraseAccessToken")
// register task
grunt.registerTask("default", ["clean", "copy", "replace"])
grunt.registerTask("translations", [
"copy:removeLanguageKey", // only relevant for angular
"i18nextract", // extract angular phrases
"i18next", // extract react phrases
"copy:addBackLanguageKey", // only relevant for angular
"json_remove_fields", // necessary for react and angular
"sortJSON", // only relevant for angular
])
grunt.registerTask("phrasePush", [
"copy:removeLanguageKey",
"exec:phrasePush",
"copy:addBackLanguageKey",
"json_remove_fields",
"sortJSON",
])
grunt.registerTask("phrasePull", [
"copy:removeLanguageKey",
"exec:phrasePull",
"copy:addBackLanguageKey",
"json_remove_fields",
"sortJSON",
])
grunt.registerTask("deploy", ["clean", "copy", "replace"])
}