Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: code problems #86

Merged
merged 3 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 28 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as path from 'path';
export type BrowserType = 'chromium' | 'firefox' | 'webkit' | 'none';
export type VSCodeQuality = 'insiders' | 'stable';

export type GalleryExtension = { readonly id: string; readonly preRelease?: boolean; }
export type GalleryExtension = { readonly id: string; readonly preRelease?: boolean };
export interface Options {

/**
Expand Down Expand Up @@ -171,21 +171,19 @@ export async function runTests(options: Options & { extensionTestsPath: string }
extensionPaths: options.extensionPaths,
extensionIds: options.extensionIds,
coi: !!options.coi,
esm: !!options.esm
esm: !!options.esm,
};


const host = options.host ?? 'localhost';
const port = options.port ?? 3000;
const server = await runServer(host, port, config);

return new Promise(async (s, e) => {

const endpoint = `http://${host}:${port}`;

const configPage = async (page: playwright.Page, browser: playwright.Browser) => {
type Severity = 'error' | 'warning' | 'info';
const unreportedOutput: { type: Severity, args: unknown[] }[] = [];
const unreportedOutput: { type: Severity; args: unknown[] }[] = [];
await page.exposeFunction('codeAutomationLog', (type: Severity, args: unknown[]) => {
console[type](...args);
});
Expand All @@ -207,8 +205,7 @@ export async function runTests(options: Options & { extensionTestsPath: string }
e(new Error('Test failed'));
}
});

}
};
console.log(`Opening browser on ${endpoint}...`);
const context = await openBrowser(endpoint, options, configPage);
if (context) {
Expand All @@ -224,11 +221,11 @@ async function getBuild(options: Options): Promise<Static | Sources> {
if (options.vsCodeDevPath) {
return {
type: 'sources',
location: options.vsCodeDevPath
location: options.vsCodeDevPath,
};
}
const quality = options.quality || options.version;
const testRunnerDataDir = options.testRunnerDataDir ?? path.resolve(process.cwd(), '.vscode-test-web')
const testRunnerDataDir = options.testRunnerDataDir ?? path.resolve(process.cwd(), '.vscode-test-web');
return await downloadAndUnzipVSCode(quality === 'stable' ? 'stable' : 'insider', testRunnerDataDir);
}

Expand All @@ -243,7 +240,7 @@ export async function open(options: Options): Promise<Disposable> {
extensionPaths: options.extensionPaths,
extensionIds: options.extensionIds,
coi: !!options.coi,
esm: !!options.esm
esm: !!options.esm,
};

const host = options.host ?? 'localhost';
Expand All @@ -258,9 +255,8 @@ export async function open(options: Options): Promise<Disposable> {
dispose: () => {
server.close();
context?.browser()?.close();
}
}

},
};
}

async function openBrowser(endpoint: string, options: Options, configPage?: (page: playwright.Page, browser: playwright.Browser) => Promise<void>): Promise<playwright.BrowserContext | undefined> {
Expand All @@ -274,7 +270,7 @@ async function openBrowser(endpoint: string, options: Options, configPage?: (pag
return undefined;
}

const args: string[] = []
const args: string[] = [];
if (process.platform === 'linux' && options.browserType === 'chromium') {
args.push('--no-sandbox');
}
Expand All @@ -300,11 +296,10 @@ async function openBrowser(endpoint: string, options: Options, configPage?: (pag
if (openPages === 0) {
browser.close();
}
})
});
});


const page = context.pages()[0] ?? await context.newPage();
const page = context.pages()[0] ?? (await context.newPage());
if (configPage) {
await configPage(page, browser);
}
Expand All @@ -314,7 +309,7 @@ async function openBrowser(endpoint: string, options: Options, configPage?: (pag
if (options.verbose) {
page.on('console', (message) => {
console.log(message.text());
})
});
}

await page.goto(endpoint);
Expand All @@ -324,23 +319,22 @@ async function openBrowser(endpoint: string, options: Options, configPage?: (pag

function validateStringOrUndefined(options: CommandLineOptions, name: keyof CommandLineOptions): string | undefined {
const value = options[name];
if (value === undefined || (typeof value === 'string')) {
if (value === undefined || typeof value === 'string') {
return value;
}
console.log(`'${name}' needs to be a string value.`);
showHelp();
process.exit(-1);
}


async function validatePathOrUndefined(options: CommandLineOptions, name: keyof CommandLineOptions, isFile?: boolean): Promise<string | undefined> {
const loc = validateStringOrUndefined(options, name);
return loc && validatePath(loc, isFile);
}

function validateBooleanOrUndefined(options: CommandLineOptions, name: keyof CommandLineOptions): boolean | undefined {
const value = options[name];
if (value === undefined || (typeof value === 'boolean')) {
if (value === undefined || typeof value === 'boolean') {
return value;
}
console.log(`'${name}' needs to be a boolean value.`);
Expand All @@ -360,7 +354,6 @@ function validatePrintServerLog(options: CommandLineOptions): boolean {
return false;
}


function validateBrowserType(options: CommandLineOptions): BrowserType {
const browserType = options.browser || options.browserType;
if (browserType === undefined) {
Expand All @@ -370,7 +363,7 @@ function validateBrowserType(options: CommandLineOptions): BrowserType {
console.log(`Ignoring browserType option '${options.browserType}' as browser option '${options.browser}' is set.`);
}

if ((typeof browserType === 'string') && ['chromium', 'firefox', 'webkit', 'none'].includes(browserType)) {
if (typeof browserType === 'string' && ['chromium', 'firefox', 'webkit', 'none'].includes(browserType)) {
return browserType as BrowserType;
}
console.log(`Invalid browser option ${browserType}.`);
Expand All @@ -380,7 +373,7 @@ function validateBrowserType(options: CommandLineOptions): BrowserType {

function validatePermissions(permissions: unknown): string[] | undefined {
if (permissions === undefined) {
return undefined
return undefined;
}
function isValidPermission(p: unknown): p is string {
return typeof p === 'string';
Expand All @@ -399,7 +392,7 @@ function validatePermissions(permissions: unknown): string[] | undefined {

async function validateExtensionPaths(extensionPaths: unknown): Promise<string[] | undefined> {
if (extensionPaths === undefined) {
return undefined
return undefined;
}
if (!Array.isArray(extensionPaths)) {
extensionPaths = [extensionPaths];
Expand All @@ -425,15 +418,15 @@ const EXTENSION_IDENTIFIER_PATTERN = /^([a-z0-9A-Z][a-z0-9-A-Z]*\.[a-z0-9A-Z][a-

async function validateExtensionIds(extensionIds: unknown): Promise<GalleryExtension[] | undefined> {
if (extensionIds === undefined) {
return undefined
return undefined;
}
if (!Array.isArray(extensionIds)) {
extensionIds = [extensionIds];
}
if (Array.isArray(extensionIds)) {
const res: GalleryExtension[] = [];
for (const extensionId of extensionIds) {
const m = (typeof extensionId === 'string' && extensionId.match(EXTENSION_IDENTIFIER_PATTERN));
const m = typeof extensionId === 'string' && extensionId.match(EXTENSION_IDENTIFIER_PATTERN);
if (m) {
if (m[2]) {
res.push({ id: m[1], preRelease: true });
Expand All @@ -457,12 +450,12 @@ async function validateExtensionIds(extensionIds: unknown): Promise<GalleryExten
async function validatePath(loc: string, isFile?: boolean): Promise<string> {
loc = path.resolve(loc);
if (isFile) {
if (!await fileExists(loc)) {
if (!(await fileExists(loc))) {
console.log(`'${loc}' must be an existing file.`);
process.exit(-1);
}
} else {
if (!await directoryExists(loc)) {
if (!(await directoryExists(loc))) {
console.log(`'${loc}' must be an existing folder.`);
process.exit(-1);
}
Expand All @@ -480,7 +473,7 @@ function validateQuality(quality: unknown, version: unknown, vsCodeDevPath: stri
console.log(`Sources folder is provided as input, quality is ignored.`);
return undefined;
}
if (quality === undefined || ((typeof quality === 'string') && ['insiders', 'stable'].includes(quality))) {
if (quality === undefined || (typeof quality === 'string' && ['insiders', 'stable'].includes(quality))) {
return quality as VSCodeQuality;
}
if (version === 'sources') {
Expand All @@ -502,7 +495,6 @@ function validatePortNumber(port: unknown): number | undefined {
return undefined;
}


interface CommandLineOptions {
browser?: string;
browserType?: string;
Expand Down Expand Up @@ -573,7 +565,7 @@ async function cliMain(): Promise<void> {
process.exit();
}
return true;
}
},
};
const args = minimist<CommandLineOptions>(process.argv.slice(2), options);
if (args.help) {
Expand Down Expand Up @@ -637,11 +629,11 @@ async function cliMain(): Promise<void> {
coi,
host,
port,
testRunnerDataDir
testRunnerDataDir,
}).catch(e => {
console.log('Error running tests:', e);
process.exit(1);
})
});
} else {
open({
extensionDevelopmentPath,
Expand All @@ -662,8 +654,8 @@ async function cliMain(): Promise<void> {
coi,
host,
port,
testRunnerDataDir
})
testRunnerDataDir,
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export default async function createApp(config: IConfig): Promise<Koa> {
app.use(kmount('/static/sources', kstatic(config.build.location, serveOptions)));
app.use(kmount('/static/sources', kstatic(join(config.build.location, 'resources', 'server'), serveOptions))); // for manifest.json, favicon and code icons.

// built-in extension are at 'extensions` as well as prebuilt extensions dowloaded from the marketplace
// built-in extension are at 'extensions` as well as prebuilt extensions downloaded from the marketplace
app.use(kmount(`/static/sources/extensions`, kstatic(join(config.build.location, prebuiltExtensionsLocation), serveOptions)));
}

Expand Down
1 change: 0 additions & 1 deletion src/server/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ async function downloadAndUntar(downloadUrl: string, destination: string, messag
process.stdout.write(`${reset}${message}: complete\n`);
});


const extract = res.pipe(gunzip()).pipe(tar.extract(destination, { strip: 1 }));
extract.on('finish', () => {
process.stdout.write(`Extracted to ${destination}\n`);
Expand Down
6 changes: 3 additions & 3 deletions src/server/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function scanForExtensions(
scheme: serverURI.scheme,
authority: serverURI.authority,
path: path.posix.join(serverURI.path, relativePosixFolderPath),
}
};
}
} catch {
return undefined;
Expand Down Expand Up @@ -71,13 +71,13 @@ export async function getScannedBuiltinExtensions(vsCodeDevLocation: string): Pr
const localExtensions : IScannedBuiltinExtension[] = extensionsUtil.scanBuiltinExtensions(path.join(vsCodeDevLocation, 'extensions'));
const prebuiltExtensions : IScannedBuiltinExtension[] = extensionsUtil.scanBuiltinExtensions(path.join(vsCodeDevLocation, prebuiltExtensionsLocation));
for (const ext of localExtensions) {
let browserMain : string | undefined = ext.packageJSON.browser;
let browserMain: string | undefined = ext.packageJSON.browser;
if (browserMain) {
if (!browserMain.endsWith('.js')) {
browserMain = browserMain + '.js';
}
const browserMainLocation = path.join(vsCodeDevLocation, 'extensions', ext.extensionPath, browserMain);
if (!await fileExists(browserMainLocation)) {
if (!(await fileExists(browserMainLocation))) {
console.log(`${browserMainLocation} not found. Make sure all extensions are compiled (use 'yarn watch-web').`);
}
}
Expand Down
Loading