Skip to content

Commit 0082161

Browse files
committed
Actually run the server and update configurations
1 parent 7b4f7a4 commit 0082161

File tree

6 files changed

+120
-42
lines changed

6 files changed

+120
-42
lines changed

packages/plugins/synthetics/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"dependencies": {
2222
"@dd/core": "workspace:*",
23+
"@dd/error-tracking-plugin": "workspace:*",
2324
"chalk": "2.3.1"
2425
}
2526
}

packages/plugins/synthetics/src/constants.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ import type { PluginName } from '@dd/core/types';
66

77
export const CONFIG_KEY = 'synthetics' as const;
88
export const PLUGIN_NAME: PluginName = 'datadog-synthetics-plugin' as const;
9+
10+
export const API_PREFIX = '_datadog-ci_';
11+
export const DEFAULT_PORT = 1234;

packages/plugins/synthetics/src/index.ts

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,82 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

5+
import { runServer } from '@dd/core/helpers/server';
56
import type { GlobalContext, GetPlugins, Options } from '@dd/core/types';
7+
import { CONFIG_KEY as ERROR_TRACKING } from '@dd/error-tracking-plugin';
8+
import { API_PREFIX, CONFIG_KEY, PLUGIN_NAME } from '@dd/synthetics-plugin/constants';
9+
import type { BuildStatus, SyntheticsOptions } from '@dd/synthetics-plugin/types';
10+
import { validateOptions } from '@dd/synthetics-plugin/validate';
611
import chalk from 'chalk';
712

8-
import { CONFIG_KEY, PLUGIN_NAME } from './constants';
9-
import type { SyntheticsOptions, SyntheticsOptionsWithDefaults } from './types';
10-
1113
export { CONFIG_KEY, PLUGIN_NAME };
1214

13-
export const helpers = {
14-
// Add the helpers you'd like to expose here.
15-
};
16-
1715
export type types = {
1816
// Add the types you'd like to expose here.
1917
SyntheticsOptions: SyntheticsOptions;
2018
};
2119

22-
// Deal with validation and defaults here.
23-
export const validateOptions = (config: Options): SyntheticsOptionsWithDefaults => {
24-
const validatedOptions: SyntheticsOptionsWithDefaults = {
25-
// We don't want to disable it by default.
26-
disabled: false,
27-
...config[CONFIG_KEY],
28-
};
29-
return validatedOptions;
30-
};
31-
3220
export const getPlugins: GetPlugins = (opts: Options, context: GlobalContext) => {
3321
const log = context.getLogger(PLUGIN_NAME);
3422
// Verify configuration.
35-
const options = validateOptions(opts);
23+
const options = validateOptions(opts, context, log);
3624

3725
if (options.disabled) {
3826
return [];
3927
}
4028

29+
const response: { outDir?: string; publicPath?: string; status: BuildStatus } = {
30+
publicPath: opts[ERROR_TRACKING]?.sourcemaps?.minifiedPathPrefix,
31+
status: 'running',
32+
};
33+
const getServerResponse = () => {
34+
return response;
35+
};
36+
37+
if (options.server.run) {
38+
const port = options.server.port;
39+
log.info(
40+
`Starting Synthetics local server on ${chalk.bold.cyan(`http://127.0.0.1:${port}`)}.`,
41+
);
42+
43+
const server = runServer({
44+
port,
45+
root: context.bundler.outDir,
46+
routes: {
47+
[`/${API_PREFIX}/build-status`]: {
48+
get: (req, res) => {
49+
res.writeHead(200, { 'Content-Type': 'application/json' });
50+
res.end(JSON.stringify(getServerResponse()));
51+
},
52+
},
53+
[`/${API_PREFIX}/kill`]: {
54+
get: (req, res) => {
55+
res.writeHead(200, { 'Content-Type': 'text/html' });
56+
res.end('ok');
57+
// kill kill kill.
58+
server.close();
59+
server.closeAllConnections();
60+
server.closeIdleConnections();
61+
},
62+
},
63+
},
64+
});
65+
}
66+
4167
return [
4268
{
4369
name: PLUGIN_NAME,
44-
async writeBundle() {
45-
// Execute code after the bundle is written.
46-
// https://rollupjs.org/plugin-development/#writebundle
47-
const { BUILD_PLUGINS_S8S_LOCAL, BUILD_PLUGINS_S8S_PORT } = process.env;
48-
const runServer =
49-
!options.disabled && BUILD_PLUGINS_S8S_LOCAL === '1' && BUILD_PLUGINS_S8S_PORT;
50-
51-
if (BUILD_PLUGINS_S8S_LOCAL && !BUILD_PLUGINS_S8S_PORT) {
52-
log.warn(
53-
`Synthetics local server port is not set, please use ${chalk.bold.yellow('$BUILD_PLUGINS_S8S_PORT=1234')}.`,
54-
);
55-
}
56-
57-
if (!BUILD_PLUGINS_S8S_LOCAL && BUILD_PLUGINS_S8S_PORT) {
58-
log.warn(
59-
`Got server port but Synthetics local server is disabled, please use ${chalk.bold.yellow('$BUILD_PLUGINS_S8S_LOCAL=1')}.`,
60-
);
61-
}
62-
if (runServer) {
63-
const port = +BUILD_PLUGINS_S8S_PORT;
64-
log.info(
65-
`Starting Synthetics local server on ${chalk.bold.cyan(`http://127.0.0.1:${port}`)}.`,
66-
);
70+
bundlerReport(bundlerReport) {
71+
response.outDir = bundlerReport.outDir;
72+
},
73+
buildReport(buildReport) {
74+
if (buildReport.errors.length) {
75+
response.status = 'fail';
6776
}
6877
},
78+
writeBundle() {
79+
response.status = 'success';
80+
},
6981
},
7082
];
7183
};

packages/plugins/synthetics/src/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,24 @@
22
// This product includes software developed at Datadog (https://www.datadoghq.com/).
33
// Copyright 2019-Present Datadog, Inc.
44

5+
import type { Assign, Ensure } from '@dd/core/types';
6+
7+
export type ServerOptions = {
8+
port?: number;
9+
root?: string;
10+
run?: boolean;
11+
};
12+
513
export type SyntheticsOptions = {
614
disabled?: boolean;
15+
server?: ServerOptions;
716
};
817

9-
export type SyntheticsOptionsWithDefaults = Required<SyntheticsOptions>;
18+
export type SyntheticsOptionsWithDefaults = Assign<
19+
Ensure<SyntheticsOptions, 'disabled'>,
20+
{
21+
server: Ensure<ServerOptions, 'port' | 'root' | 'run'>;
22+
}
23+
>;
24+
25+
export type BuildStatus = 'running' | 'success' | 'fail';
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import type { Options, Logger, GlobalContext } from '@dd/core/types';
2+
import { CONFIG_KEY, DEFAULT_PORT } from '@dd/synthetics-plugin/constants';
3+
import type { SyntheticsOptionsWithDefaults } from '@dd/synthetics-plugin/types';
4+
import chalk from 'chalk';
5+
6+
export const validateOptions = (
7+
config: Options,
8+
context: GlobalContext,
9+
log: Logger,
10+
): SyntheticsOptionsWithDefaults => {
11+
const validatedOptions: SyntheticsOptionsWithDefaults = {
12+
// We don't want to disable it by default.
13+
disabled: false,
14+
...config[CONFIG_KEY],
15+
server: {
16+
run: false,
17+
port: DEFAULT_PORT,
18+
root: context.bundler.outDir,
19+
...config[CONFIG_KEY]?.server,
20+
},
21+
};
22+
23+
const { BUILD_PLUGINS_S8S_LOCAL, BUILD_PLUGINS_S8S_PORT } = process.env;
24+
25+
if (BUILD_PLUGINS_S8S_LOCAL && !BUILD_PLUGINS_S8S_PORT) {
26+
log.warn(
27+
`Synthetics local server port is not set, please use ${chalk.bold.yellow('$BUILD_PLUGINS_S8S_PORT=1234')}.`,
28+
);
29+
}
30+
31+
if (!BUILD_PLUGINS_S8S_LOCAL && BUILD_PLUGINS_S8S_PORT) {
32+
log.warn(
33+
`Got server port but Synthetics local server is disabled, please use ${chalk.bold.yellow('$BUILD_PLUGINS_S8S_LOCAL=1')}.`,
34+
);
35+
}
36+
37+
validatedOptions.server.run =
38+
!validatedOptions.disabled && BUILD_PLUGINS_S8S_LOCAL === '1' && !!BUILD_PLUGINS_S8S_PORT;
39+
40+
if (BUILD_PLUGINS_S8S_PORT) {
41+
validatedOptions.server.port = +BUILD_PLUGINS_S8S_PORT;
42+
}
43+
44+
return validatedOptions;
45+
};

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,7 @@ __metadata:
17621762
resolution: "@dd/synthetics-plugin@workspace:packages/plugins/synthetics"
17631763
dependencies:
17641764
"@dd/core": "workspace:*"
1765+
"@dd/error-tracking-plugin": "workspace:*"
17651766
chalk: "npm:2.3.1"
17661767
languageName: unknown
17671768
linkType: soft

0 commit comments

Comments
 (0)