Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit f87f85a

Browse files
Automate toolkit test set up in a webview extension environment (#317)
Description of changes Creates a Node script that automates the steps required to copy a new toolkit build into a webview extension environment so that it can be manually/visually tested.
1 parent a0ce5e9 commit f87f85a

File tree

9 files changed

+149
-6
lines changed

9 files changed

+149
-6
lines changed

.eslintignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ coverage
99
# don't lint storybook files
1010
.storybook/
1111
# don't lint stories
12-
*.stories.*
12+
*.stories.*
13+
# don't lint scripts folder
14+
scripts
15+
# don't lint webview test environment
16+
test-webview

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ node_modules/
55
dist/
66
storybook-static/
77

8+
# Tests
9+
test-webview
10+
811
# Misc
912
.DS_Store
1013
.vscode

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CONTRIBUTING.md
1111
# Tests
1212
*.spec.*
1313
coverage/
14+
test-webview
1415

1516
# Builds
1617
build/

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ build/
66
dist/
77
storybook-static/
88

9+
# Tests
10+
test-webview
11+
912
# Misc
1013
.DS_Store
1114
.vscode

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
"options": {
3232
"printWidth": 200
3333
}
34+
},
35+
{
36+
"files": "scripts/setup-webview-test-env.js",
37+
"options": {
38+
"printWidth": 200
39+
}
3440
}
3541
]
3642
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"lint": "eslint . --ext .ts",
2727
"lint:fix": "eslint . --ext .ts --fix",
2828
"prepublishOnly": "npm run build",
29-
"test": "jest --verbose --coverage"
29+
"test": "jest --verbose --coverage",
30+
"test:webview": "npm run build && node ./scripts/setup-webview-test-env.js"
3031
},
3132
"dependencies": {
3233
"@microsoft/fast-element": "^1.6.0",

scripts/setup-webview-test-env.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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();

tsconfig.eslint.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": "./tsconfig.json",
3-
"include": ["src"],
4-
"exclude": ["node_modules"]
5-
}
2+
"extends": "./tsconfig.json",
3+
"include": ["src"],
4+
"exclude": ["node_modules", "test-webview"]
5+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"include": ["src"],
2424
"exclude": [
2525
"node_modules",
26+
"test-webview",
2627
"src/**/*.spec.ts",
2728
"src/**/*.stories.ts",
2829
"src/**/fixtures/",

0 commit comments

Comments
 (0)