@platformatic/runtime 1.13.1 → 1.13.3
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.d.ts +8 -1
- package/lib/errors.js +1 -2
- package/lib/generator/README.md +32 -0
- package/lib/generator/errors.js +1 -1
- package/lib/generator/runtime-generator.d.ts +20 -23
- package/lib/generator/runtime-generator.js +96 -3
- package/package.json +10 -10
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { InjectOptions, LightMyRequestResponse } from 'fastify'
|
|
2
2
|
import { FastifyError } from '@fastify/error'
|
|
3
|
-
|
|
3
|
+
import { BaseLogger } from 'pino'
|
|
4
|
+
import { RuntimeGenerator } from './lib/generator/runtime-generator'
|
|
4
5
|
export type pltRuntimeBuildServer = {
|
|
5
6
|
address: string
|
|
6
7
|
port: number
|
|
@@ -11,6 +12,12 @@ export type pltRuntimeBuildServer = {
|
|
|
11
12
|
|
|
12
13
|
declare module '@platformatic/runtime' {
|
|
13
14
|
export function buildServer(opts: object): Promise<pltRuntimeBuildServer>
|
|
15
|
+
export function start(args: object): Promise<object>
|
|
16
|
+
export function startCommand(args: object): Promise<void>
|
|
17
|
+
export function loadConfig(minimistConfig: object, args: object, store: object, overrides: object, replaceEnv: boolean): void
|
|
18
|
+
export function compile(argv: string[], logger: BaseLogger): void
|
|
19
|
+
export function platformaticRuntime(): Promise<void>
|
|
20
|
+
export const Generator: RuntimeGenerator.RuntimeGenerator
|
|
14
21
|
}
|
|
15
22
|
|
|
16
23
|
/**
|
package/lib/errors.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const createError = require('@fastify/error')
|
|
4
4
|
|
|
5
|
-
const ERROR_PREFIX = '
|
|
5
|
+
const ERROR_PREFIX = 'PLT_RUNTIME'
|
|
6
6
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
RuntimeExitedError: createError(`${ERROR_PREFIX}_RUNTIME_EXIT`, 'The runtime exited before the operation completed'),
|
|
@@ -22,5 +22,4 @@ module.exports = {
|
|
|
22
22
|
InspectorHostError: createError(`${ERROR_PREFIX}_INSPECTOR_HOST`, 'Inspector host cannot be empty'),
|
|
23
23
|
CannotMapSpecifierToAbsolutePathError: createError(`${ERROR_PREFIX}_CANNOT_MAP_SPECIFIER_TO_ABSOLUTE_PATH`, 'Cannot map "%s" to an absolute path'),
|
|
24
24
|
NodeInspectorFlagsNotSupportedError: createError(`${ERROR_PREFIX}_NODE_INSPECTOR_FLAGS_NOT_SUPPORTED`, 'The Node.js inspector flags are not supported. Please use \'platformatic start --inspect\' instead.')
|
|
25
|
-
|
|
26
25
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Platformatic Runtime API
|
|
2
|
+
|
|
3
|
+
This is a generated [Platformatic Runtime](https://docs.platformatic.dev/docs/reference/runtime/introduction) application.
|
|
4
|
+
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
Platformatic supports macOS, Linux and Windows ([WSL](https://docs.microsoft.com/windows/wsl/) recommended).
|
|
8
|
+
You'll need to have [Node.js](https://nodejs.org/) >= v18.8.0 or >= v20.6.0
|
|
9
|
+
|
|
10
|
+
## Setup
|
|
11
|
+
|
|
12
|
+
1. Install dependencies:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
|
|
20
|
+
Run the API with:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm start
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Adding a Service
|
|
27
|
+
|
|
28
|
+
Adding a new service to this project is as simple as running `create-platformatic` again, like so:
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
npx create-platformatic
|
|
32
|
+
```
|
package/lib/generator/errors.js
CHANGED
|
@@ -6,5 +6,5 @@ const ERROR_PREFIX = 'PLT_RUNTIME_GEN'
|
|
|
6
6
|
|
|
7
7
|
module.exports = {
|
|
8
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
|
|
9
|
+
NoEntryPointError: createError(`${ERROR_PREFIX}_NO_ENTRYPOINT`, 'No entrypoint had been defined.')
|
|
10
10
|
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { BaseGenerator, BaseGeneratorOptions } from "@platformatic/generators"
|
|
1
|
+
import { BaseGenerator } from "@platformatic/generators"
|
|
4
2
|
import { FileGenerator } from "@platformatic/generators/lib/file-generator"
|
|
5
3
|
|
|
6
4
|
type Service = {
|
|
7
|
-
config: FileGenerator | BaseGenerator
|
|
5
|
+
config: FileGenerator.FileGenerator | BaseGenerator.BaseGenerator
|
|
8
6
|
}
|
|
9
7
|
type GeneratorMetadata = {
|
|
10
8
|
targetDirectory: string
|
|
@@ -15,26 +13,25 @@ type KeyValue = {
|
|
|
15
13
|
[key: string]: string
|
|
16
14
|
}
|
|
17
15
|
|
|
18
|
-
type RuntimeGeneratorOptions = BaseGeneratorOptions & {
|
|
16
|
+
type RuntimeGeneratorOptions = BaseGenerator.BaseGeneratorOptions & {
|
|
19
17
|
logLevel: string
|
|
20
18
|
}
|
|
21
19
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
20
|
+
export namespace RuntimeGenerator {
|
|
21
|
+
export class RuntimeGenerator extends BaseGenerator.BaseGenerator {
|
|
22
|
+
services: Service[]
|
|
23
|
+
entryPoint: Service
|
|
24
|
+
constructor(opts?: RuntimeGeneratorOptions)
|
|
25
|
+
|
|
26
|
+
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
|
+
writeServicesFiles(): Promise<GeneratorMetadata>
|
|
36
|
+
}
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
export default RuntimeGenerator
|
|
40
|
-
export { RuntimeGenerator }
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
'use strict'
|
|
2
1
|
const { BaseGenerator } = require('@platformatic/generators')
|
|
3
2
|
const { NoEntryPointError, NoServiceNamedError } = require('./errors')
|
|
4
3
|
const generateName = require('boring-name-generator')
|
|
5
4
|
const { join } = require('node:path')
|
|
6
5
|
const { envObjectToString } = require('@platformatic/generators/lib/utils')
|
|
6
|
+
const { readFile } = require('node:fs/promises')
|
|
7
|
+
const { ConfigManager } = require('@platformatic/config')
|
|
8
|
+
const { platformaticRuntime } = require('../config')
|
|
9
|
+
|
|
7
10
|
class RuntimeGenerator extends BaseGenerator {
|
|
8
11
|
constructor (opts) {
|
|
9
|
-
super(
|
|
12
|
+
super({
|
|
13
|
+
...opts,
|
|
14
|
+
module: '@platformatic/runtime'
|
|
15
|
+
})
|
|
10
16
|
this.services = []
|
|
11
17
|
this.entryPoint = null
|
|
12
18
|
}
|
|
@@ -42,7 +48,7 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
async generatePackageJson () {
|
|
45
|
-
|
|
51
|
+
const template = {
|
|
46
52
|
scripts: {
|
|
47
53
|
start: 'platformatic start',
|
|
48
54
|
test: 'node --test test/*/*.test.js'
|
|
@@ -57,6 +63,13 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
57
63
|
node: '^18.8.0 || >=20.6.0'
|
|
58
64
|
}
|
|
59
65
|
}
|
|
66
|
+
if (this.config.typescript) {
|
|
67
|
+
const typescriptVersion = JSON.parse(await readFile(join(__dirname, '..', '..', 'package.json'), 'utf-8')).devDependencies.typescript
|
|
68
|
+
template.scripts.clean = 'rm -fr ./dist'
|
|
69
|
+
template.scripts.build = 'platformatic compile'
|
|
70
|
+
template.devDependencies.typescript = typescriptVersion
|
|
71
|
+
}
|
|
72
|
+
return template
|
|
60
73
|
}
|
|
61
74
|
|
|
62
75
|
async _beforePrepare () {
|
|
@@ -70,6 +83,40 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
70
83
|
}
|
|
71
84
|
}
|
|
72
85
|
|
|
86
|
+
async populateFromExistingConfig () {
|
|
87
|
+
if (this._hasCheckedForExistingConfig) {
|
|
88
|
+
return
|
|
89
|
+
}
|
|
90
|
+
this._hasCheckedForExistingConfig = true
|
|
91
|
+
const existingConfigFile = await ConfigManager.findConfigFile(this.targetDirectory, 'runtime')
|
|
92
|
+
if (existingConfigFile) {
|
|
93
|
+
const configManager = new ConfigManager({
|
|
94
|
+
...platformaticRuntime.configManagerConfig,
|
|
95
|
+
source: join(this.targetDirectory, existingConfigFile)
|
|
96
|
+
})
|
|
97
|
+
await configManager.parse()
|
|
98
|
+
this.existingConfig = configManager.current
|
|
99
|
+
this.config.env = configManager.env
|
|
100
|
+
this.config.port = configManager.env.PORT
|
|
101
|
+
this.entryPoint = configManager.current.services.find((svc) => svc.entrypoint)
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async prepare () {
|
|
106
|
+
await this.populateFromExistingConfig()
|
|
107
|
+
if (this.existingConfig) {
|
|
108
|
+
this.setServicesDirectory()
|
|
109
|
+
this.setServicesConfigValues()
|
|
110
|
+
await this._afterPrepare()
|
|
111
|
+
return {
|
|
112
|
+
env: this.config.env,
|
|
113
|
+
targetDirectory: this.targetDirectory
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
return await super.prepare()
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
73
120
|
setServicesConfigValues () {
|
|
74
121
|
this.services.forEach(({ service }) => {
|
|
75
122
|
if (!service.config) {
|
|
@@ -119,6 +166,16 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
119
166
|
contents: envObjectToString(this.config.env)
|
|
120
167
|
})
|
|
121
168
|
|
|
169
|
+
this.addFile({
|
|
170
|
+
path: '',
|
|
171
|
+
file: '.env.sample',
|
|
172
|
+
contents: envObjectToString(this.config.env)
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
if (!this.existingConfig) {
|
|
176
|
+
this.addFile({ path: '', file: 'README.md', contents: await readFile(join(__dirname, 'README.md')) })
|
|
177
|
+
}
|
|
178
|
+
|
|
122
179
|
return {
|
|
123
180
|
targetDirectory: this.targetDirectory,
|
|
124
181
|
env: servicesEnv
|
|
@@ -132,6 +189,31 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
132
189
|
}
|
|
133
190
|
}
|
|
134
191
|
|
|
192
|
+
async prepareQuestions () {
|
|
193
|
+
await this.populateFromExistingConfig()
|
|
194
|
+
|
|
195
|
+
// typescript
|
|
196
|
+
this.questions.push({
|
|
197
|
+
type: 'list',
|
|
198
|
+
name: 'typescript',
|
|
199
|
+
message: 'Do you want to use TypeScript?',
|
|
200
|
+
default: false,
|
|
201
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
if (this.existingConfig) {
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// port
|
|
209
|
+
this.questions.push({
|
|
210
|
+
type: 'input',
|
|
211
|
+
name: 'port',
|
|
212
|
+
default: 3042,
|
|
213
|
+
message: 'What port do you want to use?'
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
|
|
135
217
|
setServicesDirectory () {
|
|
136
218
|
this.services.forEach(({ service }) => {
|
|
137
219
|
if (!service.config) {
|
|
@@ -155,6 +237,11 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
155
237
|
async prepareServiceFiles () {
|
|
156
238
|
let servicesEnv = {}
|
|
157
239
|
for (const svc of this.services) {
|
|
240
|
+
// Propagate TypeScript
|
|
241
|
+
svc.service.setConfig({
|
|
242
|
+
...svc.service.config,
|
|
243
|
+
typescript: this.config.typescript
|
|
244
|
+
})
|
|
158
245
|
const svcEnv = await svc.service.prepare()
|
|
159
246
|
servicesEnv = {
|
|
160
247
|
...servicesEnv,
|
|
@@ -177,6 +264,12 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
177
264
|
PORT: this.config.port
|
|
178
265
|
}
|
|
179
266
|
}
|
|
267
|
+
|
|
268
|
+
async postInstallActions () {
|
|
269
|
+
for (const { service } of this.services) {
|
|
270
|
+
await service.postInstallActions()
|
|
271
|
+
}
|
|
272
|
+
}
|
|
180
273
|
}
|
|
181
274
|
|
|
182
275
|
module.exports = RuntimeGenerator
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/runtime",
|
|
3
|
-
"version": "1.13.
|
|
3
|
+
"version": "1.13.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"bin": {
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"standard": "^17.1.0",
|
|
29
29
|
"tsd": "^0.29.0",
|
|
30
30
|
"typescript": "^5.2.2",
|
|
31
|
-
"@platformatic/sql-graphql": "1.13.
|
|
32
|
-
"@platformatic/sql-mapper": "1.13.
|
|
31
|
+
"@platformatic/sql-graphql": "1.13.3",
|
|
32
|
+
"@platformatic/sql-mapper": "1.13.3"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"@fastify/error": "^3.4.0",
|
|
@@ -49,13 +49,13 @@
|
|
|
49
49
|
"pino": "^8.16.0",
|
|
50
50
|
"pino-pretty": "^10.2.3",
|
|
51
51
|
"undici": "^5.26.3",
|
|
52
|
-
"@platformatic/
|
|
53
|
-
"@platformatic/
|
|
54
|
-
"@platformatic/
|
|
55
|
-
"@platformatic/service": "1.13.
|
|
56
|
-
"@platformatic/
|
|
57
|
-
"@platformatic/
|
|
58
|
-
"@platformatic/
|
|
52
|
+
"@platformatic/config": "1.13.3",
|
|
53
|
+
"@platformatic/db": "1.13.3",
|
|
54
|
+
"@platformatic/composer": "1.13.3",
|
|
55
|
+
"@platformatic/service": "1.13.3",
|
|
56
|
+
"@platformatic/telemetry": "1.13.3",
|
|
57
|
+
"@platformatic/utils": "1.13.3",
|
|
58
|
+
"@platformatic/generators": "1.13.3"
|
|
59
59
|
},
|
|
60
60
|
"standard": {
|
|
61
61
|
"ignore": [
|