@platformatic/generators 2.0.0-alpha.7 → 2.0.0-alpha.8

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,995 +0,0 @@
1
- 'use strict'
2
-
3
- const { readFile, cp } = require('node:fs/promises')
4
- const { test, after, afterEach, describe } = require('node:test')
5
- const assert = require('node:assert')
6
- const { join } = require('node:path')
7
-
8
- const { fakeLogger, getTempDir, moveToTmpdir, mockNpmJsRequestForPkgs, mockAgent } = require('./helpers')
9
- const { BaseGenerator } = require('../lib/base-generator')
10
- const { convertServiceNameToPrefix } = require('../lib/utils')
11
- const { safeRemove } = require('@platformatic/utils')
12
-
13
- afterEach(async () => {
14
- try {
15
- await safeRemove(join(__dirname, 'tmp'))
16
- } catch (err) {
17
- // do nothing
18
- }
19
- })
20
-
21
- test('should write file and dirs', async t => {
22
- const dir = await getTempDir()
23
- const gen = new BaseGenerator({
24
- logger: fakeLogger,
25
- module: '@platformatic/service',
26
- })
27
-
28
- gen.setConfig({
29
- targetDirectory: dir,
30
- serviceName: 'test-service',
31
- })
32
-
33
- await gen.run()
34
- // check files are created
35
- const packageJson = JSON.parse(await readFile(join(dir, 'package.json'), 'utf8'))
36
- assert.ok(packageJson.scripts)
37
- assert.ok(packageJson.dependencies)
38
- assert.equal(packageJson.dependencies.platformatic, undefined)
39
- assert.ok(packageJson.engines)
40
-
41
- assert.equal(packageJson.name, 'test-service')
42
-
43
- const configFile = JSON.parse(await readFile(join(dir, 'platformatic.json'), 'utf8'))
44
- assert.deepEqual(configFile, {})
45
-
46
- const gitignore = await readFile(join(dir, '.gitignore'), 'utf8')
47
- assert.ok(gitignore.length > 0) // file is created and not empty
48
- })
49
-
50
- test('extended class should generate config', async t => {
51
- class ServiceClass extends BaseGenerator {
52
- constructor (opts) {
53
- super({
54
- ...opts,
55
- module: '@platformatic/service',
56
- })
57
- }
58
-
59
- async _getConfigFileContents () {
60
- // Implement when extending this class
61
- return {
62
- foo: 'bar',
63
- }
64
- }
65
- }
66
-
67
- const svc = new ServiceClass({
68
- logger: fakeLogger,
69
- })
70
-
71
- await svc.prepare()
72
-
73
- const configFile = svc.files[1]
74
- assert.deepEqual(configFile, {
75
- path: '',
76
- file: 'platformatic.json',
77
- contents: JSON.stringify({ foo: 'bar' }, null, 2),
78
- options: {},
79
- })
80
- })
81
-
82
- test('setConfig', async t => {
83
- const bg = new BaseGenerator({
84
- module: '@platformatic/service',
85
- })
86
-
87
- // should init the default config
88
- await bg.prepare()
89
-
90
- assert.deepEqual(bg.config, {
91
- port: 3042,
92
- hostname: '0.0.0.0',
93
- plugin: false,
94
- typescript: false,
95
- initGitRepository: false,
96
- env: {},
97
- defaultEnv: {},
98
- dependencies: {},
99
- devDependencies: {},
100
- isRuntimeContext: false,
101
- serviceName: '',
102
- envPrefix: '',
103
- tests: false,
104
- isUpdating: false,
105
- })
106
-
107
- // should not have undefined properties
108
- Object.entries(bg.config).forEach(kv => {
109
- assert.notStrictEqual(undefined, kv[1])
110
- })
111
-
112
- // partial config with defaults
113
- bg.setConfig({
114
- port: 3084,
115
- })
116
-
117
- assert.deepEqual(bg.config, {
118
- port: 3084, // this is the only custom value
119
- hostname: '0.0.0.0',
120
- plugin: false,
121
- typescript: false,
122
- initGitRepository: false,
123
- env: {},
124
- defaultEnv: {},
125
- dependencies: {},
126
- devDependencies: {},
127
- isRuntimeContext: false,
128
- serviceName: '',
129
- envPrefix: '',
130
- tests: false,
131
- isUpdating: false,
132
- })
133
-
134
- // reset config to defaults
135
- bg.setConfig()
136
- assert.deepEqual(bg.config, {
137
- port: 3042,
138
- hostname: '0.0.0.0',
139
- plugin: false,
140
- typescript: false,
141
- initGitRepository: false,
142
- env: {},
143
- defaultEnv: {},
144
- dependencies: {},
145
- devDependencies: {},
146
- isRuntimeContext: false,
147
- serviceName: '',
148
- envPrefix: '',
149
- tests: false,
150
- isUpdating: false,
151
- })
152
-
153
- // update only some fields
154
- bg.setConfig({
155
- hostname: '123.123.123.123',
156
- port: 3000,
157
- })
158
-
159
- bg.setConfig({
160
- port: 1234,
161
- })
162
-
163
- assert.deepEqual(bg.config, {
164
- port: 1234,
165
- hostname: '123.123.123.123',
166
- plugin: false,
167
- typescript: false,
168
- initGitRepository: false,
169
- env: {},
170
- defaultEnv: {},
171
- dependencies: {},
172
- devDependencies: {},
173
- isRuntimeContext: false,
174
- serviceName: '',
175
- envPrefix: '',
176
- tests: false,
177
- isUpdating: false,
178
- })
179
- })
180
-
181
- test('should append env values', async t => {
182
- const bg = new BaseGenerator({
183
- module: '@platformatic/service',
184
- })
185
- // partial config with defaults
186
- bg.setConfig({
187
- env: {
188
- FOO: 'bar',
189
- },
190
- })
191
-
192
- await bg.prepare()
193
- const dotEnvFile = bg.getFileObject('.env')
194
- assert.equal(dotEnvFile.contents.trim(), 'FOO=bar')
195
-
196
- const dotEnvSampleFile = bg.getFileObject('.env.sample')
197
- assert.equal(dotEnvSampleFile.contents.trim(), 'FOO=')
198
- })
199
-
200
- test('should add a default env var to the .env.sample config', async t => {
201
- const bg = new BaseGenerator({
202
- module: '@platformatic/service',
203
- })
204
- // partial config with defaults
205
- bg.setConfig({
206
- env: {
207
- FOO: 'bar',
208
- },
209
- })
210
-
211
- bg.addEnvVars(
212
- {
213
- BAR: 'baz',
214
- },
215
- { overwrite: false, default: true }
216
- )
217
-
218
- await bg.prepare()
219
- const dotEnvFile = bg.getFileObject('.env')
220
- assert.equal(dotEnvFile.contents.trim(), 'FOO=bar\nBAR=baz')
221
-
222
- const dotEnvSampleFile = bg.getFileObject('.env.sample')
223
- assert.equal(dotEnvSampleFile.contents.trim(), 'BAR=baz\nFOO=')
224
- })
225
-
226
- test('should prepare the questions', async t => {
227
- const bg = new BaseGenerator({
228
- module: '@platformatic/service',
229
- })
230
- // partial config with defaults
231
- bg.setConfig({
232
- env: {
233
- FOO: 'bar',
234
- },
235
- })
236
-
237
- await bg.prepareQuestions()
238
- assert.deepStrictEqual(bg.questions, [
239
- {
240
- type: 'input',
241
- name: 'targetDirectory',
242
- message: 'Where would you like to create your project?',
243
- },
244
- {
245
- type: 'list',
246
- name: 'typescript',
247
- message: 'Do you want to use TypeScript?',
248
- default: false,
249
- choices: [
250
- { name: 'yes', value: true },
251
- { name: 'no', value: false },
252
- ],
253
- },
254
- {
255
- type: 'input',
256
- name: 'port',
257
- message: 'What port do you want to use?',
258
- },
259
- ])
260
- })
261
-
262
- test('should prepare the questions with a targetDirectory', async t => {
263
- const bg = new BaseGenerator({
264
- module: '@platformatic/service',
265
- })
266
- // partial config with defaults
267
- bg.setConfig({
268
- targetDirectory: './foo',
269
- env: {
270
- FOO: 'bar',
271
- },
272
- })
273
-
274
- await bg.prepareQuestions()
275
- assert.deepStrictEqual(bg.questions, [
276
- {
277
- type: 'list',
278
- name: 'typescript',
279
- message: 'Do you want to use TypeScript?',
280
- default: false,
281
- choices: [
282
- { name: 'yes', value: true },
283
- { name: 'no', value: false },
284
- ],
285
- },
286
- {
287
- type: 'input',
288
- name: 'port',
289
- message: 'What port do you want to use?',
290
- },
291
- ])
292
- })
293
-
294
- test('should prepare the questions in runtime context', async t => {
295
- const bg = new BaseGenerator({
296
- module: '@platformatic/service',
297
- })
298
- // partial config with defaults
299
- bg.setConfig({
300
- isRuntimeContext: true,
301
- env: {
302
- FOO: 'bar',
303
- },
304
- })
305
-
306
- await bg.prepareQuestions()
307
- assert.deepStrictEqual(bg.questions, [])
308
- })
309
-
310
- test('should return service metadata', async t => {
311
- const bg = new BaseGenerator({
312
- module: '@platformatic/service',
313
- })
314
- // partial config with defaults
315
- bg.setConfig({
316
- targetDirectory: '/foo/bar',
317
- env: {
318
- FOO: 'bar',
319
- },
320
- })
321
-
322
- const metadata = await bg.prepare()
323
- assert.deepEqual(metadata, {
324
- targetDirectory: '/foo/bar',
325
- env: {
326
- FOO: 'bar',
327
- },
328
- })
329
- })
330
-
331
- test('should generate javascript plugin, routes and tests', async t => {
332
- const bg = new BaseGenerator({
333
- module: '@platformatic/service',
334
- })
335
- bg.setConfig({
336
- plugin: true,
337
- tests: true,
338
- })
339
- await bg.prepare()
340
- assert.ok(bg.getFileObject('example.js', 'plugins'))
341
- assert.ok(bg.getFileObject('root.js', 'routes'))
342
-
343
- assert.ok(bg.getFileObject('root.test.js', join('test', 'routes')))
344
- assert.ok(bg.getFileObject('example.test.js', join('test', 'plugins')))
345
- })
346
-
347
- test('should generate tsConfig file and typescript files', async t => {
348
- const bg = new BaseGenerator({
349
- module: '@platformatic/service',
350
- })
351
- bg.setConfig({
352
- typescript: true,
353
- plugin: true,
354
- tests: true,
355
- })
356
- const template = {
357
- compilerOptions: {
358
- module: 'commonjs',
359
- esModuleInterop: true,
360
- target: 'es2020',
361
- sourceMap: true,
362
- pretty: true,
363
- noEmitOnError: true,
364
- incremental: true,
365
- strict: true,
366
- outDir: 'dist',
367
- },
368
- watchOptions: {
369
- watchFile: 'fixedPollingInterval',
370
- watchDirectory: 'fixedPollingInterval',
371
- fallbackPolling: 'dynamicPriority',
372
- synchronousWatchDirectory: true,
373
- excludeDirectories: ['**/node_modules', 'dist'],
374
- },
375
- }
376
- await bg.prepare()
377
- const tsConfigFile = bg.getFileObject('tsconfig.json')
378
- assert.deepEqual(JSON.parse(tsConfigFile.contents), template)
379
-
380
- assert.ok(bg.getFileObject('example.ts', 'plugins'))
381
- assert.ok(bg.getFileObject('root.ts', 'routes'))
382
-
383
- assert.ok(bg.getFileObject('root.test.ts', join('test', 'routes')))
384
- assert.ok(bg.getFileObject('example.test.ts', join('test', 'plugins')))
385
- })
386
-
387
- test('should throw if prepare fails', async t => {
388
- const bg = new BaseGenerator({
389
- module: '@platformatic/service',
390
- })
391
-
392
- bg._beforePrepare = async () => {
393
- throw new Error('beforePrepare error')
394
- }
395
- try {
396
- await bg.prepare()
397
- assert.fail()
398
- } catch (err) {
399
- assert.equal(err.code, 'PLT_GEN_PREPARE_ERROR')
400
- assert.equal(err.message, 'Error while generating the files: beforePrepare error.')
401
- }
402
- })
403
-
404
- test('should throw if there is a missing env variable', async () => {
405
- const bg = new BaseGenerator({
406
- module: '@platformatic/service',
407
- })
408
-
409
- bg._getConfigFileContents = async () => {
410
- return {
411
- FOO: '{FOO}',
412
- BAR: '{BAR}',
413
- }
414
- }
415
-
416
- bg.setConfig({
417
- env: {
418
- FOO: 'foobar',
419
- },
420
- })
421
-
422
- try {
423
- await bg.prepare()
424
- assert.fail()
425
- } catch (err) {
426
- assert.equal(err.code, 'PLT_GEN_MISSING_ENV_VAR')
427
- assert.equal(
428
- err.message,
429
- 'Env variable BAR is defined in config file platformatic.json, but not in config.env object.'
430
- )
431
- }
432
- })
433
-
434
- test('should add package', async () => {
435
- const bg = new BaseGenerator({
436
- module: '@platformatic/service',
437
- })
438
-
439
- const packageDefinition = {
440
- name: '@my/package',
441
- options: [
442
- {
443
- path: 'foobar',
444
- type: 'string',
445
- value: 'foobar',
446
- },
447
- ],
448
- }
449
- await bg.addPackage(packageDefinition)
450
-
451
- assert.equal(bg.packages.length, 1)
452
- assert.deepEqual(bg.packages[0], packageDefinition)
453
- })
454
-
455
- test('support packages', async t => {
456
- {
457
- const svc = new BaseGenerator({
458
- module: '@platformatic/service',
459
- })
460
- const packageDefinitions = [
461
- {
462
- name: '@fastify/compress',
463
- options: [
464
- {
465
- path: 'threshold',
466
- value: '1',
467
- type: 'number',
468
- },
469
- {
470
- path: 'foobar',
471
- value: '123',
472
- type: 'number',
473
- name: 'FST_PLUGIN_STATIC_FOOBAR',
474
- },
475
- ],
476
- },
477
- ]
478
- svc.setConfig({
479
- isRuntimeContext: true,
480
- serviceName: 'my-service',
481
- })
482
- await svc.addPackage(packageDefinitions[0])
483
- await svc.prepare()
484
-
485
- const platformaticConfigFile = svc.getFileObject('platformatic.json')
486
- const contents = JSON.parse(platformaticConfigFile.contents)
487
-
488
- assert.deepEqual(contents.plugins, {
489
- packages: [
490
- {
491
- name: '@fastify/compress',
492
- options: {
493
- threshold: 1,
494
- foobar: '{PLT_MY_SERVICE_FST_PLUGIN_STATIC_FOOBAR}',
495
- },
496
- },
497
- ],
498
- })
499
-
500
- assert.equal(svc.config.env.PLT_MY_SERVICE_FST_PLUGIN_STATIC_FOOBAR, 123)
501
-
502
- const packageJsonFile = svc.getFileObject('package.json')
503
- const packageJson = JSON.parse(packageJsonFile.contents)
504
- assert.equal(packageJson.dependencies['@fastify/compress'], 'latest')
505
- }
506
-
507
- {
508
- // with standard platformatic plugin
509
- const svc = new BaseGenerator({
510
- module: '@platformatic/service',
511
- })
512
- svc.setConfig({
513
- plugin: true,
514
- })
515
- const packageDefinitions = [
516
- {
517
- name: '@fastify/compress',
518
- options: [
519
- {
520
- path: 'threshold',
521
- value: '1',
522
- type: 'number',
523
- },
524
- ],
525
- },
526
- ]
527
- await svc.addPackage(packageDefinitions[0])
528
- await svc.prepare()
529
-
530
- const platformaticConfigFile = svc.getFileObject('platformatic.json')
531
- const contents = JSON.parse(platformaticConfigFile.contents)
532
-
533
- assert.deepEqual(contents.plugins, {
534
- packages: [
535
- {
536
- name: '@fastify/compress',
537
- options: {
538
- threshold: 1,
539
- },
540
- },
541
- ],
542
- })
543
- }
544
-
545
- {
546
- // with relative path type but no name
547
- const svc = new BaseGenerator({
548
- module: '@platformatic/service',
549
- })
550
- svc.setConfig({
551
- isRuntimeContext: true,
552
- plugin: true,
553
- })
554
- const packageDefinitions = [
555
- {
556
- name: '@fastify/static',
557
- options: [
558
- {
559
- path: 'root',
560
- value: 'public',
561
- type: 'path',
562
- },
563
- ],
564
- },
565
- ]
566
- await svc.addPackage(packageDefinitions[0])
567
- await svc.prepare()
568
-
569
- const platformaticConfigFile = svc.getFileObject('platformatic.json')
570
- const contents = JSON.parse(platformaticConfigFile.contents)
571
-
572
- assert.deepEqual(contents.plugins, {
573
- packages: [
574
- {
575
- name: '@fastify/static',
576
- options: {
577
- root: join('{PLT_ROOT}', 'public'),
578
- },
579
- },
580
- ],
581
- })
582
- }
583
-
584
- {
585
- // with relative path type and name
586
- const svc = new BaseGenerator({
587
- module: '@platformatic/service',
588
- })
589
- svc.setConfig({
590
- isRuntimeContext: true,
591
- plugin: true,
592
- serviceName: 'my-service',
593
- })
594
- const packageDefinitions = [
595
- {
596
- name: '@fastify/static',
597
- options: [
598
- {
599
- path: 'root',
600
- value: 'public',
601
- type: 'path',
602
- name: 'FST_PLUGIN_STATIC_ROOT',
603
- },
604
- ],
605
- },
606
- ]
607
- await svc.addPackage(packageDefinitions[0])
608
- await svc.prepare()
609
-
610
- const platformaticConfigFile = svc.getFileObject('platformatic.json')
611
- const contents = JSON.parse(platformaticConfigFile.contents)
612
-
613
- assert.deepEqual(contents.plugins, {
614
- packages: [
615
- {
616
- name: '@fastify/static',
617
- options: {
618
- root: join('{PLT_ROOT}', '{PLT_MY_SERVICE_FST_PLUGIN_STATIC_ROOT}'),
619
- },
620
- },
621
- ],
622
- })
623
- }
624
-
625
- {
626
- mockNpmJsRequestForPkgs(['foobar'])
627
- // should get the version from npm
628
- // mockAgent
629
- // .get('https://registry.npmjs.org')
630
- // .intercept({
631
- // method: 'GET',
632
- // path: '/foobar'
633
- // })
634
- // .reply(200, {
635
- // 'dist-tags': {
636
- // latest: '1.42.0'
637
- // }
638
- // })
639
-
640
- const svc = new BaseGenerator({
641
- module: '@platformatic/service',
642
- })
643
- const packageDefinitions = [
644
- {
645
- name: 'foobar',
646
- options: [],
647
- },
648
- ]
649
- svc.setConfig({
650
- isRuntimeContext: true,
651
- serviceName: 'my-service',
652
- })
653
- await svc.addPackage(packageDefinitions[0])
654
- await svc.prepare()
655
-
656
- const packageJsonFile = svc.getFileObject('package.json')
657
- const packageJson = JSON.parse(packageJsonFile.contents)
658
- assert.equal(packageJson.dependencies.foobar, '1.42.0')
659
- }
660
-
661
- {
662
- // should default to `latest` if getting the version from npm fails
663
- mockAgent
664
- .get('https://registry.npmjs.org')
665
- .intercept({
666
- method: 'GET',
667
- path: '/foobar',
668
- })
669
- .reply(500, {
670
- message: 'Internal Server Error',
671
- })
672
-
673
- const svc = new BaseGenerator({
674
- module: '@platformatic/service',
675
- })
676
- const packageDefinitions = [
677
- {
678
- name: 'foobar',
679
- options: [],
680
- },
681
- ]
682
- svc.setConfig({
683
- isRuntimeContext: true,
684
- serviceName: 'my-service',
685
- })
686
- await svc.addPackage(packageDefinitions[0])
687
- await svc.prepare()
688
-
689
- const packageJsonFile = svc.getFileObject('package.json')
690
- const packageJson = JSON.parse(packageJsonFile.contents)
691
- assert.equal(packageJson.dependencies.foobar, 'latest')
692
- }
693
-
694
- {
695
- // should set latest on timeout
696
- mockAgent
697
- .get('https://registry.npmjs.org')
698
- .intercept({
699
- method: 'GET',
700
- path: '/foobarxxx',
701
- })
702
- .reply(200, {
703
- 'dist-tags': {
704
- latest: '1.42.0',
705
- },
706
- })
707
- .delay(3000)
708
-
709
- const svc = new BaseGenerator({
710
- module: '@platformatic/service',
711
- })
712
- const packageName = 'foobarxxx'
713
- const packageDefinitions = [
714
- {
715
- name: packageName,
716
- options: [],
717
- },
718
- ]
719
- svc.setConfig({
720
- isRuntimeContext: true,
721
- serviceName: 'my-service',
722
- })
723
- await svc.addPackage(packageDefinitions[0])
724
- await svc.prepare()
725
-
726
- const packageJsonFile = svc.getFileObject('package.json')
727
- const packageJson = JSON.parse(packageJsonFile.contents)
728
- assert.equal(packageJson.dependencies[packageName], 'latest')
729
- }
730
-
731
- {
732
- // should default to `latest` if getting the version from npm fails
733
- mockAgent
734
- .get('https://registry.npmjs.org')
735
- .intercept({
736
- method: 'GET',
737
- path: '/foobar',
738
- })
739
- .reply(500, {
740
- message: 'Internal Server Error',
741
- })
742
-
743
- const svc = new BaseGenerator({
744
- module: '@platformatic/service',
745
- })
746
- const packageDefinitions = [
747
- {
748
- name: 'foobar',
749
- options: [],
750
- },
751
- ]
752
- svc.setConfig({
753
- isRuntimeContext: true,
754
- serviceName: 'my-service',
755
- })
756
- await svc.addPackage(packageDefinitions[0])
757
- await svc.prepare()
758
-
759
- const packageJsonFile = svc.getFileObject('package.json')
760
- const packageJson = JSON.parse(packageJsonFile.contents)
761
- assert.equal(packageJson.dependencies.foobar, 'latest')
762
- }
763
- })
764
- test('should load data from directory', async t => {
765
- const runtimeDirectory = join(__dirname, 'fixtures', 'sample-runtime')
766
- const bg = new BaseGenerator({
767
- module: '@platformatic/service',
768
- })
769
- const data = await bg.loadFromDir('rival', runtimeDirectory)
770
- const expected = {
771
- name: 'rival',
772
- template: '@platformatic/service',
773
- fields: [],
774
- plugins: [
775
- {
776
- name: '@fastify/oauth2',
777
- options: [
778
- {
779
- path: 'name',
780
- type: 'string',
781
- value: 'googleOAuth2',
782
- name: 'FST_PLUGIN_OAUTH2_NAME',
783
- },
784
- {
785
- path: 'credentials.client.id',
786
- type: 'string',
787
- value: 'sample_client_id',
788
- name: 'FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID',
789
- },
790
- {
791
- path: 'credentials.client.secret',
792
- type: 'string',
793
- value: 'sample_client_secret',
794
- name: 'FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET',
795
- },
796
- {
797
- path: 'startRedirectPath',
798
- type: 'string',
799
- value: '/login/google',
800
- name: 'FST_PLUGIN_OAUTH2_REDIRECT_PATH',
801
- },
802
- {
803
- path: 'callbackUri',
804
- type: 'string',
805
- value: 'http://localhost:3000/login/google/callback',
806
- name: 'FST_PLUGIN_OAUTH2_CALLBACK_URI',
807
- },
808
- ],
809
- },
810
- ],
811
- }
812
- assert.deepEqual(data, expected)
813
- })
814
-
815
- test('on update should just touch the packages configuration', async t => {
816
- mockNpmJsRequestForPkgs(['@fastify/foo-plugin'])
817
- const runtimeDirectory = join(__dirname, 'fixtures', 'sample-runtime', 'services', 'rival')
818
- const dir = await moveToTmpdir(after)
819
- await cp(runtimeDirectory, dir, { recursive: true })
820
-
821
- const bg = new BaseGenerator({
822
- module: '@platformatic/service',
823
- targetDirectory: dir,
824
- })
825
- bg.setConfig({
826
- isUpdating: true,
827
- })
828
- await bg.addPackage({
829
- name: '@fastify/foo-plugin',
830
- options: [
831
- {
832
- path: 'name',
833
- type: 'string',
834
- value: 'foobar',
835
- name: 'FST_PLUGIN_FOO_FOOBAR',
836
- },
837
- ],
838
- })
839
- await bg.prepare()
840
-
841
- assert.equal(bg.files.length, 1)
842
- assert.equal(bg.files[0].file, 'platformatic.json')
843
- assert.equal(bg.files[0].path, '')
844
-
845
- const configFileContents = JSON.parse(bg.files[0].contents)
846
- assert.deepEqual(configFileContents.plugins.packages, [
847
- {
848
- name: '@fastify/foo-plugin',
849
- options: {
850
- name: '{FST_PLUGIN_FOO_FOOBAR}',
851
- },
852
- },
853
- ])
854
- assert.deepEqual(bg.config.dependencies, {
855
- '@fastify/foo-plugin': '1.42.0',
856
- })
857
- })
858
-
859
- test('on update should just touch the packages configuration', async t => {
860
- mockNpmJsRequestForPkgs(['@fastify/foo-plugin'])
861
- const runtimeDirectory = join(__dirname, 'fixtures', 'sample-runtime', 'services', 'no-plugin')
862
- const dir = await moveToTmpdir(after)
863
- await cp(runtimeDirectory, dir, { recursive: true })
864
-
865
- const bg = new BaseGenerator({
866
- module: '@platformatic/service',
867
- targetDirectory: dir,
868
- })
869
- bg.setConfig({
870
- isUpdating: true,
871
- })
872
- await bg.addPackage({
873
- name: '@fastify/foo-plugin',
874
- options: [
875
- {
876
- path: 'name',
877
- type: 'string',
878
- value: 'foobar',
879
- name: 'FST_PLUGIN_FOO_FOOBAR',
880
- },
881
- ],
882
- })
883
- await bg.prepare()
884
-
885
- assert.equal(bg.files.length, 1)
886
- assert.equal(bg.files[0].file, 'platformatic.json')
887
- assert.equal(bg.files[0].path, '')
888
-
889
- const configFileContents = JSON.parse(bg.files[0].contents)
890
- assert.equal(configFileContents.plugins, undefined)
891
- assert.deepEqual(bg.config.dependencies, {
892
- '@fastify/foo-plugin': '1.42.0',
893
- })
894
- })
895
-
896
- describe('runtime context', () => {
897
- test('should set config.envPrefix correctly', async t => {
898
- const bg = new BaseGenerator({
899
- module: '@platformatic/service',
900
- })
901
-
902
- bg.setConfig({
903
- isRuntimeContext: true,
904
- serviceName: 'sample-service',
905
- })
906
-
907
- assert.equal(bg.config.envPrefix, 'SAMPLE_SERVICE')
908
-
909
- bg.setConfig({
910
- isRuntimeContext: true,
911
- serviceName: 'sample-service',
912
- envPrefix: 'ANOTHER_PREFIX',
913
- env: {
914
- FOO: 'bar',
915
- BAZ: 'baz',
916
- },
917
- })
918
-
919
- assert.equal(bg.config.envPrefix, 'ANOTHER_PREFIX')
920
- assert.deepEqual(bg.config.env, {
921
- PLT_ANOTHER_PREFIX_FOO: 'bar',
922
- PLT_ANOTHER_PREFIX_BAZ: 'baz',
923
- })
924
- })
925
-
926
- test('should generate correct env file from config.env', async t => {
927
- const bg = new BaseGenerator({
928
- module: '@platformatic/service',
929
- })
930
-
931
- bg.setConfig({
932
- isRuntimeContext: true,
933
- serviceName: 'sample-service',
934
- envPrefix: 'ANOTHER_PREFIX',
935
- env: {
936
- FOO: 'bar',
937
- BAZ: 'baz',
938
- },
939
- })
940
-
941
- const meta = await bg.prepare()
942
-
943
- assert.deepEqual(meta.env, {
944
- PLT_ANOTHER_PREFIX_FOO: 'bar',
945
- PLT_ANOTHER_PREFIX_BAZ: 'baz',
946
- })
947
- })
948
-
949
- test('should return service metadata', async t => {
950
- const bg = new BaseGenerator({
951
- module: '@platformatic/service',
952
- })
953
- // partial config with defaults
954
- bg.setConfig({
955
- targetDirectory: '/foo/bar',
956
- isRuntimeContext: true,
957
- serviceName: 'my-service',
958
- env: {
959
- FOO: 'bar',
960
- },
961
- })
962
-
963
- const metadata = await bg.prepare()
964
- assert.deepEqual(metadata, {
965
- targetDirectory: '/foo/bar',
966
- env: {
967
- PLT_MY_SERVICE_FOO: 'bar',
968
- },
969
- })
970
- })
971
-
972
- test('should generate service name if not provided', async () => {
973
- const bg = new BaseGenerator({
974
- module: '@platformatic/service',
975
- })
976
- bg.setConfig({
977
- targetDirectory: '/foo/bar',
978
- isRuntimeContext: true,
979
- env: {
980
- FOO: 'bar',
981
- },
982
- })
983
-
984
- const metadata = await bg.prepare()
985
-
986
- assert.equal(bg.config.envPrefix, convertServiceNameToPrefix(bg.config.serviceName))
987
- const envPrefix = bg.config.envPrefix
988
- assert.deepEqual(metadata, {
989
- targetDirectory: '/foo/bar',
990
- env: {
991
- [`PLT_${envPrefix}_FOO`]: 'bar',
992
- },
993
- })
994
- })
995
- })