-
Notifications
You must be signed in to change notification settings - Fork 15
/
yaml-plugin.js
60 lines (55 loc) · 2.3 KB
/
yaml-plugin.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
/*
* Copyright (c) 2024 by frostime. All Rights Reserved.
* @Author : frostime
* @Date : 2024-04-05 21:27:55
* @FilePath : /yaml-plugin.js
* @LastEditTime : 2024-04-05 22:53:34
* @Description : 去妮玛的 json 格式,我就是要用 yaml 写 i18n
*/
// plugins/vite-plugin-parse-yaml.js
import fs from 'fs';
import yaml from 'js-yaml';
import { resolve } from 'path';
export default function vitePluginYamlI18n(options = {}) {
// Default options with a fallback
const DefaultOptions = {
inDir: 'src/i18n',
outDir: 'dist/i18n',
};
const finalOptions = { ...DefaultOptions, ...options };
return {
name: 'vite-plugin-yaml-i18n',
buildStart() {
console.log('🌈 Parse I18n: YAML to JSON..');
const inDir = finalOptions.inDir;
const outDir = finalOptions.outDir
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
}
//Parse yaml file, output to json
const files = fs.readdirSync(inDir);
for (const file of files) {
if (file.endsWith('.yaml') || file.endsWith('.yml')) {
console.log(`-- Parsing ${file}`)
//检查是否有同名的json文件
const jsonFile = file.replace(/\.(yaml|yml)$/, '.json');
if (files.includes(jsonFile)) {
console.log(`---- File ${jsonFile} already exists, skipping...`);
continue;
}
try {
const filePath = resolve(inDir, file);
const fileContents = fs.readFileSync(filePath, 'utf8');
const parsed = yaml.load(fileContents);
const jsonContent = JSON.stringify(parsed, null, 2);
const outputFilePath = resolve(outDir, file.replace(/\.(yaml|yml)$/, '.json'));
console.log(`---- Writing to ${outputFilePath}`);
fs.writeFileSync(outputFilePath, jsonContent);
} catch (error) {
this.error(`---- Error parsing YAML file ${file}: ${error.message}`);
}
}
}
},
};
}