Path: blob/main/build/lib/policies/exportPolicyData.ts
13383 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { execSync, execFileSync } from 'child_process';6import { resolve } from 'path';78const rootPath = resolve(import.meta.dirname, '..', '..', '..');910// VS Code OAuth app client ID (same as the GitHub Authentication extension)11const CLIENT_ID = '01ab8ac9400c4e429b23';1213/**14* Acquires a GitHub token via the OAuth device flow.15* Opens the browser for the user to authorize, then polls for the token.16*/17async function acquireTokenViaDeviceFlow(): Promise<string> {18const response1 = await (await fetch('https://github.com/login/device/code', {19method: 'POST',20body: JSON.stringify({ client_id: CLIENT_ID, scope: 'repo' }),21headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },22})).json() as { user_code: string; device_code: string; verification_uri: string; expires_in: number; interval: number };2324console.log(`\n Copy this code: ${response1.user_code}`);25console.log(` Then open: ${response1.verification_uri}`);26console.log(` Waiting for authorization (up to ${response1.expires_in}s)...\n`);2728let expiresIn = response1.expires_in;29while (expiresIn > 0) {30await new Promise(resolve => setTimeout(resolve, 1000 * response1.interval));31expiresIn -= response1.interval;3233const response2 = await (await fetch('https://github.com/login/oauth/access_token', {34method: 'POST',35body: JSON.stringify({36client_id: CLIENT_ID,37device_code: response1.device_code,38grant_type: 'urn:ietf:params:oauth:grant-type:device_code',39}),40headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' },41})).json() as { access_token?: string };4243if (response2.access_token) {44return response2.access_token;45}46}4748throw new Error('Timed out waiting for GitHub authorization');49}5051// Ensure sources are transpiled52console.log('Transpiling client sources...');53execSync('npm run transpile-client', { cwd: rootPath, stdio: 'inherit' });5455// Set up GITHUB_TOKEN if not already set56if (!process.env['GITHUB_TOKEN'] && !process.env['DISTRO_PRODUCT_JSON']) {57// Try gh CLI first (fast, non-interactive)58let token: string | undefined;59try {60token = execFileSync('gh', ['auth', 'token'], { encoding: 'utf8' }).trim();61console.log('Set GITHUB_TOKEN from gh CLI.');62} catch {63// Fall back to OAuth device flow (interactive)64console.log('gh CLI not available, starting GitHub OAuth device flow...');65token = await acquireTokenViaDeviceFlow();66console.log('GitHub authorization successful.');67}6869process.env['GITHUB_TOKEN'] = token;70}7172// Run the export73console.log('Exporting policy data...');74const codeScript = process.platform === 'win32'75? resolve(rootPath, 'scripts', 'code.bat')76: resolve(rootPath, 'scripts', 'code.sh');7778execSync(`"${codeScript}" --export-policy-data`, {79cwd: rootPath,80stdio: 'inherit',81env: process.env,82});8384console.log('\nPolicy data exported to build/lib/policies/policyData.jsonc');858687