|
2 | 2 | // Non-functional change to trigger release workflow |
3 | 3 |
|
4 | 4 | import { existsSync, mkdirSync, createWriteStream, chmodSync, readFileSync, readSync, writeFileSync, unlinkSync, statSync, openSync, closeSync, copyFileSync, fsyncSync, renameSync, realpathSync } from 'fs'; |
5 | | -import { join, dirname } from 'path'; |
| 5 | +import { join, dirname, resolve } from 'path'; |
6 | 6 | import { fileURLToPath } from 'url'; |
7 | 7 | import { get } from 'https'; |
8 | 8 | import { platform, arch, tmpdir } from 'os'; |
@@ -288,13 +288,21 @@ function validateDownloadedBinary(p) { |
288 | 288 | } |
289 | 289 | } |
290 | 290 |
|
291 | | -async function main() { |
| 291 | +export async function runPostinstall(options = {}) { |
| 292 | + const { skipGlobalAlias = false, invokedByRuntime = false } = options; |
| 293 | + if (process.env.CODE_POSTINSTALL_DRY_RUN === '1') { |
| 294 | + return { skipped: true }; |
| 295 | + } |
| 296 | + |
| 297 | + if (invokedByRuntime) { |
| 298 | + process.env.CODE_RUNTIME_POSTINSTALL = process.env.CODE_RUNTIME_POSTINSTALL || '1'; |
| 299 | + } |
292 | 300 | // Detect potential PATH conflict with an existing `code` command (e.g., VS Code) |
293 | 301 | // Only relevant for global installs; skip for npx/local installs to keep postinstall fast. |
294 | 302 | const ua = process.env.npm_config_user_agent || ''; |
295 | 303 | const isNpx = ua.includes('npx'); |
296 | 304 | const isGlobal = process.env.npm_config_global === 'true'; |
297 | | - if (isGlobal && !isNpx) { |
| 305 | + if (!skipGlobalAlias && isGlobal && !isNpx) { |
298 | 306 | try { |
299 | 307 | const whichCmd = process.platform === 'win32' ? 'where code' : 'command -v code || which code || true'; |
300 | 308 | const resolved = execSync(whichCmd, { stdio: ['ignore', 'pipe', 'ignore'], shell: process.platform !== 'win32' }).toString().split(/\r?\n/).filter(Boolean)[0]; |
@@ -760,7 +768,19 @@ async function main() { |
760 | 768 | } |
761 | 769 | } |
762 | 770 |
|
763 | | -main().catch(error => { |
764 | | - console.error('Installation failed:', error); |
765 | | - process.exit(1); |
766 | | -}); |
| 771 | +function isExecutedDirectly() { |
| 772 | + const entry = process.argv[1]; |
| 773 | + if (!entry) return false; |
| 774 | + try { |
| 775 | + return resolve(entry) === fileURLToPath(import.meta.url); |
| 776 | + } catch { |
| 777 | + return false; |
| 778 | + } |
| 779 | +} |
| 780 | + |
| 781 | +if (isExecutedDirectly()) { |
| 782 | + runPostinstall().catch(error => { |
| 783 | + console.error('Installation failed:', error); |
| 784 | + process.exit(1); |
| 785 | + }); |
| 786 | +} |
0 commit comments