@platformatic/runtime 1.12.1 → 1.13.0

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,39 @@
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 = {
19
+ }
20
+
21
+ class RuntimeGenerator extends BaseGenerator {
22
+ services: Service[]
23
+ entryPoint: Service
24
+ constructor(opts?: RuntimeGeneratorOptions)
25
+
26
+ async addService(service: Service, name: string): Promise<void>
27
+
28
+ setEntryPoint(entryPoint: string): void
29
+
30
+ setServicesDirectory(): void
31
+
32
+ setServicesConfig(configToOverride: object): void
33
+
34
+ getRuntimeEnv(): KeyValue
35
+ async writeServicesFiles(): Promise<GeneratorMetadata>
36
+ }
37
+
38
+ export default RuntimeGenerator
39
+ export { RuntimeGenerator }
@@ -0,0 +1,173 @@
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
+
65
+ this.config.env = {
66
+ PLT_SERVER_HOSTNAME: '0.0.0.0',
67
+ PORT: this.config.port || 3042,
68
+ PLT_SERVER_LOGGER_LEVEL: 'info',
69
+ ...this.config.env
70
+ }
71
+ }
72
+
73
+ async _getConfigFileContents () {
74
+ const config = {
75
+ $schema: `https://platformatic.dev/schemas/v${this.platformaticVersion}/runtime`,
76
+ entrypoint: this.entryPoint.name,
77
+ allowCycles: false,
78
+ hotReload: true,
79
+ autoload: {
80
+ path: 'services',
81
+ exclude: ['docs']
82
+ },
83
+ server: {
84
+ hostname: '{PLT_SERVER_HOSTNAME}',
85
+ port: '{PORT}',
86
+ logger: {
87
+ level: '{PLT_SERVER_LOGGER_LEVEL}'
88
+ }
89
+ }
90
+ }
91
+
92
+ return config
93
+ }
94
+
95
+ async _afterPrepare () {
96
+ if (!this.entryPoint) {
97
+ throw new NoEntryPointError()
98
+ }
99
+ const servicesEnv = await this.prepareServiceFiles()
100
+ this.config.env = {
101
+ ...this.config.env,
102
+ ...this.getRuntimeEnv(),
103
+ ...servicesEnv
104
+ }
105
+
106
+ this.addFile({
107
+ path: '',
108
+ file: '.env',
109
+ contents: envObjectToString(this.config.env)
110
+ })
111
+
112
+ return {
113
+ targetDirectory: this.targetDirectory,
114
+ env: servicesEnv
115
+ }
116
+ }
117
+
118
+ async writeFiles () {
119
+ await super.writeFiles()
120
+ for (const { service } of this.services) {
121
+ await service.writeFiles()
122
+ }
123
+ }
124
+
125
+ setServicesDirectory () {
126
+ this.services.forEach(({ service }) => {
127
+ if (!service.config) {
128
+ // set default config
129
+ service.setConfig()
130
+ }
131
+ service.setTargetDirectory(join(this.targetDirectory, 'services', service.config.serviceName))
132
+ })
133
+ }
134
+
135
+ setServicesConfig (configToOverride) {
136
+ this.services.forEach((service) => {
137
+ const originalConfig = service.config
138
+ service.setConfig({
139
+ ...originalConfig,
140
+ ...configToOverride
141
+ })
142
+ })
143
+ }
144
+
145
+ async prepareServiceFiles () {
146
+ let servicesEnv = {}
147
+ for (const svc of this.services) {
148
+ const svcEnv = await svc.service.prepare()
149
+ servicesEnv = {
150
+ ...servicesEnv,
151
+ ...svcEnv.env
152
+ }
153
+ }
154
+ return servicesEnv
155
+ }
156
+
157
+ getConfigFieldsDefinitions () {
158
+ return []
159
+ }
160
+
161
+ setConfigFields () {
162
+ // do nothing, makes no sense
163
+ }
164
+
165
+ getRuntimeEnv () {
166
+ return {
167
+ PORT: this.config.port
168
+ }
169
+ }
170
+ }
171
+
172
+ module.exports = RuntimeGenerator
173
+ 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.0",
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.0",
32
+ "@platformatic/sql-mapper": "1.13.0"
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/config": "1.13.0",
53
+ "@platformatic/db": "1.13.0",
54
+ "@platformatic/service": "1.13.0",
55
+ "@platformatic/telemetry": "1.13.0",
56
+ "@platformatic/utils": "1.13.0",
57
+ "@platformatic/generators": "1.13.0",
58
+ "@platformatic/composer": "1.13.0"
57
59
  },
58
60
  "standard": {
59
61
  "ignore": [