Skip to content

Commit

Permalink
💄 style: Update i18n log
Browse files Browse the repository at this point in the history
  • Loading branch information
canisminor1990 committed Nov 25, 2024
1 parent 55542ef commit 163a444
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 47 deletions.
1 change: 0 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

git add .
npx --no-install lint-staged
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"link:i18n": "npm run link --prefix=packages/lobe-i18n",
"lint": "eslint \"{src,packages}/**/*.{js,jsx,ts,tsx}\" --fix",
"lint:md": "remark . --quiet --output",
"prepare": "husky install",
"prepare": "husky",
"prettier": "prettier -c --write \"**/**\"",
"release": "multi-semantic-release",
"start": "npm run dev",
Expand All @@ -51,33 +51,33 @@
]
},
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@lobehub/cli-shebang": "^1.0.1",
"@lobehub/lint": "^1.24.3",
"@commitlint/cli": "^19.6.0",
"@lobehub/cli-shebang": "^1.0.2",
"@lobehub/lint": "^1.24.4",
"@sindresorhus/tsconfig": "^5.1.1",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.14.14",
"@types/node": "^20.17.7",
"@types/pangu": "^4.0.2",
"@types/react": "^18.3.3",
"@types/react": "^18.3.12",
"@types/update-notifier": "^6.0.8",
"@umijs/lint": "^4.3.11",
"@umijs/lint": "^4.3.35",
"@vitest/coverage-v8": "~1.2.2",
"clean-pkg-json": "^1.2.0",
"commitlint": "^19.3.0",
"eslint": "^8.57.0",
"husky": "^9.1.4",
"commitlint": "^19.6.0",
"eslint": "^8.57.1",
"husky": "^9.1.7",
"ink-testing-library": "^3.0.0",
"lerna": "^8.1.8",
"lint-staged": "^15.2.8",
"lerna": "^8.1.9",
"lint-staged": "^15.2.10",
"multi-semantic-release": "^3.0.2",
"pkgroll": "^2.4.2",
"pkgroll": "^2.5.1",
"prettier": "^3.3.3",
"remark": "^14.0.3",
"remark-cli": "^11.0.0",
"semantic-release": "^21.1.2",
"stylelint": "^15.11.0",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"typescript": "^5.7.2",
"vitest": "~1.2.2"
},
"engines": {
Expand Down
5 changes: 4 additions & 1 deletion packages/lobe-i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@
"zustand": "^4.5.4"
},
"devDependencies": {
"@types/json-stable-stringify": "^1.0.36"
"@types/json-stable-stringify": "^1.0.36",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.16.13",
"@types/unist": "^3.0.3"
},
"peerDependencies": {
"ink": ">=4",
Expand Down
13 changes: 11 additions & 2 deletions packages/lobe-i18n/src/commands/TranslateLocale/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,15 @@ class TranslateLocale {
)} files.`,
);
checkLocaleFolders(config, files);

for (const locale of config.outputLocales) {
for (const filename of files) {
consola.info(`${chalk.cyan(locale)}${chalk.gray(' - ')}${chalk.yellow(filename)}`);
for (const [index, filename] of files.entries()) {
process.stdout.clearLine(0);
process.stdout.cursorTo(0);

process.stdout.write(
`${chalk.cyan(locale)}${chalk.gray(`[${index + 1}/${files.length}] - `)}${chalk.yellow(filename)}`,
);
const targetFilename = resolve(config.output, locale, filename);
const entryObj = entry[filename] as LocaleObj;
const targetObj = diff(entryObj, getLocaleObj(targetFilename)).target;
Expand All @@ -111,6 +117,9 @@ class TranslateLocale {
});
}
}

process.stdout.clearLine(0);
process.stdout.cursorTo(0);
}

genFlatQuery() {
Expand Down
54 changes: 26 additions & 28 deletions packages/lobe-i18n/src/utils/convertMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@ import remarkGfm from 'remark-gfm';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { unified } from 'unified';
import type { Node } from 'unist';
import { visit } from 'unist-util-visit';

import { LocaleObj } from '@/types';
import { checkMdString } from '@/utils/checkMdString';

// @ts-ignore
export const convertMarkdownToMdast = async (md: string) => {
// @ts-ignore
return unified().use(remarkParse).use(remarkGfm).use(remarkFrontmatter).parse(md.trim());
export const convertMarkdownToMdast = async (md: string): Promise<Node> => {
return unified()
.use(remarkParse as any)
.use(remarkGfm as any)
.use(remarkFrontmatter as any)
.parse(md.trim());
};

export const convertMdastToMdastObj = (mdast: any, check?: string[]) => {
export const convertMdastToMdastObj = (mdast: Node, check?: string[]): LocaleObj => {
const obj: LocaleObj = {};
let index = 0;

visit(mdast, check || 'text', (node) => {
visit(mdast, check || 'text', (node: any) => {
obj[index] = node.value;
index++;
});

return obj;
};

export const pickMdastObj = (entry: LocaleObj) => {
export const pickMdastObj = (entry: LocaleObj): LocaleObj => {
const obj: LocaleObj = {};
for (const [key, value] of Object.entries(entry)) {
if (checkMdString(value as string)) continue;
Expand All @@ -36,14 +39,14 @@ export const pickMdastObj = (entry: LocaleObj) => {
};

export const mergeMdastObj = (
{ mdast, entry, target }: { entry: LocaleObj; mdast: any; target: LocaleObj },
{ mdast, entry, target }: { entry: LocaleObj; mdast: Node; target: LocaleObj },
check?: string[],
) => {
const merged = { ...entry, ...target };

let index = 0;

visit(mdast, check || 'text', (node) => {
visit(mdast, check || 'text', (node: any) => {
node.value = merged[index];
index++;
});
Expand All @@ -52,23 +55,18 @@ export const mergeMdastObj = (
};

export const convertMdastToMarkdown = async (json: any): Promise<string> => {
// @ts-ignore
return (
unified()
// @ts-ignore
.use(remarkStringify, {
bullet: '-',
emphasis: '*',
fences: true,
listItemIndent: 1,
rule: '-',
strong: '*',
tightDefinitions: true,
})
// @ts-ignore
.use(remarkFrontmatter)
// @ts-ignore
.use(remarkGfm)
.stringify(json)
);
return unified()
.use(remarkStringify as any, {
bullet: '-',
emphasis: '*',
fences: true,
listItemIndent: 1,
rule: '-',
strong: '*',
tightDefinitions: true,
})
.use(remarkFrontmatter as any)
.use(remarkGfm as any)
.stringify(json)
.toString();
};
2 changes: 1 addition & 1 deletion packages/lobe-i18n/src/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const readJSON = (filePath: string) => {

export const writeJSON = (filePath: string, data: any) => {
const jsonStr = stringify(data, { space: ' ' });
writeFileSync(filePath, jsonStr, 'utf8');
writeFileSync(filePath, jsonStr + '\r\n', 'utf8');
};

export const readMarkdown = (filePath: string): string => {
Expand Down

0 comments on commit 163a444

Please sign in to comment.