@platformatic/runtime 3.0.0-alpha.4 → 3.0.0-alpha.6
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/config.d.ts +10 -7
- package/eslint.config.js +2 -4
- package/index.d.ts +11 -11
- package/index.js +35 -46
- package/lib/config.js +80 -102
- package/lib/dependencies.js +27 -29
- package/lib/errors.js +65 -99
- package/lib/generator.js +160 -164
- package/lib/logger.js +6 -8
- package/lib/management-api.js +36 -39
- package/lib/prom-server.js +10 -14
- package/lib/runtime.js +752 -715
- package/lib/scheduler.js +13 -15
- package/lib/schema.js +11 -8
- package/lib/shared-http-cache.js +5 -9
- package/lib/upgrade.js +5 -9
- package/lib/utils.js +6 -14
- package/lib/version.js +7 -0
- package/lib/versions/v1.36.0.js +2 -4
- package/lib/versions/v1.5.0.js +2 -4
- package/lib/versions/v2.0.0.js +3 -5
- package/lib/versions/v3.0.0.js +16 -0
- package/lib/worker/{app.js → controller.js} +46 -56
- package/lib/worker/http-cache.js +11 -14
- package/lib/worker/interceptors.js +14 -18
- package/lib/worker/itc.js +74 -74
- package/lib/worker/main.js +45 -49
- package/lib/worker/messaging.js +23 -27
- package/lib/worker/round-robin-map.js +23 -19
- package/lib/worker/shared-context.js +2 -6
- package/lib/worker/symbols.js +12 -29
- package/package.json +21 -21
- package/schema.json +254 -20
package/lib/generator.js
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const { BaseGenerator } = require('@platformatic/generators')
|
|
5
|
-
const { existsSync } = require('node:fs')
|
|
6
|
-
const { join, basename } = require('node:path')
|
|
7
|
-
const { envObjectToString } = require('@platformatic/generators/lib/utils')
|
|
8
|
-
const { readFile, readdir, stat } = require('node:fs/promises')
|
|
9
|
-
const { transform } = require('./config')
|
|
10
|
-
const { getServiceTemplateFromSchemaUrl } = require('@platformatic/generators/lib/utils')
|
|
11
|
-
const { DotEnvTool } = require('dotenv-tool')
|
|
12
|
-
const { getArrayDifference } = require('./utils')
|
|
13
|
-
const { schema } = require('./schema')
|
|
14
|
-
const { pathToFileURL } = require('node:url')
|
|
15
|
-
const {
|
|
16
|
-
safeRemove,
|
|
17
|
-
generateDashedName,
|
|
1
|
+
import createError from '@fastify/error'
|
|
2
|
+
import {
|
|
3
|
+
defaultPackageManager,
|
|
18
4
|
findConfigurationFile,
|
|
5
|
+
generateDashedName,
|
|
6
|
+
kMetadata,
|
|
19
7
|
loadConfiguration,
|
|
20
8
|
loadConfigurationFile,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
9
|
+
safeRemove
|
|
10
|
+
} from '@platformatic/foundation'
|
|
11
|
+
import { BaseGenerator, envObjectToString, getApplicationTemplateFromSchemaUrl } from '@platformatic/generators'
|
|
12
|
+
import { existsSync } from 'node:fs'
|
|
13
|
+
import { readFile, readdir, stat } from 'node:fs/promises'
|
|
14
|
+
import { createRequire } from 'node:module'
|
|
15
|
+
import { basename, join } from 'node:path'
|
|
16
|
+
import { pathToFileURL } from 'node:url'
|
|
17
|
+
import { transform } from './config.js'
|
|
18
|
+
import { schema } from './schema.js'
|
|
19
|
+
import { getArrayDifference } from './utils.js'
|
|
25
20
|
|
|
26
21
|
const wrappableProperties = {
|
|
27
22
|
logger: {
|
|
@@ -38,11 +33,11 @@ const engines = {
|
|
|
38
33
|
node: '>=22.18.0'
|
|
39
34
|
}
|
|
40
35
|
|
|
41
|
-
const ERROR_PREFIX = 'PLT_RUNTIME_GEN'
|
|
36
|
+
export const ERROR_PREFIX = 'PLT_RUNTIME_GEN'
|
|
42
37
|
|
|
43
|
-
const
|
|
44
|
-
`${ERROR_PREFIX}
|
|
45
|
-
"No
|
|
38
|
+
const NoApplicationNamedError = createError(
|
|
39
|
+
`${ERROR_PREFIX}_NO_APPLICATION_FOUND`,
|
|
40
|
+
"No application named '%s' has been added to this runtime."
|
|
46
41
|
)
|
|
47
42
|
const NoEntryPointError = createError(`${ERROR_PREFIX}_NO_ENTRYPOINT`, 'No entrypoint had been defined.')
|
|
48
43
|
|
|
@@ -55,46 +50,53 @@ function getRuntimeBaseEnvVars (config) {
|
|
|
55
50
|
}
|
|
56
51
|
}
|
|
57
52
|
|
|
58
|
-
|
|
53
|
+
// This is needed as dotenv-tool is not loading with ESM currently
|
|
54
|
+
export function createDotenvTool (...args) {
|
|
55
|
+
const { DotEnvTool } = createRequire(import.meta.url)('dotenv-tool')
|
|
56
|
+
return new DotEnvTool(...args)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export class RuntimeGenerator extends BaseGenerator {
|
|
59
60
|
constructor (opts) {
|
|
60
61
|
super({
|
|
61
62
|
...opts,
|
|
62
63
|
module: '@platformatic/runtime'
|
|
63
64
|
})
|
|
64
65
|
this.runtimeName = opts.name
|
|
65
|
-
this.
|
|
66
|
-
this.
|
|
67
|
-
this.
|
|
66
|
+
this.applicationsFolder = opts.applicationsFolder ?? 'applications'
|
|
67
|
+
this.applications = []
|
|
68
|
+
this.existingApplications = []
|
|
68
69
|
this.entryPoint = null
|
|
69
70
|
this.packageManager = opts.packageManager ?? defaultPackageManager
|
|
70
71
|
}
|
|
71
72
|
|
|
72
|
-
async
|
|
73
|
-
// ensure
|
|
74
|
-
const originalConfig =
|
|
75
|
-
const
|
|
73
|
+
async addApplication (application, name) {
|
|
74
|
+
// ensure application config is correct
|
|
75
|
+
const originalConfig = application.config
|
|
76
|
+
const applicationName = name || generateDashedName()
|
|
76
77
|
const newConfig = {
|
|
77
78
|
...originalConfig,
|
|
78
79
|
isRuntimeContext: true,
|
|
79
|
-
|
|
80
|
+
applicationName
|
|
80
81
|
}
|
|
81
|
-
// reset all files previously generated by the
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
this.
|
|
85
|
-
name:
|
|
86
|
-
|
|
82
|
+
// reset all files previously generated by the application
|
|
83
|
+
application.reset()
|
|
84
|
+
application.setConfig(newConfig)
|
|
85
|
+
this.applications.push({
|
|
86
|
+
name: applicationName,
|
|
87
|
+
application
|
|
87
88
|
})
|
|
88
89
|
|
|
89
|
-
|
|
90
|
+
application.setRuntime(this)
|
|
90
91
|
}
|
|
91
92
|
|
|
92
93
|
setEntryPoint (entryPoint) {
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
94
|
+
const application =
|
|
95
|
+
this.existingApplications.includes(entryPoint) || this.applications.find(svc => svc.name === entryPoint)
|
|
96
|
+
if (!application) {
|
|
97
|
+
throw new NoApplicationNamedError(entryPoint)
|
|
96
98
|
}
|
|
97
|
-
this.entryPoint =
|
|
99
|
+
this.entryPoint = application
|
|
98
100
|
}
|
|
99
101
|
|
|
100
102
|
async generatePackageJson () {
|
|
@@ -106,8 +108,7 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
106
108
|
start: this.config.startCommand ?? 'platformatic start'
|
|
107
109
|
},
|
|
108
110
|
devDependencies: {
|
|
109
|
-
fastify: `^${this.fastifyVersion}
|
|
110
|
-
borp: `${this.pkgData.devDependencies.borp}`
|
|
111
|
+
fastify: `^${this.fastifyVersion}`
|
|
111
112
|
},
|
|
112
113
|
dependencies: {
|
|
113
114
|
'@platformatic/runtime': `^${this.platformaticVersion}`,
|
|
@@ -119,24 +120,24 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
if (this.packageManager === 'npm' || this.packageManager === 'yarn') {
|
|
122
|
-
template.workspaces = [this.
|
|
123
|
+
template.workspaces = [this.applicationsFolder + '/*']
|
|
123
124
|
}
|
|
124
125
|
|
|
125
126
|
return template
|
|
126
127
|
}
|
|
127
128
|
|
|
128
129
|
async _beforePrepare () {
|
|
129
|
-
this.
|
|
130
|
-
this.
|
|
131
|
-
this.
|
|
130
|
+
this.setApplicationsDirectory()
|
|
131
|
+
this.setApplicationsConfigValues()
|
|
132
|
+
this.addApplicationsDependencies()
|
|
132
133
|
|
|
133
134
|
this.addEnvVars(getRuntimeBaseEnvVars(this.config), { overwrite: false, default: true })
|
|
134
135
|
}
|
|
135
136
|
|
|
136
|
-
|
|
137
|
-
this.
|
|
138
|
-
if (
|
|
139
|
-
Object.entries(
|
|
137
|
+
addApplicationsDependencies () {
|
|
138
|
+
this.applications.forEach(({ application }) => {
|
|
139
|
+
if (application.config.dependencies) {
|
|
140
|
+
Object.entries(application.config.dependencies).forEach(kv => {
|
|
140
141
|
this.config.dependencies[kv[0]] = kv[1]
|
|
141
142
|
})
|
|
142
143
|
}
|
|
@@ -159,8 +160,8 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
159
160
|
const { PLT_ROOT, ...existingEnvironment } = this.existingConfig[kMetadata].env
|
|
160
161
|
this.config.env = existingEnvironment
|
|
161
162
|
this.config.port = this.config.env.PORT
|
|
162
|
-
this.entryPoint = this.existingConfig.
|
|
163
|
-
this.
|
|
163
|
+
this.entryPoint = this.existingConfig.applications.find(svc => svc.entrypoint)
|
|
164
|
+
this.existingApplications = this.existingConfig.applications.map(s => s.id)
|
|
164
165
|
|
|
165
166
|
this.updateRuntimeConfig(this.existingConfigRaw)
|
|
166
167
|
this.updateRuntimeEnv(await readFile(join(this.targetDirectory, '.env'), 'utf-8'))
|
|
@@ -170,8 +171,8 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
170
171
|
async prepare () {
|
|
171
172
|
await this.populateFromExistingConfig()
|
|
172
173
|
if (this.existingConfig) {
|
|
173
|
-
this.
|
|
174
|
-
this.
|
|
174
|
+
this.setApplicationsDirectory()
|
|
175
|
+
this.setApplicationsConfigValues()
|
|
175
176
|
await this._afterPrepare()
|
|
176
177
|
return {
|
|
177
178
|
env: this.config.env,
|
|
@@ -182,11 +183,11 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
182
183
|
}
|
|
183
184
|
}
|
|
184
185
|
|
|
185
|
-
|
|
186
|
-
this.
|
|
187
|
-
if (!
|
|
186
|
+
setApplicationsConfigValues () {
|
|
187
|
+
this.applications.forEach(({ application }) => {
|
|
188
|
+
if (!application.config) {
|
|
188
189
|
// set default config
|
|
189
|
-
|
|
190
|
+
application.setConfig()
|
|
190
191
|
}
|
|
191
192
|
})
|
|
192
193
|
}
|
|
@@ -197,7 +198,7 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
197
198
|
entrypoint: this.entryPoint.name,
|
|
198
199
|
watch: true,
|
|
199
200
|
autoload: {
|
|
200
|
-
path: this.config.autoload || this.
|
|
201
|
+
path: this.config.autoload || this.applicationsFolder,
|
|
201
202
|
exclude: ['docs']
|
|
202
203
|
},
|
|
203
204
|
...wrappableProperties
|
|
@@ -210,11 +211,11 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
210
211
|
if (!this.entryPoint) {
|
|
211
212
|
throw new NoEntryPointError()
|
|
212
213
|
}
|
|
213
|
-
const
|
|
214
|
+
const applicationsEnv = await this.prepareApplicationFiles()
|
|
214
215
|
this.addEnvVars({
|
|
215
216
|
...this.config.env,
|
|
216
217
|
...this.getRuntimeEnv(),
|
|
217
|
-
...
|
|
218
|
+
...applicationsEnv
|
|
218
219
|
})
|
|
219
220
|
|
|
220
221
|
this.updateRuntimeEnv(envObjectToString(this.config.env))
|
|
@@ -227,25 +228,25 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
227
228
|
|
|
228
229
|
return {
|
|
229
230
|
targetDirectory: this.targetDirectory,
|
|
230
|
-
env:
|
|
231
|
+
env: applicationsEnv
|
|
231
232
|
}
|
|
232
233
|
}
|
|
233
234
|
|
|
234
235
|
async writeFiles () {
|
|
235
|
-
for (const {
|
|
236
|
-
await
|
|
236
|
+
for (const { application } of this.applications) {
|
|
237
|
+
await application._beforeWriteFiles?.(this)
|
|
237
238
|
}
|
|
238
239
|
|
|
239
240
|
await super.writeFiles()
|
|
240
241
|
|
|
241
242
|
if (!this.config.isUpdating) {
|
|
242
|
-
for (const {
|
|
243
|
-
await
|
|
243
|
+
for (const { application } of this.applications) {
|
|
244
|
+
await application.writeFiles()
|
|
244
245
|
}
|
|
245
246
|
}
|
|
246
247
|
|
|
247
|
-
for (const {
|
|
248
|
-
await
|
|
248
|
+
for (const { application } of this.applications) {
|
|
249
|
+
await application._afterWriteFiles?.(this)
|
|
249
250
|
}
|
|
250
251
|
}
|
|
251
252
|
|
|
@@ -265,43 +266,43 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
265
266
|
})
|
|
266
267
|
}
|
|
267
268
|
|
|
268
|
-
|
|
269
|
-
this.
|
|
270
|
-
if (!
|
|
269
|
+
setApplicationsDirectory () {
|
|
270
|
+
this.applications.forEach(({ application }) => {
|
|
271
|
+
if (!application.config) {
|
|
271
272
|
// set default config
|
|
272
|
-
|
|
273
|
+
application.setConfig()
|
|
273
274
|
}
|
|
274
275
|
let basePath
|
|
275
276
|
if (this.existingConfig) {
|
|
276
277
|
basePath = this.existingConfig.autoload.path
|
|
277
278
|
} else {
|
|
278
|
-
basePath = join(this.targetDirectory, this.config.autoload || this.
|
|
279
|
+
basePath = join(this.targetDirectory, this.config.autoload || this.applicationsFolder)
|
|
279
280
|
}
|
|
280
|
-
this.
|
|
281
|
-
|
|
281
|
+
this.applicationsBasePath = basePath
|
|
282
|
+
application.setTargetDirectory(join(basePath, application.config.applicationName))
|
|
282
283
|
})
|
|
283
284
|
}
|
|
284
285
|
|
|
285
|
-
|
|
286
|
-
this.
|
|
287
|
-
const originalConfig =
|
|
288
|
-
|
|
286
|
+
setApplicationsConfig (configToOverride) {
|
|
287
|
+
this.applications.forEach(application => {
|
|
288
|
+
const originalConfig = application.config
|
|
289
|
+
application.setConfig({
|
|
289
290
|
...originalConfig,
|
|
290
291
|
...configToOverride
|
|
291
292
|
})
|
|
292
293
|
})
|
|
293
294
|
}
|
|
294
295
|
|
|
295
|
-
async
|
|
296
|
-
let
|
|
297
|
-
for (const svc of this.
|
|
298
|
-
const svcEnv = await svc.
|
|
299
|
-
|
|
300
|
-
...
|
|
296
|
+
async prepareApplicationFiles () {
|
|
297
|
+
let applicationsEnv = {}
|
|
298
|
+
for (const svc of this.applications) {
|
|
299
|
+
const svcEnv = await svc.application.prepare()
|
|
300
|
+
applicationsEnv = {
|
|
301
|
+
...applicationsEnv,
|
|
301
302
|
...svcEnv.env
|
|
302
303
|
}
|
|
303
304
|
}
|
|
304
|
-
return
|
|
305
|
+
return applicationsEnv
|
|
305
306
|
}
|
|
306
307
|
|
|
307
308
|
getConfigFieldsDefinitions () {
|
|
@@ -319,8 +320,8 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
319
320
|
}
|
|
320
321
|
|
|
321
322
|
async postInstallActions () {
|
|
322
|
-
for (const {
|
|
323
|
-
await
|
|
323
|
+
for (const { application } of this.applications) {
|
|
324
|
+
await application.postInstallActions()
|
|
324
325
|
}
|
|
325
326
|
}
|
|
326
327
|
|
|
@@ -332,122 +333,122 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
332
333
|
|
|
333
334
|
async loadFromDir () {
|
|
334
335
|
const output = {
|
|
335
|
-
|
|
336
|
+
applications: []
|
|
336
337
|
}
|
|
337
338
|
const runtimePkgConfigFileData = JSON.parse(await readFile(join(this.targetDirectory, this.runtimeConfig), 'utf-8'))
|
|
338
|
-
const
|
|
339
|
+
const applicationsPath = join(this.targetDirectory, runtimePkgConfigFileData.autoload.path)
|
|
339
340
|
|
|
340
|
-
// load all
|
|
341
|
-
const
|
|
342
|
-
for (const s of
|
|
341
|
+
// load all applications
|
|
342
|
+
const allApplications = await readdir(applicationsPath)
|
|
343
|
+
for (const s of allApplications) {
|
|
343
344
|
// check is a directory
|
|
344
|
-
const
|
|
345
|
-
const dirStat = await stat(
|
|
345
|
+
const currentApplicationPath = join(applicationsPath, s)
|
|
346
|
+
const dirStat = await stat(currentApplicationPath)
|
|
346
347
|
if (dirStat.isDirectory()) {
|
|
347
|
-
// load the
|
|
348
|
-
const configFile = await findConfigurationFile(
|
|
349
|
-
const
|
|
348
|
+
// load the application config
|
|
349
|
+
const configFile = await findConfigurationFile(currentApplicationPath)
|
|
350
|
+
const applicationPltJson = JSON.parse(await readFile(join(currentApplicationPath, configFile), 'utf-8'))
|
|
350
351
|
// get module to load
|
|
351
|
-
const template =
|
|
352
|
-
const Generator = await this._getGeneratorForTemplate(
|
|
352
|
+
const template = applicationPltJson.module || getApplicationTemplateFromSchemaUrl(applicationPltJson.$schema)
|
|
353
|
+
const Generator = await this._getGeneratorForTemplate(currentApplicationPath, template)
|
|
353
354
|
const instance = new Generator({
|
|
354
355
|
logger: this.logger
|
|
355
356
|
})
|
|
356
|
-
this.
|
|
357
|
-
output.
|
|
357
|
+
this.addApplication(instance, s)
|
|
358
|
+
output.applications.push(await instance.loadFromDir(s, this.targetDirectory))
|
|
358
359
|
}
|
|
359
360
|
}
|
|
360
361
|
return output
|
|
361
362
|
}
|
|
362
363
|
|
|
363
364
|
async update (newConfig) {
|
|
364
|
-
let
|
|
365
|
+
let allApplicationsDependencies = {}
|
|
365
366
|
const runtimeAddedEnvKeys = []
|
|
366
367
|
|
|
367
368
|
this.config.isUpdating = true
|
|
368
369
|
const currrentPackageJson = JSON.parse(await readFile(join(this.targetDirectory, 'package.json'), 'utf-8'))
|
|
369
370
|
const currentRuntimeDependencies = currrentPackageJson.dependencies
|
|
370
|
-
// check all
|
|
371
|
-
const
|
|
372
|
-
const
|
|
371
|
+
// check all applications are present with the same template
|
|
372
|
+
const allCurrentApplicationsNames = this.applications.map(s => s.name)
|
|
373
|
+
const allNewApplicationsNames = newConfig.applications.map(s => s.name)
|
|
373
374
|
// load dotenv tool
|
|
374
|
-
const envTool =
|
|
375
|
+
const envTool = createDotenvTool({
|
|
375
376
|
path: join(this.targetDirectory, '.env')
|
|
376
377
|
})
|
|
377
378
|
|
|
378
379
|
await envTool.load()
|
|
379
380
|
|
|
380
|
-
const
|
|
381
|
-
if (
|
|
382
|
-
for (const
|
|
383
|
-
// handle
|
|
381
|
+
const removedApplications = getArrayDifference(allCurrentApplicationsNames, allNewApplicationsNames)
|
|
382
|
+
if (removedApplications.length > 0) {
|
|
383
|
+
for (const removedApplication of removedApplications) {
|
|
384
|
+
// handle application delete
|
|
384
385
|
|
|
385
386
|
// delete env variables
|
|
386
|
-
const s = this.
|
|
387
|
+
const s = this.applications.find(f => f.name === removedApplication)
|
|
387
388
|
const allKeys = envTool.getKeys()
|
|
388
389
|
allKeys.forEach(k => {
|
|
389
|
-
if (k.startsWith(`PLT_${s.
|
|
390
|
+
if (k.startsWith(`PLT_${s.application.config.envPrefix}`)) {
|
|
390
391
|
envTool.deleteKey(k)
|
|
391
392
|
}
|
|
392
393
|
})
|
|
393
394
|
|
|
394
395
|
// delete dependencies
|
|
395
|
-
const
|
|
396
|
-
const configFile = await findConfigurationFile(
|
|
397
|
-
const
|
|
398
|
-
if (
|
|
399
|
-
|
|
396
|
+
const applicationPath = join(this.targetDirectory, this.applicationsFolder, s.name)
|
|
397
|
+
const configFile = await findConfigurationFile(applicationPath)
|
|
398
|
+
const applicationPackageJson = JSON.parse(await readFile(join(applicationPath, configFile), 'utf-8'))
|
|
399
|
+
if (applicationPackageJson.plugins && applicationPackageJson.plugins.packages) {
|
|
400
|
+
applicationPackageJson.plugins.packages.forEach(p => {
|
|
400
401
|
delete currrentPackageJson.dependencies[p.name]
|
|
401
402
|
})
|
|
402
403
|
}
|
|
403
404
|
// delete directory
|
|
404
|
-
await safeRemove(join(this.targetDirectory, this.
|
|
405
|
+
await safeRemove(join(this.targetDirectory, this.applicationsFolder, s.name))
|
|
405
406
|
}
|
|
406
|
-
// throw new
|
|
407
|
+
// throw new CannotRemoveApplicationOnUpdateError(removedApplications.join(', '))
|
|
407
408
|
}
|
|
408
409
|
|
|
409
|
-
// handle new
|
|
410
|
-
for (const
|
|
411
|
-
// create generator for the
|
|
412
|
-
const
|
|
410
|
+
// handle new applications
|
|
411
|
+
for (const newApplication of newConfig.applications) {
|
|
412
|
+
// create generator for the application
|
|
413
|
+
const ApplicationGenerator = await this._getGeneratorForTemplate(
|
|
413
414
|
join(this.targetDirectory, 'package.json'),
|
|
414
|
-
|
|
415
|
+
newApplication.template
|
|
415
416
|
)
|
|
416
|
-
const
|
|
417
|
+
const applicationInstance = new ApplicationGenerator({
|
|
417
418
|
logger: this.logger
|
|
418
419
|
})
|
|
419
420
|
const baseConfig = {
|
|
420
421
|
isRuntimeContext: true,
|
|
421
|
-
targetDirectory: join(this.targetDirectory, this.
|
|
422
|
-
|
|
422
|
+
targetDirectory: join(this.targetDirectory, this.applicationsFolder, newApplication.name),
|
|
423
|
+
applicationName: newApplication.name,
|
|
423
424
|
plugin: true
|
|
424
425
|
}
|
|
425
|
-
if (
|
|
426
|
-
// update existing
|
|
427
|
-
// otherwise, is a new
|
|
426
|
+
if (allCurrentApplicationsNames.includes(newApplication.name)) {
|
|
427
|
+
// update existing applications env values
|
|
428
|
+
// otherwise, is a new application
|
|
428
429
|
baseConfig.isUpdating = true
|
|
429
430
|
|
|
430
|
-
// handle
|
|
431
|
-
const
|
|
432
|
-
const
|
|
433
|
-
const
|
|
434
|
-
const pluginsToRemove = getArrayDifference(
|
|
431
|
+
// handle application's plugin differences
|
|
432
|
+
const oldApplicationMetadata = await applicationInstance.loadFromDir(newApplication.name, this.targetDirectory)
|
|
433
|
+
const oldApplicationPackages = oldApplicationMetadata.plugins.map(meta => meta.name)
|
|
434
|
+
const newApplicationPackages = newApplication.plugins.map(meta => meta.name)
|
|
435
|
+
const pluginsToRemove = getArrayDifference(oldApplicationPackages, newApplicationPackages)
|
|
435
436
|
pluginsToRemove.forEach(p => delete currentRuntimeDependencies[p])
|
|
436
437
|
} else {
|
|
437
|
-
// add
|
|
438
|
-
this.
|
|
439
|
-
name:
|
|
440
|
-
|
|
438
|
+
// add application to the generator
|
|
439
|
+
this.applications.push({
|
|
440
|
+
name: newApplication.name,
|
|
441
|
+
application: applicationInstance
|
|
441
442
|
})
|
|
442
443
|
}
|
|
443
|
-
|
|
444
|
-
|
|
444
|
+
applicationInstance.setConfig(baseConfig)
|
|
445
|
+
applicationInstance.setConfigFields(newApplication.fields)
|
|
445
446
|
|
|
446
|
-
const
|
|
447
|
-
for (const plug of
|
|
448
|
-
await
|
|
447
|
+
const applicationEnvPrefix = `PLT_${applicationInstance.config.envPrefix}`
|
|
448
|
+
for (const plug of newApplication.plugins) {
|
|
449
|
+
await applicationInstance.addPackage(plug)
|
|
449
450
|
for (const opt of plug.options) {
|
|
450
|
-
const key = `${
|
|
451
|
+
const key = `${applicationEnvPrefix}_${opt.name}`
|
|
451
452
|
runtimeAddedEnvKeys.push(key)
|
|
452
453
|
const value = opt.value
|
|
453
454
|
if (envTool.hasKey(key)) {
|
|
@@ -457,18 +458,18 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
457
458
|
}
|
|
458
459
|
}
|
|
459
460
|
}
|
|
460
|
-
|
|
461
|
-
const afterPrepareMetadata = await
|
|
462
|
-
await
|
|
463
|
-
// cleanup runtime env removing keys not present anymore in
|
|
461
|
+
allApplicationsDependencies = { ...allApplicationsDependencies, ...applicationInstance.config.dependencies }
|
|
462
|
+
const afterPrepareMetadata = await applicationInstance.prepare()
|
|
463
|
+
await applicationInstance.writeFiles()
|
|
464
|
+
// cleanup runtime env removing keys not present anymore in application plugins
|
|
464
465
|
const allKeys = envTool.getKeys()
|
|
465
466
|
allKeys.forEach(k => {
|
|
466
|
-
if (k.startsWith(`${
|
|
467
|
+
if (k.startsWith(`${applicationEnvPrefix}_FST_PLUGIN`) && !runtimeAddedEnvKeys.includes(k)) {
|
|
467
468
|
envTool.deleteKey(k)
|
|
468
469
|
}
|
|
469
470
|
})
|
|
470
471
|
|
|
471
|
-
// add
|
|
472
|
+
// add application env variables to runtime env
|
|
472
473
|
Object.entries(afterPrepareMetadata.env).forEach(([key, value]) => {
|
|
473
474
|
envTool.addKey(key, value)
|
|
474
475
|
})
|
|
@@ -476,7 +477,7 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
476
477
|
// update runtime package.json dependencies
|
|
477
478
|
currrentPackageJson.dependencies = {
|
|
478
479
|
...currrentPackageJson.dependencies,
|
|
479
|
-
...
|
|
480
|
+
...allApplicationsDependencies
|
|
480
481
|
}
|
|
481
482
|
this.addFile({
|
|
482
483
|
path: '',
|
|
@@ -549,7 +550,7 @@ class RuntimeGenerator extends BaseGenerator {
|
|
|
549
550
|
}
|
|
550
551
|
}
|
|
551
552
|
|
|
552
|
-
class WrappedGenerator extends BaseGenerator {
|
|
553
|
+
export class WrappedGenerator extends BaseGenerator {
|
|
553
554
|
async prepare () {
|
|
554
555
|
await this.getPlatformaticVersion()
|
|
555
556
|
await this.#updateEnvironment()
|
|
@@ -637,8 +638,3 @@ class WrappedGenerator extends BaseGenerator {
|
|
|
637
638
|
return contents + suffix
|
|
638
639
|
}
|
|
639
640
|
}
|
|
640
|
-
|
|
641
|
-
module.exports = {
|
|
642
|
-
RuntimeGenerator,
|
|
643
|
-
WrappedGenerator
|
|
644
|
-
}
|
package/lib/logger.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import { buildPinoFormatters, buildPinoTimestamp } from '@platformatic/foundation'
|
|
2
|
+
import { isatty } from 'node:tty'
|
|
3
|
+
import pino from 'pino'
|
|
4
|
+
import pretty from 'pino-pretty'
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
const pino = require('pino')
|
|
5
|
-
const pretty = require('pino-pretty')
|
|
6
|
-
const { abstractLogger, buildPinoFormatters, buildPinoTimestamp } = require('@platformatic/foundation')
|
|
6
|
+
export { abstractLogger } from '@platformatic/foundation'
|
|
7
7
|
|
|
8
8
|
const customPrettifiers = {
|
|
9
9
|
name (name, _, obj) {
|
|
@@ -17,7 +17,7 @@ const customPrettifiers = {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
// Create the runtime logger
|
|
20
|
-
async function createLogger (config) {
|
|
20
|
+
export async function createLogger (config) {
|
|
21
21
|
const loggerConfig = { ...config.logger, transport: undefined }
|
|
22
22
|
if (config.logger.base === null) {
|
|
23
23
|
loggerConfig.base = undefined
|
|
@@ -55,5 +55,3 @@ async function createLogger (config) {
|
|
|
55
55
|
|
|
56
56
|
return [pino(loggerConfig, multiStream), multiStream]
|
|
57
57
|
}
|
|
58
|
-
|
|
59
|
-
module.exports = { abstractLogger, createLogger }
|