Skip to content

Commit 289a6b1

Browse files
committed
feat: add resolveCliArgsFromVSCodeExecutablePath
1 parent fd0e599 commit 289a6b1

File tree

6 files changed

+55
-29
lines changed

6 files changed

+55
-29
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 2.0.2 | 2021-12-29
4+
5+
- Add `resolveCliArgsFromVSCodeExecutablePath`
6+
37
### 2.0.1 | 2021-12-29
48

59
- Fix extra new lines added to test output

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ async function go() {
8282
/**
8383
* Install Python extension
8484
*/
85-
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath)
86-
cp.spawnSync(cliPath, ['--install-extension', 'ms-python.python'], {
85+
const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
86+
cp.spawnSync(cli, [...args, '--install-extension', 'ms-python.python'], {
8787
encoding: 'utf-8',
8888
stdio: 'inherit'
89-
})
89+
});
9090

9191
/**
9292
* - Add additional launch flags for VS Code

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55

66
export { download, downloadAndUnzipVSCode } from './download';
77
export { runTests } from './runTest';
8-
export { resolveCliPathFromVSCodeExecutablePath } from './util';
8+
export { resolveCliPathFromVSCodeExecutablePath, resolveCliArgsFromVSCodeExecutablePath } from './util';

lib/runTest.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,26 @@ export async function runTests(options: TestOptions): Promise<number> {
113113
}
114114

115115
if (!options.reuseMachineInstall) {
116-
if (!hasArg('extensions-dir', args)) {
117-
args.push(`--extensions-dir=${path.join(defaultCachePath, 'extensions')}`)
118-
}
119-
120-
if (!hasArg('user-data-dir', args)) {
121-
args.push(`--user-data-dir=${path.join(defaultCachePath, 'user-data')}`)
122-
}
116+
args.push(...getProfileArguments(args));
123117
}
124118

125119
return innerRunTests(options.vscodeExecutablePath, args, options.extensionTestsEnv);
126120
}
127121

122+
/** Adds the extensions and user data dir to the arguments for the VS Code CLI */
123+
export function getProfileArguments(args: readonly string[]) {
124+
const out: string[] = [];
125+
if (!hasArg('extensions-dir', args)) {
126+
out.push(`--extensions-dir=${path.join(defaultCachePath, 'extensions')}`)
127+
}
128+
129+
if (!hasArg('user-data-dir', args)) {
130+
out.push(`--user-data-dir=${path.join(defaultCachePath, 'user-data')}`)
131+
}
132+
133+
return out;
134+
}
135+
128136
function hasArg(argName: string, argList: readonly string[]) {
129137
return argList.some(a => a === `--${argName}` || a.startsWith(`--${argName}=`));
130138
}

lib/util.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { DownloadPlatform } from './download';
1111
import * as createHttpsProxyAgent from 'https-proxy-agent';
1212
import * as createHttpProxyAgent from 'http-proxy-agent';
1313
import { readFileSync } from 'fs';
14+
import { getProfileArguments, TestOptions } from './runTest';
1415

1516
export let systemDefaultPlatform: string;
1617

@@ -112,23 +113,10 @@ export async function getLatestInsidersMetadata(platform: string) {
112113
return await request.getJSON<IUpdateMetadata>(remoteUrl);
113114
}
114115

116+
115117
/**
116118
* Resolve the VS Code cli path from executable path returned from `downloadAndUnzipVSCode`.
117-
* You can use this path to spawn processes for extension management. For example:
118-
*
119-
* ```ts
120-
* const cp = require('child_process');
121-
* const { downloadAndUnzipVSCode, resolveCliPathFromExecutablePath } = require('@vscode/test-electron')
122-
* const vscodeExecutablePath = await downloadAndUnzipVSCode('1.36.0');
123-
* const cliPath = resolveCliPathFromExecutablePath(vscodeExecutablePath);
124-
*
125-
* cp.spawnSync(cliPath, ['--install-extension', '<EXTENSION-ID-OR-PATH-TO-VSIX>'], {
126-
* encoding: 'utf-8',
127-
* stdio: 'inherit'
128-
* });
129-
* ```
130-
*
131-
* @param vscodeExecutablePath The `vscodeExecutablePath` from `downloadAndUnzipVSCode`.
119+
* Usually you will want {@link resolveCliArgsFromVSCodeExecutablePath} instead.
132120
*/
133121
export function resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath: string) {
134122
if (process.platform === 'win32') {
@@ -147,3 +135,29 @@ export function resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath: str
147135
}
148136
}
149137
}
138+
/**
139+
* Resolve the VS Code cli arguments from executable path returned from `downloadAndUnzipVSCode`.
140+
* You can use this path to spawn processes for extension management. For example:
141+
*
142+
* ```ts
143+
* const cp = require('child_process');
144+
* const { downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } = require('@vscode/test-electron')
145+
* const vscodeExecutablePath = await downloadAndUnzipVSCode('1.36.0');
146+
* const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
147+
*
148+
* cp.spawnSync(cli, [...args, '--install-extension', '<EXTENSION-ID-OR-PATH-TO-VSIX>'], {
149+
* encoding: 'utf-8',
150+
* stdio: 'inherit'
151+
* });
152+
* ```
153+
*
154+
* @param vscodeExecutablePath The `vscodeExecutablePath` from `downloadAndUnzipVSCode`.
155+
*/
156+
export function resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath: string, options?: Pick<TestOptions, 'reuseMachineInstall'>) {
157+
const args = [resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath)];
158+
if (!options?.reuseMachineInstall) {
159+
args.push(...getProfileArguments(args));
160+
}
161+
162+
return args;
163+
}

sample/test/runTest.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from 'path';
22
import * as cp from 'child_process';
33

4-
import { runTests, downloadAndUnzipVSCode, resolveCliPathFromVSCodeExecutablePath } from '../../lib/index';
4+
import { runTests, downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } from '../../lib/index';
55

66
async function go() {
77
try {
@@ -67,8 +67,8 @@ async function go() {
6767
/**
6868
* Install Python extension
6969
*/
70-
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath);
71-
cp.spawnSync(cliPath, ['--install-extension', 'ms-python.python'], {
70+
const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
71+
cp.spawnSync(cli, [...args, '--install-extension', 'ms-python.python'], {
7272
encoding: 'utf-8',
7373
stdio: 'inherit'
7474
});

0 commit comments

Comments
 (0)