@tnotesjs/core 0.1.0 → 0.1.1

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.

Potentially problematic release.


This version of @tnotesjs/core might be problematic. Click here for more details.

@@ -0,0 +1,207 @@
1
+ #!/usr/bin/env node
2
+
3
+ // config/ConfigManager.ts
4
+ import fs from "fs";
5
+ import path from "path";
6
+ import { fileURLToPath } from "url";
7
+
8
+ // config/defaultConfig.ts
9
+ function getDefaultConfig(repoName) {
10
+ const name = repoName || "TNotes.default";
11
+ const cleanName = name.replace("TNotes.", "");
12
+ return {
13
+ // 基础信息
14
+ author: "tnotesjs",
15
+ repoName: name,
16
+ keywords: [name],
17
+ id: "",
18
+ // 由外部生成的 UUID
19
+ // Sidebar 配置
20
+ sidebarShowNoteId: true,
21
+ // 忽略目录
22
+ ignore_dirs: [
23
+ ".vscode",
24
+ "0000",
25
+ "assets",
26
+ "node_modules",
27
+ "hidden",
28
+ "demos"
29
+ ],
30
+ // 根目录项配置
31
+ root_item: {
32
+ icon: {
33
+ src: `https://cdn.jsdelivr.net/gh/tnotesjs/imgs@main/assets/icon--${cleanName}.svg`
34
+ },
35
+ title: cleanName,
36
+ completed_notes_count: {},
37
+ details: `${name} \u77E5\u8BC6\u5E93`,
38
+ link: `https://tnotesjs.github.io/${name}/`,
39
+ created_at: Date.now(),
40
+ updated_at: Date.now(),
41
+ days_since_birth: 0
42
+ },
43
+ // 端口配置(根据仓库名生成)
44
+ port: 8e3,
45
+ // 菜单项
46
+ menuItems: [
47
+ {
48
+ text: "\u{1F3E0} Home",
49
+ link: "/"
50
+ },
51
+ {
52
+ text: "\u2699\uFE0F Settings",
53
+ link: "/Settings"
54
+ },
55
+ {
56
+ text: "\u{1F4D2} TNotes",
57
+ link: "https://tnotesjs.github.io/notes"
58
+ },
59
+ {
60
+ text: "\u{1F4C2} TNotes.yuque",
61
+ link: "https://www.yuque.com/tdahuyou/tnotes.yuque"
62
+ }
63
+ ],
64
+ // 社交链接
65
+ socialLinks: [
66
+ {
67
+ ariaLabel: "Tdahuyou \u8BED\u96C0\u4E3B\u9875\u94FE\u63A5",
68
+ link: "https://www.yuque.com/tdahuyou",
69
+ icon: {
70
+ svg: '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="currentColor" d="M17.28 2.955c2.97.203 3.756 2.342 3.84 2.597l1.297.096c.13 0 .169.18.054.236c-1.323.716-1.727 2.17-1.49 3.118c.09.358.254.69.412 1.02c.307.642.651 1.418.707 2.981c.117 3.24-2.51 6.175-5.789 6.593c1.17-1.187 1.815-2.444 2.12-3.375c.606-1.846.508-3.316.055-4.44a4.46 4.46 0 0 0-1.782-2.141c-1.683-1.02-3.22-1.09-4.444-.762c.465-.594.876-1.201 1.2-1.864c.584-1.65-.102-2.848-.704-3.519c-.192-.246-.061-.655.305-.655c1.41 0 2.813.02 4.22.115M3.32 19.107c1.924-2.202 4.712-5.394 7.162-8.15c.559-.63 2.769-2.338 5.748-.533c.878.532 2.43 2.165 1.332 5.51c-.803 2.446-4.408 7.796-15.76 5.844c-.227-.039-.511-.354-.218-.687z"/></svg>'
71
+ }
72
+ },
73
+ {
74
+ ariaLabel: "Tdahuyou B \u7AD9\u4E3B\u9875\u94FE\u63A5",
75
+ link: "https://space.bilibili.com/407241004",
76
+ icon: {
77
+ svg: '<svg xmlns="http://www.w3.org/2000/svg" width="1024" height="1024" viewBox="0 0 1024 1024"><g fill="currentColor"><path d="M310.134 596.45c-7.999-4.463-16.498-8.43-24.997-11.9a274 274 0 0 0-26.996-7.438c-2.5-.992-2.5.991-2.5 1.487c0 7.934.5 18.843 1.5 27.768c1 7.438 2 15.372 4 22.81c0 .496 0 .991.5 1.487c.999.992 1.999 1.488 2.999.496c7.999-4.463 15.998-8.43 22.997-13.388c7.499-5.454 15.498-11.9 21.997-18.347c1.5-1.487 0-2.479.5-2.975m323.96-11.9a274 274 0 0 0-26.997-7.438c-2.5-.992-2.5.991-2.5 1.487c0 7.934.5 18.843 1.5 27.768c1 7.438 2 15.372 4 22.81c0 .496 0 .991.5 1.487c1 .992 2 1.488 3 .496c7.999-4.463 15.998-8.43 22.997-13.388c7.499-5.454 15.498-11.9 21.997-18.347c2-1.487.5-2.479.5-2.975c-7.5-4.463-16.498-8.43-24.997-11.9"/><path d="M741.496 112H283c-94.501 0-171 76.5-171 171.5v458c.5 94 77 170.5 170.999 170.5h457.997c94.5 0 171.002-76.5 171.002-170.5v-458c.497-95-76.002-171.5-170.502-171.5m95 343.5h15.5v48h-15.5zm-95.5-1.5l2 43l-13.5 1.5l-5-44.5zm-23.5 0l4 45.5l-14.5 1.5l-6.5-47.5h17zm-230.498 1.5h15v48h-15zm-96-1.5l2 43l-13.5 1.5l-5-44.5zm-23.5 0l4 45.5l-14.5 2l-6-47.5zm-3.5 149C343.498 668.5 216 662.5 204.5 660.5C195.5 499 181.5 464 170 385l54.5-22.5c1 71.5 9 185 9 185s108.5-15.5 132 47c.5 3 0 6-1.5 8.5m20.5 35.5l-23.5-124h35.5l13 123zm44.5-8l-27-235l33.5-1.5l21 236H429zm34-175h17.5v48H467zm41 190h-26.5l-9.5-126h36zm209.998-43C693.496 668 565.997 662 554.497 660c-9-161-23-196-34.5-275l54.5-22.5c1 71.5 9 185 9 185s108.5-15.5 132 46.5c.5 3 0 6-1.5 8.5m19.5 36l-23-124h35.5l13 123zm45.5-8l-27.5-235l33.5-1.5l21 236h-27zm33.5-175h17.5v48h-13zm41 190h-26.5l-9.5-126h36z"/></g></svg>'
78
+ }
79
+ },
80
+ {
81
+ ariaLabel: `${name} github \u4ED3\u5E93\u94FE\u63A5`,
82
+ link: `https://github.com/tnotesjs/${name}`,
83
+ icon: "github"
84
+ }
85
+ ]
86
+ };
87
+ }
88
+ function mergeConfig(target, source) {
89
+ const result = { ...target };
90
+ for (const key in source) {
91
+ if (!(key in result)) {
92
+ result[key] = source[key];
93
+ } else if (typeof source[key] === "object" && source[key] !== null && !Array.isArray(source[key])) {
94
+ result[key] = mergeConfig(result[key] || {}, source[key]);
95
+ }
96
+ }
97
+ return result;
98
+ }
99
+ function validateAndCompleteConfig(config) {
100
+ const repoName = config.repoName || "TNotes.default";
101
+ const defaultConfig = getDefaultConfig(repoName);
102
+ const mergedConfig = mergeConfig(config, defaultConfig);
103
+ const modified = JSON.stringify(config) !== JSON.stringify(mergedConfig);
104
+ return {
105
+ config: mergedConfig,
106
+ modified
107
+ };
108
+ }
109
+
110
+ // config/ConfigManager.ts
111
+ var ConfigManager = class _ConfigManager {
112
+ static instance;
113
+ config = null;
114
+ configPath;
115
+ rootPath;
116
+ constructor(rootPath) {
117
+ if (rootPath) {
118
+ this.rootPath = rootPath;
119
+ } else {
120
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
121
+ this.rootPath = path.resolve(__dirname, "..", "..", "..");
122
+ }
123
+ this.configPath = path.normalize(
124
+ path.resolve(this.rootPath, ".tnotes.json")
125
+ );
126
+ }
127
+ /**
128
+ * 使用指定的根路径初始化配置管理器
129
+ *
130
+ * 必须在 getInstance() 之前调用(如果需要指定 rootPath)。
131
+ * 如果已经初始化,则忽略重复调用。
132
+ */
133
+ static init({ rootPath }) {
134
+ if (!_ConfigManager.instance) {
135
+ _ConfigManager.instance = new _ConfigManager(rootPath);
136
+ }
137
+ return _ConfigManager.instance;
138
+ }
139
+ /**
140
+ * 获取单例实例
141
+ */
142
+ static getInstance() {
143
+ if (!_ConfigManager.instance) {
144
+ _ConfigManager.instance = new _ConfigManager();
145
+ }
146
+ return _ConfigManager.instance;
147
+ }
148
+ /**
149
+ * 加载配置文件
150
+ */
151
+ loadConfig(configPath) {
152
+ if (this.config) return this.config;
153
+ const path2 = configPath || this.configPath;
154
+ if (!fs.existsSync(path2)) {
155
+ throw new Error(`\u914D\u7F6E\u6587\u4EF6\u4E0D\u5B58\u5728: ${path2}`);
156
+ }
157
+ const configContent = fs.readFileSync(path2, "utf-8");
158
+ const rawConfig = JSON.parse(configContent);
159
+ const { config: validatedConfig, modified } = validateAndCompleteConfig(rawConfig);
160
+ if (modified) {
161
+ console.warn(`[ConfigManager] \u68C0\u6D4B\u5230\u77E5\u8BC6\u5E93\u914D\u7F6E\u7F3A\u5931\u5B57\u6BB5\uFF0C\u5DF2\u81EA\u52A8\u8865\u5168`);
162
+ fs.writeFileSync(path2, JSON.stringify(validatedConfig, null, 2), "utf-8");
163
+ console.log(`[ConfigManager] \u77E5\u8BC6\u5E93\u914D\u7F6E\u6587\u4EF6\u5DF2\u66F4\u65B0`);
164
+ console.log(`[ConfigManager] \u77E5\u8BC6\u5E93\u914D\u7F6E\u6587\u4EF6\u8DEF\u5F84: ${path2}`);
165
+ }
166
+ this.config = validatedConfig;
167
+ return this.config;
168
+ }
169
+ /**
170
+ * 获取配置项
171
+ */
172
+ get(key) {
173
+ if (!this.config) {
174
+ this.loadConfig();
175
+ }
176
+ return this.config[key];
177
+ }
178
+ /**
179
+ * 获取所有配置
180
+ */
181
+ getAll() {
182
+ if (!this.config) {
183
+ this.loadConfig();
184
+ }
185
+ return this.config;
186
+ }
187
+ /**
188
+ * 获取项目根路径
189
+ */
190
+ getRootPath() {
191
+ return this.rootPath;
192
+ }
193
+ /**
194
+ * @deprecated 使用 getRootPath() 替代
195
+ */
196
+ getDirname() {
197
+ return path.resolve(this.rootPath, ".vitepress", "tnotes", "config");
198
+ }
199
+ };
200
+ function getConfigManager() {
201
+ return ConfigManager.getInstance();
202
+ }
203
+
204
+ export {
205
+ ConfigManager,
206
+ getConfigManager
207
+ };