|
| 1 | +import { promises as fs } from 'fs' |
| 2 | +import path from 'path' |
| 3 | +import { createServer } from 'vite' |
| 4 | +import { $fetch } from 'ohmyfetch' |
| 5 | +import { generateJSON } from '@intlify/bundle-utils' |
| 6 | +import { locales } from '../src/constants' |
| 7 | + |
| 8 | +import type { Locale } from 'vue-i18n' |
| 9 | +import type { ResourceSchema } from '../db/message' |
| 10 | + |
| 11 | +async function compile(locale: string, message: ResourceSchema) { |
| 12 | + const filename = path.join(__dirname, `../public/${locale}.js`) |
| 13 | + const ret = generateJSON(JSON.stringify(message), { |
| 14 | + type: 'plain', |
| 15 | + filename, |
| 16 | + env: 'production', |
| 17 | + onError(msg) { |
| 18 | + console.error('compile error', msg) |
| 19 | + }, |
| 20 | + onWarn(msg) { |
| 21 | + console.warn('compile waring', msg) |
| 22 | + } |
| 23 | + }) |
| 24 | + return { ...ret, filename } |
| 25 | +} |
| 26 | + |
| 27 | +;(async (locales: readonly Locale[]) => { |
| 28 | + // start vite server |
| 29 | + const vite = await createServer({ root: process.cwd() }) |
| 30 | + await vite.listen(3000) |
| 31 | + vite.printUrls() |
| 32 | + |
| 33 | + // compile reousrces |
| 34 | + for (const locale of locales) { |
| 35 | + const resource = await $fetch<ResourceSchema>( |
| 36 | + `http://localhost:3000/api/resources/${locale}` |
| 37 | + ) |
| 38 | + const { code, map, filename } = await compile(locale, resource) |
| 39 | + await fs.writeFile(filename, code) |
| 40 | + } |
| 41 | + |
| 42 | + process.on('uncaughtException', err => { |
| 43 | + console.error(`uncaught exception: ${err}\n`) |
| 44 | + process.exit(1) |
| 45 | + }) |
| 46 | + |
| 47 | + process.on('unhandledRejection', (reason, p) => { |
| 48 | + console.error('unhandled rejection at:', p, 'reason:', reason) |
| 49 | + process.exit(1) |
| 50 | + }) |
| 51 | + |
| 52 | + process.exit(0) |
| 53 | +})(locales) |
0 commit comments