Skip to content

Commit 29e1ffa

Browse files
committed
fix(cli): 修复cli工具提取问题
1 parent 1311c62 commit 29e1ffa

File tree

13 files changed

+46
-43
lines changed

13 files changed

+46
-43
lines changed

.github/workflows/deploy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
uses: actions/setup-node@v2
1818
with:
1919
node-version: ${{ matrix.node-version }}
20+
registry-url: "https://registry.npmmirror.com"
2021
cache: "pnpm"
2122
- name: Install dependencies
2223
run: pnpm install

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# compiled output
22
dist
33
build
4+
lib
45
node_modules
56

67
# Logs
@@ -36,3 +37,5 @@ lerna-debug.log*
3637
!.vscode/launch.json
3738
!.vscode/extensions.json
3839

40+
# example directory
41+
examples

.npmrc

Lines changed: 0 additions & 2 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<img src="./public/logo.png" height="150">
33
</p>
44
<p align="center">
5-
<a href="https://badge.fury.io/js/tvt"><img src="https://badge.fury.io/js/tvt.svg" alt="npm version" height="18"></a>
5+
<a href="https://badge.fury.io/js/sugar18"><img src="https://badge.fury.io/js/sugar18.svg" alt="npm version" height="18"></a>
66
<a href="https://github.com/wood3n/tvt/actions"><img src="https://github.com/wood3n/tvt/actions/workflows/ci.yml/badge.svg" alt="ci Status"></a>
77
</p>
88

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"doc:build": "cd packages/docs && yarn build",
2525
"doc:serve": "cd packages/docs && yarn serve",
2626
"serve": "cd packages/coop-backend && pnpm run start",
27-
"postgres": "cd packages/coop-backend && pnpm postgres"
27+
"postgres": "cd packages/coop-backend && pnpm postgres",
28+
"publish": "zx scripts/publish.js"
2829
},
2930
"dependencies": {
3031
"@algolia/client-search": "^4.12.0",

packages/sugar18/lib/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sugar18/lib/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/sugar18/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"name": "sugar18",
3-
"version": "1.0.0",
3+
"version": "1.0.5",
4+
"homepage": "https://github.com/wood3n/sweet-i18n",
5+
"description": "a nodejs cli tool that transforms chinese characters automaticly",
46
"scripts": {
57
"build": "node esbuild.js"
68
},

packages/sugar18/src/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import packageJson from "../package.json";
2424
path.resolve(process.cwd(), "package.json"),
2525
"utf8"
2626
);
27-
const config: { tvt: ConfigOptions } = JSON.parse(localPackageJson);
28-
if (config.tvt) {
27+
const config: { sugar18: ConfigOptions } = JSON.parse(localPackageJson);
28+
if (config.sugar18) {
2929
options = {
3030
...options,
31-
...config.tvt,
31+
...config.sugar18,
3232
};
3333
}
3434
} catch (err) {
@@ -38,17 +38,18 @@ import packageJson from "../package.json";
3838

3939
const program = new Command();
4040
program
41+
.name("sugar18")
4142
.version(packageJson.version)
42-
.name("🤖tvt🤖")
43+
.command("trust")
4344
.option("-i --import", "importExpression's filepath")
4445
.option("-p --output", "json output filepath")
45-
.action((cli) => {
46-
if (cli.output) {
47-
options.output = cli.output;
46+
.action((_, options) => {
47+
if (options.output) {
48+
options.output = options.output;
4849
}
4950

50-
if (cli.import) {
51-
options.importPath = cli.import;
51+
if (options.import) {
52+
options.importPath = options.import;
5253
}
5354
})
5455
.parse(process.argv);
@@ -61,16 +62,13 @@ import packageJson from "../package.json";
6162

6263
let locales = {};
6364
const outputJSONPath = path.resolve(process.cwd(), options.output!);
64-
try {
65+
if (fs.existsSync(outputJSONPath)) {
6566
const content = fs.readFileSync(outputJSONPath, "utf8");
6667
if (content) {
6768
locales = JSON.parse(content);
6869
}
69-
} catch (err) {
70-
console.log(err);
7170
}
7271

73-
const files = glob.sync(options.pattern!, { ignore: options.ignore });
7472
glob
7573
.sync(options.pattern!, { ignore: options.ignore })
7674
.forEach((filename) => {

packages/sugar18/src/transform.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import babelTraverse, { Visitor } from "@babel/traverse";
22
import * as t from "@babel/types";
33
import path from "path";
4-
import {
5-
ElementNode,
6-
SimpleExpressionNode,
7-
NodeTypes,
8-
} from "@vue/compiler-core";
4+
import { ElementNode, SimpleExpressionNode } from "@vue/compiler-core";
95
import { parseVue, parseJS } from "./parse";
106
import {
117
generateTemplate,
@@ -15,7 +11,7 @@ import {
1511
} from "./generator";
1612
import { hasChineseCharacter, logError } from "./utils";
1713
import { generateHash } from "./hash";
18-
import { ConfigOptions, FileType } from "./typings";
14+
import { ConfigOptions, FileType, NodeTypes } from "./typings";
1915

2016
function createDirectiveAttr(type: string, name: string, value: string) {
2117
// 处理特殊的事件属性
@@ -95,7 +91,7 @@ class Transformer {
9591
hasChineseCharacter(descriptor?.template?.content)
9692
) {
9793
descriptor.template.content = generateTemplate({
98-
...this.transformTemplate(descriptor?.template.ast),
94+
...this.transformTemplate(descriptor?.template?.ast),
9995
tag: "",
10096
});
10197
}
@@ -165,14 +161,14 @@ class Transformer {
165161
* https://github.com/vuejs/vue-next/issues/4975
166162
*/
167163
if (
168-
ast.type === NodeTypes.ELEMENT &&
164+
ast.type === 1 &&
169165
/^<+?[^>]+\s+(v-pre)[^>]*>+?[\s\S]*<+?\/[\s\S]*>+?$/gm.test(
170166
ast.loc.source
171167
)
172168
) {
173169
ast.props = [
174170
{
175-
type: NodeTypes.DIRECTIVE,
171+
type: 7,
176172
name: "pre",
177173
// @ts-expect-error 类型“{ source: string; }”缺少类型“SourceLocation”中的以下属性: start, endts(2739)
178174
loc: {
@@ -188,7 +184,7 @@ class Transformer {
188184
ast.props = ast.props.map((prop) => {
189185
// vue指令
190186
if (
191-
prop.type === NodeTypes.DIRECTIVE &&
187+
prop.type === 7 &&
192188
hasChineseCharacter((prop.exp as SimpleExpressionNode)?.content)
193189
) {
194190
const jsCode = generateInterpolation(
@@ -201,10 +197,7 @@ class Transformer {
201197
);
202198
}
203199
// 普通属性
204-
if (
205-
prop.type === NodeTypes.ATTRIBUTE &&
206-
hasChineseCharacter(prop.value?.content)
207-
) {
200+
if (prop.type === 6 && hasChineseCharacter(prop.value?.content)) {
208201
const localeKey = this.extractChar(prop.value!.content);
209202
return createDirectiveAttr("bind", prop.name, `$t('${localeKey}')`);
210203
}
@@ -216,17 +209,14 @@ class Transformer {
216209
if (ast.children.length) {
217210
// @ts-expect-error 类型“{ type: number; loc: { source: string; }; }”缺少类型“TextCallNode”中的以下属性: content, codegenNodets(2322)
218211
ast.children = ast.children.map((child) => {
219-
if (
220-
child.type === NodeTypes.TEXT &&
221-
hasChineseCharacter(child.content)
222-
) {
212+
if (child.type === 2 && hasChineseCharacter(child.content)) {
223213
const localeKey = this.extractChar(child.content);
224214
return createInterpolationNode(`$t('${localeKey}')`);
225215
}
226216

227217
// 插值语法,插值语法的内容包含在child.content内部,如果匹配到中文字符,则进行JS表达式解析并替换
228218
if (
229-
child.type === NodeTypes.INTERPOLATION &&
219+
child.type === 5 &&
230220
hasChineseCharacter((child.content as SimpleExpressionNode)?.content)
231221
) {
232222
const jsCode = generateInterpolation(
@@ -239,7 +229,7 @@ class Transformer {
239229
}
240230

241231
// 元素
242-
if (child.type === NodeTypes.ELEMENT) {
232+
if (child.type === 1) {
243233
return this.transformTemplate(child);
244234
}
245235

0 commit comments

Comments
 (0)