|  | 
|  | 1 | +#!/usr/bin/env node | 
|  | 2 | + | 
|  | 3 | +const {exec} = require('child_process'); | 
|  | 4 | +const fs = require('fs'); | 
|  | 5 | +const path = require('path'); | 
|  | 6 | +const process = require('process'); | 
|  | 7 | +const util = require('util'); | 
|  | 8 | + | 
|  | 9 | +const execPromise = util.promisify(exec); | 
|  | 10 | + | 
|  | 11 | +async function main() { | 
|  | 12 | +	// Empty print line to pretty-ify command line output | 
|  | 13 | +	console.log(); | 
|  | 14 | + | 
|  | 15 | +	// Copy webview test environment locally if it does not already exist | 
|  | 16 | +	if (!fs.existsSync('./test-webview')) { | 
|  | 17 | +		try { | 
|  | 18 | +			console.log(color(['dim'], 'Copying webview test environment locally...')); | 
|  | 19 | +			await execShellCommand('npx degit microsoft/vscode-webview-ui-toolkit-samples/all-components test-webview'); | 
|  | 20 | +		} catch (err) { | 
|  | 21 | +			console.log(`${color(['red'], 'Error: Could not copy webview test environment locally')}\n    ${err}`); | 
|  | 22 | +			process.exit(); | 
|  | 23 | +		} | 
|  | 24 | +	} | 
|  | 25 | + | 
|  | 26 | +	// Install the webview test environment dependencies if they do not exist | 
|  | 27 | +	if (!fs.existsSync('./test-webview/node_modules')) { | 
|  | 28 | +		try { | 
|  | 29 | +			console.log(color(['dim'], 'Installing webview test environment dependencies...')); | 
|  | 30 | +			await execShellCommand('cd ./test-webview && npm install'); | 
|  | 31 | +		} catch (err) { | 
|  | 32 | +			console.log(`${color(['red'], 'Error: Could not install webview test environment dependencies')}\n    ${err}`); | 
|  | 33 | +			process.exit(); | 
|  | 34 | +		} | 
|  | 35 | +	} | 
|  | 36 | + | 
|  | 37 | +	// Copy latest toolkit build into the webview test environment | 
|  | 38 | +	console.log(color(['dim'], 'Copying latest toolkit build into webview test environment...')); | 
|  | 39 | +	delDir('./test-webview/node_modules/@vscode/webview-ui-toolkit/dist'); | 
|  | 40 | +	createDir('./test-webview/node_modules/@vscode/webview-ui-toolkit/dist'); | 
|  | 41 | +	copyDir('./dist', './test-webview/node_modules/@vscode/webview-ui-toolkit'); | 
|  | 42 | + | 
|  | 43 | +	// Print success and next steps messages | 
|  | 44 | +	console.log(); | 
|  | 45 | +	console.log(color(['bold', 'green'], 'Webview test environment successfully configured!')); | 
|  | 46 | +	console.log(); | 
|  | 47 | +	console.log('Next steps:'); | 
|  | 48 | +	console.log(`  1. Open the ${color(['cyan'], 'test-webview')} folder in a new VS Code window`); | 
|  | 49 | +	console.log(`  2. Press ${color(['cyan'], 'F5')} to open the webview test extension with the most recent toolkit build loaded`); | 
|  | 50 | +	console.log(`  3. Run the "${color(['cyan'], 'Webview UI Toolkit: All Components')}" command using the VS Code command palette`); | 
|  | 51 | +	console.log(); | 
|  | 52 | +} | 
|  | 53 | + | 
|  | 54 | +async function execShellCommand(command) { | 
|  | 55 | +	return await execPromise(command); | 
|  | 56 | +} | 
|  | 57 | + | 
|  | 58 | +function delDir(path) { | 
|  | 59 | +	if (fs.existsSync(path) && fs.lstatSync(path).isDirectory()) { | 
|  | 60 | +		fs.readdirSync(path).forEach(function (file, index) { | 
|  | 61 | +			const currPath = path + '/' + file; | 
|  | 62 | +			if (fs.lstatSync(currPath).isDirectory()) { | 
|  | 63 | +				delDir(currPath); | 
|  | 64 | +			} else { | 
|  | 65 | +				fs.unlinkSync(currPath); | 
|  | 66 | +			} | 
|  | 67 | +		}); | 
|  | 68 | +		fs.rmdirSync(path); | 
|  | 69 | +	} | 
|  | 70 | +} | 
|  | 71 | + | 
|  | 72 | +function createDir(dir) { | 
|  | 73 | +	if (!fs.existsSync(dir)) { | 
|  | 74 | +		fs.mkdirSync(dir); | 
|  | 75 | +	} | 
|  | 76 | +} | 
|  | 77 | + | 
|  | 78 | +function copyDir(source, target) { | 
|  | 79 | +	let files = []; | 
|  | 80 | +	const targetFolder = path.join(target, path.basename(source)); | 
|  | 81 | +	if (!fs.existsSync(targetFolder)) { | 
|  | 82 | +		fs.mkdirSync(targetFolder); | 
|  | 83 | +	} | 
|  | 84 | +	if (fs.lstatSync(source).isDirectory()) { | 
|  | 85 | +		files = fs.readdirSync(source); | 
|  | 86 | +		files.forEach(function (file) { | 
|  | 87 | +			const curSource = path.join(source, file); | 
|  | 88 | +			if (fs.lstatSync(curSource).isDirectory()) { | 
|  | 89 | +				copyDir(curSource, targetFolder); | 
|  | 90 | +			} else { | 
|  | 91 | +				copyFileSync(curSource, targetFolder); | 
|  | 92 | +			} | 
|  | 93 | +		}); | 
|  | 94 | +	} | 
|  | 95 | +} | 
|  | 96 | + | 
|  | 97 | +function copyFileSync(source, target) { | 
|  | 98 | +	let targetFile = target; | 
|  | 99 | +	if (fs.existsSync(target)) { | 
|  | 100 | +		if (fs.lstatSync(target).isDirectory()) { | 
|  | 101 | +			targetFile = path.join(target, path.basename(source)); | 
|  | 102 | +		} | 
|  | 103 | +	} | 
|  | 104 | +	fs.writeFileSync(targetFile, fs.readFileSync(source)); | 
|  | 105 | +} | 
|  | 106 | + | 
|  | 107 | +const colors = { | 
|  | 108 | +	reset: '\x1b[0m', | 
|  | 109 | +	bold: '\x1b[1m', | 
|  | 110 | +	dim: '\x1b[2m', | 
|  | 111 | +	red: '\x1b[31m', | 
|  | 112 | +	green: '\x1b[32m', | 
|  | 113 | +	cyan: '\x1b[36m', | 
|  | 114 | +}; | 
|  | 115 | + | 
|  | 116 | +function color(opts, text) { | 
|  | 117 | +	let colorString = ''; | 
|  | 118 | +	for (const opt of opts) { | 
|  | 119 | +		colorString += colors[opt]; | 
|  | 120 | +	} | 
|  | 121 | +	return `${colorString}${text}${colors.reset}`; | 
|  | 122 | +} | 
|  | 123 | + | 
|  | 124 | +main(); | 
0 commit comments