summaryrefslogtreecommitdiff
path: root/packages/frontend/lib/vite-plugin-watch-locales.ts
blob: 372e9039d5dca745dfdf19e3498b0a4ace5edf5f (plain)
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
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import path from 'node:path'
import locales from 'i18n';

const localesDir = path.resolve(__dirname, '../../../locales')

/**
 * 外部ファイルを監視し、必要に応じてwebSocketでメッセージを送るViteプラグイン
 * @returns {import('vite').Plugin}
 */
export default function pluginWatchLocales() {
	return {
		name: 'watch-locales',

		configureServer(server) {
			const localeYmlPaths = Object.keys(locales).map(locale => path.join(localesDir, `${locale}.yml`));

			// watcherにパスを追加
			server.watcher.add(localeYmlPaths);

			server.watcher.on('change', (filePath) => {
				if (localeYmlPaths.includes(filePath)) {
					server.ws.send({
						type: 'custom',
						event: 'locale-update',
						data: filePath.match(/([^\/]+)\.yml$/)?.[1] || null,
					})
				}
			});
		},
	};
}