microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
gulp_scripts/smoke-build.js
92lines · modeblame
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 1 | const fs = require('fs'); |
| 2 | const path = require('path'); | |
| 3 | const { execSync } = require('child_process'); | |
| 4 | | |
| 5 | // Read package.json to compute VSIX file name as <name>-<version>.vsix | |
| 6 | const pkg = require('../package.json'); | |
| 7 | const vsixName = `${pkg.name}-${pkg.version}.vsix`; | |
| 8 | | |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 9 | // VSIX output path (saved at repo root temporarily; removed after copy) |
| 10 | const repoRoot = path.resolve(__dirname, '..'); | |
| 11 | const vsixSource = path.resolve(repoRoot, vsixName); | |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 12 | |
1f8dc9e5Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 13 | // Target directory for smoke tests (repo-relative, matches test expectations) |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 14 | const targetDir = path.resolve( |
1f8dc9e5Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 15 | repoRoot, |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 16 | 'test', |
| 17 | 'smoke', | |
| 18 | 'resources', | |
| 19 | 'extension' | |
| 20 | ); | |
| 21 | | |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 22 | function removeVsixFilesInDir(dir) { |
| 23 | if (!fs.existsSync(dir)) return; | |
| 24 | const entries = fs.readdirSync(dir); | |
| 25 | for (const entry of entries) { | |
| 26 | if (entry.toLowerCase().endsWith('.vsix')) { | |
| 27 | try { | |
| 28 | fs.unlinkSync(path.join(dir, entry)); | |
| 29 | console.log(`Removed VSIX: ${path.join(dir, entry)}`); | |
| 30 | } catch (e) { | |
| 31 | console.warn(`Failed to remove VSIX ${entry} in ${dir}: ${String(e)}`); | |
| 32 | } | |
| 33 | } | |
| 34 | } | |
| 35 | } | |
| 36 | | |
| 37 | function removeOtherVsixFilesExceptTarget(rootDir, targetDirAbs) { | |
| 38 | // Remove VSIX files in repo root and known locations except target directory | |
| 39 | // 1) repo root | |
| 40 | removeVsixFilesInDir(rootDir); | |
| 41 | // 2) test/smoke root | |
| 42 | removeVsixFilesInDir(path.join(rootDir, 'test')); | |
| 43 | removeVsixFilesInDir(path.join(rootDir, 'test', 'smoke')); | |
| 44 | removeVsixFilesInDir(path.join(rootDir, 'test', 'smoke', 'resources')); | |
| 45 | // 3) explicit extension target handled separately | |
| 46 | // Any accidental VSIX in node_modules or dist should be ignored/not expected. | |
| 47 | } | |
| 48 | | |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 49 | function buildVsix() { |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 50 | console.log('Packaging VSIX...'); |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 51 | execSync(`npx vsce package -o "${vsixSource}"`, { stdio: 'inherit' }); |
| 52 | if (!fs.existsSync(vsixSource)) { | |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 53 | throw new Error('VSIX not found. Ensure `vsce package` succeeded and entry file is not ignored.'); |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 54 | } |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 55 | console.log(`VSIX packaged: ${vsixSource}`); |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 56 | } |
| 57 | | |
| 58 | function copyVsix() { | |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 59 | console.log('Copying VSIX to smoke resources...'); |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 60 | if (!fs.existsSync(targetDir)) fs.mkdirSync(targetDir, { recursive: true }); |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 61 | // Ensure only one VSIX exists in target by cleaning any existing ones |
| 62 | removeVsixFilesInDir(targetDir); | |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 63 | const targetPath = path.join(targetDir, path.basename(vsixSource)); |
| 64 | fs.copyFileSync(vsixSource, targetPath); | |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 65 | console.log(`Copied to: ${targetPath}`); |
| 66 | // Remove the source VSIX from repo root to avoid duplicates elsewhere | |
| 67 | try { | |
| 68 | fs.unlinkSync(vsixSource); | |
| 69 | console.log(`Removed source VSIX from repo root: ${vsixSource}`); | |
| 70 | } catch (e) { | |
| 71 | console.warn(`Failed to remove source VSIX at repo root: ${String(e)}`); | |
| 72 | } | |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 73 | } |
| 74 | | |
| 75 | function runSmokeTests() { | |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 76 | console.log('Running smoke tests...'); |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 77 | execSync('npm run smoke-tests', { stdio: 'inherit' }); |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 78 | console.log('Smoke tests completed'); |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 79 | } |
| 80 | | |
| 81 | try { | |
bdbc6a68Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)6 months ago | 82 | // Clean any stray VSIX files in common locations before building |
| 83 | removeOtherVsixFilesExceptTarget(repoRoot, targetDir); | |
617154f8Zhen Zhen Yuan (BEYONDSOFT CONSULTING INC)8 months ago | 84 | buildVsix(); |
| 85 | copyVsix(); | |
| 86 | if (process.argv.includes('--test')) { | |
| 87 | runSmokeTests(); | |
| 88 | } | |
| 89 | } catch (err) { | |
| 90 | console.error(err.message || err); | |
| 91 | process.exit(1); | |
| 92 | } |