autosnippet 1.1.13 → 1.1.14

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/install.js CHANGED
@@ -1,35 +1,144 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const fs = require('fs');
4
+ const path = require('path');
4
5
  const cache = require('./cache.js');
6
+ const config = require('./config.js');
5
7
  // 全局路径
6
- const USER_HOME = process.env.HOME || process.env.USERPROFILE;
7
- const SNIPPETS_PATH = USER_HOME + '/Library/Developer/Xcode/UserData/CodeSnippets';
8
8
  const HOLDER_KEYS = ['{identifier}', '{title}', '{completion}', '{summary}', '{content}', '{language}'];
9
9
 
10
- function addCodeSnippets(specFile) {
10
+ /**
11
+ * 将单个代码片段写入文件
12
+ */
13
+ function writeSingleSnippet(snippet, template) {
14
+ if (!snippet || !template) {
15
+ return;
16
+ }
17
+
18
+ let content = '';
19
+ let identifier = '';
20
+ let holderArr = [];
21
+
22
+ // 添加主代码片段
23
+ holderArr.push(snippet);
24
+
25
+ // 如果有头文件,添加 headerVersion
26
+ if (snippet['{headName}']) {
27
+ let extPlace = Object.assign({}, snippet);
28
+ let header = '<' + extPlace['{specName}'] + '/' + extPlace['{headName}'] + '>';
29
+ header = escapeString(header);
30
+
31
+ // swift只需要考虑工作空间是否引入
32
+ if (extPlace['{language}'] === 'Xcode.SourceCodeLanguage.Swift') {
33
+ header = extPlace['{specName}'];
34
+ }
35
+
36
+ extPlace['{identifier}'] = extPlace['{identifier}'] + 'Ext';
37
+ extPlace['{title}'] = extPlace['{title}'] + ' headerVersion';
38
+ extPlace['{completion}'] = extPlace['{completion}'] + 'Z';
39
+ extPlace['{summary}'] = extPlace['{summary}'] + header;
40
+
41
+ // 添加替换header标识位
42
+ let array = ['// ahead ' + header];
43
+ extPlace['{content}'].forEach(element => {
44
+ array.push(element);
45
+ });
46
+ extPlace['{content}'] = array;
47
+
48
+ holderArr.push(extPlace);
49
+ }
50
+
51
+ // 处理每个代码片段(主片段和 headerVersion)
52
+ holderArr.forEach(function (placeVal) {
53
+ content = '';
54
+ template.list.forEach(function (tempVal) {
55
+ // 保存id,文件名和id一致
56
+ if (HOLDER_KEYS.indexOf(tempVal) === 0) {
57
+ identifier = placeVal[tempVal];
58
+ }
59
+
60
+ if (HOLDER_KEYS.indexOf(tempVal) > -1) {
61
+ let value = placeVal[tempVal];
62
+
63
+ // 数组需要遍历取出每一行内容
64
+ if (Array.isArray(value)) {
65
+ let turnValue = '';
66
+
67
+ for (var index = 0; index < value.length; index++) {
68
+ if (index === 0) {
69
+ turnValue += value[index] + '\n';
70
+ } else {
71
+ turnValue += '\t' + value[index] + '\n';
72
+ }
73
+ }
74
+ value = turnValue;
75
+ }
76
+ tempVal = '\t<string>' + value + '</string>\n';
77
+ }
78
+
79
+ content += tempVal;
80
+ });
81
+
82
+ if (identifier && content) {
83
+ // ✅ 使用配置模块获取代码片段输出路径(测试模式写入 AutoSnippetCache)
84
+ const snippetsPath = config.getSnippetsPath();
85
+
86
+ try {
87
+ fs.accessSync(snippetsPath, fs.constants.F_OK);
88
+ } catch (err) {
89
+ fs.mkdirSync(snippetsPath, { recursive: true });
90
+ }
91
+ try {
92
+ const snippetFile = path.join(snippetsPath, identifier + '.codesnippet');
93
+ fs.writeFileSync(snippetFile, content);
94
+ } catch (err) {
95
+ console.log(err);
96
+ }
97
+ }
98
+ });
99
+ }
100
+
101
+ function addCodeSnippets(specFile, singleSnippet) {
11
102
  let placeholder = null;
12
103
  let template = null;
13
104
 
14
105
  try {
15
- // 读取AutoSnippet的占位配置
16
- const data = fs.readFileSync(specFile, 'utf8');
106
+ // 读取模板信息
107
+ const data = fs.readFileSync(__dirname + '/../template.json', 'utf8');
17
108
  if (data) {
18
- placeholder = JSON.parse(data);
19
- cache.updateCache(specFile, data);
109
+ template = JSON.parse(data);
20
110
  }
21
111
  } catch (err) {
22
112
  console.error(err);
113
+ return;
23
114
  }
24
115
 
116
+ // ✅ 如果指定了单个代码片段,只处理这个片段
117
+ if (singleSnippet) {
118
+ writeSingleSnippet(singleSnippet, template);
119
+ // 更新缓存
120
+ try {
121
+ const data = fs.readFileSync(specFile, 'utf8');
122
+ if (data) {
123
+ cache.updateCache(specFile, data);
124
+ }
125
+ } catch (err) {
126
+ console.error(err);
127
+ }
128
+ return;
129
+ }
130
+
131
+ // 原有逻辑:处理所有代码片段(用于 install 命令)
25
132
  try {
26
- // 读取模板信息
27
- const data = fs.readFileSync(__dirname + '/../template.json', 'utf8');
133
+ // 读取AutoSnippet的占位配置
134
+ const data = fs.readFileSync(specFile, 'utf8');
28
135
  if (data) {
29
- template = JSON.parse(data);
136
+ placeholder = JSON.parse(data);
137
+ cache.updateCache(specFile, data);
30
138
  }
31
139
  } catch (err) {
32
140
  console.error(err);
141
+ return;
33
142
  }
34
143
 
35
144
  // 拼装配置文件
@@ -99,13 +208,22 @@ function addCodeSnippets(specFile) {
99
208
  });
100
209
 
101
210
  if (identifier && content) {
211
+ // ✅ 使用配置模块获取代码片段输出路径(测试模式写入 AutoSnippetCache)
212
+ const snippetsPath = config.getSnippetsPath();
213
+
102
214
  try {
103
- fs.accessSync(SNIPPETS_PATH, fs.F_OK);
215
+ fs.accessSync(snippetsPath, fs.constants.F_OK);
104
216
  } catch (err) {
105
- fs.mkdirSync(SNIPPETS_PATH);
217
+ fs.mkdirSync(snippetsPath, { recursive: true });
106
218
  }
107
219
  try {
108
- fs.writeFileSync(SNIPPETS_PATH + '/' + identifier + '.codesnippet', content);
220
+ const snippetFile = path.join(snippetsPath, identifier + '.codesnippet');
221
+ fs.writeFileSync(snippetFile, content);
222
+
223
+ // 测试模式下显示提示信息
224
+ if (config.isTestMode()) {
225
+ console.log(`[测试模式] 代码片段已写入: ${snippetFile}`);
226
+ }
109
227
  } catch (err) {
110
228
  console.log(err);
111
229
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "autosnippet",
3
- "version": "1.1.13",
3
+ "version": "1.1.14",
4
4
  "description": "A iOS module management tool.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,10 +16,10 @@
16
16
  "iOS",
17
17
  "Snippet"
18
18
  ],
19
- "homepage": "https://github.com/ggsoul/AutoSnippet#readme",
19
+ "homepage": "https://github.com/GxFn/AutoSnippet#readme",
20
20
  "repository": {
21
21
  "type": "git",
22
- "url": "git+https://github.com/ggsoul/AutoSnippet.git"
22
+ "url": "git+https://github.com/GxFn/AutoSnippet.git"
23
23
  },
24
24
  "dependencies": {
25
25
  "commander": "^7.2.0",
@@ -30,6 +30,6 @@
30
30
  },
31
31
  "devDependencies": {},
32
32
  "bugs": {
33
- "url": "https://github.com/ggsoul/AutoSnippet/issues"
33
+ "url": "https://github.com/GxFn/AutoSnippet/issues"
34
34
  }
35
35
  }