|
| 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(); |
0 commit comments