Skip to content

Commit

Permalink
feat: opensource yaml tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lyzhang1999 committed Nov 17, 2023
1 parent 27e8f4b commit 430e3dd
Show file tree
Hide file tree
Showing 55 changed files with 21,812 additions and 0 deletions.
6,594 changes: 6,594 additions & 0 deletions batch-yaml/meta.locale.json

Large diffs are not rendered by default.

12,006 changes: 12,006 additions & 0 deletions batch-yaml/package-lock.json

Large diffs are not rendered by default.

948 changes: 948 additions & 0 deletions batch-yaml/package.json

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions batch-yaml/src/isYaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import YAML from 'yaml';
function isJson(text: string) {
if (text.length < 30) {
return false;
}

if (!text.trim().startsWith('{') && !text.trim().startsWith('\\{')) {
return false;
}

try {
JSON.parse(text);
} catch {
return false;
}
return true;
}
export function isYaml(text: string) {
if (text.length < 30) {
return false;
}

if (text.search(': ') === -1 || text.split('\n').length <= 1 || isJson(text)) {
return false;
}

try {
YAML.parse(text);
} catch {
return false;
}
return true;
}
29 changes: 29 additions & 0 deletions batch-yaml/src/locale/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createI18n } from 'vue-i18n';
import en from './lang/en.json';
import de from './lang/de.json';
import es from './lang/es.json';
import fr from './lang/fr.json';
import it from './lang/it.json';
import ja from './lang/ja.json';
import ko from './lang/ko.json';
import pt from './lang/pt.json';
import ru from './lang/ru.json';
import zh_tw from './lang/zh-tw.json';
import zh from './lang/zh.json';

export const i18n = createI18n({
locale: window?.$he3?.lang ?? 'en',
messages: {
de,
en,
es,
fr,
it,
ja,
ko,
pt,
ru,
zh_tw,
zh,
},
});
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/de.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "Die JSON -Analyse ist fehlgeschlagen",
"yamlParseError": "Die YAML -Analyse ist fehlgeschlagen"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "JSON Parse Error",
"yamlParseError": "YAML Parse Error"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/es.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "El análisis JSON falló",
"yamlParseError": "El análisis YAML falló"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/fr.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "L'analyse JSON a échoué",
"yamlParseError": "L'analyse YAML a échoué"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/it.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "L'analisi JSON non è riuscita",
"yamlParseError": "L'analisi YAML non è riuscita"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/ja.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "JSON分析に失敗しました",
"yamlParseError": "YAML分析に失敗しました"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/ko.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "JSON 분석에 실패했습니다",
"yamlParseError": "YAML 분석에 실패했습니다"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/pt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "A análise JSON falhou",
"yamlParseError": "A análise YAML falhou"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/ru.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "Анализ JSON потерпел неудачу",
"yamlParseError": "Анализ YAML не удался"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/zh-tw.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "JSON 解析失敗",
"yamlParseError": "YAML 解析失敗"
}
4 changes: 4 additions & 0 deletions batch-yaml/src/locale/lang/zh.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jsonParseError": "JSON 解析失败",
"yamlParseError": "YAML 解析失败"
}
31 changes: 31 additions & 0 deletions batch-yaml/src/properties-to-yaml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { transformTool } from '@he3-kit/utils';
import prettyJson from 'json-stringify-pretty-compact';
import yaml, { JSON_SCHEMA } from 'js-yaml';
import { propertiesExample } from './utils/utils';
import { JSONToProperty, jsonToYaml, propertyToJSON } from './utils/utils';
import { ErrorLine } from './utils/ErrorLine';
import { isYaml } from './isYaml';
export default transformTool({
inputLang: 'PROPERTIES',
/** 输出语法格式 */
outputLang: 'YAML',
inputHandler(str: string) {
return jsonToYaml(prettyJson(propertyToJSON(str), { maxLength: 20 }));
},
/** 输出转换函数 */
resultHandler(str: string) {
try {
const obj = yaml.load(str, { schema: JSON_SCHEMA });
return JSONToProperty(obj as { [x: string]: any }).join('\n');
} catch (err: any) {
if (err.mark) {
throw new (ErrorLine as any)(err.message, err.mark.line + 1);
} else {
throw new Error(err.message);
}
}
},
/** 默认值 */
sampleData: propertiesExample,
autoFillInputCondition: isYaml,
});
12 changes: 12 additions & 0 deletions batch-yaml/src/utils/ErrorLine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/** 用于实现报错时添加line信息 */
export function ErrorLine(
this: {
message: string;
line: number;
},
message: string,
line: number
) {
this.message = message;
this.line = line;
}
8 changes: 8 additions & 0 deletions batch-yaml/src/utils/OutputType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum OutputType {
JSON = 'json',
YAML = 'yaml',
GO = 'go',
CPLUS = 'c++',
SWIFT = 'swift',
PYTHON = 'python',
}
7 changes: 7 additions & 0 deletions batch-yaml/src/utils/handleMinify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { parse } from 'yaml';
export function handleMinify(str: string) {
if (!str) {
return '';
}
return JSON.stringify(parse(str));
}
Loading

0 comments on commit 430e3dd

Please sign in to comment.