This repository was archived by the owner on Mar 23, 2026. It is now read-only.
forked from GoogleChrome/developer.chrome.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.mjs
More file actions
275 lines (240 loc) · 9.38 KB
/
utils.mjs
File metadata and controls
275 lines (240 loc) · 9.38 KB
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
import {readFile, writeFile, mkdir} from 'fs/promises';
import nunjucks from 'nunjucks';
import {Octokit} from 'octokit';
import {dirname} from 'path';
nunjucks.configure({
autoescape: false,
tags: {blockStart: '<%', blockEnd: '%>'},
});
const blogTemplate = './tools/devtools/templates/new-in-devtools.md';
const bannerTemplate = './tools/devtools/templates/new-in-devtools-banner.svg';
const blogDest = './site/{{lang}}/blog/new-in-devtools-{{version}}/index.md';
const outlineDest = './site/{{lang}}/_partials/devtools/whats-new.md';
const bannerDest = './tools/devtools/_temp/new-in-devtools-banner-{{lang}}.svg';
export const locales = [
{lang: 'en', isDefault: true},
{lang: 'es'},
{lang: 'ja'},
{lang: 'pt'},
{lang: 'ru'},
{lang: 'zh'},
];
const translation = {
language: {
es: 'Spanish',
ja: 'Japanese',
ko: 'Korean',
pt: 'Portuguese',
ru: 'Russian',
zh: 'Chinese',
},
title: {
en: "What's New in DevTools (Chrome {{version}})",
es: 'Qué hay de nuevo en DevTools (Chrome {{version}})',
ja: 'DevTools の新機能 (Chrome {{version}})',
ko: 'DevTools 의 새로운 기능 (Chrome {{version}})',
pt: 'O que há de novo no DevTools (Chrome {{version}})',
ru: 'Новинки DevTools (Chrome {{version}})',
zh: 'DevTools 新功能(Chrome {{version}})',
},
thankful: {
es: '*Gracias por la traducción [Miguel Ángel](https://midu.dev) y por la revisión [Carlos Caballero](https://carloscaballero.io).*',
ja: '*翻訳者の [technohippy](https://github.com/technohippy) さん、レビュアーの [yoichiro](https://github.com/yoichiro) さん、 [lacolaco](https://github.com/lacolaco) さん、 [yoshiko-pg](https://github.com/yoshiko-pg) さんに感謝いたします。*',
ko: '*이 게시글의 번역에는 [최원영](https://www.linkedin.com/in/toruchoi)님이 참여하셨으며, [조은](https://developers.google.com/community/experts/directory/profile/profile-eun-cho)님과 [도창욱](https://developers.google.com/community/experts/directory/profile/profile-changwook-doh)님이 리뷰를 맡아 주셨습니다.*',
pt: '*Tradução realizada por [Alvaro Camillo Neto](https://www.linkedin.com/in/alvarocamillont/) . Revisão por [Lucas Santos](https://lsantos.dev).*',
ru: '*Переводы предоставлены [Alena Batitskaia](https://twitter.com/ABatickaya). Редактор — [Maxim Salnikov](https://twitter.com/webmaxru).*',
zh: '*感谢 [Poong Zui Yong](https://www.linkedin.com/in/zui-yong-poong-1b507b14/) 提供的翻译*',
},
reviewers: {
es: ['midudev', 'Caballerog', 'and-oli'],
ja: ['yoshiko-pg', 'lacolaco', 'technohippy', 'yoichiro'],
ko: ['syang0624', 'TORU0239', 'cwdoh'],
pt: ['alvarocamillont', 'khaosdoctor'],
ru: ['solarrust', 'webmaxru', 'kateryna-prokopenko'],
zh: ['xyugroup', 'aquaMAX', 'liuliangsir', 'louisyoong', 'hanselfmu'],
},
banner: {
en: "What's new in",
es: 'Qué hay de nuevo en',
ja: '新機能',
ko: '새로운 기능',
pt: 'O que há de novo no',
ru: 'Новинки',
zh: '新功能',
},
};
/**
* Get Today's date in ISO 8601
* @returns YYYY-MM-DD
*/
function getToday() {
return new Intl.DateTimeFormat('fr-CA', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
}).format(Date.now());
}
/**
* Generate What's New in DevTools blog post banner
* @param {String} version - release version
* @param {String[]} langs - languages you want to generate
*/
export async function createWndtBanners(version, langs) {
for (const lang of langs) {
const output = nunjucks.render(bannerTemplate, {
banner: translation.banner[lang],
version: version,
});
// TODO: Upload images to CDN
const fileName = nunjucks.renderString(bannerDest, {lang});
await mkdir(dirname(fileName), {recursive: true});
await writeFile(fileName, output, 'utf-8');
}
console.log(`Created WNDT banners for: ${langs.join(', ')}`);
}
/**
* Generate What's New in DevTools blog posts templates
* @param {String} version - release version
* @param {String[]} langs - languages you want to generate
*/
export async function createWndtBlogPosts(version, langs, images = {}) {
for (const lang of langs) {
const output = nunjucks
.render(blogTemplate, {
title: nunjucks.renderString(translation.title[lang], {version}),
thankful: translation.thankful[lang] || '',
date: getToday(),
lang: lang,
version: version,
image: images[lang] || '{{image}}',
desc: lang === 'en' ? '""' : '{{desc}}',
content: lang === 'en' ? '' : '{{content}}',
})
.replaceAll('\n\n\n', '\n');
const fileName = nunjucks.renderString(blogDest, {lang, version});
await mkdir(dirname(fileName), {recursive: true});
await writeFile(fileName, output, 'utf-8');
}
console.log(`Created WNDT blog posts for: ${langs.join(', ')}`);
}
/**
* Populate What's New in DevTools blog posts content to translation templates
* @param {String} version - release version
* @param {String[]} langs - languages you want to generate
*/
export async function populateTranslationContent(version, langs) {
const enFileName = nunjucks.renderString(blogDest, {lang: 'en', version});
const enFile = await readFile(enFileName, 'utf-8');
// Extract English description and content paragraph
const enDesc = enFile.match(/(?<=description: )(.*)/gm)?.[0] || '""';
let enContent =
enFile.match(
/(?<=<!-- \$contentStart -->)(.+?)(?=<!-- \$contentEnd -->)/s
)?.[0] || '';
const enParagraphs = (
enContent.match(
/(?<=\n)((?!{%|\s*{%|{#|Chromium issue: |Chromium issues: ).*)(?=\n)$/gm
) || []
).filter(x => x);
for (const p of enParagraphs) {
if (p.startsWith('```'))
console.warn(
"Output contains code sample. Please check if it's commented correctly."
);
enContent = enContent.replace(p, `<!-- ${p} -->`);
}
// Append the English description and commented content to translation files
for (const lang of langs) {
const fileName = nunjucks.renderString(blogDest, {lang, version});
const output = nunjucks.render(fileName, {
desc: enDesc,
content: enContent,
});
await writeFile(fileName, output, 'utf-8');
}
console.log(`Populated commented content for: ${langs.join(', ')}`);
}
/**
* Append What's New in DevTools outline in 7 languages
* @param {String} version - release version
* @param {String[]} langs - languages you want to generate
*/
export async function createWndtOutline(version, langs) {
// Read the file
const fileName = nunjucks.renderString(blogDest, {lang: 'en', version});
const content = await readFile(fileName, 'utf-8');
// Abstract the title and hash id
const list = (content.match(/^(## |### )(.*)$/gm) || []).map(x => {
const title = x.match(/(?<=## )(.*?)(?= {:)/gm);
const hash = x.match(/(?<={: )(.*?)(?= })/g);
return `* [${title}]({{hashLang}}/blog/new-in-devtools-{{version}}/${hash})`;
});
const output =
'### Chrome {{version}} {: #chrome{{version}} }\n\n' +
list.join('\n') +
'\n';
// Append outline to translation files
for (const lang of langs) {
let langOutput = nunjucks.renderString(output, {
hashLang: lang === 'en' ? '' : `/${lang}`,
version,
});
if (lang !== 'en') {
langOutput = `{# ${langOutput} #}`;
}
const outlineFileName = nunjucks.renderString(outlineDest, {lang});
const outlineFileContent = await readFile(outlineFileName, 'utf-8');
const contentHolder = '{# $content #}';
const out = outlineFileContent.replace(
contentHolder,
contentHolder + '\n\n' + langOutput
);
await writeFile(outlineFileName, out, 'utf-8');
}
console.log(`Appended WNDT outlines for: ${langs.join(', ')}`);
}
/**
* Create Github Issues for translators to work on
* @param {String} version - release version
* @param {String} dueDate - due date to translate
* @param {String[]} langs - languages you want to generate
* @param {String} auth - GitHub Token
* @param {Object} translators - Translators of different locales { en: 'githubHandler' }
*/
export async function createGitHubIssues(
version,
dueDate,
langs,
auth,
translators = {}
) {
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({auth});
const {data} = await octokit.rest.users.getAuthenticated();
console.log(`Login to GitHub successfully: ${data.login}`);
for (const lang of langs) {
const translator = translators[lang] ? `@${translators[lang]}` : '...';
const reviewers = translation.reviewers[lang]
.map(r => `@${r}`)
.filter(r => r !== translator);
const {status} = await octokit.request(
'POST /repos/GoogleChrome/developer.chrome.com/issues',
{
owner: 'GoogleChrome',
repo: 'developer.chrome.com',
title: `[devtools-translate] ${translation.language[lang]} - What's New in DevTools (Chrome ${version})`,
body:
`Post: https://developer.chrome.com/blog/new-in-devtools-${version}/\n` +
`Translated by: ${dueDate}\n` +
'\n' +
`Translator: ${translator}\n` +
`Reviewers: ${reviewers.join(', ')}`,
assignees: ['jecfish'],
labels: ['devtools-translate', 'content', 'translation'],
}
);
console.log(
`Created Github issue for WNDT ${translation.language[lang]}: status ${status}`
);
}
}