autosnippet 1.1.17 → 1.1.18

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/findPath.js CHANGED
@@ -440,9 +440,146 @@ async function findSubASSpecPath(filePath) {
440
440
  return resultArray;
441
441
  }
442
442
 
443
+ /**
444
+ * 向上查找项目根目录(包含 .git、Package.swift 等的目录)
445
+ * @param {string} filePath - 起始文件路径
446
+ * @returns {Promise<string|null>} 项目根目录路径,如果找不到返回 null
447
+ */
448
+ async function findProjectRoot(filePath) {
449
+ let currentPath = path.resolve(filePath);
450
+ const maxLevels = 20;
451
+ let levelsChecked = 0;
452
+
453
+ while (currentPath && levelsChecked < maxLevels) {
454
+ try {
455
+ const entries = await getDirectoryEntries(currentPath);
456
+ if (entries && isProjectRoot(currentPath, entries)) {
457
+ return currentPath;
458
+ }
459
+
460
+ const parentPath = path.dirname(currentPath);
461
+ if (parentPath === currentPath) {
462
+ break;
463
+ }
464
+ currentPath = parentPath;
465
+ levelsChecked++;
466
+ } catch (err) {
467
+ if (err.code === 'ENOENT' || err.code === 'EACCES') {
468
+ const parentPath = path.dirname(currentPath);
469
+ if (parentPath === currentPath) {
470
+ break;
471
+ }
472
+ currentPath = parentPath;
473
+ levelsChecked++;
474
+ continue;
475
+ }
476
+ throw err;
477
+ }
478
+ }
479
+
480
+ return null;
481
+ }
482
+
483
+ /**
484
+ * 从项目根目录找到模块,并返回模块名和头文件名
485
+ * @param {string} projectRoot - 项目根目录
486
+ * @param {string} specName - 模块名(target 名称)
487
+ * @param {string} headName - 头文件相对路径(相对于模块根目录)
488
+ * @returns {Promise<{moduleName: string, headerFileName: string}|null>}
489
+ */
490
+ async function findModuleHeaderFromRoot(projectRoot, specName, headName) {
491
+ if (!projectRoot || !specName || !headName) {
492
+ return null;
493
+ }
494
+
495
+ // 从项目根目录向下查找包含 specName 的目录(可能是模块目录)
496
+ // 例如:BDVideoPlayer、UI/BDVideoPlayer 等
497
+ const headerFileName = path.basename(headName);
498
+
499
+ // 尝试在项目根目录下查找模块目录
500
+ // 1. 直接查找 specName 目录
501
+ const directModulePath = path.join(projectRoot, specName);
502
+ try {
503
+ const entries = await getDirectoryEntries(directModulePath);
504
+ if (entries) {
505
+ // 检查是否有 include/ModuleName/ 目录
506
+ const includePath = path.join(directModulePath, 'include', specName, headerFileName);
507
+ try {
508
+ await fs.promises.access(includePath);
509
+ return {
510
+ moduleName: specName,
511
+ headerFileName: headerFileName
512
+ };
513
+ } catch (err) {
514
+ // 继续查找
515
+ }
516
+ }
517
+ } catch (err) {
518
+ // 继续查找
519
+ }
520
+
521
+ // 2. 递归查找包含 specName 的目录
522
+ const foundModule = await findModuleDirectory(projectRoot, specName, headerFileName);
523
+ if (foundModule) {
524
+ return foundModule;
525
+ }
526
+
527
+ // 如果找不到,返回默认值(使用 specName 和 headerFileName)
528
+ return {
529
+ moduleName: specName,
530
+ headerFileName: headerFileName
531
+ };
532
+ }
533
+
534
+ /**
535
+ * 递归查找模块目录
536
+ */
537
+ async function findModuleDirectory(dirPath, moduleName, headerFileName) {
538
+ try {
539
+ const entries = await getDirectoryEntries(dirPath);
540
+ if (!entries) {
541
+ return null;
542
+ }
543
+
544
+ for (const entry of entries) {
545
+ if (entry.isDirectory()) {
546
+ const dirName = entry.name;
547
+ // 跳过隐藏目录和常见目录
548
+ if (dirName.startsWith('.') || dirName === 'node_modules') {
549
+ continue;
550
+ }
551
+
552
+ const fullPath = path.join(dirPath, dirName);
553
+
554
+ // 检查是否是模块目录(包含 include/ModuleName/ 结构)
555
+ const includePath = path.join(fullPath, 'include', moduleName, headerFileName);
556
+ try {
557
+ await fs.promises.access(includePath);
558
+ return {
559
+ moduleName: moduleName,
560
+ headerFileName: headerFileName
561
+ };
562
+ } catch (err) {
563
+ // 继续递归查找
564
+ const found = await findModuleDirectory(fullPath, moduleName, headerFileName);
565
+ if (found) {
566
+ return found;
567
+ }
568
+ }
569
+ }
570
+ }
571
+ } catch (err) {
572
+ // 忽略错误
573
+ }
574
+
575
+ return null;
576
+ }
577
+
443
578
  exports.findASSpecPath = findASSpecPath;
444
579
  exports.findASSpecPathAsync = findASSpecPathAsync;
445
580
  exports.findPackageSwiftPath = findPackageSwiftPath;
446
581
  exports.parsePackageSwift = parsePackageSwift;
447
582
  exports.findSubHeaderPath = findSubHeaderPath;
448
583
  exports.findSubASSpecPath = findSubASSpecPath;
584
+ exports.findProjectRoot = findProjectRoot;
585
+ exports.findModuleHeaderFromRoot = findModuleHeaderFromRoot;
package/bin/init.js CHANGED
@@ -16,11 +16,16 @@ async function mergeSubSpecs(mainSpecFile) {
16
16
  list: []
17
17
  };
18
18
 
19
+ // ✅ 从项目根目录开始查找,而不是从主配置文件所在目录
20
+ // 找到项目根目录(包含 .git、Package.swift 等的目录)
21
+ const projectRoot = await findPath.findProjectRoot(mainSpecFile);
22
+ const searchRoot = projectRoot || path.dirname(mainSpecFile);
23
+
19
24
  const specSlashIndex = mainSpecFile.lastIndexOf('/');
20
25
  const specFilePath = mainSpecFile.substring(0, specSlashIndex + 1);
21
26
 
22
- // 从主配置文件所在目录向下查找子模块的 AutoSnippet.boxspec.json
23
- const array = await findPath.findSubASSpecPath(path.dirname(mainSpecFile));
27
+ // 从项目根目录向下查找子模块的 AutoSnippet.boxspec.json
28
+ const array = await findPath.findSubASSpecPath(searchRoot);
24
29
 
25
30
  for (let i = 0; i < array.length; i++) {
26
31
  const filename = array[i];
package/bin/install.js CHANGED
@@ -4,6 +4,7 @@ const fs = require('fs');
4
4
  const path = require('path');
5
5
  const cache = require('./cache.js');
6
6
  const config = require('./config.js');
7
+ const findPath = require('./findPath.js');
7
8
  // 全局路径
8
9
  const HOLDER_KEYS = ['{identifier}', '{title}', '{completion}', '{summary}', '{content}', '{language}'];
9
10
 
@@ -25,7 +26,10 @@ function writeSingleSnippet(snippet, template) {
25
26
  // 如果有头文件,添加 headerVersion
26
27
  if (snippet['{headName}']) {
27
28
  let extPlace = Object.assign({}, snippet);
28
- let header = '<' + extPlace['{specName}'] + '/' + extPlace['{headName}'] + '>';
29
+ // 从项目根目录找到模块,只使用模块名和头文件名
30
+ // 例如:<BDVideoPlayer/BDVideoCacheManager.h> 而不是 <BDVideoPlayer/UI/BDVideoPlayer/Code/BDVideoCacheManager.h>
31
+ const headerFileName = path.basename(extPlace['{headName}']);
32
+ let header = '<' + extPlace['{specName}'] + '/' + headerFileName + '>';
29
33
  header = escapeString(header);
30
34
 
31
35
  // swift只需要考虑工作空间是否引入
@@ -152,7 +156,9 @@ function addCodeSnippets(specFile, singleSnippet) {
152
156
 
153
157
  if (placeVal['{headName}']) {
154
158
  let extPlace = Object.assign({}, placeVal);
155
- let header = '<' + extPlace['{specName}'] + '/' + extPlace['{headName}'] + '>';
159
+ // 从项目根目录找到模块,只使用模块名和头文件名
160
+ const headerFileName = path.basename(extPlace['{headName}']);
161
+ let header = '<' + extPlace['{specName}'] + '/' + headerFileName + '>';
156
162
  header = escapeString(header);
157
163
 
158
164
  // swift只需要考虑工作空间是否引入
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosnippet",
3
- "version": "1.1.17",
3
+ "version": "1.1.18",
4
4
  "description": "A iOS module management tool.",
5
5
  "main": "index.js",
6
6
  "scripts": {