@platformatic/runtime 1.12.1 → 1.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -6,6 +6,7 @@ const RuntimeApi = require('./lib/api')
6
6
  const { compile } = require('./lib/compile')
7
7
  const { loadConfig } = require('./lib/load-config')
8
8
  const errors = require('./lib/errors')
9
+ const RuntimeGenerator = require('./lib/generator/runtime-generator')
9
10
 
10
11
  module.exports.buildServer = buildServer
11
12
  module.exports.platformaticRuntime = platformaticRuntime
@@ -16,3 +17,4 @@ module.exports.startCommand = startCommand
16
17
  module.exports.compile = compile
17
18
  module.exports.loadConfig = loadConfig
18
19
  module.exports.errors = errors
20
+ module.exports.Generator = RuntimeGenerator
@@ -0,0 +1,10 @@
1
+ 'use strict'
2
+
3
+ const createError = require('@fastify/error')
4
+
5
+ const ERROR_PREFIX = 'PLT_RUNTIME_GEN'
6
+
7
+ module.exports = {
8
+ NoServiceNamedError: createError(`${ERROR_PREFIX}_NO_SERVICE_FOUND`, 'No service named \'%s\' has been added to this runtime.'),
9
+ NoEntryPointError: createError(`${ERROR_PREFIX}_NO_ENTRYPOINT`, 'No entrypoint had been deinfed.')
10
+ }
@@ -0,0 +1,40 @@
1
+ 'use strict'
2
+
3
+ import { BaseGenerator, BaseGeneratorOptions } from "@platformatic/generators"
4
+ import { FileGenerator } from "@platformatic/generators/lib/file-generator"
5
+
6
+ type Service = {
7
+ config: FileGenerator | BaseGenerator
8
+ }
9
+ type GeneratorMetadata = {
10
+ targetDirectory: string
11
+ env: KeyValue
12
+ }
13
+
14
+ type KeyValue = {
15
+ [key: string]: string
16
+ }
17
+
18
+ type RuntimeGeneratorOptions = BaseGeneratorOptions & {
19
+ logLevel: string
20
+ }
21
+
22
+ class RuntimeGenerator extends BaseGenerator {
23
+ services: Service[]
24
+ entryPoint: Service
25
+ constructor(opts?: RuntimeGeneratorOptions)
26
+
27
+ async addService(service: Service, name: string): Promise<void>
28
+
29
+ setEntryPoint(entryPoint: string): void
30
+
31
+ setServicesDirectory(): void
32
+
33
+ setServicesConfig(configToOverride: object): void
34
+
35
+ getRuntimeEnv(): KeyValue
36
+ async writeServicesFiles(): Promise<GeneratorMetadata>
37
+ }
38
+
39
+ export default RuntimeGenerator
40
+ export { RuntimeGenerator }
@@ -0,0 +1,183 @@
1
+ 'use strict'
2
+ const { BaseGenerator } = require('@platformatic/generators')
3
+ const { NoEntryPointError, NoServiceNamedError } = require('./errors')
4
+ const generateName = require('boring-name-generator')
5
+ const { join } = require('node:path')
6
+ const { envObjectToString } = require('@platformatic/generators/lib/utils')
7
+ class RuntimeGenerator extends BaseGenerator {
8
+ constructor (opts) {
9
+ super(opts)
10
+ this.services = []
11
+ this.entryPoint = null
12
+ }
13
+
14
+ async addService (service, name) {
15
+ // ensure service config is correct
16
+ const originalConfig = service.config
17
+ const serviceName = name || generateName().dashed
18
+ const newConfig = {
19
+ ...originalConfig,
20
+ isRuntimeContext: true,
21
+ serviceName
22
+ }
23
+ // reset all files previously generated by the service
24
+ service.reset()
25
+ service.setConfig(newConfig)
26
+ this.services.push({
27
+ name: serviceName,
28
+ service
29
+ })
30
+
31
+ if (typeof service.setRuntime === 'function') {
32
+ service.setRuntime(this)
33
+ }
34
+ }
35
+
36
+ setEntryPoint (entryPoint) {
37
+ const service = this.services.find((svc) => svc.name === entryPoint)
38
+ if (!service) {
39
+ throw new NoServiceNamedError(entryPoint)
40
+ }
41
+ this.entryPoint = service
42
+ }
43
+
44
+ async generatePackageJson () {
45
+ return {
46
+ scripts: {
47
+ start: 'platformatic start',
48
+ test: 'node --test test/*/*.test.js'
49
+ },
50
+ devDependencies: {
51
+ fastify: `^${this.fastifyVersion}`
52
+ },
53
+ dependencies: {
54
+ platformatic: `^${this.platformaticVersion}`
55
+ },
56
+ engines: {
57
+ node: '^18.8.0 || >=20.6.0'
58
+ }
59
+ }
60
+ }
61
+
62
+ async _beforePrepare () {
63
+ this.setServicesDirectory()
64
+ this.setServicesConfigValues()
65
+ this.config.env = {
66
+ PLT_SERVER_HOSTNAME: '0.0.0.0',
67
+ PORT: this.config.port || 3042,
68
+ PLT_SERVER_LOGGER_LEVEL: this.config.logLevel || 'info',
69
+ ...this.config.env
70
+ }
71
+ }
72
+
73
+ setServicesConfigValues () {
74
+ this.services.forEach(({ service }) => {
75
+ if (!service.config) {
76
+ // set default config
77
+ service.setConfig()
78
+ }
79
+ service.config.typescript = this.config.typescript
80
+ })
81
+ }
82
+
83
+ async _getConfigFileContents () {
84
+ const config = {
85
+ $schema: `https://platformatic.dev/schemas/v${this.platformaticVersion}/runtime`,
86
+ entrypoint: this.entryPoint.name,
87
+ allowCycles: false,
88
+ hotReload: true,
89
+ autoload: {
90
+ path: 'services',
91
+ exclude: ['docs']
92
+ },
93
+ server: {
94
+ hostname: '{PLT_SERVER_HOSTNAME}',
95
+ port: '{PORT}',
96
+ logger: {
97
+ level: '{PLT_SERVER_LOGGER_LEVEL}'
98
+ }
99
+ }
100
+ }
101
+
102
+ return config
103
+ }
104
+
105
+ async _afterPrepare () {
106
+ if (!this.entryPoint) {
107
+ throw new NoEntryPointError()
108
+ }
109
+ const servicesEnv = await this.prepareServiceFiles()
110
+ this.config.env = {
111
+ ...this.config.env,
112
+ ...this.getRuntimeEnv(),
113
+ ...servicesEnv
114
+ }
115
+
116
+ this.addFile({
117
+ path: '',
118
+ file: '.env',
119
+ contents: envObjectToString(this.config.env)
120
+ })
121
+
122
+ return {
123
+ targetDirectory: this.targetDirectory,
124
+ env: servicesEnv
125
+ }
126
+ }
127
+
128
+ async writeFiles () {
129
+ await super.writeFiles()
130
+ for (const { service } of this.services) {
131
+ await service.writeFiles()
132
+ }
133
+ }
134
+
135
+ setServicesDirectory () {
136
+ this.services.forEach(({ service }) => {
137
+ if (!service.config) {
138
+ // set default config
139
+ service.setConfig()
140
+ }
141
+ service.setTargetDirectory(join(this.targetDirectory, 'services', service.config.serviceName))
142
+ })
143
+ }
144
+
145
+ setServicesConfig (configToOverride) {
146
+ this.services.forEach((service) => {
147
+ const originalConfig = service.config
148
+ service.setConfig({
149
+ ...originalConfig,
150
+ ...configToOverride
151
+ })
152
+ })
153
+ }
154
+
155
+ async prepareServiceFiles () {
156
+ let servicesEnv = {}
157
+ for (const svc of this.services) {
158
+ const svcEnv = await svc.service.prepare()
159
+ servicesEnv = {
160
+ ...servicesEnv,
161
+ ...svcEnv.env
162
+ }
163
+ }
164
+ return servicesEnv
165
+ }
166
+
167
+ getConfigFieldsDefinitions () {
168
+ return []
169
+ }
170
+
171
+ setConfigFields () {
172
+ // do nothing, makes no sense
173
+ }
174
+
175
+ getRuntimeEnv () {
176
+ return {
177
+ PORT: this.config.port
178
+ }
179
+ }
180
+ }
181
+
182
+ module.exports = RuntimeGenerator
183
+ module.exports.RuntimeGenerator = RuntimeGenerator
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/runtime",
3
- "version": "1.12.1",
3
+ "version": "1.13.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -28,12 +28,13 @@
28
28
  "standard": "^17.1.0",
29
29
  "tsd": "^0.29.0",
30
30
  "typescript": "^5.2.2",
31
- "@platformatic/sql-graphql": "1.12.1",
32
- "@platformatic/sql-mapper": "1.12.1"
31
+ "@platformatic/sql-graphql": "1.13.1",
32
+ "@platformatic/sql-mapper": "1.13.1"
33
33
  },
34
34
  "dependencies": {
35
35
  "@fastify/error": "^3.4.0",
36
36
  "@hapi/topo": "^6.0.2",
37
+ "boring-name-generator": "^1.0.3",
37
38
  "close-with-grace": "^1.2.0",
38
39
  "commist": "^3.2.0",
39
40
  "debounce": "^2.0.0",
@@ -48,12 +49,13 @@
48
49
  "pino": "^8.16.0",
49
50
  "pino-pretty": "^10.2.3",
50
51
  "undici": "^5.26.3",
51
- "@platformatic/composer": "1.12.1",
52
- "@platformatic/service": "1.12.1",
53
- "@platformatic/db": "1.12.1",
54
- "@platformatic/config": "1.12.1",
55
- "@platformatic/telemetry": "1.12.1",
56
- "@platformatic/utils": "1.12.1"
52
+ "@platformatic/composer": "1.13.1",
53
+ "@platformatic/generators": "1.13.1",
54
+ "@platformatic/db": "1.13.1",
55
+ "@platformatic/service": "1.13.1",
56
+ "@platformatic/config": "1.13.1",
57
+ "@platformatic/telemetry": "1.13.1",
58
+ "@platformatic/utils": "1.13.1"
57
59
  },
58
60
  "standard": {
59
61
  "ignore": [