@platformatic/runtime 1.13.0 → 1.13.2

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 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 = 'PLT_SQL_RUNTIME'
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
+ ```
@@ -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 deinfed.')
9
+ NoEntryPointError: createError(`${ERROR_PREFIX}_NO_ENTRYPOINT`, 'No entrypoint had been defined.')
10
10
  }
@@ -1,10 +1,8 @@
1
- 'use strict'
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,25 +13,25 @@ type KeyValue = {
15
13
  [key: string]: string
16
14
  }
17
15
 
18
- type RuntimeGeneratorOptions = {
16
+ type RuntimeGeneratorOptions = BaseGenerator.BaseGeneratorOptions & {
17
+ logLevel: string
19
18
  }
20
19
 
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>
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
+ }
36
37
  }
37
-
38
- export default RuntimeGenerator
39
- 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(opts)
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
- return {
51
+ const template = {
46
52
  scripts: {
47
53
  start: 'platformatic start',
48
54
  test: 'node --test test/*/*.test.js'
@@ -57,19 +63,70 @@ 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 () {
63
76
  this.setServicesDirectory()
64
-
77
+ this.setServicesConfigValues()
65
78
  this.config.env = {
66
79
  PLT_SERVER_HOSTNAME: '0.0.0.0',
67
80
  PORT: this.config.port || 3042,
68
- PLT_SERVER_LOGGER_LEVEL: 'info',
81
+ PLT_SERVER_LOGGER_LEVEL: this.config.logLevel || 'info',
69
82
  ...this.config.env
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
+
120
+ setServicesConfigValues () {
121
+ this.services.forEach(({ service }) => {
122
+ if (!service.config) {
123
+ // set default config
124
+ service.setConfig()
125
+ }
126
+ service.config.typescript = this.config.typescript
127
+ })
128
+ }
129
+
73
130
  async _getConfigFileContents () {
74
131
  const config = {
75
132
  $schema: `https://platformatic.dev/schemas/v${this.platformaticVersion}/runtime`,
@@ -109,6 +166,16 @@ class RuntimeGenerator extends BaseGenerator {
109
166
  contents: envObjectToString(this.config.env)
110
167
  })
111
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
+
112
179
  return {
113
180
  targetDirectory: this.targetDirectory,
114
181
  env: servicesEnv
@@ -122,6 +189,31 @@ class RuntimeGenerator extends BaseGenerator {
122
189
  }
123
190
  }
124
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
+
125
217
  setServicesDirectory () {
126
218
  this.services.forEach(({ service }) => {
127
219
  if (!service.config) {
@@ -145,6 +237,11 @@ class RuntimeGenerator extends BaseGenerator {
145
237
  async prepareServiceFiles () {
146
238
  let servicesEnv = {}
147
239
  for (const svc of this.services) {
240
+ // Propagate TypeScript
241
+ svc.service.setConfig({
242
+ ...svc.service.config,
243
+ typescript: this.config.typescript
244
+ })
148
245
  const svcEnv = await svc.service.prepare()
149
246
  servicesEnv = {
150
247
  ...servicesEnv,
@@ -167,6 +264,12 @@ class RuntimeGenerator extends BaseGenerator {
167
264
  PORT: this.config.port
168
265
  }
169
266
  }
267
+
268
+ async postInstallActions () {
269
+ for (const { service } of this.services) {
270
+ await service.postInstallActions()
271
+ }
272
+ }
170
273
  }
171
274
 
172
275
  module.exports = RuntimeGenerator
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/runtime",
3
- "version": "1.13.0",
3
+ "version": "1.13.2",
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.0",
32
- "@platformatic/sql-mapper": "1.13.0"
31
+ "@platformatic/sql-graphql": "1.13.2",
32
+ "@platformatic/sql-mapper": "1.13.2"
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/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"
52
+ "@platformatic/composer": "1.13.2",
53
+ "@platformatic/db": "1.13.2",
54
+ "@platformatic/generators": "1.13.2",
55
+ "@platformatic/config": "1.13.2",
56
+ "@platformatic/service": "1.13.2",
57
+ "@platformatic/telemetry": "1.13.2",
58
+ "@platformatic/utils": "1.13.2"
59
59
  },
60
60
  "standard": {
61
61
  "ignore": [