node-shortcuts 1.0.0 → 1.0.1
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/README.md +4 -1
- package/package.json +1 -1
- package/sync-snippets.js +41 -26
package/README.md
CHANGED
|
@@ -47,8 +47,11 @@ Covers all major Node.js core modules, including:
|
|
|
47
47
|
```bash
|
|
48
48
|
npm i node-shorcuts -D
|
|
49
49
|
```
|
|
50
|
+
You may need to restart VSC/VSC-I via:
|
|
51
|
+
```text
|
|
52
|
+
Ctrl + Shift + P → Reload Window
|
|
53
|
+
```
|
|
50
54
|
|
|
51
|
-
.vscode/node.code-snippets
|
|
52
55
|
Snippets load automatically when the workspace opens.
|
|
53
56
|
|
|
54
57
|
### Node Version
|
package/package.json
CHANGED
package/sync-snippets.js
CHANGED
|
@@ -3,64 +3,79 @@
|
|
|
3
3
|
|
|
4
4
|
import fs from 'fs';
|
|
5
5
|
import path from 'path';
|
|
6
|
+
import os from 'os';
|
|
6
7
|
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Paths
|
|
10
|
+
*/
|
|
11
|
+
const USER_HOME = os.homedir();
|
|
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
|
+
];
|
|
17
|
+
|
|
18
|
+
const SOURCE_FILE = path.resolve('./snippets.source.json');
|
|
9
19
|
|
|
10
20
|
/**
|
|
11
|
-
*
|
|
21
|
+
* Read JSON safely
|
|
12
22
|
*/
|
|
13
|
-
function readJSON(
|
|
14
|
-
if (!fs.existsSync(
|
|
15
|
-
const raw = fs.readFileSync(
|
|
23
|
+
function readJSON(file) {
|
|
24
|
+
if (!fs.existsSync(file)) return {};
|
|
25
|
+
const raw = fs.readFileSync(file, 'utf8');
|
|
16
26
|
return raw.trim() ? JSON.parse(raw) : {};
|
|
17
27
|
}
|
|
18
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Ensure directory exists
|
|
31
|
+
*/
|
|
32
|
+
function ensureDir(file) {
|
|
33
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
34
|
+
}
|
|
35
|
+
|
|
19
36
|
/**
|
|
20
37
|
* Write formatted JSON
|
|
21
38
|
*/
|
|
22
|
-
function writeJSON(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
JSON.stringify(data, null, 2) + '\n',
|
|
26
|
-
'utf8'
|
|
27
|
-
);
|
|
39
|
+
function writeJSON(file, data) {
|
|
40
|
+
ensureDir(file);
|
|
41
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2) + '\n', 'utf8');
|
|
28
42
|
}
|
|
29
43
|
|
|
30
44
|
/**
|
|
31
|
-
* Sync snippets
|
|
45
|
+
* Sync snippets into javascript.json
|
|
32
46
|
*/
|
|
33
|
-
function
|
|
47
|
+
function syncToVSCode(targetFile) {
|
|
34
48
|
const sourceSnippets = readJSON(SOURCE_FILE);
|
|
35
|
-
const
|
|
49
|
+
const userSnippets = readJSON(targetFile);
|
|
36
50
|
|
|
37
51
|
let added = 0;
|
|
38
52
|
let updated = 0;
|
|
39
53
|
|
|
40
|
-
for (const [
|
|
41
|
-
if (!
|
|
54
|
+
for (const [key, snippet] of Object.entries(sourceSnippets)) {
|
|
55
|
+
if (!userSnippets[key]) {
|
|
42
56
|
added++;
|
|
43
|
-
} else if (JSON.stringify(
|
|
57
|
+
} else if (JSON.stringify(userSnippets[key]) !== JSON.stringify(snippet)) {
|
|
44
58
|
updated++;
|
|
45
59
|
}
|
|
46
|
-
|
|
47
|
-
vscSnippets[name] = snippet;
|
|
60
|
+
userSnippets[key] = snippet;
|
|
48
61
|
}
|
|
49
62
|
|
|
50
|
-
writeJSON(
|
|
63
|
+
writeJSON(targetFile, userSnippets);
|
|
51
64
|
|
|
52
|
-
console.log(`✔
|
|
53
|
-
console.log(
|
|
54
|
-
console.log(
|
|
65
|
+
console.log(`✔ Synced: ${targetFile}`);
|
|
66
|
+
console.log(` ➕ Added: ${added}`);
|
|
67
|
+
console.log(` 🔄 Updated: ${updated}`);
|
|
55
68
|
}
|
|
56
69
|
|
|
57
70
|
/**
|
|
58
71
|
* Run
|
|
59
72
|
*/
|
|
60
73
|
try {
|
|
61
|
-
|
|
74
|
+
for (const target of TARGETS) {
|
|
75
|
+
syncToVSCode(target);
|
|
76
|
+
}
|
|
62
77
|
} catch (err) {
|
|
63
|
-
console.error('❌
|
|
78
|
+
console.error('❌ Snippet sync failed');
|
|
64
79
|
console.error(err);
|
|
65
80
|
process.exit(1);
|
|
66
81
|
}
|