Skip to content

Commit c1b6386

Browse files
committed
feat: added vscode settings patcher as an install script
1 parent 2fee3f3 commit c1b6386

File tree

5 files changed

+304
-27
lines changed

5 files changed

+304
-27
lines changed

libs/core/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
"scripts": {
5454
"build": "npm run build.stencil",
5555
"build.all": "run-s build.stencil build.storybook",
56-
"build.stencil": "stencil build --docs",
56+
"build.stencil": "stencil build --docs && mkdir -p dist/scripts && cp scripts/vscode-settings-patcher.cjs dist/scripts/",
57+
"postinstall": "node ./dist/scripts/vscode-settings-patcher.cjs || true",
5758
"build.storybook": "storybook build",
5859
"build.ts": "tsc -p scripts/tsconfig.json",
5960
"deploy": "npm run build.all",
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* VSCode Settings Patcher for Pine Design System
5+
*
6+
* This script automatically configures VSCode to use Pine's HTML custom data
7+
* for web component autocomplete. It runs as a postinstall hook.
8+
*
9+
* Features:
10+
* - Detects monorepo vs external package context
11+
* - Creates .vscode directory if needed
12+
* - Backs up existing settings.json before modifying
13+
* - Merges html.customData without duplicates
14+
* - Fails gracefully (won't break npm install)
15+
*/
16+
17+
const fs = require('fs');
18+
const path = require('path');
19+
20+
function patchVSCodeSettings() {
21+
try {
22+
// Find project root (where npm install was run)
23+
// When running as postinstall, process.cwd() is the consuming project's root
24+
// But when run from node_modules, we need to traverse up
25+
let projectRoot = process.cwd();
26+
27+
// If we're inside node_modules, traverse up to find project root
28+
if (projectRoot.includes('node_modules')) {
29+
projectRoot = projectRoot.split('node_modules')[0].replace(/[/\\]$/, '');
30+
}
31+
32+
// Detect context: monorepo vs external package
33+
const isMonorepo = fs.existsSync(path.join(projectRoot, 'libs/core/package.json'));
34+
35+
// Determine the correct path to the HTML custom data file
36+
const customDataPath = isMonorepo
37+
? './libs/core/dist/vscode.html-data.json'
38+
: './node_modules/@pine-ds/core/dist/vscode.html-data.json';
39+
40+
const vscodeDir = path.join(projectRoot, '.vscode');
41+
const settingsPath = path.join(vscodeDir, 'settings.json');
42+
const backupPath = path.join(vscodeDir, 'settings.json.backup');
43+
44+
// Create .vscode directory if it doesn't exist
45+
if (!fs.existsSync(vscodeDir)) {
46+
fs.mkdirSync(vscodeDir, { recursive: true });
47+
console.log('[pine-ds] Created .vscode directory');
48+
}
49+
50+
// Read existing settings or start fresh
51+
let settings = {};
52+
let existingContent = null;
53+
if (fs.existsSync(settingsPath)) {
54+
existingContent = fs.readFileSync(settingsPath, 'utf8');
55+
try {
56+
settings = JSON.parse(existingContent);
57+
} catch (e) {
58+
console.warn('[pine-ds] Warning: Could not parse existing settings.json, starting fresh');
59+
settings = {};
60+
}
61+
}
62+
63+
// Check if we need to add the custom data path
64+
const existingCustomData = settings['html.customData'] || [];
65+
if (!existingCustomData.includes(customDataPath)) {
66+
// Backup only if we're about to make changes
67+
if (existingContent !== null) {
68+
fs.writeFileSync(backupPath, existingContent);
69+
console.log(`[pine-ds] Backed up existing settings to ${path.relative(projectRoot, backupPath)}`);
70+
}
71+
72+
// Merge html.customData
73+
settings['html.customData'] = [...existingCustomData, customDataPath];
74+
75+
// Write updated settings
76+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n');
77+
console.log(`[pine-ds] VSCode settings updated with Pine HTML custom data: ${customDataPath}`);
78+
console.log('[pine-ds] Restart VSCode for autocomplete to take effect');
79+
} else {
80+
console.log('[pine-ds] Pine HTML custom data already configured in VSCode settings');
81+
}
82+
} catch (error) {
83+
// Don't fail the install if VSCode setup fails
84+
console.warn('[pine-ds] Warning: Could not set up VSCode HTML custom data:', error.message);
85+
console.warn('[pine-ds] You can manually add html.customData to your .vscode/settings.json');
86+
}
87+
}
88+
89+
patchVSCodeSettings();

libs/core/stencil.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export const config: Config = {
4848
// file: 'vscode-data.json',
4949
// },
5050
// Custom VS Code data generator (enhanced with full spec support)
51-
// Output to project root (outside libs/core to avoid watch loops)
52-
vscodeCustomDataOutputTarget('../../vscode.html-data.json'),
51+
// Output to dist/ so it's included in the npm package
52+
vscodeCustomDataOutputTarget('./dist/vscode.html-data.json'),
5353
{
5454
type: 'dist-hydrate-script',
5555
},

0 commit comments

Comments
 (0)