@platformatic/generators 1.14.3 → 1.15.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.
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { readFile } = require('node:fs/promises')
4
- const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, extractEnvVariablesFromText } = require('./utils')
4
+ const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, extractEnvVariablesFromText, getPackageConfigurationObject } = require('./utils')
5
5
  const { join } = require('node:path')
6
6
  const { FileGenerator } = require('./file-generator')
7
7
  const { generateTests, generatePlugins } = require('./create-plugin')
@@ -273,6 +273,33 @@ class BaseGenerator extends FileGenerator {
273
273
  async generateConfigFile () {
274
274
  const configFileName = 'platformatic.json'
275
275
  const contents = await this._getConfigFileContents()
276
+
277
+ // handle packages
278
+ if (this.packages.length > 0) {
279
+ if (!contents.plugins) {
280
+ contents.plugins = {}
281
+ }
282
+ contents.plugins.packages = this.packages.map((packageDefinition) => {
283
+ const packageConfigOutput = getPackageConfigurationObject(packageDefinition.options, this.config.serviceName)
284
+ if (Object.keys(packageConfigOutput.env).length > 0) {
285
+ const envForPackages = {}
286
+ Object.entries(packageConfigOutput.env).forEach((kv) => {
287
+ envForPackages[kv[0]] = kv[1]
288
+ })
289
+ if (this.config.isRuntimeContext) {
290
+ this.config.env = {
291
+ ...this.config.env,
292
+ ...addPrefixToEnv(envForPackages, this.config.envPrefix)
293
+ }
294
+ }
295
+ }
296
+ return {
297
+ name: packageDefinition.name,
298
+ options: packageConfigOutput.config
299
+ }
300
+ })
301
+ }
302
+
276
303
  this.addFile({
277
304
  path: '',
278
305
  file: configFileName,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/generators",
3
- "version": "1.14.3",
3
+ "version": "1.15.0",
4
4
  "description": "Main classes and utils for generators.",
5
5
  "main": "index.js",
6
6
  "keywords": [],
@@ -404,6 +404,92 @@ test('should add package', async () => {
404
404
  assert.deepEqual(bg.packages[0], packageDefinition)
405
405
  })
406
406
 
407
+ test('support packages', async (t) => {
408
+ {
409
+ const svc = new BaseGenerator({
410
+ module: '@platformatic/service'
411
+ })
412
+ const packageDefinitions = [
413
+ {
414
+ name: '@fastify/compress',
415
+ options: [
416
+ {
417
+ path: 'threshold',
418
+ value: '1',
419
+ type: 'number'
420
+ },
421
+ {
422
+ path: 'foobar',
423
+ value: '123',
424
+ type: 'number',
425
+ name: 'FST_PLUGIN_STATIC_FOOBAR'
426
+ }
427
+ ]
428
+ }
429
+ ]
430
+ svc.setConfig({
431
+ isRuntimeContext: true,
432
+ serviceName: 'my-service'
433
+ })
434
+ svc.addPackage(packageDefinitions[0])
435
+ await svc.prepare()
436
+
437
+ const platformaticConfigFile = svc.getFileObject('platformatic.json')
438
+ const contents = JSON.parse(platformaticConfigFile.contents)
439
+
440
+ assert.deepEqual(contents.plugins, {
441
+ packages: [
442
+ {
443
+ name: '@fastify/compress',
444
+ options: {
445
+ threshold: 1,
446
+ foobar: '{PLT_MY_SERVICE_FST_PLUGIN_STATIC_FOOBAR}'
447
+ }
448
+ }
449
+ ]
450
+ })
451
+
452
+ assert.equal(svc.config.env.PLT_MY_SERVICE_FST_PLUGIN_STATIC_FOOBAR, 123)
453
+ }
454
+ {
455
+ // with standard platformatic plugin
456
+ const svc = new BaseGenerator({
457
+ module: '@platformatic/service'
458
+ })
459
+ svc.setConfig({
460
+ plugin: true
461
+ })
462
+ const packageDefinitions = [
463
+ {
464
+ name: '@fastify/compress',
465
+ options: [
466
+ {
467
+ path: 'threshold',
468
+ value: '1',
469
+ type: 'number'
470
+ }
471
+ ]
472
+ }
473
+ ]
474
+ svc.addPackage(packageDefinitions[0])
475
+ await svc.prepare()
476
+
477
+ const platformaticConfigFile = svc.getFileObject('platformatic.json')
478
+ const contents = JSON.parse(platformaticConfigFile.contents)
479
+
480
+ assert.deepEqual(contents.plugins, {
481
+ packages: [
482
+ {
483
+ name: '@fastify/compress',
484
+ options: {
485
+ threshold: 1
486
+ }
487
+ }
488
+ ]
489
+ })
490
+ }
491
+ })
492
+
407
493
  describe('runtime context', () => {
408
494
  test('should set config.envPrefix correctly', async (t) => {
409
495
  const bg = new BaseGenerator({