-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserve.ts
More file actions
176 lines (168 loc) · 5.68 KB
/
serve.ts
File metadata and controls
176 lines (168 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import path from 'node:path';
import { valid as semverValid } from 'semver';
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import { startServer, Network } from '../grpc/server.js';
import { LogFormat, LogLevel, createLogger } from '../logger/logger.js';
import { packageDocker } from '../package/docker.js';
import type { Plugin } from './plugin.js';
const TELEMETRY_LEVEL_CHOICES = ['none', 'errors', 'stats', 'all'] as const;
export type BaseArguments = {
logLevel: LogLevel;
logFormat: LogFormat;
sentry: boolean;
otelEndpoint: string;
otelEndpointInsecure: boolean;
telemetryLevel: (typeof TELEMETRY_LEVEL_CHOICES)[number];
};
export type ServeArguments = BaseArguments & {
address: string;
network: Network;
};
export type PackageArguments = BaseArguments & {
message: string;
distDir: string;
docsDir: string;
pluginVersion: string;
pluginDirectory: string;
};
export const createServeCommand = (plugin: Plugin) => {
return yargs(hideBin(process.argv))
.command<ServeArguments>(
'serve',
'start plugin gRPC server',
(yargs) => {
return yargs.options({
address: {
alias: 'a',
type: 'string',
description: 'address to bind to',
default: 'localhost:7777',
},
network: {
alias: 'n',
type: 'string',
choices: Object.values(Network),
description: 'network to bind to',
default: 'tcp',
},
license: {
type: 'string',
description: 'set offline license file (placeholder for future use)',
default: '',
},
});
},
({ address, logLevel, logFormat }: ServeArguments) => {
const logger = createLogger(logLevel, logFormat);
plugin.setLogger(logger);
startServer(logger, address, plugin);
},
)
.command<PackageArguments>(
'package <pluginVersion> <pluginDirectory>',
'package the plugin as a Docker image',
(yargs) => {
return yargs
.options({
message: {
alias: 'm',
type: 'string',
description:
'message that summarizes what is new or changed in this version. Use @<file> to read from file. Supports markdown.',
demandOption: true,
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'dist-dir': {
alias: 'D',
type: 'string',
description: 'dist directory to output the built plugin. (default: <plugin_directory>/dist)',
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'docs-dir': {
type: 'string',
description:
'docs directory containing markdown files to copy to the dist directory. (default: <plugin_directory>/docs)',
},
})
.positional('pluginVersion', {
type: 'string',
description: 'version to tag the Docker image with',
})
.positional('pluginDirectory', {
type: 'string',
});
},
async ({ message, distDir, docsDir, pluginVersion, pluginDirectory, logLevel, logFormat }: PackageArguments) => {
const logger = createLogger(logLevel, logFormat);
plugin.setLogger(logger);
if (!plugin.name()) {
throw new Error('plugin name is required');
}
if (!plugin.team()) {
throw new Error('plugin team is required');
}
if (!plugin.kind()) {
throw new Error('plugin kind is required');
}
if (!plugin.dockerFile()) {
throw new Error('docker file is required');
}
if (plugin.buildTargets().length === 0) {
throw new Error('at least one build target is required');
}
if (!semverValid(pluginVersion)) {
throw new Error(`invalid plugin version: ${pluginVersion}`);
}
distDir = distDir || path.join(pluginDirectory, 'dist');
docsDir = docsDir || path.join(pluginDirectory, 'docs');
await packageDocker({ logger, message, distDir, docsDir, pluginVersion, pluginDirectory, plugin });
},
)
.options({
// eslint-disable-next-line @typescript-eslint/naming-convention
'log-level': {
alias: 'l',
type: 'string',
choices: Object.values(LogLevel),
description: 'log level',
default: 'info',
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'log-format': {
alias: 'f',
type: 'string',
choices: Object.values(LogFormat),
description: 'log format',
default: 'text',
},
sentry: {
type: 'boolean',
description: 'enable sentry reporting. Pass `--no-sentry` to disable.',
default: true,
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'otel-endpoint': {
type: 'string',
description: 'OpenTelemetry collector endpoint',
default: '',
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'otel-endpoint-insecure': {
type: 'boolean',
description: 'use Open Telemetry HTTP endpoint (for development only)',
default: false,
},
// eslint-disable-next-line @typescript-eslint/naming-convention
'telemetry-level': {
type: 'string',
description: 'CQ Telemetry level',
hidden: true,
choices: TELEMETRY_LEVEL_CHOICES,
default: 'all',
},
})
.env('CQ_')
.strict()
.demandCommand(1, 1, 'Specify a command to run');
};