From ba3b10f988e63ec14789c2a50bcb712b9d2874bc Mon Sep 17 00:00:00 2001 From: yiliang114 <1204183885@qq.com> Date: Tue, 27 Jun 2023 16:48:06 +0800 Subject: [PATCH] fix: code format --- src/index.ts | 64 ++++++++++++-------------- src/server/app.ts | 2 +- src/server/download.ts | 1 - src/server/extensions.ts | 6 +-- src/server/mounts.ts | 98 ++++++++++++++++++++-------------------- src/server/workbench.ts | 6 +-- 6 files changed, 83 insertions(+), 94 deletions(-) diff --git a/src/index.ts b/src/index.ts index 0de2147..231193f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { /** @@ -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); }); @@ -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) { @@ -224,11 +221,11 @@ async function getBuild(options: Options): Promise { 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); } @@ -243,7 +240,7 @@ export async function open(options: Options): Promise { extensionPaths: options.extensionPaths, extensionIds: options.extensionIds, coi: !!options.coi, - esm: !!options.esm + esm: !!options.esm, }; const host = options.host ?? 'localhost'; @@ -258,9 +255,8 @@ export async function open(options: Options): Promise { dispose: () => { server.close(); context?.browser()?.close(); - } - } - + }, + }; } async function openBrowser(endpoint: string, options: Options, configPage?: (page: playwright.Page, browser: playwright.Browser) => Promise): Promise { @@ -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'); } @@ -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); } @@ -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); @@ -324,7 +319,7 @@ 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.`); @@ -332,7 +327,6 @@ function validateStringOrUndefined(options: CommandLineOptions, name: keyof Comm process.exit(-1); } - async function validatePathOrUndefined(options: CommandLineOptions, name: keyof CommandLineOptions, isFile?: boolean): Promise { const loc = validateStringOrUndefined(options, name); return loc && validatePath(loc, isFile); @@ -340,7 +334,7 @@ async function validatePathOrUndefined(options: CommandLineOptions, name: keyof 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.`); @@ -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) { @@ -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}.`); @@ -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'; @@ -399,7 +392,7 @@ function validatePermissions(permissions: unknown): string[] | undefined { async function validateExtensionPaths(extensionPaths: unknown): Promise { if (extensionPaths === undefined) { - return undefined + return undefined; } if (!Array.isArray(extensionPaths)) { extensionPaths = [extensionPaths]; @@ -425,7 +418,7 @@ const EXTENSION_IDENTIFIER_PATTERN = /^([a-z0-9A-Z][a-z0-9-A-Z]*\.[a-z0-9A-Z][a- async function validateExtensionIds(extensionIds: unknown): Promise { if (extensionIds === undefined) { - return undefined + return undefined; } if (!Array.isArray(extensionIds)) { extensionIds = [extensionIds]; @@ -433,7 +426,7 @@ async function validateExtensionIds(extensionIds: unknown): Promise { 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); } @@ -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') { @@ -502,7 +495,6 @@ function validatePortNumber(port: unknown): number | undefined { return undefined; } - interface CommandLineOptions { browser?: string; browserType?: string; @@ -573,7 +565,7 @@ async function cliMain(): Promise { process.exit(); } return true; - } + }, }; const args = minimist(process.argv.slice(2), options); if (args.help) { @@ -637,11 +629,11 @@ async function cliMain(): Promise { coi, host, port, - testRunnerDataDir + testRunnerDataDir, }).catch(e => { console.log('Error running tests:', e); process.exit(1); - }) + }); } else { open({ extensionDevelopmentPath, @@ -662,8 +654,8 @@ async function cliMain(): Promise { coi, host, port, - testRunnerDataDir - }) + testRunnerDataDir, + }); } } diff --git a/src/server/app.ts b/src/server/app.ts index 06635a3..255fb94 100644 --- a/src/server/app.ts +++ b/src/server/app.ts @@ -92,7 +92,7 @@ export default async function createApp(config: IConfig): Promise { 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))); } diff --git a/src/server/download.ts b/src/server/download.ts index ec0836c..027b231 100644 --- a/src/server/download.ts +++ b/src/server/download.ts @@ -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`); diff --git a/src/server/extensions.ts b/src/server/extensions.ts index 07e3697..45c0a15 100644 --- a/src/server/extensions.ts +++ b/src/server/extensions.ts @@ -26,7 +26,7 @@ export async function scanForExtensions( scheme: serverURI.scheme, authority: serverURI.authority, path: path.posix.join(serverURI.path, relativePosixFolderPath), - } + }; } } catch { return undefined; @@ -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').`); } } diff --git a/src/server/mounts.ts b/src/server/mounts.ts index a58330b..5d65cc4 100644 --- a/src/server/mounts.ts +++ b/src/server/mounts.ts @@ -3,7 +3,7 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import { IConfig } from "./main"; +import { IConfig } from './main'; import * as Koa from 'koa'; import * as kstatic from 'koa-static'; @@ -18,61 +18,61 @@ export const fsProviderExtensionPrefix = '/static/extensions/fs'; export const fsProviderFolderUri = 'vscode-test-web://mount/'; export function configureMounts(config: IConfig, app: Koa): void { - const folderMountPath = config.folderMountPath; - if (folderMountPath) { - console.log(`Serving local content ${folderMountPath} at ${mountPrefix}`); - app.use(fileOps(mountPrefix, folderMountPath)); - app.use(kmount(mountPrefix, kstatic(folderMountPath, { hidden: true }))); + const folderMountPath = config.folderMountPath; + if (folderMountPath) { + console.log(`Serving local content ${folderMountPath} at ${mountPrefix}`); + app.use(fileOps(mountPrefix, folderMountPath)); + app.use(kmount(mountPrefix, kstatic(folderMountPath, { hidden: true }))); - app.use(kmount(fsProviderExtensionPrefix, kstatic(path.join(__dirname, '../../fs-provider'), { hidden: true }))); - } + app.use(kmount(fsProviderExtensionPrefix, kstatic(path.join(__dirname, '../../fs-provider'), { hidden: true }))); + } } function fileOps(mountPrefix: string, folderMountPath: string): Router.Middleware { - const router = new Router(); - router.get(`${mountPrefix}(/.*)?`, async (ctx, next) => { - if (ctx.query.stat !== undefined) { - const p = path.join(folderMountPath, ctx.path.substring(mountPrefix.length)); - try { - const stats = await fs.stat(p); - ctx.body = { - type: getFileType(stats), - ctime: stats.ctime.getTime(), - mtime: stats.mtime.getTime(), - size: stats.size - } - } catch (e) { - ctx.body = { error: (e as NodeJS.ErrnoException).code }; - } - } else if (ctx.query.readdir !== undefined) { - const p = path.join(folderMountPath, ctx.path.substring(mountPrefix.length)); - try { - const entries = await fs.readdir(p, { withFileTypes: true }); - ctx.body = entries.map(d => ({ name: d.name, type: getFileType(d) })); - } catch (e) { - ctx.body = { error: (e as NodeJS.ErrnoException).code }; - } - } else { - return next(); - } - }); - return router.routes(); + const router = new Router(); + router.get(`${mountPrefix}(/.*)?`, async (ctx, next) => { + if (ctx.query.stat !== undefined) { + const p = path.join(folderMountPath, ctx.path.substring(mountPrefix.length)); + try { + const stats = await fs.stat(p); + ctx.body = { + type: getFileType(stats), + ctime: stats.ctime.getTime(), + mtime: stats.mtime.getTime(), + size: stats.size, + }; + } catch (e) { + ctx.body = { error: (e as NodeJS.ErrnoException).code }; + } + } else if (ctx.query.readdir !== undefined) { + const p = path.join(folderMountPath, ctx.path.substring(mountPrefix.length)); + try { + const entries = await fs.readdir(p, { withFileTypes: true }); + ctx.body = entries.map((d) => ({ name: d.name, type: getFileType(d) })); + } catch (e) { + ctx.body = { error: (e as NodeJS.ErrnoException).code }; + } + } else { + return next(); + } + }); + return router.routes(); } enum FileType { - Unknown = 0, - File = 1, - Directory = 2, - SymbolicLink = 64 + Unknown = 0, + File = 1, + Directory = 2, + SymbolicLink = 64, } function getFileType(stats: Stats | Dirent) { - if (stats.isFile()) { - return FileType.File; - } else if (stats.isDirectory()) { - return FileType.Directory; - } else if (stats.isSymbolicLink()) { - return FileType.SymbolicLink; - } - return FileType.Unknown; -} \ No newline at end of file + if (stats.isFile()) { + return FileType.File; + } else if (stats.isDirectory()) { + return FileType.Directory; + } else if (stats.isSymbolicLink()) { + return FileType.SymbolicLink; + } + return FileType.Unknown; +} diff --git a/src/server/workbench.ts b/src/server/workbench.ts index a410d60..e1918ed 100644 --- a/src/server/workbench.ts +++ b/src/server/workbench.ts @@ -18,7 +18,6 @@ interface IDevelopmentOptions { extensions?: URIComponents[]; } - interface IWorkbenchOptions { additionalBuiltinExtensions?: (string | URIComponents | GalleryExtensionInfo)[]; developmentOptions?: IDevelopmentOptions; @@ -45,7 +44,7 @@ class Workbench { WORKBENCH_AUTH_SESSION: '', WORKBENCH_WEB_BASE_URL: this.baseUrl, WORKBENCH_BUILTIN_EXTENSIONS: asJSON(this.builtInExtensions), - WORKBENCH_MAIN: this.getMain() + WORKBENCH_MAIN: this.getMain(), }; try { @@ -68,7 +67,6 @@ class Workbench { + ``; } - async renderCallback(): Promise { return await fetch(`${this.baseUrl}/out/vs/code/browser/workbench/callback.html`); } @@ -97,7 +95,7 @@ async function getWorkbenchOptions( options.additionalBuiltinExtensions.push(...config.extensionIds); } if (config.extensionDevelopmentPath) { - const developmentOptions: IDevelopmentOptions = options.developmentOptions = {} + const developmentOptions: IDevelopmentOptions = (options.developmentOptions = {}); developmentOptions.extensions = await scanForExtensions( config.extensionDevelopmentPath,