node-shortcuts 1.0.4 → 1.0.5
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 +4 -2
- package/sync-snippets.js +57 -29
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-shortcuts",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Node Shortcut snippets",
|
|
5
5
|
"main": "sync-snippets.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
|
|
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"
|
package/sync-snippets.js
CHANGED
|
@@ -5,37 +5,63 @@ import fs from 'fs';
|
|
|
5
5
|
import path from 'path';
|
|
6
6
|
import os from 'os';
|
|
7
7
|
|
|
8
|
+
const HOME = os.homedir();
|
|
9
|
+
|
|
8
10
|
/**
|
|
9
|
-
*
|
|
11
|
+
* Where VS Code stores user snippets (cross-platform)
|
|
10
12
|
*/
|
|
11
|
-
|
|
13
|
+
function getSnippetTargets() {
|
|
14
|
+
const platform = process.platform;
|
|
12
15
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
if (platform === 'win32') {
|
|
17
|
+
return [
|
|
18
|
+
path.join(HOME, 'AppData', 'Roaming', 'Code', 'User', 'snippets', 'javascript.json'),
|
|
19
|
+
path.join(HOME, 'AppData', 'Roaming', 'Code - Insiders', 'User', 'snippets', 'javascript.json')
|
|
20
|
+
];
|
|
21
|
+
}
|
|
17
22
|
|
|
18
|
-
|
|
23
|
+
if (platform === 'darwin') {
|
|
24
|
+
return [
|
|
25
|
+
path.join(HOME, 'Library', 'Application Support', 'Code', 'User', 'snippets', 'javascript.json'),
|
|
26
|
+
path.join(HOME, 'Library', 'Application Support', 'Code - Insiders', 'User', 'snippets', 'javascript.json')
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// linux
|
|
31
|
+
return [
|
|
32
|
+
path.join(HOME, '.config', 'Code', 'User', 'snippets', 'javascript.json'),
|
|
33
|
+
path.join(HOME, '.config', 'Code - Insiders', 'User', 'snippets', 'javascript.json')
|
|
34
|
+
];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const TARGETS = getSnippetTargets();
|
|
38
|
+
const SOURCE_FILE = path.resolve('./snippets/node.json');
|
|
19
39
|
|
|
20
40
|
/**
|
|
21
|
-
* Read
|
|
41
|
+
* Read JSONC safely (VS Code compatible)
|
|
22
42
|
*/
|
|
23
43
|
function readJSON(file) {
|
|
24
44
|
if (!fs.existsSync(file)) return {};
|
|
25
|
-
|
|
26
|
-
|
|
45
|
+
|
|
46
|
+
let raw = fs.readFileSync(file, 'utf8');
|
|
47
|
+
if (!raw.trim()) return {};
|
|
48
|
+
|
|
49
|
+
// Strip // comments
|
|
50
|
+
raw = raw.replace(/\/\/.*$/gm, '');
|
|
51
|
+
|
|
52
|
+
// Strip /* */ comments
|
|
53
|
+
raw = raw.replace(/\/\*[\s\S]*?\*\//g, '');
|
|
54
|
+
|
|
55
|
+
// Strip trailing commas
|
|
56
|
+
raw = raw.replace(/,\s*([}\]])/g, '$1');
|
|
57
|
+
|
|
58
|
+
return JSON.parse(raw);
|
|
27
59
|
}
|
|
28
60
|
|
|
29
|
-
/**
|
|
30
|
-
* Ensure directory exists
|
|
31
|
-
*/
|
|
32
61
|
function ensureDir(file) {
|
|
33
62
|
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
34
63
|
}
|
|
35
64
|
|
|
36
|
-
/**
|
|
37
|
-
* Write formatted JSON
|
|
38
|
-
*/
|
|
39
65
|
function writeJSON(file, data) {
|
|
40
66
|
ensureDir(file);
|
|
41
67
|
fs.writeFileSync(file, JSON.stringify(data, null, 2) + '\n', 'utf8');
|
|
@@ -44,25 +70,23 @@ function writeJSON(file, data) {
|
|
|
44
70
|
/**
|
|
45
71
|
* Sync snippets into javascript.json
|
|
46
72
|
*/
|
|
47
|
-
function
|
|
48
|
-
const
|
|
49
|
-
const
|
|
73
|
+
function sync(target) {
|
|
74
|
+
const source = readJSON(SOURCE_FILE);
|
|
75
|
+
const user = readJSON(target);
|
|
50
76
|
|
|
51
77
|
let added = 0;
|
|
52
78
|
let updated = 0;
|
|
53
79
|
|
|
54
|
-
for (const [key, snippet] of Object.entries(
|
|
55
|
-
if (!
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
userSnippets[key] = snippet;
|
|
80
|
+
for (const [key, snippet] of Object.entries(source)) {
|
|
81
|
+
if (!user[key]) added++;
|
|
82
|
+
else if (JSON.stringify(user[key]) !== JSON.stringify(snippet)) updated++;
|
|
83
|
+
|
|
84
|
+
user[key] = snippet;
|
|
61
85
|
}
|
|
62
86
|
|
|
63
|
-
writeJSON(
|
|
87
|
+
writeJSON(target, user);
|
|
64
88
|
|
|
65
|
-
console.log(`✔ Synced: ${
|
|
89
|
+
console.log(`✔ Synced: ${target}`);
|
|
66
90
|
console.log(` ➕ Added: ${added}`);
|
|
67
91
|
console.log(` 🔄 Updated: ${updated}`);
|
|
68
92
|
}
|
|
@@ -72,7 +96,11 @@ function syncToVSCode(targetFile) {
|
|
|
72
96
|
*/
|
|
73
97
|
try {
|
|
74
98
|
for (const target of TARGETS) {
|
|
75
|
-
|
|
99
|
+
try {
|
|
100
|
+
sync(target);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.warn(`⚠ Skipped invalid snippet file: ${target}`);
|
|
103
|
+
}
|
|
76
104
|
}
|
|
77
105
|
} catch (err) {
|
|
78
106
|
console.error('❌ Snippet sync failed');
|