@elonzh/fafa 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +44 -2
- package/dist/bin.js +8 -0
- package/dist/chunk-OBB66GA5.js +3084 -0
- package/dist/chunk-T24ISKK3.js +849 -0
- package/dist/dist-HLWFT422.js +12 -0
- package/dist/index.js +9 -0
- package/package.json +64 -4
- package/themes/base.css +191 -0
- package/themes/default.css +32 -0
- package/themes/elegant.css +50 -0
- package/themes/green.css +47 -0
- package/themes/minimal.css +47 -0
- package/themes/official.css +74 -0
- package/themes/tech.css +47 -0
- package/themes/warm.css +48 -0
|
@@ -0,0 +1,849 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EXPORT_APPEARANCES,
|
|
3
|
+
EXPORT_FORMATS,
|
|
4
|
+
FaFaError,
|
|
5
|
+
createDefaultWechatClient,
|
|
6
|
+
createProject,
|
|
7
|
+
createTranslator,
|
|
8
|
+
exportFilename,
|
|
9
|
+
getFaFaHome,
|
|
10
|
+
hasErrors,
|
|
11
|
+
inspectExportBrowser,
|
|
12
|
+
installExportBrowser,
|
|
13
|
+
listCodeHighlightThemes,
|
|
14
|
+
listImageCaptionModes,
|
|
15
|
+
listWechatThemePresets,
|
|
16
|
+
loadProject,
|
|
17
|
+
loadUserConfig,
|
|
18
|
+
localeFromEnvironment,
|
|
19
|
+
normalizeLocale,
|
|
20
|
+
removeAccount,
|
|
21
|
+
resolveCodeHighlightTheme,
|
|
22
|
+
resolveImageCaptionMode,
|
|
23
|
+
resolveWechatCredential,
|
|
24
|
+
resolveWechatTheme,
|
|
25
|
+
safeFilename,
|
|
26
|
+
setProjectDefaultCodeTheme,
|
|
27
|
+
setProjectDefaultImageCaption,
|
|
28
|
+
setProjectDefaultTheme,
|
|
29
|
+
toPublicUserConfig,
|
|
30
|
+
upsertAccount
|
|
31
|
+
} from "./chunk-OBB66GA5.js";
|
|
32
|
+
|
|
33
|
+
// src/i18n.ts
|
|
34
|
+
var englishMessages = {
|
|
35
|
+
"app.description": "FaFa local-first content rendering and platform draft delivery",
|
|
36
|
+
"option.json": "write a stable JSON result to stdout",
|
|
37
|
+
"option.project": "project path (defaults to the current directory)",
|
|
38
|
+
"option.home": "user configuration directory (or use FAFA_HOME)",
|
|
39
|
+
"option.language": "interface language: en or zh-CN (or use FAFA_LANGUAGE)",
|
|
40
|
+
"init.description": "create a FaFa project",
|
|
41
|
+
"init.success": "Created {root}",
|
|
42
|
+
"articles.description": "list local source articles",
|
|
43
|
+
"articles.count": "{count} article(s)",
|
|
44
|
+
"theme.description": "manage WeChat CSS themes",
|
|
45
|
+
"theme.list.description": "list built-in themes",
|
|
46
|
+
"theme.list.count": "{count} built-in theme(s)",
|
|
47
|
+
"theme.set.description": "set the project default theme",
|
|
48
|
+
"theme.set.preset": "built-in theme id",
|
|
49
|
+
"theme.set.file": "project-local CSS theme file",
|
|
50
|
+
"theme.set.choice": "Specify exactly one of --preset or --file",
|
|
51
|
+
"theme.set.unknown": "Unknown built-in WeChat theme: {id}",
|
|
52
|
+
"theme.set.success": "Set project theme to {reference}",
|
|
53
|
+
"codeTheme.description": "manage Shiki code themes",
|
|
54
|
+
"codeTheme.list.description": "list built-in Shiki code themes",
|
|
55
|
+
"codeTheme.list.count": "{count} Shiki code theme(s)",
|
|
56
|
+
"codeTheme.set.description": "set the project default Shiki code theme",
|
|
57
|
+
"codeTheme.set.preset": "built-in Shiki code theme id",
|
|
58
|
+
"codeTheme.set.success": "Set project code theme to {reference}",
|
|
59
|
+
"caption.description": "manage image caption modes",
|
|
60
|
+
"caption.list.description": "list supported image caption modes",
|
|
61
|
+
"caption.list.count": "{count} image caption mode(s)",
|
|
62
|
+
"caption.set.description": "set the project default image caption mode",
|
|
63
|
+
"caption.set.mode": "image caption mode id",
|
|
64
|
+
"caption.set.success": "Set project image caption mode to {reference}",
|
|
65
|
+
"stdin.sourcePath": "Reading Markdown from stdin requires --source-path",
|
|
66
|
+
"check.failed": "Article failed validation",
|
|
67
|
+
"check.description": "validate an article without network access",
|
|
68
|
+
"sourcePath.description": "existing note identity and asset base for stdin",
|
|
69
|
+
"check.success": "Check passed: {articleId}",
|
|
70
|
+
"render.description": "render an offline WeChat preview",
|
|
71
|
+
"render.output": "preview output path",
|
|
72
|
+
"render.success": "Rendered {output}",
|
|
73
|
+
"export.description": "export an article as HTML, PDF, or PNG",
|
|
74
|
+
"export.format": "output format: html, pdf, or png",
|
|
75
|
+
"export.appearance": "export appearance: day or night",
|
|
76
|
+
"export.output": "export output path",
|
|
77
|
+
"export.width": "PNG width in CSS pixels (240-2000)",
|
|
78
|
+
"export.browserPath": "explicit Chrome or Edge executable path",
|
|
79
|
+
"export.formatError": "Unsupported export format: {format}. Use html, pdf, or png.",
|
|
80
|
+
"export.appearanceError": "Unsupported export appearance: {appearance}. Use day or night.",
|
|
81
|
+
"export.widthError": "--width must be an integer from 240 to 2000",
|
|
82
|
+
"export.widthFormatError": "--width can only be used with PNG export",
|
|
83
|
+
"export.success": "Exported {format} to {output}",
|
|
84
|
+
"browser.description": "manage the export browser runtime",
|
|
85
|
+
"browser.status.description": "inspect the available export browser",
|
|
86
|
+
"browser.status.ready": "Export browser is ready: {source}",
|
|
87
|
+
"browser.status.missing": "No export browser was found",
|
|
88
|
+
"browser.install.description": "install the managed export browser",
|
|
89
|
+
"browser.install.success": "Installed export browser: {source}",
|
|
90
|
+
"draft.description": "manage platform drafts",
|
|
91
|
+
"draft.push.description": "create or update a WeChat draft",
|
|
92
|
+
"draft.account": "target WeChat account",
|
|
93
|
+
"draft.dryRun": "plan without remote calls",
|
|
94
|
+
"draft.planFailed": "Draft plan failed validation",
|
|
95
|
+
"draft.planSuccess": "Dry run: {action} draft, upload {uploads}, reuse {reused}",
|
|
96
|
+
"draft.pushSuccess": "{action} WeChat draft {mediaId}",
|
|
97
|
+
"draft.created": "Created",
|
|
98
|
+
"draft.updated": "Updated",
|
|
99
|
+
"operations.description": "list recent local operations",
|
|
100
|
+
"operations.limit": "maximum records",
|
|
101
|
+
"operations.limitError": "--limit must be an integer from 1 to 500",
|
|
102
|
+
"operations.count": "{count} operation(s)",
|
|
103
|
+
"account.description": "manage WeChat accounts and credentials",
|
|
104
|
+
"account.add.description": "add or update an account",
|
|
105
|
+
"account.id": "stable local account id",
|
|
106
|
+
"account.name": "display name",
|
|
107
|
+
"account.appId": "WeChat AppID",
|
|
108
|
+
"account.test": "mark as a test account",
|
|
109
|
+
"account.secretEnv": "read AppSecret from this environment variable and store it in config",
|
|
110
|
+
"account.emptyEnv": "Environment variable {name} is empty",
|
|
111
|
+
"account.saved": "Saved account {id}",
|
|
112
|
+
"account.list.description": "list configured accounts (never prints secrets)",
|
|
113
|
+
"account.count": "{count} account(s)",
|
|
114
|
+
"account.remove.description": "remove an account and its stored credential",
|
|
115
|
+
"account.removed": "Removed account {id}",
|
|
116
|
+
"account.probe.description": "verify token and draft API capability without changing drafts",
|
|
117
|
+
"account.available": "Account {id} draft API is available",
|
|
118
|
+
"language.unsupported": "Unsupported language: {language}. Use en or zh-CN."
|
|
119
|
+
};
|
|
120
|
+
var chineseMessages = {
|
|
121
|
+
"app.description": "FaFa \u672C\u5730\u4F18\u5148\u5185\u5BB9\u6E32\u67D3\u4E0E\u5E73\u53F0\u8349\u7A3F\u6295\u9012\u5DE5\u5177",
|
|
122
|
+
"option.json": "\u5C06\u7A33\u5B9A\u7684 JSON \u7ED3\u679C\u5199\u5165\u6807\u51C6\u8F93\u51FA",
|
|
123
|
+
"option.project": "\u9879\u76EE\u8DEF\u5F84\uFF08\u9ED8\u8BA4\u4F7F\u7528\u5F53\u524D\u76EE\u5F55\uFF09",
|
|
124
|
+
"option.home": "\u7528\u6237\u914D\u7F6E\u76EE\u5F55\uFF08\u4E5F\u53EF\u4F7F\u7528 FAFA_HOME\uFF09",
|
|
125
|
+
"option.language": "\u754C\u9762\u8BED\u8A00\uFF1Aen \u6216 zh-CN\uFF08\u4E5F\u53EF\u4F7F\u7528 FAFA_LANGUAGE\uFF09",
|
|
126
|
+
"init.description": "\u521B\u5EFA FaFa \u9879\u76EE",
|
|
127
|
+
"init.success": "\u5DF2\u521B\u5EFA {root}",
|
|
128
|
+
"articles.description": "\u5217\u51FA\u672C\u5730\u6E90\u6587\u7A3F",
|
|
129
|
+
"articles.count": "\u5171 {count} \u7BC7\u6E90\u6587\u7A3F",
|
|
130
|
+
"theme.description": "\u7BA1\u7406\u5FAE\u4FE1\u516C\u4F17\u53F7 CSS \u4E3B\u9898",
|
|
131
|
+
"theme.list.description": "\u5217\u51FA\u5185\u7F6E\u4E3B\u9898",
|
|
132
|
+
"theme.list.count": "\u5171 {count} \u4E2A\u5185\u7F6E\u4E3B\u9898",
|
|
133
|
+
"theme.set.description": "\u8BBE\u7F6E\u9879\u76EE\u9ED8\u8BA4\u4E3B\u9898",
|
|
134
|
+
"theme.set.preset": "\u5185\u7F6E\u4E3B\u9898 ID",
|
|
135
|
+
"theme.set.file": "\u9879\u76EE\u5185 CSS \u4E3B\u9898\u6587\u4EF6",
|
|
136
|
+
"theme.set.choice": "--preset \u4E0E --file \u5FC5\u987B\u4E14\u53EA\u80FD\u6307\u5B9A\u4E00\u4E2A",
|
|
137
|
+
"theme.set.unknown": "\u672A\u77E5\u7684\u5185\u7F6E\u5FAE\u4FE1\u4E3B\u9898\uFF1A{id}",
|
|
138
|
+
"theme.set.success": "\u9879\u76EE\u4E3B\u9898\u5DF2\u8BBE\u4E3A {reference}",
|
|
139
|
+
"codeTheme.description": "\u7BA1\u7406 Shiki \u4EE3\u7801\u4E3B\u9898",
|
|
140
|
+
"codeTheme.list.description": "\u5217\u51FA\u5185\u7F6E Shiki \u4EE3\u7801\u4E3B\u9898",
|
|
141
|
+
"codeTheme.list.count": "\u5171 {count} \u4E2A\u5185\u7F6E Shiki \u4EE3\u7801\u4E3B\u9898",
|
|
142
|
+
"codeTheme.set.description": "\u8BBE\u7F6E\u9879\u76EE\u9ED8\u8BA4 Shiki \u4EE3\u7801\u4E3B\u9898",
|
|
143
|
+
"codeTheme.set.preset": "\u5185\u7F6E Shiki \u4EE3\u7801\u4E3B\u9898 ID",
|
|
144
|
+
"codeTheme.set.success": "\u9879\u76EE\u4EE3\u7801\u4E3B\u9898\u5DF2\u8BBE\u4E3A {reference}",
|
|
145
|
+
"caption.description": "\u7BA1\u7406\u56FE\u7247\u56FE\u6CE8\u6A21\u5F0F",
|
|
146
|
+
"caption.list.description": "\u5217\u51FA\u652F\u6301\u7684\u56FE\u7247\u56FE\u6CE8\u6A21\u5F0F",
|
|
147
|
+
"caption.list.count": "\u5171 {count} \u4E2A\u56FE\u7247\u56FE\u6CE8\u6A21\u5F0F",
|
|
148
|
+
"caption.set.description": "\u8BBE\u7F6E\u9879\u76EE\u9ED8\u8BA4\u56FE\u7247\u56FE\u6CE8\u6A21\u5F0F",
|
|
149
|
+
"caption.set.mode": "\u56FE\u7247\u56FE\u6CE8\u6A21\u5F0F ID",
|
|
150
|
+
"caption.set.success": "\u9879\u76EE\u56FE\u7247\u56FE\u6CE8\u6A21\u5F0F\u5DF2\u8BBE\u4E3A {reference}",
|
|
151
|
+
"stdin.sourcePath": "\u4ECE\u6807\u51C6\u8F93\u5165\u8BFB\u53D6 Markdown \u65F6\u5FC5\u987B\u63D0\u4F9B --source-path",
|
|
152
|
+
"check.failed": "\u6E90\u6587\u7A3F\u6821\u9A8C\u672A\u901A\u8FC7",
|
|
153
|
+
"check.description": "\u79BB\u7EBF\u6821\u9A8C\u6E90\u6587\u7A3F",
|
|
154
|
+
"sourcePath.description": "\u6807\u51C6\u8F93\u5165\u5BF9\u5E94\u7684\u73B0\u6709\u7B14\u8BB0\u8EAB\u4EFD\u4E0E\u7D20\u6750\u57FA\u51C6\u8DEF\u5F84",
|
|
155
|
+
"check.success": "\u6821\u9A8C\u901A\u8FC7\uFF1A{articleId}",
|
|
156
|
+
"render.description": "\u751F\u6210\u79BB\u7EBF\u5FAE\u4FE1\u9884\u89C8",
|
|
157
|
+
"render.output": "\u9884\u89C8\u8F93\u51FA\u8DEF\u5F84",
|
|
158
|
+
"render.success": "\u5DF2\u6E32\u67D3 {output}",
|
|
159
|
+
"export.description": "\u5C06\u6587\u7AE0\u5BFC\u51FA\u4E3A HTML\u3001PDF \u6216 PNG",
|
|
160
|
+
"export.format": "\u8F93\u51FA\u683C\u5F0F\uFF1Ahtml\u3001pdf \u6216 png",
|
|
161
|
+
"export.appearance": "\u5BFC\u51FA\u663E\u793A\u6A21\u5F0F\uFF1Aday \u6216 night",
|
|
162
|
+
"export.output": "\u5BFC\u51FA\u6587\u4EF6\u8DEF\u5F84",
|
|
163
|
+
"export.width": "PNG \u7684 CSS \u50CF\u7D20\u5BBD\u5EA6\uFF08240-2000\uFF09",
|
|
164
|
+
"export.browserPath": "\u663E\u5F0F\u6307\u5B9A Chrome \u6216 Edge \u53EF\u6267\u884C\u6587\u4EF6\u8DEF\u5F84",
|
|
165
|
+
"export.formatError": "\u4E0D\u652F\u6301\u5BFC\u51FA\u683C\u5F0F {format}\uFF0C\u8BF7\u4F7F\u7528 html\u3001pdf \u6216 png\u3002",
|
|
166
|
+
"export.appearanceError": "\u4E0D\u652F\u6301\u5BFC\u51FA\u663E\u793A\u6A21\u5F0F {appearance}\uFF0C\u8BF7\u4F7F\u7528 day \u6216 night\u3002",
|
|
167
|
+
"export.widthError": "--width \u5FC5\u987B\u662F 240 \u5230 2000 \u4E4B\u95F4\u7684\u6574\u6570",
|
|
168
|
+
"export.widthFormatError": "--width \u53EA\u80FD\u7528\u4E8E PNG \u5BFC\u51FA",
|
|
169
|
+
"export.success": "\u5DF2\u5C06 {format} \u5BFC\u51FA\u5230 {output}",
|
|
170
|
+
"browser.description": "\u7BA1\u7406\u5BFC\u51FA\u6D4F\u89C8\u5668\u8FD0\u884C\u65F6",
|
|
171
|
+
"browser.status.description": "\u68C0\u67E5\u53EF\u7528\u7684\u5BFC\u51FA\u6D4F\u89C8\u5668",
|
|
172
|
+
"browser.status.ready": "\u5BFC\u51FA\u6D4F\u89C8\u5668\u5DF2\u5C31\u7EEA\uFF1A{source}",
|
|
173
|
+
"browser.status.missing": "\u672A\u627E\u5230\u5BFC\u51FA\u6D4F\u89C8\u5668",
|
|
174
|
+
"browser.install.description": "\u5B89\u88C5\u53D7\u7BA1\u7406\u7684\u5BFC\u51FA\u6D4F\u89C8\u5668",
|
|
175
|
+
"browser.install.success": "\u5DF2\u5B89\u88C5\u5BFC\u51FA\u6D4F\u89C8\u5668\uFF1A{source}",
|
|
176
|
+
"draft.description": "\u7BA1\u7406\u5E73\u53F0\u8349\u7A3F",
|
|
177
|
+
"draft.push.description": "\u521B\u5EFA\u6216\u66F4\u65B0\u5FAE\u4FE1\u516C\u4F17\u53F7\u8349\u7A3F",
|
|
178
|
+
"draft.account": "\u76EE\u6807\u5FAE\u4FE1\u516C\u4F17\u53F7\u8D26\u53F7",
|
|
179
|
+
"draft.dryRun": "\u53EA\u751F\u6210\u8BA1\u5212\uFF0C\u4E0D\u8C03\u7528\u8FDC\u7AEF\u63A5\u53E3",
|
|
180
|
+
"draft.planFailed": "\u8349\u7A3F\u6295\u9012\u9884\u68C0\u672A\u901A\u8FC7",
|
|
181
|
+
"draft.planSuccess": "\u9884\u68C0\uFF1A{action}\u8349\u7A3F\uFF0C\u4E0A\u4F20 {uploads} \u4E2A\u7D20\u6750\uFF0C\u590D\u7528 {reused} \u4E2A\u7D20\u6750",
|
|
182
|
+
"draft.pushSuccess": "{action}\u5FAE\u4FE1\u516C\u4F17\u53F7\u8349\u7A3F {mediaId}",
|
|
183
|
+
"draft.created": "\u5DF2\u521B\u5EFA",
|
|
184
|
+
"draft.updated": "\u5DF2\u66F4\u65B0",
|
|
185
|
+
"operations.description": "\u5217\u51FA\u6700\u8FD1\u7684\u672C\u5730\u64CD\u4F5C",
|
|
186
|
+
"operations.limit": "\u6700\u5927\u8BB0\u5F55\u6570",
|
|
187
|
+
"operations.limitError": "--limit \u5FC5\u987B\u662F 1 \u5230 500 \u4E4B\u95F4\u7684\u6574\u6570",
|
|
188
|
+
"operations.count": "\u5171 {count} \u6761\u64CD\u4F5C",
|
|
189
|
+
"account.description": "\u7BA1\u7406\u5FAE\u4FE1\u516C\u4F17\u53F7\u8D26\u53F7\u4E0E\u51ED\u636E",
|
|
190
|
+
"account.add.description": "\u6DFB\u52A0\u6216\u66F4\u65B0\u8D26\u53F7",
|
|
191
|
+
"account.id": "\u7A33\u5B9A\u7684\u672C\u5730\u8D26\u53F7 ID",
|
|
192
|
+
"account.name": "\u663E\u793A\u540D\u79F0",
|
|
193
|
+
"account.appId": "\u5FAE\u4FE1\u516C\u4F17\u53F7 AppID",
|
|
194
|
+
"account.test": "\u6807\u8BB0\u4E3A\u6D4B\u8BD5\u8D26\u53F7",
|
|
195
|
+
"account.secretEnv": "\u4ECE\u6B64\u73AF\u5883\u53D8\u91CF\u8BFB\u53D6 AppSecret \u5E76\u4FDD\u5B58\u5230\u914D\u7F6E",
|
|
196
|
+
"account.emptyEnv": "\u73AF\u5883\u53D8\u91CF {name} \u4E3A\u7A7A",
|
|
197
|
+
"account.saved": "\u5DF2\u4FDD\u5B58\u8D26\u53F7 {id}",
|
|
198
|
+
"account.list.description": "\u5217\u51FA\u5DF2\u914D\u7F6E\u8D26\u53F7\uFF08\u7EDD\u4E0D\u8F93\u51FA\u51ED\u636E\uFF09",
|
|
199
|
+
"account.count": "\u5171 {count} \u4E2A\u8D26\u53F7",
|
|
200
|
+
"account.remove.description": "\u79FB\u9664\u8D26\u53F7\u53CA\u5176\u5DF2\u4FDD\u5B58\u51ED\u636E",
|
|
201
|
+
"account.removed": "\u5DF2\u79FB\u9664\u8D26\u53F7 {id}",
|
|
202
|
+
"account.probe.description": "\u5728\u4E0D\u4FEE\u6539\u8349\u7A3F\u7684\u524D\u63D0\u4E0B\u9A8C\u8BC1 Token \u4E0E\u8349\u7A3F\u63A5\u53E3\u80FD\u529B",
|
|
203
|
+
"account.available": "\u8D26\u53F7 {id} \u7684\u8349\u7A3F\u63A5\u53E3\u53EF\u7528",
|
|
204
|
+
"language.unsupported": "\u4E0D\u652F\u6301\u8BED\u8A00 {language}\uFF0C\u8BF7\u4F7F\u7528 en \u6216 zh-CN\u3002"
|
|
205
|
+
};
|
|
206
|
+
var cliMessages = {
|
|
207
|
+
en: englishMessages,
|
|
208
|
+
"zh-CN": chineseMessages
|
|
209
|
+
};
|
|
210
|
+
function createCliTranslator(locale) {
|
|
211
|
+
return createTranslator(locale, cliMessages);
|
|
212
|
+
}
|
|
213
|
+
function resolveCliLocale(value, fallback) {
|
|
214
|
+
if (!value) return fallback;
|
|
215
|
+
if (!/^(?:en|en[-_].*|zh|zh[-_].*)$/i.test(value.trim())) {
|
|
216
|
+
throw new FaFaError(
|
|
217
|
+
"CONFIG_INVALID",
|
|
218
|
+
createCliTranslator(fallback)("language.unsupported", { language: value })
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
return normalizeLocale(value, fallback);
|
|
222
|
+
}
|
|
223
|
+
function languageFromArgv(argv) {
|
|
224
|
+
const index = argv.findIndex((argument) => argument === "--language");
|
|
225
|
+
if (index >= 0) return argv[index + 1];
|
|
226
|
+
return argv.find((argument) => argument.startsWith("--language="))?.slice("--language=".length);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// src/io.ts
|
|
230
|
+
import { CommanderError } from "commander";
|
|
231
|
+
var defaultIo = {
|
|
232
|
+
stdout: (message) => process.stdout.write(message),
|
|
233
|
+
stderr: (message) => process.stderr.write(message),
|
|
234
|
+
readStdin: async () => {
|
|
235
|
+
const chunks = [];
|
|
236
|
+
for await (const chunk of process.stdin) {
|
|
237
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
238
|
+
}
|
|
239
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
function line(value) {
|
|
243
|
+
return value.endsWith("\n") ? value : `${value}
|
|
244
|
+
`;
|
|
245
|
+
}
|
|
246
|
+
function isDisplayExit(error) {
|
|
247
|
+
return error.code === "commander.helpDisplayed" || error.code === "commander.version";
|
|
248
|
+
}
|
|
249
|
+
function exitCode(error) {
|
|
250
|
+
if (error instanceof CommanderError) return isDisplayExit(error) ? 0 : 2;
|
|
251
|
+
if (!(error instanceof FaFaError)) return 1;
|
|
252
|
+
if (error.code === "CHECK_FAILED" || error.code === "ASSET_INVALID" || error.code === "STALE_BUILD") {
|
|
253
|
+
return 3;
|
|
254
|
+
}
|
|
255
|
+
if (error.code === "CREDENTIAL_MISSING") return 4;
|
|
256
|
+
if (error.code === "WECHAT_API_ERROR" || error.code === "DELIVERY_FAILED") return 5;
|
|
257
|
+
if (error.code === "BROWSER_INSTALL_FAILED" || error.code === "EXPORT_FAILED") return 1;
|
|
258
|
+
return 2;
|
|
259
|
+
}
|
|
260
|
+
function errorCode(error) {
|
|
261
|
+
if (error instanceof FaFaError) return error.code;
|
|
262
|
+
if (error instanceof CommanderError) return "CLI_USAGE";
|
|
263
|
+
return "INTERNAL_ERROR";
|
|
264
|
+
}
|
|
265
|
+
function emitDiagnostics(io, diagnostics) {
|
|
266
|
+
for (const diagnostic of diagnostics) {
|
|
267
|
+
const position = diagnostic.location ? ` ${diagnostic.location.file}${diagnostic.location.line ? `:${diagnostic.location.line}` : ""}` : "";
|
|
268
|
+
io.stderr(
|
|
269
|
+
line(`${diagnostic.severity.toUpperCase()} ${diagnostic.code}${position}: ${diagnostic.message}`)
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
function success(io, json, command, data, human) {
|
|
274
|
+
io.stdout(json ? line(JSON.stringify({ ok: true, command, data })) : line(human));
|
|
275
|
+
}
|
|
276
|
+
function failure(io, json, error, code) {
|
|
277
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
278
|
+
if (json) {
|
|
279
|
+
io.stdout(
|
|
280
|
+
line(
|
|
281
|
+
JSON.stringify({
|
|
282
|
+
ok: false,
|
|
283
|
+
error: {
|
|
284
|
+
code: errorCode(error),
|
|
285
|
+
message,
|
|
286
|
+
exitCode: code,
|
|
287
|
+
...error instanceof FaFaError && error.details ? { details: error.details } : {}
|
|
288
|
+
}
|
|
289
|
+
})
|
|
290
|
+
)
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
if (!(error instanceof CommanderError && isDisplayExit(error))) {
|
|
294
|
+
io.stderr(line(`${errorCode(error)}: ${message}`));
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// src/program.ts
|
|
299
|
+
import { Command } from "commander";
|
|
300
|
+
|
|
301
|
+
// package.json
|
|
302
|
+
var package_default = { name: "@elonzh/fafa", version: "0.1.0" };
|
|
303
|
+
|
|
304
|
+
// src/commands/account.ts
|
|
305
|
+
function registerAccountCommands(program, context) {
|
|
306
|
+
const t = context.translate();
|
|
307
|
+
const account = program.command("account").description(t("account.description"));
|
|
308
|
+
const accountAdd = account.command("add").description(t("account.add.description"));
|
|
309
|
+
accountAdd.requiredOption("--id <id>", t("account.id")).requiredOption("--name <name>", t("account.name")).requiredOption("--app-id <id>", t("account.appId")).option("--test", t("account.test")).option("--app-secret-env <name>", t("account.secretEnv"));
|
|
310
|
+
accountAdd.action(async () => {
|
|
311
|
+
const options = accountAdd.opts();
|
|
312
|
+
const secret = options.appSecretEnv ? process.env[options.appSecretEnv] : void 0;
|
|
313
|
+
if (options.appSecretEnv && !secret) {
|
|
314
|
+
throw new FaFaError(
|
|
315
|
+
"CREDENTIAL_MISSING",
|
|
316
|
+
context.translate()("account.emptyEnv", { name: options.appSecretEnv })
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
const config = await upsertAccount(
|
|
320
|
+
{
|
|
321
|
+
id: options.id,
|
|
322
|
+
platform: "wechat",
|
|
323
|
+
name: options.name,
|
|
324
|
+
appId: options.appId,
|
|
325
|
+
...secret ? { appSecret: secret } : {},
|
|
326
|
+
test: Boolean(options.test)
|
|
327
|
+
},
|
|
328
|
+
context.home()
|
|
329
|
+
);
|
|
330
|
+
const publicConfig = toPublicUserConfig(config);
|
|
331
|
+
success(
|
|
332
|
+
context.io,
|
|
333
|
+
context.json(),
|
|
334
|
+
"account.add",
|
|
335
|
+
{ account: publicConfig.accounts.find((candidate) => candidate.id === options.id) },
|
|
336
|
+
context.translate()("account.saved", { id: options.id })
|
|
337
|
+
);
|
|
338
|
+
});
|
|
339
|
+
account.command("list").description(t("account.list.description")).action(async () => {
|
|
340
|
+
const config = await loadUserConfig(context.home());
|
|
341
|
+
const publicConfig = toPublicUserConfig(config);
|
|
342
|
+
success(
|
|
343
|
+
context.io,
|
|
344
|
+
context.json(),
|
|
345
|
+
"account.list",
|
|
346
|
+
publicConfig,
|
|
347
|
+
context.translate()("account.count", { count: publicConfig.accounts.length })
|
|
348
|
+
);
|
|
349
|
+
if (!context.json()) {
|
|
350
|
+
for (const item of publicConfig.accounts) {
|
|
351
|
+
context.io.stdout(line(`- ${item.id} ${item.name} ${item.appId}`));
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
account.command("remove <id>").description(t("account.remove.description")).action(async (id) => {
|
|
356
|
+
await removeAccount(id, context.home());
|
|
357
|
+
success(
|
|
358
|
+
context.io,
|
|
359
|
+
context.json(),
|
|
360
|
+
"account.remove",
|
|
361
|
+
{ id },
|
|
362
|
+
context.translate()("account.removed", { id })
|
|
363
|
+
);
|
|
364
|
+
});
|
|
365
|
+
account.command("probe <id>").description(t("account.probe.description")).action(async (id) => {
|
|
366
|
+
const credential = await resolveWechatCredential(id, context.home());
|
|
367
|
+
const result = await createDefaultWechatClient(context.home()).probeDraftCapability(credential);
|
|
368
|
+
success(
|
|
369
|
+
context.io,
|
|
370
|
+
context.json(),
|
|
371
|
+
"account.probe",
|
|
372
|
+
{ accountId: id, ...result },
|
|
373
|
+
context.translate()("account.available", { id })
|
|
374
|
+
);
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// src/commands/article.ts
|
|
379
|
+
import { mkdir, writeFile } from "fs/promises";
|
|
380
|
+
import { dirname, join, resolve } from "path";
|
|
381
|
+
|
|
382
|
+
// src/article.ts
|
|
383
|
+
async function buildInput(service, article, sourcePath, io, locale) {
|
|
384
|
+
if (article !== "-") return service.build(article);
|
|
385
|
+
const source = await readStdinSource(sourcePath, io, locale);
|
|
386
|
+
return service.buildSource(source.markdown, source.sourcePath);
|
|
387
|
+
}
|
|
388
|
+
async function readStdinSource(sourcePath, io, locale) {
|
|
389
|
+
if (!sourcePath) {
|
|
390
|
+
throw new FaFaError("CONFIG_INVALID", createCliTranslator(locale)("stdin.sourcePath"));
|
|
391
|
+
}
|
|
392
|
+
return { markdown: await io.readStdin(), sourcePath };
|
|
393
|
+
}
|
|
394
|
+
function assertValid(build, locale) {
|
|
395
|
+
if (hasErrors(build.artifact.diagnostics)) {
|
|
396
|
+
throw new FaFaError("CHECK_FAILED", createCliTranslator(locale)("check.failed"), {
|
|
397
|
+
diagnostics: build.artifact.diagnostics
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// src/context.ts
|
|
403
|
+
function createCliContext(program, io, initialLocale) {
|
|
404
|
+
const globals = () => program.opts();
|
|
405
|
+
const locale = () => resolveCliLocale(globals().language, initialLocale);
|
|
406
|
+
return {
|
|
407
|
+
io,
|
|
408
|
+
globals,
|
|
409
|
+
locale,
|
|
410
|
+
translate: () => createCliTranslator(locale()),
|
|
411
|
+
json: () => Boolean(globals().json),
|
|
412
|
+
home: () => globals().home ?? getFaFaHome(),
|
|
413
|
+
serviceStart: (input) => globals().project ?? input ?? process.cwd()
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
async function withService(startPath, home, callback) {
|
|
417
|
+
const { FaFaService } = await import("./dist-HLWFT422.js");
|
|
418
|
+
const service = await FaFaService.open(startPath, home ? { home } : {});
|
|
419
|
+
try {
|
|
420
|
+
return await callback(service);
|
|
421
|
+
} finally {
|
|
422
|
+
service.close();
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
// src/commands/article.ts
|
|
427
|
+
function registerArticleCommands(program, context) {
|
|
428
|
+
const t = context.translate();
|
|
429
|
+
const check = program.command("check <article>").description(t("check.description"));
|
|
430
|
+
check.option("--source-path <path>", t("sourcePath.description"));
|
|
431
|
+
check.action(async (article) => {
|
|
432
|
+
const options = check.opts();
|
|
433
|
+
const start = context.serviceStart(article === "-" ? options.sourcePath : article);
|
|
434
|
+
await withService(start, context.globals().home, async (service) => {
|
|
435
|
+
const build = await buildInput(service, article, options.sourcePath, context.io, context.locale());
|
|
436
|
+
emitDiagnostics(context.io, build.artifact.diagnostics);
|
|
437
|
+
assertValid(build, context.locale());
|
|
438
|
+
success(
|
|
439
|
+
context.io,
|
|
440
|
+
context.json(),
|
|
441
|
+
"check",
|
|
442
|
+
{
|
|
443
|
+
articleId: build.artifact.articleId,
|
|
444
|
+
buildHash: build.artifact.buildHash,
|
|
445
|
+
diagnostics: build.artifact.diagnostics
|
|
446
|
+
},
|
|
447
|
+
context.translate()("check.success", { articleId: build.artifact.articleId })
|
|
448
|
+
);
|
|
449
|
+
});
|
|
450
|
+
});
|
|
451
|
+
const render = program.command("render <article>").description(t("render.description"));
|
|
452
|
+
render.option("--source-path <path>", t("sourcePath.description")).option("--out <path>", t("render.output"));
|
|
453
|
+
render.action(async (article) => {
|
|
454
|
+
const options = render.opts();
|
|
455
|
+
const start = context.serviceStart(article === "-" ? options.sourcePath : article);
|
|
456
|
+
await withService(start, context.globals().home, async (service) => {
|
|
457
|
+
const build = await buildInput(service, article, options.sourcePath, context.io, context.locale());
|
|
458
|
+
emitDiagnostics(context.io, build.artifact.diagnostics);
|
|
459
|
+
assertValid(build, context.locale());
|
|
460
|
+
const output = options.out ? resolve(options.out) : join(build.project.root, ".fafa", "previews", `${safeFilename(build.artifact.articleId)}.html`);
|
|
461
|
+
await mkdir(dirname(output), { recursive: true });
|
|
462
|
+
await writeFile(output, build.previewHtml, "utf8");
|
|
463
|
+
success(
|
|
464
|
+
context.io,
|
|
465
|
+
context.json(),
|
|
466
|
+
"render",
|
|
467
|
+
{
|
|
468
|
+
articleId: build.artifact.articleId,
|
|
469
|
+
buildHash: build.artifact.buildHash,
|
|
470
|
+
output,
|
|
471
|
+
diagnostics: build.artifact.diagnostics
|
|
472
|
+
},
|
|
473
|
+
context.translate()("render.success", { output })
|
|
474
|
+
);
|
|
475
|
+
});
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
// src/commands/browser.ts
|
|
480
|
+
function registerBrowserCommands(program, context, dependencies = {}) {
|
|
481
|
+
const t = context.translate();
|
|
482
|
+
const browser = program.command("browser").description(t("browser.description"));
|
|
483
|
+
const status = browser.command("status").description(t("browser.status.description"));
|
|
484
|
+
status.option("--browser-path <path>", t("export.browserPath"));
|
|
485
|
+
status.action(async () => {
|
|
486
|
+
const options = status.opts();
|
|
487
|
+
const inspect = dependencies.inspectExportBrowser ?? inspectExportBrowser;
|
|
488
|
+
const result = await inspect(
|
|
489
|
+
context.home(),
|
|
490
|
+
options.browserPath ? { browserPath: options.browserPath } : {}
|
|
491
|
+
);
|
|
492
|
+
success(
|
|
493
|
+
context.io,
|
|
494
|
+
context.json(),
|
|
495
|
+
"browser.status",
|
|
496
|
+
result,
|
|
497
|
+
result.status === "ready" ? context.translate()("browser.status.ready", { source: result.source }) : context.translate()("browser.status.missing")
|
|
498
|
+
);
|
|
499
|
+
});
|
|
500
|
+
const install = browser.command("install").description(t("browser.install.description"));
|
|
501
|
+
install.action(async () => {
|
|
502
|
+
const installBrowser = dependencies.installExportBrowser ?? installExportBrowser;
|
|
503
|
+
const result = await installBrowser(context.home());
|
|
504
|
+
success(
|
|
505
|
+
context.io,
|
|
506
|
+
context.json(),
|
|
507
|
+
"browser.install",
|
|
508
|
+
result,
|
|
509
|
+
context.translate()("browser.install.success", { source: result.source })
|
|
510
|
+
);
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// src/commands/draft.ts
|
|
515
|
+
function registerDraftCommands(program, context) {
|
|
516
|
+
const t = context.translate();
|
|
517
|
+
const draft = program.command("draft").description(t("draft.description"));
|
|
518
|
+
const push = draft.command("push <article>").description(t("draft.push.description"));
|
|
519
|
+
push.option("--account <id>", t("draft.account")).option("--dry-run", t("draft.dryRun"));
|
|
520
|
+
push.action(async (article) => {
|
|
521
|
+
const options = push.opts();
|
|
522
|
+
await withService(context.serviceStart(article), context.globals().home, async (service) => {
|
|
523
|
+
if (options.dryRun) {
|
|
524
|
+
const plan = await service.planDraftPush(article, options.account);
|
|
525
|
+
emitDiagnostics(context.io, plan.diagnostics);
|
|
526
|
+
if (hasErrors(plan.diagnostics)) {
|
|
527
|
+
throw new FaFaError("CHECK_FAILED", context.translate()("draft.planFailed"), {
|
|
528
|
+
diagnostics: plan.diagnostics
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
success(
|
|
532
|
+
context.io,
|
|
533
|
+
context.json(),
|
|
534
|
+
"draft.push.dry-run",
|
|
535
|
+
plan,
|
|
536
|
+
context.translate()("draft.planSuccess", {
|
|
537
|
+
action: plan.action,
|
|
538
|
+
uploads: plan.uploads,
|
|
539
|
+
reused: plan.reusedUploads
|
|
540
|
+
})
|
|
541
|
+
);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
const receipt = await service.pushDraft(article, options.account);
|
|
545
|
+
success(
|
|
546
|
+
context.io,
|
|
547
|
+
context.json(),
|
|
548
|
+
"draft.push",
|
|
549
|
+
receipt,
|
|
550
|
+
context.translate()("draft.pushSuccess", {
|
|
551
|
+
action: receipt.action === "created" ? context.translate()("draft.created") : context.translate()("draft.updated"),
|
|
552
|
+
mediaId: receipt.mediaId
|
|
553
|
+
})
|
|
554
|
+
);
|
|
555
|
+
});
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// src/commands/export.ts
|
|
560
|
+
import { mkdir as mkdir2, writeFile as writeFile2 } from "fs/promises";
|
|
561
|
+
import { dirname as dirname2, join as join2, resolve as resolve2 } from "path";
|
|
562
|
+
function parseAppearance(value, context) {
|
|
563
|
+
if (EXPORT_APPEARANCES.includes(value)) {
|
|
564
|
+
return value;
|
|
565
|
+
}
|
|
566
|
+
throw new FaFaError("CONFIG_INVALID", context.translate()("export.appearanceError", { appearance: value }));
|
|
567
|
+
}
|
|
568
|
+
function parseFormat(value, context) {
|
|
569
|
+
if (EXPORT_FORMATS.includes(value)) return value;
|
|
570
|
+
throw new FaFaError("CONFIG_INVALID", context.translate()("export.formatError", { format: value }));
|
|
571
|
+
}
|
|
572
|
+
function parseWidth(value, format, context) {
|
|
573
|
+
if (value === void 0) return void 0;
|
|
574
|
+
if (format !== "png") {
|
|
575
|
+
throw new FaFaError("CONFIG_INVALID", context.translate()("export.widthFormatError"));
|
|
576
|
+
}
|
|
577
|
+
const width = Number(value);
|
|
578
|
+
if (!Number.isInteger(width) || width < 240 || width > 2e3) {
|
|
579
|
+
throw new FaFaError("CONFIG_INVALID", context.translate()("export.widthError"));
|
|
580
|
+
}
|
|
581
|
+
return width;
|
|
582
|
+
}
|
|
583
|
+
function defaultOutput(root, articleId, format, appearance) {
|
|
584
|
+
return join2(root, ".fafa", "exports", exportFilename(articleId, format, appearance));
|
|
585
|
+
}
|
|
586
|
+
function registerExportCommand(program, context, dependencies = {}) {
|
|
587
|
+
const t = context.translate();
|
|
588
|
+
const command = program.command("export <article>").description(t("export.description"));
|
|
589
|
+
command.option("--format <format>", t("export.format"), "html").option("--appearance <appearance>", t("export.appearance"), "day").option("--out <path>", t("export.output")).option("--width <pixels>", t("export.width")).option("--browser-path <path>", t("export.browserPath")).option("--source-path <path>", t("sourcePath.description"));
|
|
590
|
+
command.action(async (article) => {
|
|
591
|
+
const options = command.opts();
|
|
592
|
+
const format = parseFormat(options.format, context);
|
|
593
|
+
const appearance = parseAppearance(options.appearance, context);
|
|
594
|
+
const width = parseWidth(options.width, format, context);
|
|
595
|
+
const exportArticle = dependencies.exportArticle ?? ((service, articleInput, exportOptions2) => service.exportArticle(articleInput, exportOptions2));
|
|
596
|
+
const exportOptions = {
|
|
597
|
+
format,
|
|
598
|
+
appearance,
|
|
599
|
+
...width === void 0 ? {} : { width },
|
|
600
|
+
...options.browserPath ? { browserPath: options.browserPath } : {}
|
|
601
|
+
};
|
|
602
|
+
const start = context.serviceStart(article === "-" ? options.sourcePath : article);
|
|
603
|
+
await withService(start, context.globals().home, async (service) => {
|
|
604
|
+
const exported = article === "-" ? await readStdinSource(options.sourcePath, context.io, context.locale()).then(
|
|
605
|
+
(source) => service.exportSource(source.markdown, source.sourcePath, exportOptions)
|
|
606
|
+
) : await exportArticle(service, article, exportOptions);
|
|
607
|
+
const output = options.out ? resolve2(options.out) : defaultOutput(service.project.root, exported.articleId, format, appearance);
|
|
608
|
+
await mkdir2(dirname2(output), { recursive: true });
|
|
609
|
+
await writeFile2(output, exported.bytes);
|
|
610
|
+
success(
|
|
611
|
+
context.io,
|
|
612
|
+
context.json(),
|
|
613
|
+
"export",
|
|
614
|
+
{
|
|
615
|
+
format: exported.format,
|
|
616
|
+
appearance: exported.appearance,
|
|
617
|
+
output,
|
|
618
|
+
buildHash: exported.buildHash,
|
|
619
|
+
browserSource: exported.browserSource
|
|
620
|
+
},
|
|
621
|
+
context.translate()("export.success", { format: exported.format.toUpperCase(), output })
|
|
622
|
+
);
|
|
623
|
+
});
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// src/commands/operations.ts
|
|
628
|
+
function registerOperationsCommand(program, context) {
|
|
629
|
+
const t = context.translate();
|
|
630
|
+
const operations = program.command("operations").description(t("operations.description"));
|
|
631
|
+
operations.option("--limit <number>", t("operations.limit"), "50");
|
|
632
|
+
operations.action(async () => {
|
|
633
|
+
const options = operations.opts();
|
|
634
|
+
const limit = Number.parseInt(options.limit, 10);
|
|
635
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 500) {
|
|
636
|
+
throw new FaFaError("CONFIG_INVALID", context.translate()("operations.limitError"));
|
|
637
|
+
}
|
|
638
|
+
const records = await withService(
|
|
639
|
+
context.serviceStart(),
|
|
640
|
+
context.globals().home,
|
|
641
|
+
async (service) => service.state.listOperations(limit)
|
|
642
|
+
);
|
|
643
|
+
success(
|
|
644
|
+
context.io,
|
|
645
|
+
context.json(),
|
|
646
|
+
"operations",
|
|
647
|
+
{ operations: records },
|
|
648
|
+
context.translate()("operations.count", { count: records.length })
|
|
649
|
+
);
|
|
650
|
+
if (!context.json()) {
|
|
651
|
+
for (const record of records) {
|
|
652
|
+
context.io.stdout(line(`- ${record.status} ${record.type} ${record.id}`));
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
});
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
// src/commands/project.ts
|
|
659
|
+
import { resolve as resolve3 } from "path";
|
|
660
|
+
function registerProjectCommands(program, context) {
|
|
661
|
+
const t = context.translate();
|
|
662
|
+
program.command("init [directory]").description(t("init.description")).action(async (directory = ".") => {
|
|
663
|
+
const project = await createProject(resolve3(directory));
|
|
664
|
+
success(
|
|
665
|
+
context.io,
|
|
666
|
+
context.json(),
|
|
667
|
+
"init",
|
|
668
|
+
{ root: project.root, configPath: project.configPath },
|
|
669
|
+
context.translate()("init.success", { root: project.root })
|
|
670
|
+
);
|
|
671
|
+
});
|
|
672
|
+
program.command("articles").description(t("articles.description")).action(async () => {
|
|
673
|
+
const articles = await withService(
|
|
674
|
+
context.serviceStart(),
|
|
675
|
+
context.globals().home,
|
|
676
|
+
(service) => service.listArticles()
|
|
677
|
+
);
|
|
678
|
+
success(
|
|
679
|
+
context.io,
|
|
680
|
+
context.json(),
|
|
681
|
+
"articles",
|
|
682
|
+
{ articles },
|
|
683
|
+
context.translate()("articles.count", { count: articles.length })
|
|
684
|
+
);
|
|
685
|
+
if (!context.json()) {
|
|
686
|
+
for (const article of articles) context.io.stdout(line(`- ${article.id} ${article.path}`));
|
|
687
|
+
}
|
|
688
|
+
});
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// src/commands/style.ts
|
|
692
|
+
function registerStyleCommands(program, context) {
|
|
693
|
+
const t = context.translate();
|
|
694
|
+
const theme = program.command("theme").description(t("theme.description"));
|
|
695
|
+
theme.command("list").description(t("theme.list.description")).action(() => {
|
|
696
|
+
const presets = listWechatThemePresets(context.locale());
|
|
697
|
+
success(
|
|
698
|
+
context.io,
|
|
699
|
+
context.json(),
|
|
700
|
+
"theme.list",
|
|
701
|
+
{ presets },
|
|
702
|
+
context.translate()("theme.list.count", { count: presets.length })
|
|
703
|
+
);
|
|
704
|
+
if (!context.json()) {
|
|
705
|
+
for (const preset of presets) {
|
|
706
|
+
context.io.stdout(line(`- ${preset.id} ${preset.name} ${preset.description}`));
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
const themeSet = theme.command("set").description(t("theme.set.description"));
|
|
711
|
+
themeSet.option("--preset <id>", t("theme.set.preset")).option("--file <path>", t("theme.set.file"));
|
|
712
|
+
themeSet.action(async () => {
|
|
713
|
+
const options = themeSet.opts();
|
|
714
|
+
if (Boolean(options.preset) === Boolean(options.file)) {
|
|
715
|
+
throw new FaFaError("CONFIG_INVALID", context.translate()("theme.set.choice"));
|
|
716
|
+
}
|
|
717
|
+
const project = await loadProject(context.serviceStart());
|
|
718
|
+
if (options.preset && !listWechatThemePresets(context.locale()).some(({ id }) => id === options.preset)) {
|
|
719
|
+
throw new FaFaError("CONFIG_INVALID", context.translate()("theme.set.unknown", { id: options.preset }));
|
|
720
|
+
}
|
|
721
|
+
const selected = await resolveWechatTheme(
|
|
722
|
+
project.root,
|
|
723
|
+
options.preset ?? options.file,
|
|
724
|
+
context.locale()
|
|
725
|
+
);
|
|
726
|
+
const updated = await setProjectDefaultTheme(project.root, selected.reference);
|
|
727
|
+
success(
|
|
728
|
+
context.io,
|
|
729
|
+
context.json(),
|
|
730
|
+
"theme.set",
|
|
731
|
+
{
|
|
732
|
+
reference: selected.reference,
|
|
733
|
+
source: selected.source,
|
|
734
|
+
name: selected.name,
|
|
735
|
+
configPath: updated.configPath
|
|
736
|
+
},
|
|
737
|
+
context.translate()("theme.set.success", { reference: selected.reference })
|
|
738
|
+
);
|
|
739
|
+
});
|
|
740
|
+
const codeTheme = program.command("code-theme").description(t("codeTheme.description"));
|
|
741
|
+
codeTheme.command("list").description(t("codeTheme.list.description")).action(() => {
|
|
742
|
+
const presets = listCodeHighlightThemes(context.locale());
|
|
743
|
+
success(
|
|
744
|
+
context.io,
|
|
745
|
+
context.json(),
|
|
746
|
+
"code-theme.list",
|
|
747
|
+
{ presets },
|
|
748
|
+
context.translate()("codeTheme.list.count", { count: presets.length })
|
|
749
|
+
);
|
|
750
|
+
if (!context.json()) {
|
|
751
|
+
for (const preset of presets) {
|
|
752
|
+
context.io.stdout(line(`- ${preset.id} ${preset.name} ${preset.description}`));
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
const codeThemeSet = codeTheme.command("set").description(t("codeTheme.set.description")).requiredOption("--preset <id>", t("codeTheme.set.preset"));
|
|
757
|
+
codeThemeSet.action(async () => {
|
|
758
|
+
const { preset } = codeThemeSet.opts();
|
|
759
|
+
const selected = resolveCodeHighlightTheme(preset, context.locale());
|
|
760
|
+
const project = await loadProject(context.serviceStart());
|
|
761
|
+
const updated = await setProjectDefaultCodeTheme(project.root, selected.id);
|
|
762
|
+
success(
|
|
763
|
+
context.io,
|
|
764
|
+
context.json(),
|
|
765
|
+
"code-theme.set",
|
|
766
|
+
{
|
|
767
|
+
reference: selected.id,
|
|
768
|
+
name: selected.name,
|
|
769
|
+
configPath: updated.configPath
|
|
770
|
+
},
|
|
771
|
+
context.translate()("codeTheme.set.success", { reference: selected.id })
|
|
772
|
+
);
|
|
773
|
+
});
|
|
774
|
+
const imageCaption = program.command("image-caption").description(t("caption.description"));
|
|
775
|
+
imageCaption.command("list").description(t("caption.list.description")).action(() => {
|
|
776
|
+
const modes = listImageCaptionModes(context.locale());
|
|
777
|
+
success(
|
|
778
|
+
context.io,
|
|
779
|
+
context.json(),
|
|
780
|
+
"image-caption.list",
|
|
781
|
+
{ modes },
|
|
782
|
+
context.translate()("caption.list.count", { count: modes.length })
|
|
783
|
+
);
|
|
784
|
+
if (!context.json()) {
|
|
785
|
+
for (const mode of modes) {
|
|
786
|
+
context.io.stdout(line(`- ${mode.id} ${mode.name} ${mode.description}`));
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
});
|
|
790
|
+
const imageCaptionSet = imageCaption.command("set").description(t("caption.set.description")).requiredOption("--mode <id>", t("caption.set.mode"));
|
|
791
|
+
imageCaptionSet.action(async () => {
|
|
792
|
+
const { mode } = imageCaptionSet.opts();
|
|
793
|
+
const selected = resolveImageCaptionMode(mode);
|
|
794
|
+
const project = await loadProject(context.serviceStart());
|
|
795
|
+
const updated = await setProjectDefaultImageCaption(project.root, selected);
|
|
796
|
+
const summary = listImageCaptionModes(context.locale()).find(({ id }) => id === selected);
|
|
797
|
+
success(
|
|
798
|
+
context.io,
|
|
799
|
+
context.json(),
|
|
800
|
+
"image-caption.set",
|
|
801
|
+
{
|
|
802
|
+
reference: selected,
|
|
803
|
+
name: summary?.name ?? selected,
|
|
804
|
+
configPath: updated.configPath
|
|
805
|
+
},
|
|
806
|
+
context.translate()("caption.set.success", { reference: selected })
|
|
807
|
+
);
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
// src/program.ts
|
|
812
|
+
function createCli(io = defaultIo, initialLocale = localeFromEnvironment(process.env), dependencies = {}) {
|
|
813
|
+
const t = createCliTranslator(initialLocale);
|
|
814
|
+
const program = new Command();
|
|
815
|
+
program.name("fafa").description(t("app.description")).version(package_default.version).option("--json", t("option.json")).option("--project <path>", t("option.project")).option("--home <path>", t("option.home")).option("--language <locale>", t("option.language")).showHelpAfterError().exitOverride().configureOutput({
|
|
816
|
+
writeOut: (message) => io.stdout(message),
|
|
817
|
+
writeErr: (message) => io.stderr(message)
|
|
818
|
+
});
|
|
819
|
+
const context = createCliContext(program, io, initialLocale);
|
|
820
|
+
registerProjectCommands(program, context);
|
|
821
|
+
registerStyleCommands(program, context);
|
|
822
|
+
registerArticleCommands(program, context);
|
|
823
|
+
registerExportCommand(program, context, dependencies);
|
|
824
|
+
registerBrowserCommands(program, context, dependencies);
|
|
825
|
+
registerDraftCommands(program, context);
|
|
826
|
+
registerOperationsCommand(program, context);
|
|
827
|
+
registerAccountCommands(program, context);
|
|
828
|
+
return program;
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// src/index.ts
|
|
832
|
+
async function runCli(argv = process.argv, io = defaultIo, dependencies = {}) {
|
|
833
|
+
const json = argv.includes("--json");
|
|
834
|
+
const environmentLocale = localeFromEnvironment(process.env);
|
|
835
|
+
try {
|
|
836
|
+
const locale = resolveCliLocale(languageFromArgv(argv), environmentLocale);
|
|
837
|
+
await createCli(io, locale, dependencies).parseAsync(argv, { from: "node" });
|
|
838
|
+
return 0;
|
|
839
|
+
} catch (error) {
|
|
840
|
+
const code = exitCode(error);
|
|
841
|
+
failure(io, json, error, code);
|
|
842
|
+
return code;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
export {
|
|
847
|
+
createCli,
|
|
848
|
+
runCli
|
|
849
|
+
};
|