autosnippet 1.1.14 → 1.1.15

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/bin/asnip.js CHANGED
@@ -17,18 +17,9 @@ const share = require('./share.js');
17
17
  const init = require('./init.js');
18
18
  const config = require('./config.js');
19
19
 
20
- // 获取配置文件路径(测试模式优先使用 BiliDemo 配置)
20
+ // 获取配置文件路径
21
21
  function getSpecFile(callback) {
22
- // 测试模式下,优先使用 BiliDemo 的配置文件
23
- if (config.isTestMode()) {
24
- const configPath = config.getConfigPath();
25
- if (configPath && fs.existsSync(configPath)) {
26
- callback(configPath);
27
- return;
28
- }
29
- }
30
-
31
- // 否则使用原有的查找逻辑
22
+ // 向上查找 AutoSnippet.boxspec.json 配置文件
32
23
  findPath.findASSpecPath(CMD_PATH, callback);
33
24
  }
34
25
 
package/bin/config.js CHANGED
@@ -3,87 +3,37 @@
3
3
  const path = require('path');
4
4
  const fs = require('fs');
5
5
 
6
- // 检测是否为测试模式
7
- // 通过环境变量 AUTOSNIPPET_TEST_MODE 或检测 BiliDemo 目录
8
- function isTestMode() {
9
- // 优先检查环境变量
10
- if (process.env.AUTOSNIPPET_TEST_MODE === 'true') {
11
- return true;
12
- }
13
-
14
- // 检查当前工作目录是否在 BiliDemo 项目内
15
- const cwd = process.cwd();
16
- if (cwd.includes('BiliDemo')) {
17
- return true;
18
- }
19
-
20
- return false;
21
- }
22
-
23
- // 获取 BiliDemo 项目根目录
24
- function getBiliDemoRoot() {
25
- const cwd = process.cwd();
26
- const parts = cwd.split(path.sep);
6
+ // 获取代码片段输出路径(写入 Xcode CodeSnippets 目录)
7
+ function getSnippetsPath() {
8
+ const USER_HOME = process.env.HOME || process.env.USERPROFILE;
9
+ const snippetsPath = path.join(USER_HOME, 'Library/Developer/Xcode/UserData/CodeSnippets');
27
10
 
28
- for (let i = parts.length; i > 0; i--) {
29
- const testPath = parts.slice(0, i).join(path.sep);
30
- if (fs.existsSync(path.join(testPath, 'BiliDili', 'AutoSnippet.boxspec.json'))) {
31
- return testPath;
32
- }
11
+ // 确保目录存在
12
+ try {
13
+ fs.accessSync(snippetsPath, fs.constants.F_OK);
14
+ } catch (err) {
15
+ fs.mkdirSync(snippetsPath, { recursive: true });
33
16
  }
34
17
 
35
- return null;
18
+ return snippetsPath;
36
19
  }
37
20
 
38
- // 获取配置文件路径
39
- function getConfigPath() {
40
- if (isTestMode()) {
41
- const biliDemoRoot = getBiliDemoRoot();
42
- if (biliDemoRoot) {
43
- return path.join(biliDemoRoot, 'BiliDili', 'AutoSnippet.boxspec.json');
44
- }
45
- }
46
- return null;
47
- }
48
-
49
- // 获取代码片段输出路径(测试模式写入 AutoSnippetCache,生产模式写入 Xcode)
50
- function getSnippetsPath() {
51
- if (isTestMode()) {
52
- // ✅ 测试模式:写入 AutoSnippetCache/Snippets/
53
- // __dirname: AutoSnippet/bin
54
- // 目标路径: AutoSnippet/AutoSnippetCache/Snippets
55
- const cachePath = path.join(__dirname, '../../AutoSnippetCache/Snippets');
56
- try {
57
- fs.accessSync(cachePath, fs.constants.F_OK);
58
- } catch (err) {
59
- fs.mkdirSync(cachePath, { recursive: true });
60
- }
61
- return cachePath;
62
- } else {
63
- // 生产模式:写入 Xcode CodeSnippets 目录
64
- const USER_HOME = process.env.HOME || process.env.USERPROFILE;
65
- return path.join(USER_HOME, 'Library/Developer/Xcode/UserData/CodeSnippets');
66
- }
67
- }
68
-
69
- // 获取缓存目录路径
21
+ // 获取缓存目录路径(使用用户 home 目录下的缓存)
70
22
  function getCachePath() {
71
- // 缓存始终在 AutoSnippetCache 目录
72
- // __dirname: AutoSnippet/bin
73
- // 目标路径: AutoSnippet/AutoSnippetCache
74
- const cachePath = path.join(__dirname, '../../AutoSnippetCache');
23
+ const USER_HOME = process.env.HOME || process.env.USERPROFILE;
24
+ const cachePath = path.join(USER_HOME, '.autosnippet', 'cache');
25
+
26
+ // 确保目录存在
75
27
  try {
76
28
  fs.accessSync(cachePath, fs.constants.F_OK);
77
29
  } catch (err) {
78
30
  fs.mkdirSync(cachePath, { recursive: true });
79
31
  }
32
+
80
33
  return cachePath;
81
34
  }
82
35
 
83
36
  module.exports = {
84
- isTestMode,
85
- getBiliDemoRoot,
86
- getConfigPath,
87
37
  getSnippetsPath,
88
38
  getCachePath
89
39
  };
package/bin/create.js CHANGED
@@ -229,9 +229,6 @@ function readStream(specFile, filePathArr, snippet, isHaveHeader) {
229
229
  const fileName = filePath.substring(slashIndex + 1, dotIndex + 1) + 'h';
230
230
  const thePath = filePath.substring(0, slashIndex + 1);
231
231
 
232
- const specSlashIndex = specFile.lastIndexOf('/');
233
- const specFilePath = specFile.substring(0, specSlashIndex + 1);
234
-
235
232
  // ✅ 使用 SPM 的 Package.swift 查找(替代 .boxspec)
236
233
  findPath.findPackageSwiftPath(thePath).then(async function (packagePath) {
237
234
  if (!packagePath) {
@@ -261,14 +258,20 @@ function readStream(specFile, filePathArr, snippet, isHaveHeader) {
261
258
  snippet['{headName}'] = fileName;
262
259
 
263
260
  if (headerPath) {
264
- snippet['{specHeadPath}'] = encodeURI(headerPath.replace(specFilePath, ''));
261
+ // 使用 path.relative() 计算相对于配置文件的相对路径
262
+ const specFileDir = path.dirname(specFile);
263
+ const relativePath = path.relative(specFileDir, headerPath);
264
+ snippet['{specHeadPath}'] = encodeURI(relativePath);
265
265
  }
266
266
 
267
267
  // 查找 README.md
268
268
  try {
269
269
  const readmePath = path.join(packageInfo.path, README_NAME);
270
270
  await fs.promises.access(readmePath);
271
- snippet['{readme}'] = encodeURI(readmePath.replace(specFilePath, ''));
271
+ // 使用 path.relative() 计算相对于配置文件的相对路径
272
+ const specFileDir = path.dirname(specFile);
273
+ const readmeRelativePath = path.relative(specFileDir, readmePath);
274
+ snippet['{readme}'] = encodeURI(readmeRelativePath);
272
275
  } catch {
273
276
  // README.md 不存在,跳过
274
277
  }
package/bin/findPath.js CHANGED
@@ -51,46 +51,77 @@ async function getDirectoryEntries(dirPath) {
51
51
  }
52
52
  }
53
53
 
54
- // 向上查找AutoSnippet配置文件(优化:找到配置文件后,最多再向上查找两层停止)
54
+ /**
55
+ * 检测是否为工程根目录
56
+ * 通过查找常见的工程根目录标记来判断
57
+ */
58
+ function isProjectRoot(dirPath, entries) {
59
+ if (!entries) {
60
+ return false;
61
+ }
62
+
63
+ // 工程根目录标记(按优先级)
64
+ const rootMarkers = [
65
+ '.git', // Git 仓库根目录
66
+ PACKAGE_SWIFT, // SPM 项目根目录
67
+ '.xcodeproj', // Xcode 项目根目录
68
+ '.xcworkspace', // Xcode 工作空间根目录
69
+ 'Podfile', // CocoaPods 项目根目录
70
+ '.swiftpm', // Swift Package Manager 元数据目录
71
+ ];
72
+
73
+ for (const entry of entries) {
74
+ const name = entry.name;
75
+
76
+ // 检查文件标记
77
+ if (entry.isFile() && rootMarkers.includes(name)) {
78
+ return true;
79
+ }
80
+
81
+ // 检查目录标记(.git 可能是目录)
82
+ if (entry.isDirectory() && rootMarkers.includes(name)) {
83
+ return true;
84
+ }
85
+ }
86
+
87
+ return false;
88
+ }
89
+
90
+ // 向上查找AutoSnippet配置文件(优化:找到配置文件后,继续向上查找直到找到工程根目录)
55
91
  async function findASSpecPathAsync(filePath) {
56
92
  let configPath = null;
57
- let levelsUp = 0;
93
+ let configDir = null;
58
94
  let currentPath = path.resolve(filePath);
95
+ const maxLevels = 20; // 安全限制:最多向上查找 20 层
96
+ let levelsChecked = 0;
59
97
 
60
- while (currentPath) {
98
+ while (currentPath && levelsChecked < maxLevels) {
61
99
  try {
62
100
  const entries = await getDirectoryEntries(currentPath);
63
- if (!entries) {
64
- // 目录不存在,继续向上
65
- const parentPath = path.dirname(currentPath);
66
- if (parentPath === currentPath) {
67
- break; // 已到达根目录
68
- }
69
- currentPath = parentPath;
70
- continue;
71
- }
72
101
 
73
102
  // 查找 AutoSnippet.boxspec.json
74
- for (const entry of entries) {
75
- if (entry.isFile() && entry.name === HOLDER_NAME) {
76
- if (!configPath) {
77
- // 第一次找到配置文件
78
- configPath = path.join(currentPath, entry.name);
103
+ if (entries) {
104
+ for (const entry of entries) {
105
+ if (entry.isFile() && entry.name === HOLDER_NAME) {
106
+ if (!configPath) {
107
+ // 第一次找到配置文件
108
+ configPath = path.join(currentPath, entry.name);
109
+ configDir = currentPath;
110
+ }
79
111
  }
80
- // 找到后,标记为已找到,继续向上最多两层
81
112
  }
82
113
  }
83
114
 
84
- // 如果已经找到配置文件,检查是否已经向上查找了两层
115
+ // 如果已经找到配置文件,检查是否到达工程根目录
85
116
  if (configPath) {
86
- if (path.resolve(currentPath) !== path.dirname(configPath)) {
87
- // 当前目录不是配置文件所在目录,说明已经向上查找了
88
- levelsUp++;
117
+ // 如果在配置文件所在目录或以上发现了工程根目录标记,停止查找
118
+ if (isProjectRoot(currentPath, entries)) {
119
+ return configPath;
89
120
  }
90
121
 
91
- // 如果已经向上查找了两层,停止查找
92
- if (levelsUp >= 2) {
93
- return configPath;
122
+ // 如果已经向上查找了,但没有找到工程根目录标记,继续查找
123
+ if (path.resolve(currentPath) !== configDir) {
124
+ // 当前目录不是配置文件所在目录,继续向上查找
94
125
  }
95
126
  }
96
127
 
@@ -101,6 +132,7 @@ async function findASSpecPathAsync(filePath) {
101
132
  break;
102
133
  }
103
134
  currentPath = parentPath;
135
+ levelsChecked++;
104
136
 
105
137
  } catch (err) {
106
138
  // 错误处理:权限错误继续查找
@@ -110,6 +142,7 @@ async function findASSpecPathAsync(filePath) {
110
142
  break;
111
143
  }
112
144
  currentPath = parentPath;
145
+ levelsChecked++;
113
146
  continue;
114
147
  }
115
148
  // 其他错误,抛出
@@ -117,73 +150,134 @@ async function findASSpecPathAsync(filePath) {
117
150
  }
118
151
  }
119
152
 
153
+ // 如果找到配置文件,返回它(即使没有找到明确的工程根目录)
120
154
  return configPath || null;
121
155
  }
122
156
 
157
+ /**
158
+ * 同步版本:检测是否为工程根目录
159
+ */
160
+ function isProjectRootSync(dirPath, files) {
161
+ if (!files || files.length === 0) {
162
+ return false;
163
+ }
164
+
165
+ // 工程根目录标记(按优先级)
166
+ const rootMarkers = [
167
+ '.git', // Git 仓库根目录
168
+ PACKAGE_SWIFT, // SPM 项目根目录
169
+ '.xcodeproj', // Xcode 项目根目录
170
+ '.xcworkspace', // Xcode 工作空间根目录
171
+ 'Podfile', // CocoaPods 项目根目录
172
+ ];
173
+
174
+ // 检查文件名
175
+ for (const filename of files) {
176
+ if (rootMarkers.includes(filename)) {
177
+ // 检查是否为文件或目录
178
+ try {
179
+ const filePath = path.join(dirPath, filename);
180
+ const stats = fs.lstatSync(filePath);
181
+ if (stats.isFile() || stats.isDirectory()) {
182
+ return true;
183
+ }
184
+ } catch (err) {
185
+ // 忽略错误,继续检查
186
+ }
187
+ }
188
+ }
189
+
190
+ return false;
191
+ }
192
+
123
193
  // 向上查找AutoSnippet配置文件(保留回调版本,兼容现有代码)
124
- function findASSpecPath(filePath, callback, configPath, levelsUp) {
194
+ function findASSpecPath(filePath, callback, configPath, configDir) {
125
195
  // 初始化参数
126
196
  if (configPath === undefined) configPath = null;
127
- if (levelsUp === undefined) levelsUp = 0;
197
+ if (configDir === undefined) configDir = null;
128
198
 
129
- fs.readdir(filePath, function (err, files) {
130
- if (err) {
131
- console.log(err);
199
+ const maxLevels = 20; // 安全限制:最多向上查找 20 层
200
+ let levelsChecked = 0;
201
+
202
+ function search(currentPath, foundConfigPath, foundConfigDir, level) {
203
+ if (level >= maxLevels) {
204
+ if (foundConfigPath) {
205
+ callback(foundConfigPath);
206
+ } else {
207
+ console.log('未找到 AutoSnippet.boxspec.json 文件,请检查路径。');
208
+ }
132
209
  return;
133
210
  }
134
-
135
- let isFound = false;
136
- let foundConfigPath = configPath;
137
- files.forEach(function (filename) {
138
- if (filename === HOLDER_NAME) {
139
- isFound = true;
140
- if (!foundConfigPath) {
141
- // 第一次找到配置文件,记录路径并调用回调
142
- foundConfigPath = path.resolve(filePath);
143
- callback(path.join(filePath, filename));
211
+
212
+ fs.readdir(currentPath, function (err, files) {
213
+ if (err) {
214
+ // 错误处理:权限错误继续查找
215
+ if (err.code === 'ENOENT' || err.code === 'EACCES') {
216
+ const parentPath = path.join(currentPath, '/..');
217
+ const parentResolvedPath = path.resolve(parentPath);
218
+
219
+ // 如果已经到达根目录,停止查找
220
+ if (parentResolvedPath === path.resolve(currentPath)) {
221
+ if (foundConfigPath) {
222
+ callback(foundConfigPath);
223
+ } else {
224
+ console.log('未找到 AutoSnippet.boxspec.json 文件,请检查路径。');
225
+ }
226
+ return;
227
+ }
228
+
229
+ search(parentPath, foundConfigPath, foundConfigDir, level + 1);
230
+ } else {
231
+ console.log(err);
144
232
  }
145
- }
146
- });
147
-
148
- // 如果已经找到配置文件,检查是否已经向上查找了两层
149
- if (foundConfigPath) {
150
- if (!isFound) {
151
- // 当前目录不是配置文件所在目录,说明已经向上查找了
152
- levelsUp++;
153
- }
154
-
155
- // 如果已经向上查找了两层,停止查找
156
- if (levelsUp >= 2) {
157
233
  return;
158
234
  }
159
- }
160
235
 
161
- // 如果还没找到配置文件,继续向上查找
162
- if (!foundConfigPath) {
163
- // 继续向上查找
164
- const parentPath = path.join(filePath, '/..');
165
- const parentResolvedPath = path.resolve(parentPath);
236
+ let isFound = false;
237
+ let currentConfigPath = foundConfigPath;
238
+ let currentConfigDir = foundConfigDir;
166
239
 
167
- // 如果已经到达根目录,停止查找
168
- if (parentResolvedPath === path.resolve(filePath)) {
169
- console.log('未找到 AutoSnippet.boxspec.json 文件,请检查路径。');
170
- return;
240
+ // 查找 AutoSnippet.boxspec.json
241
+ files.forEach(function (filename) {
242
+ if (filename === HOLDER_NAME) {
243
+ isFound = true;
244
+ if (!currentConfigPath) {
245
+ // 第一次找到配置文件,记录路径(不立即调用回调)
246
+ currentConfigPath = path.join(currentPath, filename);
247
+ currentConfigDir = path.resolve(currentPath);
248
+ }
249
+ }
250
+ });
251
+
252
+ // 如果已经找到配置文件,检查是否到达工程根目录
253
+ if (currentConfigPath) {
254
+ // 如果在配置文件所在目录或以上发现了工程根目录标记,停止查找并调用回调
255
+ if (isProjectRootSync(currentPath, files)) {
256
+ callback(currentConfigPath);
257
+ return;
258
+ }
171
259
  }
172
260
 
173
- findASSpecPath(parentPath, callback, foundConfigPath, 0);
174
- } else {
175
- // 已经找到配置文件,继续向上查找(最多两层)
176
- const parentPath = path.join(filePath, '/..');
261
+ // 继续向上查找
262
+ const parentPath = path.join(currentPath, '/..');
177
263
  const parentResolvedPath = path.resolve(parentPath);
178
264
 
179
265
  // 如果已经到达根目录,停止查找
180
- if (parentResolvedPath === path.resolve(filePath)) {
266
+ if (parentResolvedPath === path.resolve(currentPath)) {
267
+ // 如果找到了配置文件,即使没找到工程根目录标记,也调用回调
268
+ if (currentConfigPath) {
269
+ callback(currentConfigPath);
270
+ } else {
271
+ console.log('未找到 AutoSnippet.boxspec.json 文件,请检查路径。');
272
+ }
181
273
  return;
182
274
  }
183
275
 
184
- findASSpecPath(parentPath, callback, foundConfigPath, levelsUp);
185
- }
186
- });
276
+ search(parentPath, currentConfigPath, currentConfigDir, level + 1);
277
+ });
278
+ }
279
+
280
+ search(filePath, configPath, configDir, 0);
187
281
  }
188
282
 
189
283
  // 向上查找 Package.swift 文件(SPM 模块规范文件)
package/bin/init.js CHANGED
@@ -47,10 +47,14 @@ async function initSpec() {
47
47
  return false;
48
48
  }
49
49
  }
50
- idsArray.push(item['{identifier}']);
51
- // 头文件相对路径需要补齐
50
+ idsArray.push(item['{identifier}']);
51
+ // ✅ 头文件相对路径需要补齐(将子配置的相对路径转换为相对于主配置的路径)
52
52
  if (item['{specHeadPath}']) {
53
- item['{specHeadPath}'] = thePath + item['{specHeadPath}'];
53
+ // 解码原始路径,拼接子配置路径,再编码
54
+ const decodedPath = decodeURI(item['{specHeadPath}']);
55
+ const combinedPath = thePath ? path.join(thePath, decodedPath) : decodedPath;
56
+ // 规范化路径(处理 .. 和 .)
57
+ item['{specHeadPath}'] = encodeURI(path.normalize(combinedPath));
54
58
  }
55
59
  return true;
56
60
  });
package/bin/injection.js CHANGED
@@ -129,9 +129,10 @@ function handelAddHeaderStatus(specFile, updateFile, header, isAddedHeader, isAd
129
129
  function isAddedToSpecHeader(specFile, header, callback) {
130
130
  cache.getHeadCache(specFile).then(function (headCache) {
131
131
  if (headCache) {
132
- const specSlashIndex = specFile.lastIndexOf('/');
133
- const specFilePath = specFile.substring(0, specSlashIndex + 1);
134
- const headPath = specFilePath + headCache[header.headerName];
132
+ // 使用 path.join() 正确拼接配置目录和相对路径
133
+ const specFileDir = path.dirname(specFile);
134
+ const relativePath = decodeURI(headCache[header.headerName]);
135
+ const headPath = path.join(specFileDir, relativePath);
135
136
 
136
137
  try {
137
138
  // 读取当前头文件所在工作空间里默认暴露的头文件
package/bin/install.js CHANGED
@@ -80,7 +80,7 @@ function writeSingleSnippet(snippet, template) {
80
80
  });
81
81
 
82
82
  if (identifier && content) {
83
- // ✅ 使用配置模块获取代码片段输出路径(测试模式写入 AutoSnippetCache
83
+ // ✅ 使用配置模块获取代码片段输出路径(写入 Xcode CodeSnippets
84
84
  const snippetsPath = config.getSnippetsPath();
85
85
 
86
86
  try {
@@ -208,7 +208,7 @@ function addCodeSnippets(specFile, singleSnippet) {
208
208
  });
209
209
 
210
210
  if (identifier && content) {
211
- // ✅ 使用配置模块获取代码片段输出路径(测试模式写入 AutoSnippetCache
211
+ // ✅ 使用配置模块获取代码片段输出路径(写入 Xcode CodeSnippets
212
212
  const snippetsPath = config.getSnippetsPath();
213
213
 
214
214
  try {
@@ -219,11 +219,6 @@ function addCodeSnippets(specFile, singleSnippet) {
219
219
  try {
220
220
  const snippetFile = path.join(snippetsPath, identifier + '.codesnippet');
221
221
  fs.writeFileSync(snippetFile, content);
222
-
223
- // 测试模式下显示提示信息
224
- if (config.isTestMode()) {
225
- console.log(`[测试模式] 代码片段已写入: ${snippetFile}`);
226
- }
227
222
  } catch (err) {
228
223
  console.log(err);
229
224
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosnippet",
3
- "version": "1.1.14",
3
+ "version": "1.1.15",
4
4
  "description": "A iOS module management tool.",
5
5
  "main": "index.js",
6
6
  "scripts": {