|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const { execSync } = require('child_process'); |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | + |
| 7 | +console.log('🔄 Setting up Jest...'); |
| 8 | + |
| 9 | +// Ensure src directory exists |
| 10 | +if (!fs.existsSync('src')) { |
| 11 | + fs.mkdirSync('src'); |
| 12 | + console.log('✅ Created src directory'); |
| 13 | +} |
| 14 | + |
| 15 | +// Clean up Mocha files |
| 16 | +try { |
| 17 | + if (fs.existsSync('.mocharc.json')) { |
| 18 | + fs.unlinkSync('.mocharc.json'); |
| 19 | + console.log('✅ Removed .mocharc.json'); |
| 20 | + } |
| 21 | +} catch (error) { |
| 22 | + console.log('⚠️ Could not remove .mocharc.json:', error.message); |
| 23 | +} |
| 24 | + |
| 25 | +// Uninstall Mocha dependencies |
| 26 | +console.log('📦 Uninstalling Mocha dependencies...'); |
| 27 | +try { |
| 28 | + execSync('npm uninstall @types/chai @types/mocha chai mocha', { stdio: 'inherit' }); |
| 29 | + console.log('✅ Mocha dependencies uninstalled'); |
| 30 | +} catch (error) { |
| 31 | + console.log('⚠️ Some Mocha dependencies may not have been installed'); |
| 32 | +} |
| 33 | + |
| 34 | +// Install Jest dependencies |
| 35 | +console.log('📦 Installing Jest dependencies...'); |
| 36 | +try { |
| 37 | + execSync('npm install --save-dev jest ts-jest @types/jest', { stdio: 'inherit' }); |
| 38 | + console.log('✅ Jest dependencies installed'); |
| 39 | +} catch (error) { |
| 40 | + console.error('❌ Failed to install Jest dependencies:', error.message); |
| 41 | + process.exit(1); |
| 42 | +} |
| 43 | + |
| 44 | +// Create Jest config |
| 45 | +try { |
| 46 | + const jestConfig = `const { createDefaultPreset } = require("ts-jest"); |
| 47 | +
|
| 48 | +const tsJestTransformCfg = createDefaultPreset().transform; |
| 49 | +
|
| 50 | +/** @type {import("jest").Config} **/ |
| 51 | +module.exports = { |
| 52 | + testEnvironment: "node", |
| 53 | + transform: { |
| 54 | + ...tsJestTransformCfg, |
| 55 | + }, |
| 56 | +};`; |
| 57 | + fs.writeFileSync('jest.config.js', jestConfig); |
| 58 | + console.log('✅ Created jest.config.js'); |
| 59 | +} catch (error) { |
| 60 | + console.error('❌ Failed to setup Jest config:', error.message); |
| 61 | + process.exit(1); |
| 62 | +} |
| 63 | + |
| 64 | +// Create Jest test example |
| 65 | +try { |
| 66 | + const jestTest = `// This is a sample test written for Jest. |
| 67 | +// |
| 68 | +// If you prefer using Mocha+Chai instead, run \`npm run init:mocha\` in the terminal to setup Mocha dependencies. |
| 69 | +// This will switch your project to use Mocha as the test runner with Chai for assertions. |
| 70 | +
|
| 71 | +import assert from "assert"; |
| 72 | +
|
| 73 | +describe('Jest Test suite', function () { |
| 74 | + it('should expect to add', function () { |
| 75 | + assert.equal(2 + 3, 5); |
| 76 | + }); |
| 77 | +});`; |
| 78 | + fs.writeFileSync('src/main.test.ts', jestTest); |
| 79 | + console.log('✅ Created Jest test example in src/main.test.ts'); |
| 80 | +} catch (error) { |
| 81 | + console.error('❌ Failed to setup Jest test:', error.message); |
| 82 | + process.exit(1); |
| 83 | +} |
| 84 | + |
| 85 | +// Update package.json test script |
| 86 | +try { |
| 87 | + const packageJsonPath = 'package.json'; |
| 88 | + if (fs.existsSync(packageJsonPath)) { |
| 89 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 90 | + packageJson.scripts = packageJson.scripts || {}; |
| 91 | + packageJson.scripts.test = 'jest'; |
| 92 | + fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); |
| 93 | + console.log('✅ Updated package.json test script'); |
| 94 | + } |
| 95 | +} catch (error) { |
| 96 | + console.error('❌ Failed to update package.json:', error.message); |
| 97 | +} |
| 98 | + |
| 99 | +console.log('🎉 Jest setup complete! Reloading window now ...'); |
| 100 | + |
| 101 | + |
| 102 | +try { |
| 103 | + const vscodeDir = '.vscode'; |
| 104 | + const settingsFile = path.join(vscodeDir, 'settings.json'); |
| 105 | + |
| 106 | + if (!fs.existsSync(vscodeDir)) { |
| 107 | + fs.mkdirSync(vscodeDir); |
| 108 | + } |
| 109 | + |
| 110 | + let settings = {}; |
| 111 | + if (fs.existsSync(settingsFile)) { |
| 112 | + try { |
| 113 | + const settingsContent = fs.readFileSync(settingsFile, 'utf8'); |
| 114 | + settings = JSON.parse(settingsContent); |
| 115 | + } catch (error) { |
| 116 | + console.log('⚠️ Could not parse existing settings.json, creating new one'); |
| 117 | + settings = {}; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Increment reload trigger counter |
| 122 | + const currentCounter = settings["live-interview-companion.reloadTriggerCounter"] || 0; |
| 123 | + settings["live-interview-companion.reloadTriggerCounter"] = currentCounter + 1; |
| 124 | + |
| 125 | + // Write updated settings |
| 126 | + console.log(`🔄 Reloading workspace (counter: ${currentCounter + 1})`); |
| 127 | + fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2)); |
| 128 | +} catch (error) { |
| 129 | + console.log('⚠️ Could not reload workspace:', error.message); |
| 130 | + console.log('Please reload the window manually...'); |
| 131 | +} |
| 132 | + |
| 133 | +console.log('✅ Reloaded workspace'); |
| 134 | +console.log('🎉 Workspace setup complete! Run "npm test" to run tests.'); |
0 commit comments