node-shortcuts 1.0.4 → 1.1.0

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/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "node-shortcuts",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "Node Shortcut snippets",
5
5
  "main": "sync-snippets.js",
6
6
  "scripts": {
7
- "postinstall": "node sync-snippets.js"
7
+ "postinstall": "node sync-snippets.js",
8
+ "preuninstall": "node cleanup-snippets.js"
8
9
  },
9
10
  "keywords": [],
10
11
  "author": "krispowers",
@@ -14,6 +15,7 @@
14
15
  },
15
16
  "files": [
16
17
  "sync-snippets.js",
18
+ "cleanup-snippets",
17
19
  "snippets/",
18
20
  "README.md",
19
21
  "LICENSE"
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "Node.js HTTP Import": {
3
3
  "prefix": "@http",
4
- "scope": "javascript",
5
4
  "body": [
6
5
  "import http from 'node:http';"
7
6
  ],
@@ -9,7 +8,6 @@
9
8
  },
10
9
  "Node.js HTTPS Import": {
11
10
  "prefix": "@https",
12
- "scope": "javascript",
13
11
  "body": [
14
12
  "import https from 'node:https';"
15
13
  ],
@@ -17,7 +15,6 @@
17
15
  },
18
16
  "Node.js NET Import": {
19
17
  "prefix": "@net",
20
- "scope": "javascript",
21
18
  "body": [
22
19
  "import net from 'node:net';"
23
20
  ],
@@ -25,7 +22,6 @@
25
22
  },
26
23
  "Node.js DGRAM Import": {
27
24
  "prefix": "@dgram",
28
- "scope": "javascript",
29
25
  "body": [
30
26
  "import dgram from 'node:dgram';"
31
27
  ],
@@ -33,7 +29,6 @@
33
29
  },
34
30
  "Node.js FS Import": {
35
31
  "prefix": "@fs",
36
- "scope": "javascript",
37
32
  "body": [
38
33
  "import fs from 'node:fs';"
39
34
  ],
@@ -41,7 +36,6 @@
41
36
  },
42
37
  "Node.js fsPromises Import": {
43
38
  "prefix": ["@fspromises", "@fsp"],
44
- "scope": "javascript",
45
39
  "body": [
46
40
  "import fsPromises from 'node:fs/promises';"
47
41
  ],
@@ -49,7 +43,6 @@
49
43
  },
50
44
  "Node.js Path Import": {
51
45
  "prefix": "@path",
52
- "scope": "javascript",
53
46
  "body": [
54
47
  "import path from 'node:path';"
55
48
  ],
@@ -57,7 +50,6 @@
57
50
  },
58
51
  "Node.js OS Import": {
59
52
  "prefix": "@os",
60
- "scope": "javascript",
61
53
  "body": [
62
54
  "import os from 'node:os';"
63
55
  ],
@@ -65,7 +57,6 @@
65
57
  },
66
58
  "Node.js PROCESS Import": {
67
59
  "prefix": "@process",
68
- "scope": "javascript",
69
60
  "body": [
70
61
  "import process from 'node:process';"
71
62
  ],
@@ -73,7 +64,6 @@
73
64
  },
74
65
  "Node.js STREAM Import": {
75
66
  "prefix": "@stream",
76
- "scope": "javascript",
77
67
  "body": [
78
68
  "import stream from 'node:stream';"
79
69
  ],
@@ -81,7 +71,6 @@
81
71
  },
82
72
  "Node.js BUFFER Import": {
83
73
  "prefix": "@buffer",
84
- "scope": "javascript",
85
74
  "body": [
86
75
  "import buffer from 'node:buffer';"
87
76
  ],
@@ -89,7 +78,6 @@
89
78
  },
90
79
  "Node.js URL Import": {
91
80
  "prefix": "@url",
92
- "scope": "javascript",
93
81
  "body": [
94
82
  "import url from 'node:url';"
95
83
  ],
@@ -97,7 +85,6 @@
97
85
  },
98
86
  "Node.js QUERYSTRING Import": {
99
87
  "prefix": ["@querystring", "@qs"],
100
- "scope": "javascript",
101
88
  "body": [
102
89
  "import querystring from 'node:querystring';"
103
90
  ],
@@ -105,7 +92,6 @@
105
92
  },
106
93
  "Node.js PUNYCODE Import": {
107
94
  "prefix": ["@punycode", "@pc", "@puny"],
108
- "scope": "javascript",
109
95
  "body": [
110
96
  "import punycode from 'node:punycode';"
111
97
  ],
@@ -113,7 +99,6 @@
113
99
  },
114
100
  "Node.js CRYPTO Import": {
115
101
  "prefix": "@crypto",
116
- "scope": "javascript",
117
102
  "body": [
118
103
  "import crypto from 'node:crypto';"
119
104
  ],
@@ -121,7 +106,6 @@
121
106
  },
122
107
  "Node.js TLS Import": {
123
108
  "prefix": "@tls",
124
- "scope": "javascript",
125
109
  "body": [
126
110
  "import tls from 'node:tls';"
127
111
  ],
@@ -129,7 +113,6 @@
129
113
  },
130
114
  "Node.js ZLIB Import": {
131
115
  "prefix": "@zlib",
132
- "scope": "javascript",
133
116
  "body": [
134
117
  "import zlib from 'node:zlib';"
135
118
  ],
@@ -137,7 +120,6 @@
137
120
  },
138
121
  "Node.js UTIL Import": {
139
122
  "prefix": "@util",
140
- "scope": "javascript",
141
123
  "body": [
142
124
  "import util from 'node:util';"
143
125
  ],
@@ -145,7 +127,6 @@
145
127
  },
146
128
  "Node.js EVENTS Import": {
147
129
  "prefix": "@events",
148
- "scope": "javascript",
149
130
  "body": [
150
131
  "import events from 'node:events';"
151
132
  ],
@@ -153,7 +134,6 @@
153
134
  },
154
135
  "Node.js ASSERT Import": {
155
136
  "prefix": "@assert",
156
- "scope": "javascript",
157
137
  "body": [
158
138
  "import assert from 'node:assert';"
159
139
  ],
@@ -161,7 +141,6 @@
161
141
  },
162
142
  "Node.js STRING DECODER Import": {
163
143
  "prefix": ["@stringdecoder", "@sd"],
164
- "scope": "javascript",
165
144
  "body": [
166
145
  "import stringDecoder from 'node:string_decoder';"
167
146
  ],
@@ -169,7 +148,6 @@
169
148
  },
170
149
  "Node.js CHILD PROCESS Import": {
171
150
  "prefix": ["@childprocess", "@cp"],
172
- "scope": "javascript",
173
151
  "body": [
174
152
  "import childProcess from 'node:child_process';"
175
153
  ],
@@ -177,7 +155,6 @@
177
155
  },
178
156
  "Node.js WORKER THREADS Import": {
179
157
  "prefix": ["@workerthreads", "@wt"],
180
- "scope": "javascript",
181
158
  "body": [
182
159
  "import workerThreads from 'node:worker_threads';"
183
160
  ],
@@ -185,7 +162,6 @@
185
162
  },
186
163
  "Node.js CLUSTER Import": {
187
164
  "prefix": "@cluster",
188
- "scope": "javascript",
189
165
  "body": [
190
166
  "import cluster from 'node:cluster';"
191
167
  ],
@@ -193,7 +169,6 @@
193
169
  },
194
170
  "Node.js PERF HOOKS Import": {
195
171
  "prefix": ["@perfhooks", "@perf"],
196
- "scope": "javascript",
197
172
  "body": [
198
173
  "import perfHooks from 'node:perf_hooks';"
199
174
  ],
@@ -201,7 +176,6 @@
201
176
  },
202
177
  "Node.js INSPECTOR Import": {
203
178
  "prefix": "@inspector",
204
- "scope": "javascript",
205
179
  "body": [
206
180
  "import inspector from 'node:inspector';"
207
181
  ],
@@ -209,7 +183,6 @@
209
183
  },
210
184
  "Node.js DIAGNOSTICS CHANNEL Import": {
211
185
  "prefix": ["@diagnostics", "@dc"],
212
- "scope": "javascript",
213
186
  "body": [
214
187
  "import diagnosticsChannel from 'node:diagnostics_channel';"
215
188
  ],
@@ -217,7 +190,6 @@
217
190
  },
218
191
  "Node.js MODULE Import": {
219
192
  "prefix": "@module",
220
- "scope": "javascript",
221
193
  "body": [
222
194
  "import module from 'node:module';"
223
195
  ],
@@ -225,7 +197,6 @@
225
197
  },
226
198
  "Node.js VM Import": {
227
199
  "prefix": "@vm",
228
- "scope": "javascript",
229
200
  "body": [
230
201
  "import vm from 'node:vm';"
231
202
  ],
@@ -233,7 +204,6 @@
233
204
  },
234
205
  "Node.js TIMERS Import": {
235
206
  "prefix": "@timers",
236
- "scope": "javascript",
237
207
  "body": [
238
208
  "import timers from 'node:timers';"
239
209
  ],
@@ -241,7 +211,6 @@
241
211
  },
242
212
  "Node.js TIMERS PROMISES Import": {
243
213
  "prefix": ["@timerspromises", "@tp"],
244
- "scope": "javascript",
245
214
  "body": [
246
215
  "import timersPromises from 'node:timers/promises';"
247
216
  ],
@@ -249,7 +218,6 @@
249
218
  },
250
219
  "Node.js REPL Import": {
251
220
  "prefix": "@repl",
252
- "scope": "javascript",
253
221
  "body": [
254
222
  "import repl from 'node:repl';"
255
223
  ],
@@ -257,7 +225,6 @@
257
225
  },
258
226
  "Node.js READLINE Import": {
259
227
  "prefix": "@readline",
260
- "scope": "javascript",
261
228
  "body": [
262
229
  "import readline from 'node:readline';"
263
230
  ],
@@ -265,7 +232,6 @@
265
232
  },
266
233
  "Node.js TTY Import": {
267
234
  "prefix": "@tty",
268
- "scope": "javascript",
269
235
  "body": [
270
236
  "import tty from 'node:tty';"
271
237
  ],
@@ -273,7 +239,6 @@
273
239
  },
274
240
  "Node.js DNS Import": {
275
241
  "prefix": "@dns",
276
- "scope": "javascript",
277
242
  "body": [
278
243
  "import dns from 'node:dns';"
279
244
  ],
@@ -281,7 +246,6 @@
281
246
  },
282
247
  "Node.js DNS PROMISES Import": {
283
248
  "prefix": ["@dnspromises", "@dnsp"],
284
- "scope": "javascript",
285
249
  "body": [
286
250
  "import dnsPromises from 'node:dns/promises';"
287
251
  ],
@@ -289,7 +253,6 @@
289
253
  },
290
254
  "Node.js V8 Import": {
291
255
  "prefix": "@v8",
292
- "scope": "javascript",
293
256
  "body": [
294
257
  "import v8 from 'node:v8';"
295
258
  ],
@@ -297,7 +260,6 @@
297
260
  },
298
261
  "Node.js WASI Import": {
299
262
  "prefix": "@wasi",
300
- "scope": "javascript",
301
263
  "body": [
302
264
  "import wasi from 'node:wasi';"
303
265
  ],
package/sync-snippets.js CHANGED
@@ -5,74 +5,110 @@ import fs from 'fs';
5
5
  import path from 'path';
6
6
  import os from 'os';
7
7
 
8
- /**
9
- * Paths
10
- */
11
- const USER_HOME = os.homedir();
8
+ // -----------------------------
9
+ // Source file (canonical snippets)
10
+ // -----------------------------
11
+ const SOURCE_FILE = path.resolve('./snippets/node.json');
12
12
 
13
- const TARGETS = [
14
- path.join(USER_HOME, 'AppData', 'Roaming', 'Code', 'User', 'snippets', 'javascript.json'),
15
- path.join(USER_HOME, 'AppData', 'Roaming', 'Code - Insiders', 'User', 'snippets', 'javascript.json')
16
- ];
13
+ // -----------------------------
14
+ // Cross-platform VS Code + Insiders targets
15
+ // -----------------------------
16
+ function getVSCodeTargets() {
17
+ const home = os.homedir();
18
+ const files = ['javascript.json', 'typescript.json']; // Auto JS + TS
17
19
 
18
- const SOURCE_FILE = path.resolve('./node.json');
20
+ const bases = (() => {
21
+ switch (process.platform) {
22
+ case 'win32':
23
+ return [
24
+ path.join(home, 'AppData', 'Roaming', 'Code', 'User', 'snippets'),
25
+ path.join(home, 'AppData', 'Roaming', 'Code - Insiders', 'User', 'snippets')
26
+ ];
27
+ case 'darwin':
28
+ return [
29
+ path.join(home, 'Library', 'Application Support', 'Code', 'User', 'snippets'),
30
+ path.join(home, 'Library', 'Application Support', 'Code - Insiders', 'User', 'snippets')
31
+ ];
32
+ default: // linux
33
+ return [
34
+ path.join(home, '.config', 'Code', 'User', 'snippets'),
35
+ path.join(home, '.config', 'Code - Insiders', 'User', 'snippets')
36
+ ];
37
+ }
38
+ })();
39
+
40
+ // Return all combinations: each base × each file
41
+ return bases.flatMap(base => files.map(file => path.join(base, file)));
42
+ }
19
43
 
20
- /**
21
- * Read JSON safely
22
- */
44
+ // -----------------------------
45
+ // JSONC-safe read
46
+ // -----------------------------
23
47
  function readJSON(file) {
24
48
  if (!fs.existsSync(file)) return {};
25
- const raw = fs.readFileSync(file, 'utf8');
26
- return raw.trim() ? JSON.parse(raw) : {};
49
+
50
+ let raw = fs.readFileSync(file, 'utf8');
51
+ if (!raw.trim()) return {};
52
+
53
+ // Strip comments
54
+ raw = raw.replace(/\/\/.*$/gm, '');
55
+ raw = raw.replace(/\/\*[\s\S]*?\*\//g, '');
56
+ // Strip trailing commas
57
+ raw = raw.replace(/,\s*([}\]])/g, '$1');
58
+
59
+ return JSON.parse(raw);
27
60
  }
28
61
 
29
- /**
30
- * Ensure directory exists
31
- */
62
+ // -----------------------------
63
+ // Ensure directory exists
64
+ // -----------------------------
32
65
  function ensureDir(file) {
33
66
  fs.mkdirSync(path.dirname(file), { recursive: true });
34
67
  }
35
68
 
36
- /**
37
- * Write formatted JSON
38
- */
69
+ // -----------------------------
70
+ // Write JSON safely
71
+ // -----------------------------
39
72
  function writeJSON(file, data) {
40
73
  ensureDir(file);
41
74
  fs.writeFileSync(file, JSON.stringify(data, null, 2) + '\n', 'utf8');
42
75
  }
43
76
 
44
- /**
45
- * Sync snippets into javascript.json
46
- */
47
- function syncToVSCode(targetFile) {
77
+ // -----------------------------
78
+ // Sync snippets
79
+ // -----------------------------
80
+ function sync(target) {
48
81
  const sourceSnippets = readJSON(SOURCE_FILE);
49
- const userSnippets = readJSON(targetFile);
82
+ const userSnippets = readJSON(target);
50
83
 
51
84
  let added = 0;
52
85
  let updated = 0;
53
86
 
54
87
  for (const [key, snippet] of Object.entries(sourceSnippets)) {
55
- if (!userSnippets[key]) {
56
- added++;
57
- } else if (JSON.stringify(userSnippets[key]) !== JSON.stringify(snippet)) {
58
- updated++;
59
- }
88
+ if (!userSnippets[key]) added++;
89
+ else if (JSON.stringify(userSnippets[key]) !== JSON.stringify(snippet)) updated++;
90
+
60
91
  userSnippets[key] = snippet;
61
92
  }
62
93
 
63
- writeJSON(targetFile, userSnippets);
94
+ writeJSON(target, userSnippets);
64
95
 
65
- console.log(`✔ Synced: ${targetFile}`);
96
+ console.log(`✔ Synced: ${target}`);
66
97
  console.log(` ➕ Added: ${added}`);
67
98
  console.log(` 🔄 Updated: ${updated}`);
68
99
  }
69
100
 
70
- /**
71
- * Run
72
- */
101
+ // -----------------------------
102
+ // Run sync for all targets
103
+ // -----------------------------
73
104
  try {
105
+ const TARGETS = getVSCodeTargets();
74
106
  for (const target of TARGETS) {
75
- syncToVSCode(target);
107
+ try {
108
+ sync(target);
109
+ } catch (err) {
110
+ console.warn(`⚠ Skipped invalid snippet file: ${target}`);
111
+ }
76
112
  }
77
113
  } catch (err) {
78
114
  console.error('❌ Snippet sync failed');