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

feat(config-cli): new command tool to bundle config json from a config path #2252

Merged
merged 7 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
8 changes: 8 additions & 0 deletions packages/config-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 bundle --config config/ --output config.json
```
42 changes: 42 additions & 0 deletions packages/config-cli/src/cli/screenshot/bundle.config.ts
Original file line number Diff line number Diff line change
@@ -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 DefaultOutput = 'config/config.json';

export class CommandBundle extends CommandLineAction {
private config: CommandLineStringParameter;
private output: 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.output = this.defineStringParameter({
argumentName: 'OUTPUT',
parameterLongName: '--output',
description: 'Output of the bundle file',
});
}

async onExecute(): Promise<void> {
const logger = LogConfig.get();
const config = this.config.value ?? DefaultConfig;
const bundle = this.output.value ?? DefaultOutput;
const mem = await ConfigJson.fromPath(config, logger);
await fsa.writeJson(bundle, mem.toJson());
logger.info({ path: bundle }, 'ConfigBundled');
return;
}
}
2 changes: 2 additions & 0 deletions packages/config-cli/src/cli/screenshot/index.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -9,5 +10,6 @@ export class BasemapsConfig extends BaseCommandLine {
toolDescription: 'Basemaps config command tools',
});
this.addAction(new CommandScreenShot());
this.addAction(new CommandBundle());
}
}
25 changes: 11 additions & 14 deletions packages/config-cli/src/cli/screenshot/screenshot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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';
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';
Expand Down Expand Up @@ -75,9 +75,12 @@ 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);
// Force a default url base so WMTS requests know their relative url
process.env[Env.PublicUrlBase] = host;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if this is set we need to respect it we should not be overwriting env vars.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can set this back after the script running in finally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Proab safer to just use it if it exists the same as basemaps-server cli

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, updated.

BasemapsServer = await startServer(port, config, logger);
logger.info({ url: host }, 'ServerStarted');
}

logger.info('Page:Launch');
Expand All @@ -102,12 +105,12 @@ 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 });

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');

Expand All @@ -127,19 +130,13 @@ export async function takeScreenshots(host: string, tiles: string, chrome: Brows
}
}

async function startServer(host: string, port: number, config: string, logger: LogType): Promise<FastifyInstance> {
async function startServer(port: number, config: string, logger: LogType): Promise<FastifyInstance> {
// Bundle Config
const configJson = await fsa.readJson<ConfigBundled>(config);
const mem = ConfigProviderMemory.fromJson(configJson);
Config.setConfigProvider(mem);

// Start server
const server = createServer(logger);
await new Promise<void>((resolve) =>
server.listen(port, '0.0.0.0', () => {
logger.info({ url: host }, 'ServerStarted');
resolve();
}),
);
return server;
return await new Promise<FastifyInstance>((resolve) => server.listen(port, '0.0.0.0', () => resolve(server)));
}