1+ # Windows PowerShell script for testing setNodeModulesBinPermissions functionality
2+
3+ Write-Host " === Testing setNodeModulesBinPermissions Functionality ==="
4+
5+ # Change to the nodejs directory
6+ Set-Location - Path " $PSScriptRoot "
7+
8+ # Run the function locally first
9+ Write-Host " Running permissions validation test..." - ForegroundColor Green
10+ cd test-permission - code
11+
12+ # Check if node_modules exists, install if not
13+ if (-not (Test-Path " node_modules" )) {
14+ Write-Host " Installing dependencies..."
15+ npm install
16+ }
17+
18+ # Function to check and record permissions
19+ function Check-Permissions {
20+ param ([string ]$Label )
21+
22+ Write-Host " [$Label ] Checking permissions..." - ForegroundColor Yellow
23+
24+ # Create temporary JS file for checking permissions
25+ $jsCode = @"
26+ const fs = require('fs');
27+ const path = require('path');
28+
29+ console.log('=== ' + process.argv[1] + ' Permissions State ===');
30+
31+ const binPath = path.join('.', 'node_modules', '.bin');
32+ if (fs.existsSync(binPath)) {
33+ console.log('✓ node_modules/.bin found');
34+ const files = fs.readdirSync(binPath);
35+
36+ let executableCount = 0;
37+ let totalCount = 0;
38+
39+ // Record permissions of first 5 files
40+ files.slice(0, 5).forEach(file => {
41+ const filePath = path.join(binPath, file);
42+ try {
43+ const stat = fs.statSync(filePath);
44+ if (!stat.isDirectory()) {
45+ totalCount++;
46+ // Cross-platform executable check
47+ let hasExecutePermission = false;
48+
49+ if (process.platform === 'win32') {
50+ // On Windows, check file extension for common executable types
51+ const ext = path.extname(file).toLowerCase();
52+ hasExecutePermission = (
53+ ext === '.exe' ||
54+ ext === '.cmd' ||
55+ ext === '.bat' ||
56+ ext === '.ps1' ||
57+ ext === '.com' ||
58+ ext === '.msi'
59+ );
60+ } else {
61+ // Unix way
62+ hasExecutePermission = (stat.mode & 0o111) !== 0;
63+ }
64+
65+ if (hasExecutePermission) {
66+ executableCount++;
67+ }
68+ console.log(file + ': ' + (hasExecutePermission ? 'EXECUTABLE' : 'NON-EXECUTABLE') + ' (mode: ' + stat.mode.toString(8) + ', platform: ' + process.platform + ')');
69+ } else {
70+ console.log(file + ': DIRECTORY');
71+ }
72+ } catch (e) {
73+ console.log(file + ': ERROR - ' + e.message);
74+ }
75+ });
76+
77+ // Return result for validation
78+ console.log('=== RESULT_FOR_VALIDATION ===');
79+ console.log('TOTAL_FILES:' + totalCount);
80+ console.log('EXECUTABLE_FILES:' + executableCount);
81+ console.log('=== END_RESULT ===');
82+ } else {
83+ console.log('node_modules/.bin not found');
84+ console.log('=== RESULT_FOR_VALIDATION ===');
85+ console.log('TOTAL_FILES:0');
86+ console.log('EXECUTABLE_FILES:0');
87+ console.log('=== END_RESULT ===');
88+ }
89+
90+ console.log('=== ' + process.argv[1] + ' State Recorded ===');
91+ "@
92+
93+ # Save to temp file and execute
94+ $tempFile = [System.IO.Path ]::GetTempFileName() + " .js"
95+ $jsCode | Out-File - FilePath $tempFile - Encoding UTF8
96+ try {
97+ $result = node $tempFile $Label
98+ return $result
99+ } finally {
100+ if (Test-Path $tempFile ) {
101+ Remove-Item $tempFile - Force
102+ }
103+ }
104+ }
105+
106+ # Record initial permissions
107+ $resultLines = Check- Permissions " BEFORE"
108+
109+ # Parse initial validation results
110+ $initialTotalFiles = 0
111+ $initialExecutableFiles = 0
112+
113+ foreach ($line in $resultLines ) {
114+ if ($line -match " TOTAL_FILES:(\d+)" ) {
115+ $initialTotalFiles = [int ]$matches [1 ]
116+ }
117+ if ($line -match " EXECUTABLE_FILES:(\d+)" ) {
118+ $initialExecutableFiles = [int ]$matches [1 ]
119+ }
120+ }
121+
122+ Write-Host " [BEFORE] Found $initialExecutableFiles /$initialTotalFiles executable files" - ForegroundColor Cyan
123+
124+ # Modify permissions to make them non-executable (simulate issue)
125+ Write-Host " [MODIFY] Making files non-executable to test fix..." - ForegroundColor Yellow
126+
127+ $modifyJsCode = @"
128+ const fs = require('fs');
129+ const path = require('path');
130+
131+ const binPath = path.join('.', 'node_modules', '.bin');
132+ if (fs.existsSync(binPath)) {
133+ const files = fs.readdirSync(binPath);
134+
135+ // Remove executable permissions from first 3 files
136+ files.slice(0, 3).forEach(file => {
137+ const filePath = path.join(binPath, file);
138+ try {
139+ const stat = fs.statSync(filePath);
140+ if (!stat.isDirectory()) {
141+ // Remove executable bits
142+ const newMode = stat.mode & ~0o111;
143+ fs.chmodSync(filePath, newMode);
144+ console.log('✓ Made non-executable: ' + file + ' (new mode: ' + newMode.toString(8) + ')');
145+ }
146+ } catch (e) {
147+ console.log('✗ Error modifying ' + file + ': ' + e.message);
148+ }
149+ });
150+ }
151+ "@
152+
153+ # Save to temp file and execute
154+ $tempModifyFile = [System.IO.Path ]::GetTempFileName() + " .js"
155+ $modifyJsCode | Out-File - FilePath $tempModifyFile - Encoding UTF8
156+ try {
157+ node $tempModifyFile
158+ } finally {
159+ if (Test-Path $tempModifyFile ) {
160+ Remove-Item $tempModifyFile - Force
161+ }
162+ }
163+
164+ # Record permissions after modification
165+ $resultLines = Check- Permissions " AFTER_MODIFY"
166+
167+ # Parse modified validation results
168+ $modifiedTotalFiles = 0
169+ $modifiedExecutableFiles = 0
170+
171+ foreach ($line in $resultLines ) {
172+ if ($line -match " TOTAL_FILES:(\d+)" ) {
173+ $modifiedTotalFiles = [int ]$matches [1 ]
174+ }
175+ if ($line -match " EXECUTABLE_FILES:(\d+)" ) {
176+ $modifiedExecutableFiles = [int ]$matches [1 ]
177+ }
178+ }
179+
180+ Write-Host " [AFTER_MODIFY] Found $modifiedExecutableFiles /$modifiedTotalFiles executable files" - ForegroundColor Cyan
181+
182+ # Verify that we successfully reduced executable files
183+ if ($initialExecutableFiles -gt $modifiedExecutableFiles ) {
184+ Write-Host " SUCCESS: Successfully reduced executable files from $initialExecutableFiles to $modifiedExecutableFiles " - ForegroundColor Green
185+ } else {
186+ Write-Host " Warning: Executable file count not reduced (before: $initialExecutableFiles , after: $modifiedExecutableFiles )" - ForegroundColor Yellow
187+ }
188+
189+ Write-Host " === Pre-deployment setup completed ===" - ForegroundColor Cyan
190+ Write-Host " Now proceeding with: s deploy -y -t s_permission.yaml" - ForegroundColor Cyan
191+ Write-Host " After deployment, check if permissions were restored!" - ForegroundColor Cyan
192+
193+ cd ..
0 commit comments