From 226c128d5032eed7cf0edec38616a5de49cda21f Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Wed, 15 Jun 2022 11:23:58 +1200 Subject: [PATCH 1/7] Some tidy up to the screenshot --- .../src/cli/screenshot/screenshot.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/packages/config-cli/src/cli/screenshot/screenshot.ts b/packages/config-cli/src/cli/screenshot/screenshot.ts index 49f323d9e..32b1f4c4f 100644 --- a/packages/config-cli/src/cli/screenshot/screenshot.ts +++ b/packages/config-cli/src/cli/screenshot/screenshot.ts @@ -3,7 +3,7 @@ import { mkdir } from 'fs/promises'; import { Browser, chromium } from 'playwright'; import { CommandLineAction, CommandLineStringParameter } from '@rushstack/ts-command-line'; import { z } from 'zod'; -import getPort, { portNumbers } from 'get-port'; +import getPort from 'get-port'; import { createServer } from '@basemaps/server'; import { FastifyInstance } from 'fastify/types/instance'; import { ConfigBundled, ConfigProviderMemory } from '@basemaps/config'; @@ -75,9 +75,10 @@ export class CommandScreenShot extends CommandLineAction { let BasemapsServer: FastifyInstance | undefined = undefined; if (config != null) { - const port = await getPort({ port: portNumbers(10000, 11000) }); + const port = await getPort(); host = `http://localhost:${port}`; - BasemapsServer = await startServer(host, port, config, logger); + BasemapsServer = await startServer(port, config, logger); + logger.info({ url: host }, 'ServerStarted'); } logger.info('Page:Launch'); @@ -107,7 +108,7 @@ export async function takeScreenshots(host: string, tiles: string, chrome: Brows await mkdir(`.artifacts/visual-snapshots/`, { recursive: true }); let url = `${host}/?${searchParam.toString()}&debug=true&debug.screenshot=true#${loc}`; - if (!url.startsWith('http')) url = `https"//${url}`; + if (!url.startsWith('http')) url = `https://${url}`; logger.info({ url, expected: fileName }, 'Page:Load'); @@ -127,7 +128,7 @@ export async function takeScreenshots(host: string, tiles: string, chrome: Brows } } -async function startServer(host: string, port: number, config: string, logger: LogType): Promise { +async function startServer(port: number, config: string, logger: LogType): Promise { // Bundle Config const configJson = await fsa.readJson(config); const mem = ConfigProviderMemory.fromJson(configJson); @@ -135,11 +136,5 @@ async function startServer(host: string, port: number, config: string, logger: L // Start server const server = createServer(logger); - await new Promise((resolve) => - server.listen(port, '0.0.0.0', () => { - logger.info({ url: host }, 'ServerStarted'); - resolve(); - }), - ); - return server; + return await new Promise((resolve) => server.listen(port, '0.0.0.0', () => resolve(server))); } From 0c5f38e588545127b01fc4c12fe205227266bf68 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Wed, 15 Jun 2022 13:56:52 +1200 Subject: [PATCH 2/7] New command tool to bundle config files from a give path. --- packages/config-cli/README.md | 8 ++++ .../src/cli/screenshot/bundle.config.ts | 42 +++++++++++++++++++ .../config-cli/src/cli/screenshot/index.ts | 2 + 3 files changed, 52 insertions(+) create mode 100644 packages/config-cli/src/cli/screenshot/bundle.config.ts diff --git a/packages/config-cli/README.md b/packages/config-cli/README.md index 39b528f2c..9ef21fe99 100644 --- a/packages/config-cli/README.md +++ b/packages/config-cli/README.md @@ -23,3 +23,11 @@ Dump the screenshots with config file ./bin/bmc.js screenshot --config s3://..../config.json.gz ``` + +## Usage -- Bundle + +Bundle config files into config bundle json from a given config path. + +```bash +./bin/bmc.js screenshot --config config/ --bundle config/config.json +``` diff --git a/packages/config-cli/src/cli/screenshot/bundle.config.ts b/packages/config-cli/src/cli/screenshot/bundle.config.ts new file mode 100644 index 000000000..be2e747e2 --- /dev/null +++ b/packages/config-cli/src/cli/screenshot/bundle.config.ts @@ -0,0 +1,42 @@ +import { fsa, LogConfig } from '@basemaps/shared'; +import { ConfigJson } from '@basemaps/config'; +import { CommandLineAction, CommandLineStringParameter } from '@rushstack/ts-command-line'; + +export const DefaultConfig = 'config/'; +export const DefaultBundle = 'config/config.json'; + +export class CommandBundle extends CommandLineAction { + private config: CommandLineStringParameter; + private bundle: CommandLineStringParameter; + + public constructor() { + super({ + actionName: 'bundle', + summary: 'bundle a config json from config files', + documentation: 'Given a path of config files and bundle them into one config json', + }); + } + + protected onDefineParameters(): void { + this.config = this.defineStringParameter({ + argumentName: 'CONFIG', + parameterLongName: '--config', + description: 'Path of config files', + }); + this.bundle = this.defineStringParameter({ + argumentName: 'BUNDLE', + parameterLongName: '--bundle', + description: 'Output of the bundle file', + }); + } + + async onExecute(): Promise { + const logger = LogConfig.get(); + const config = this.config.value ?? DefaultConfig; + const bundle = this.bundle.value ?? DefaultBundle; + const mem = await ConfigJson.fromPath(config, logger); + await fsa.writeJson(bundle, mem.toJson()); + logger.info({ path: bundle }, 'ConfigBundled'); + return; + } +} diff --git a/packages/config-cli/src/cli/screenshot/index.ts b/packages/config-cli/src/cli/screenshot/index.ts index fc685cda6..d57360ce3 100644 --- a/packages/config-cli/src/cli/screenshot/index.ts +++ b/packages/config-cli/src/cli/screenshot/index.ts @@ -1,5 +1,6 @@ import { BaseCommandLine } from '@basemaps/shared/build/cli/base.js'; import 'source-map-support/register.js'; +import { CommandBundle } from './bundle.config.js'; import { CommandScreenShot } from './screenshot.js'; export class BasemapsConfig extends BaseCommandLine { @@ -9,5 +10,6 @@ export class BasemapsConfig extends BaseCommandLine { toolDescription: 'Basemaps config command tools', }); this.addAction(new CommandScreenShot()); + this.addAction(new CommandBundle()); } } From 1454049a487f2d941ee68c784aa377dabddece34 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Wed, 15 Jun 2022 14:10:48 +1200 Subject: [PATCH 3/7] Use output parameter for the output path --- packages/config-cli/README.md | 2 +- .../config-cli/src/cli/screenshot/bundle.config.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/config-cli/README.md b/packages/config-cli/README.md index 9ef21fe99..850350318 100644 --- a/packages/config-cli/README.md +++ b/packages/config-cli/README.md @@ -29,5 +29,5 @@ Dump the screenshots with config file Bundle config files into config bundle json from a given config path. ```bash -./bin/bmc.js screenshot --config config/ --bundle config/config.json +./bin/bmc.js screenshot --config config/ --output config.json ``` diff --git a/packages/config-cli/src/cli/screenshot/bundle.config.ts b/packages/config-cli/src/cli/screenshot/bundle.config.ts index be2e747e2..1bd9db374 100644 --- a/packages/config-cli/src/cli/screenshot/bundle.config.ts +++ b/packages/config-cli/src/cli/screenshot/bundle.config.ts @@ -3,11 +3,11 @@ import { ConfigJson } from '@basemaps/config'; import { CommandLineAction, CommandLineStringParameter } from '@rushstack/ts-command-line'; export const DefaultConfig = 'config/'; -export const DefaultBundle = 'config/config.json'; +export const DefaultOutput = 'config/config.json'; export class CommandBundle extends CommandLineAction { private config: CommandLineStringParameter; - private bundle: CommandLineStringParameter; + private output: CommandLineStringParameter; public constructor() { super({ @@ -23,9 +23,9 @@ export class CommandBundle extends CommandLineAction { parameterLongName: '--config', description: 'Path of config files', }); - this.bundle = this.defineStringParameter({ - argumentName: 'BUNDLE', - parameterLongName: '--bundle', + this.output = this.defineStringParameter({ + argumentName: 'OUTPUT', + parameterLongName: '--output', description: 'Output of the bundle file', }); } @@ -33,7 +33,7 @@ export class CommandBundle extends CommandLineAction { async onExecute(): Promise { const logger = LogConfig.get(); const config = this.config.value ?? DefaultConfig; - const bundle = this.bundle.value ?? DefaultBundle; + const bundle = this.output.value ?? DefaultOutput; const mem = await ConfigJson.fromPath(config, logger); await fsa.writeJson(bundle, mem.toJson()); logger.info({ path: bundle }, 'ConfigBundled'); From 756d0b55c23926fe5a57f83f5ac6e0cea73b3b61 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Wed, 15 Jun 2022 14:20:19 +1200 Subject: [PATCH 4/7] Fix readme --- packages/config-cli/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/config-cli/README.md b/packages/config-cli/README.md index 850350318..c5926257d 100644 --- a/packages/config-cli/README.md +++ b/packages/config-cli/README.md @@ -29,5 +29,5 @@ Dump the screenshots with config file Bundle config files into config bundle json from a given config path. ```bash -./bin/bmc.js screenshot --config config/ --output config.json +./bin/bmc.js bundle --config config/ --output config.json ``` From c6a291cf1c26c6970e8b533e2f3d832c8eea0139 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Wed, 15 Jun 2022 14:39:48 +1200 Subject: [PATCH 5/7] set PublicUrlBase for the temporary server. --- packages/config-cli/src/cli/screenshot/screenshot.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/config-cli/src/cli/screenshot/screenshot.ts b/packages/config-cli/src/cli/screenshot/screenshot.ts index 32b1f4c4f..38258b605 100644 --- a/packages/config-cli/src/cli/screenshot/screenshot.ts +++ b/packages/config-cli/src/cli/screenshot/screenshot.ts @@ -1,4 +1,4 @@ -import { Config, fsa, LogConfig, LogType } from '@basemaps/shared'; +import { Config, Env, fsa, LogConfig, LogType } from '@basemaps/shared'; import { mkdir } from 'fs/promises'; import { Browser, chromium } from 'playwright'; import { CommandLineAction, CommandLineStringParameter } from '@rushstack/ts-command-line'; @@ -77,6 +77,8 @@ export class CommandScreenShot extends CommandLineAction { if (config != null) { const port = await getPort(); host = `http://localhost:${port}`; + // Force a default url base so WMTS requests know their relative url + process.env[Env.PublicUrlBase] = host; BasemapsServer = await startServer(port, config, logger); logger.info({ url: host }, 'ServerStarted'); } From e52237753519f3320af9d07ad58e14aa923d45b8 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Wed, 15 Jun 2022 14:47:51 +1200 Subject: [PATCH 6/7] Remove host name from the output screenshot name --- packages/config-cli/src/cli/screenshot/screenshot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/config-cli/src/cli/screenshot/screenshot.ts b/packages/config-cli/src/cli/screenshot/screenshot.ts index 38258b605..363e420c3 100644 --- a/packages/config-cli/src/cli/screenshot/screenshot.ts +++ b/packages/config-cli/src/cli/screenshot/screenshot.ts @@ -105,7 +105,7 @@ export async function takeScreenshots(host: string, tiles: string, chrome: Brows if (test.style) searchParam.set('s', test.style); const loc = `@${test.location.lat},${test.location.lng},z${test.location.z}`; - const fileName = '.artifacts/visual-snapshots/' + host + '_' + test.name + '.png'; + const fileName = '.artifacts/visual-snapshots/' + test.name + '.png'; await mkdir(`.artifacts/visual-snapshots/`, { recursive: true }); From e500c3c2be84be68fc4748a507898ecfe7ba2c77 Mon Sep 17 00:00:00 2001 From: Wentao Kuang Date: Thu, 16 Jun 2022 10:19:16 +1200 Subject: [PATCH 7/7] Using the PublicUrlBase if exists --- packages/config-cli/src/cli/screenshot/screenshot.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/config-cli/src/cli/screenshot/screenshot.ts b/packages/config-cli/src/cli/screenshot/screenshot.ts index 363e420c3..af222787d 100644 --- a/packages/config-cli/src/cli/screenshot/screenshot.ts +++ b/packages/config-cli/src/cli/screenshot/screenshot.ts @@ -76,7 +76,7 @@ export class CommandScreenShot extends CommandLineAction { let BasemapsServer: FastifyInstance | undefined = undefined; if (config != null) { const port = await getPort(); - host = `http://localhost:${port}`; + host = Env.get(Env.PublicUrlBase) ?? `http://localhost:${port}`; // Force a default url base so WMTS requests know their relative url process.env[Env.PublicUrlBase] = host; BasemapsServer = await startServer(port, config, logger);