Skip to content

Commit

Permalink
feat: add resolveCliArgsFromVSCodeExecutablePath
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed Jan 8, 2022
1 parent fd0e599 commit 289a6b1
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### 2.0.2 | 2021-12-29

- Add `resolveCliArgsFromVSCodeExecutablePath`

### 2.0.1 | 2021-12-29

- Fix extra new lines added to test output
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ async function go() {
/**
* Install Python extension
*/
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath)
cp.spawnSync(cliPath, ['--install-extension', 'ms-python.python'], {
const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
cp.spawnSync(cli, [...args, '--install-extension', 'ms-python.python'], {
encoding: 'utf-8',
stdio: 'inherit'
})
});

/**
* - Add additional launch flags for VS Code
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

export { download, downloadAndUnzipVSCode } from './download';
export { runTests } from './runTest';
export { resolveCliPathFromVSCodeExecutablePath } from './util';
export { resolveCliPathFromVSCodeExecutablePath, resolveCliArgsFromVSCodeExecutablePath } from './util';
22 changes: 15 additions & 7 deletions lib/runTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,26 @@ export async function runTests(options: TestOptions): Promise<number> {
}

if (!options.reuseMachineInstall) {
if (!hasArg('extensions-dir', args)) {
args.push(`--extensions-dir=${path.join(defaultCachePath, 'extensions')}`)
}

if (!hasArg('user-data-dir', args)) {
args.push(`--user-data-dir=${path.join(defaultCachePath, 'user-data')}`)
}
args.push(...getProfileArguments(args));
}

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

/** Adds the extensions and user data dir to the arguments for the VS Code CLI */
export function getProfileArguments(args: readonly string[]) {
const out: string[] = [];
if (!hasArg('extensions-dir', args)) {
out.push(`--extensions-dir=${path.join(defaultCachePath, 'extensions')}`)
}

if (!hasArg('user-data-dir', args)) {
out.push(`--user-data-dir=${path.join(defaultCachePath, 'user-data')}`)
}

return out;
}

function hasArg(argName: string, argList: readonly string[]) {
return argList.some(a => a === `--${argName}` || a.startsWith(`--${argName}=`));
}
Expand Down
44 changes: 29 additions & 15 deletions lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { DownloadPlatform } from './download';
import * as createHttpsProxyAgent from 'https-proxy-agent';
import * as createHttpProxyAgent from 'http-proxy-agent';
import { readFileSync } from 'fs';
import { getProfileArguments, TestOptions } from './runTest';

export let systemDefaultPlatform: string;

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


/**
* Resolve the VS Code cli path from executable path returned from `downloadAndUnzipVSCode`.
* You can use this path to spawn processes for extension management. For example:
*
* ```ts
* const cp = require('child_process');
* const { downloadAndUnzipVSCode, resolveCliPathFromExecutablePath } = require('@vscode/test-electron')
* const vscodeExecutablePath = await downloadAndUnzipVSCode('1.36.0');
* const cliPath = resolveCliPathFromExecutablePath(vscodeExecutablePath);
*
* cp.spawnSync(cliPath, ['--install-extension', '<EXTENSION-ID-OR-PATH-TO-VSIX>'], {
* encoding: 'utf-8',
* stdio: 'inherit'
* });
* ```
*
* @param vscodeExecutablePath The `vscodeExecutablePath` from `downloadAndUnzipVSCode`.
* Usually you will want {@link resolveCliArgsFromVSCodeExecutablePath} instead.
*/
export function resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath: string) {
if (process.platform === 'win32') {
Expand All @@ -147,3 +135,29 @@ export function resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath: str
}
}
}
/**
* Resolve the VS Code cli arguments from executable path returned from `downloadAndUnzipVSCode`.
* You can use this path to spawn processes for extension management. For example:
*
* ```ts
* const cp = require('child_process');
* const { downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } = require('@vscode/test-electron')
* const vscodeExecutablePath = await downloadAndUnzipVSCode('1.36.0');
* const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
*
* cp.spawnSync(cli, [...args, '--install-extension', '<EXTENSION-ID-OR-PATH-TO-VSIX>'], {
* encoding: 'utf-8',
* stdio: 'inherit'
* });
* ```
*
* @param vscodeExecutablePath The `vscodeExecutablePath` from `downloadAndUnzipVSCode`.
*/
export function resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath: string, options?: Pick<TestOptions, 'reuseMachineInstall'>) {
const args = [resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath)];
if (!options?.reuseMachineInstall) {
args.push(...getProfileArguments(args));
}

return args;
}
6 changes: 3 additions & 3 deletions sample/test/runTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as path from 'path';
import * as cp from 'child_process';

import { runTests, downloadAndUnzipVSCode, resolveCliPathFromVSCodeExecutablePath } from '../../lib/index';
import { runTests, downloadAndUnzipVSCode, resolveCliArgsFromVSCodeExecutablePath } from '../../lib/index';

async function go() {
try {
Expand Down Expand Up @@ -67,8 +67,8 @@ async function go() {
/**
* Install Python extension
*/
const cliPath = resolveCliPathFromVSCodeExecutablePath(vscodeExecutablePath);
cp.spawnSync(cliPath, ['--install-extension', 'ms-python.python'], {
const [cli, ...args] = resolveCliArgsFromVSCodeExecutablePath(vscodeExecutablePath);
cp.spawnSync(cli, [...args, '--install-extension', 'ms-python.python'], {
encoding: 'utf-8',
stdio: 'inherit'
});
Expand Down

0 comments on commit 289a6b1

Please sign in to comment.