autosnippet 1.1.16 → 1.1.17

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.
Files changed (3) hide show
  1. package/bin/asnip.js +11 -4
  2. package/bin/init.js +55 -35
  3. package/package.json +1 -1
package/bin/asnip.js CHANGED
@@ -176,10 +176,17 @@ commander
176
176
  commander
177
177
  .command('i')
178
178
  .description('add the shared Snippet to the Xcode environment')
179
- .action(() => {
180
- getSpecFile(function (specFile) {
181
- install.addCodeSnippets(specFile);
182
- });
179
+ .action(async () => {
180
+ // 使用异步版本查找配置文件
181
+ const specFile = await findPath.findASSpecPathAsync(CMD_PATH);
182
+ if (!specFile) {
183
+ console.error('未找到 AutoSnippet.boxspec.json 配置文件');
184
+ return;
185
+ }
186
+ // ✅ 先聚合子模块配置到主配置文件
187
+ await init.mergeSubSpecs(specFile);
188
+ // 然后安装 snippets
189
+ install.addCodeSnippets(specFile);
183
190
  });
184
191
 
185
192
  commander
package/bin/init.js CHANGED
@@ -5,31 +5,29 @@ const path = require('path');
5
5
  const CMD_PATH = process.cwd();
6
6
  const findPath = require('./findPath.js');
7
7
 
8
- async function initSpec() {
9
- const filePath = path.join(CMD_PATH, '/AutoSnippet.boxspec.json');
8
+ /**
9
+ * 聚合子模块的 AutoSnippet.boxspec.json 到主配置文件
10
+ * @param {string} mainSpecFile - 主配置文件路径
11
+ * @returns {Promise<void>}
12
+ */
13
+ async function mergeSubSpecs(mainSpecFile) {
10
14
  let idsArray = [];
11
15
  let specObj = {
12
16
  list: []
13
17
  };
14
18
 
15
- try {
16
- await fs.promises.access(filePath);
17
- } catch (error) {
18
- const content = JSON.stringify(specObj, null, 4);
19
- fs.writeFileSync(filePath, content, 'utf8');
20
- }
21
-
22
- const specSlashIndex = filePath.lastIndexOf('/');
23
- const specFilePath = filePath.substring(0, specSlashIndex + 1);
19
+ const specSlashIndex = mainSpecFile.lastIndexOf('/');
20
+ const specFilePath = mainSpecFile.substring(0, specSlashIndex + 1);
24
21
 
25
- const array = await findPath.findSubASSpecPath(CMD_PATH);
22
+ // 从主配置文件所在目录向下查找子模块的 AutoSnippet.boxspec.json
23
+ const array = await findPath.findSubASSpecPath(path.dirname(mainSpecFile));
26
24
 
27
25
  for (let i = 0; i < array.length; i++) {
28
26
  const filename = array[i];
29
27
 
30
28
  const slashIndex = filename.lastIndexOf('/');
31
29
  let thePath = filename.substring(0, slashIndex + 1);
32
- if (filename === filePath) {
30
+ if (filename === mainSpecFile) {
33
31
  thePath = '';
34
32
  } else {
35
33
  thePath = thePath.replace(specFilePath, '');
@@ -38,36 +36,58 @@ async function initSpec() {
38
36
  try {
39
37
  // 读取AutoSnippet的占位配置
40
38
  const data = fs.readFileSync(filename, 'utf8');
41
- const config = JSON.parse(data);
39
+ const config = JSON.parse(data);
42
40
  if (config && config.list) {
43
-
44
- const arr = config.list.filter(function (item, index, array) {
45
- for (let i = 0; i < idsArray.length; i++) {
46
- if (item['{identifier}'] === idsArray[i]) {
47
- return false;
48
- }
49
- }
41
+ const arr = config.list.filter(function (item, index, array) {
42
+ for (let i = 0; i < idsArray.length; i++) {
43
+ if (item['{identifier}'] === idsArray[i]) {
44
+ return false;
45
+ }
46
+ }
50
47
  idsArray.push(item['{identifier}']);
51
- // ✅ SPM 模块:headName 已经是相对于模块根目录的相对路径,如果子模块与主配置不在同一目录,需要调整
48
+ // ✅ SPM 模块:headName 已经是相对于模块根目录的相对路径
52
49
  // headName 保持相对于各自模块根目录的路径(不需要转换)
53
- // 去掉 specHeadPath 的处理逻辑
54
- return true;
55
- });
56
- specObj.list = specObj.list.concat(arr);
50
+ return true;
51
+ });
52
+ specObj.list = specObj.list.concat(arr);
57
53
  }
58
54
  } catch (err) {
59
55
  console.error(err);
60
56
  }
61
57
  }
62
58
 
63
- try {
64
- const content = JSON.stringify(specObj, null, 4);
65
- if (content) {
66
- fs.writeFileSync(filePath, content, 'utf8');
67
- }
68
- } catch (err) {
69
- console.error(err);
70
- }
59
+ try {
60
+ const content = JSON.stringify(specObj, null, 4);
61
+ if (content) {
62
+ fs.writeFileSync(mainSpecFile, content, 'utf8');
63
+ }
64
+ } catch (err) {
65
+ console.error(err);
66
+ }
67
+ }
68
+
69
+ async function initSpec() {
70
+ // ✅ 查找 Package.swift,在其同级目录创建 AutoSnippet.boxspec.json
71
+ let packagePath = await findPath.findPackageSwiftPath(CMD_PATH);
72
+
73
+ // 如果找不到 Package.swift,使用当前目录
74
+ const configDir = packagePath ? path.dirname(packagePath) : CMD_PATH;
75
+ const filePath = path.join(configDir, 'AutoSnippet.boxspec.json');
76
+
77
+ // 如果配置文件不存在,创建一个空的
78
+ try {
79
+ await fs.promises.access(filePath);
80
+ } catch (error) {
81
+ const specObj = {
82
+ list: []
83
+ };
84
+ const content = JSON.stringify(specObj, null, 4);
85
+ fs.writeFileSync(filePath, content, 'utf8');
86
+ }
87
+
88
+ // ✅ 聚合子模块配置到主配置文件
89
+ await mergeSubSpecs(filePath);
71
90
  }
72
91
 
73
- exports.initSpec = initSpec;
92
+ exports.initSpec = initSpec;
93
+ exports.mergeSubSpecs = mergeSubSpecs;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosnippet",
3
- "version": "1.1.16",
3
+ "version": "1.1.17",
4
4
  "description": "A iOS module management tool.",
5
5
  "main": "index.js",
6
6
  "scripts": {