@platformatic/generators 1.14.4 → 1.15.1
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/lib/base-generator.d.ts +21 -21
- package/lib/base-generator.js +34 -3
- package/lib/utils.d.ts +2 -1
- package/lib/utils.js +16 -2
- package/package.json +1 -1
- package/test/base-generator.test.js +165 -0
package/lib/base-generator.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export namespace BaseGenerator {
|
|
|
6
6
|
module: string
|
|
7
7
|
inquirer?: object
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
|
|
10
10
|
export type Env = {
|
|
11
11
|
[key: string]: string | number | boolean
|
|
12
12
|
}
|
|
@@ -14,13 +14,13 @@ export namespace BaseGenerator {
|
|
|
14
14
|
[key: string]: string | number | undefined | null | boolean | object
|
|
15
15
|
}
|
|
16
16
|
type JSONValue =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
17
|
+
| string
|
|
18
|
+
| number
|
|
19
|
+
| boolean
|
|
20
|
+
| { [x: string]: JSONValue }
|
|
21
|
+
| object
|
|
22
|
+
| Array<JSONValue>
|
|
23
|
+
|
|
24
24
|
type Dependency = {
|
|
25
25
|
[key: string]: string
|
|
26
26
|
}
|
|
@@ -34,7 +34,7 @@ export namespace BaseGenerator {
|
|
|
34
34
|
hostname?: string
|
|
35
35
|
plugin?: boolean
|
|
36
36
|
dependencies?: Dependency
|
|
37
|
-
devDependencies?: Dependency
|
|
37
|
+
devDependencies?: Dependency
|
|
38
38
|
typescript?: boolean
|
|
39
39
|
initGitRepository?: boolean
|
|
40
40
|
staticWorkspaceGitHubActions?: boolean
|
|
@@ -44,12 +44,12 @@ export namespace BaseGenerator {
|
|
|
44
44
|
serviceName?: string,
|
|
45
45
|
envPrefix?: string
|
|
46
46
|
}
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
type WhereClause = {
|
|
49
49
|
before?: string
|
|
50
50
|
after?: string
|
|
51
51
|
}
|
|
52
|
-
|
|
52
|
+
|
|
53
53
|
type GeneratorMetadata = {
|
|
54
54
|
targetDirectory: string
|
|
55
55
|
env: KeyValue
|
|
@@ -59,7 +59,7 @@ export namespace BaseGenerator {
|
|
|
59
59
|
label: string
|
|
60
60
|
var: string
|
|
61
61
|
default: string
|
|
62
|
-
type: 'number' | 'string' | 'boolean'
|
|
62
|
+
type: 'number' | 'string' | 'boolean' | 'path'
|
|
63
63
|
configValue?: 'string'
|
|
64
64
|
|
|
65
65
|
}
|
|
@@ -72,22 +72,22 @@ export namespace BaseGenerator {
|
|
|
72
72
|
logger: BaseLogger
|
|
73
73
|
platformaticVersion: string
|
|
74
74
|
fastifyVersion: string
|
|
75
|
-
|
|
75
|
+
|
|
76
76
|
config: BaseGeneratorConfig
|
|
77
77
|
questions: Array<object>
|
|
78
|
-
|
|
78
|
+
|
|
79
79
|
packages: PackageConfiguration[]
|
|
80
80
|
constructor(opts?: BaseGeneratorOptions)
|
|
81
|
-
|
|
81
|
+
|
|
82
82
|
setConfig(config?: BaseGeneratorConfig): void
|
|
83
|
-
setEnv(env?: Env
|
|
84
|
-
|
|
83
|
+
setEnv(env?: Env): void
|
|
84
|
+
|
|
85
85
|
getDefaultConfig(): JSONValue
|
|
86
|
-
getDefaultEnv(): Env
|
|
87
|
-
|
|
86
|
+
getDefaultEnv(): Env
|
|
87
|
+
|
|
88
88
|
getFastifyVersion(): Promise<string>
|
|
89
89
|
getPlatformaticVersion(): Promise<string>
|
|
90
|
-
|
|
90
|
+
|
|
91
91
|
addPackage(pkg: PackageDefinition): void
|
|
92
92
|
|
|
93
93
|
prepare(): Promise<GeneratorMetadata>
|
|
@@ -95,7 +95,7 @@ export namespace BaseGenerator {
|
|
|
95
95
|
addQuestion(question: any, where?: WhereClause): Promise<void>
|
|
96
96
|
removeQuestion(variableName: string): void
|
|
97
97
|
getTSConfig(): JSONValue
|
|
98
|
-
|
|
98
|
+
|
|
99
99
|
getConfigFieldsDefinitions(): ConfigFieldDefinition[]
|
|
100
100
|
setConfigFields(fields: ConfigField[]): void
|
|
101
101
|
|
package/lib/base-generator.js
CHANGED
|
@@ -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, PLT_ROOT } = require('./utils')
|
|
5
5
|
const { join } = require('node:path')
|
|
6
6
|
const { FileGenerator } = require('./file-generator')
|
|
7
7
|
const { generateTests, generatePlugins } = require('./create-plugin')
|
|
@@ -203,16 +203,20 @@ class BaseGenerator extends FileGenerator {
|
|
|
203
203
|
}
|
|
204
204
|
|
|
205
205
|
checkEnvVariablesInConfigFile () {
|
|
206
|
+
const excludedEnvs = [PLT_ROOT]
|
|
206
207
|
const configFileName = 'platformatic.json'
|
|
207
208
|
const fileOjbect = this.getFileObject(configFileName)
|
|
208
209
|
const envVars = extractEnvVariablesFromText(fileOjbect.contents)
|
|
209
210
|
const envKeys = Object.keys(this.config.env)
|
|
210
211
|
if (envVars.length > 0) {
|
|
211
|
-
|
|
212
|
+
for (const ev of envVars) {
|
|
213
|
+
if (excludedEnvs.includes(ev)) {
|
|
214
|
+
continue
|
|
215
|
+
}
|
|
212
216
|
if (!envKeys.includes(ev)) {
|
|
213
217
|
throw new MissingEnvVariable(ev, configFileName)
|
|
214
218
|
}
|
|
215
|
-
}
|
|
219
|
+
}
|
|
216
220
|
}
|
|
217
221
|
|
|
218
222
|
return true
|
|
@@ -273,6 +277,33 @@ class BaseGenerator extends FileGenerator {
|
|
|
273
277
|
async generateConfigFile () {
|
|
274
278
|
const configFileName = 'platformatic.json'
|
|
275
279
|
const contents = await this._getConfigFileContents()
|
|
280
|
+
|
|
281
|
+
// handle packages
|
|
282
|
+
if (this.packages.length > 0) {
|
|
283
|
+
if (!contents.plugins) {
|
|
284
|
+
contents.plugins = {}
|
|
285
|
+
}
|
|
286
|
+
contents.plugins.packages = this.packages.map((packageDefinition) => {
|
|
287
|
+
const packageConfigOutput = getPackageConfigurationObject(packageDefinition.options, this.config.serviceName)
|
|
288
|
+
if (Object.keys(packageConfigOutput.env).length > 0) {
|
|
289
|
+
const envForPackages = {}
|
|
290
|
+
Object.entries(packageConfigOutput.env).forEach((kv) => {
|
|
291
|
+
envForPackages[kv[0]] = kv[1]
|
|
292
|
+
})
|
|
293
|
+
if (this.config.isRuntimeContext) {
|
|
294
|
+
this.config.env = {
|
|
295
|
+
...this.config.env,
|
|
296
|
+
...addPrefixToEnv(envForPackages, this.config.envPrefix)
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return {
|
|
301
|
+
name: packageDefinition.name,
|
|
302
|
+
options: packageConfigOutput.config
|
|
303
|
+
}
|
|
304
|
+
})
|
|
305
|
+
}
|
|
306
|
+
|
|
276
307
|
this.addFile({
|
|
277
308
|
path: '',
|
|
278
309
|
file: configFileName,
|
package/lib/utils.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ type Env = {
|
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export type PackageConfiguration = {
|
|
6
|
-
type: 'number' | 'string' | 'boolean'
|
|
6
|
+
type: 'number' | 'string' | 'boolean' | 'path'
|
|
7
7
|
path: string
|
|
8
8
|
value: number | string | boolean
|
|
9
9
|
}
|
|
@@ -17,4 +17,5 @@ export namespace GeneratorUtils {
|
|
|
17
17
|
export function envObjectToString(env: Env): string
|
|
18
18
|
export function extractEnvVariablesFromText(text: string): string[]
|
|
19
19
|
export function getPackageConfigurationObject(config: PackageConfiguration[]): object
|
|
20
|
+
export const PLT_ROOT: string
|
|
20
21
|
}
|
package/lib/utils.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
const { mkdir } = require('node:fs/promises')
|
|
4
4
|
const { WrongTypeError } = require('./errors')
|
|
5
|
+
const { join } = require('node:path')
|
|
6
|
+
const PLT_ROOT = 'PLT_ROOT'
|
|
5
7
|
|
|
6
8
|
async function safeMkdir (dir) {
|
|
7
9
|
try {
|
|
@@ -89,6 +91,7 @@ function getPackageConfigurationObject (config, serviceName = '') {
|
|
|
89
91
|
props.forEach((prop, idx) => {
|
|
90
92
|
if (idx === props.length - 1) {
|
|
91
93
|
let value
|
|
94
|
+
let isPath = false
|
|
92
95
|
switch (param.type) {
|
|
93
96
|
case 'string' :
|
|
94
97
|
value = param.value.toString()
|
|
@@ -99,6 +102,10 @@ function getPackageConfigurationObject (config, serviceName = '') {
|
|
|
99
102
|
case 'boolean':
|
|
100
103
|
value = (param.value === 'true')
|
|
101
104
|
break
|
|
105
|
+
case 'path':
|
|
106
|
+
value = `${join(`{${PLT_ROOT}}`, param.value)}`
|
|
107
|
+
isPath = true
|
|
108
|
+
break
|
|
102
109
|
default:
|
|
103
110
|
throw new WrongTypeError(param.type)
|
|
104
111
|
}
|
|
@@ -106,7 +113,13 @@ function getPackageConfigurationObject (config, serviceName = '') {
|
|
|
106
113
|
current[prop] = value
|
|
107
114
|
} else {
|
|
108
115
|
const key = addPrefixToString(param.name, convertServiceNameToPrefix(serviceName))
|
|
109
|
-
|
|
116
|
+
// If it's a path, we need to add it to the env only the relative part of the path
|
|
117
|
+
if (isPath) {
|
|
118
|
+
current[prop] = `${join(`{${PLT_ROOT}}`, `{${key}}`)}`
|
|
119
|
+
value = param.value
|
|
120
|
+
} else {
|
|
121
|
+
current[prop] = `{${key}}`
|
|
122
|
+
}
|
|
110
123
|
output.env[key] = value
|
|
111
124
|
}
|
|
112
125
|
current = output.config
|
|
@@ -128,5 +141,6 @@ module.exports = {
|
|
|
128
141
|
envObjectToString,
|
|
129
142
|
extractEnvVariablesFromText,
|
|
130
143
|
safeMkdir,
|
|
131
|
-
stripVersion
|
|
144
|
+
stripVersion,
|
|
145
|
+
PLT_ROOT
|
|
132
146
|
}
|
package/package.json
CHANGED
|
@@ -404,6 +404,171 @@ 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
|
+
{
|
|
493
|
+
// with relative path type but no name
|
|
494
|
+
const svc = new BaseGenerator({
|
|
495
|
+
module: '@platformatic/service'
|
|
496
|
+
})
|
|
497
|
+
svc.setConfig({
|
|
498
|
+
isRuntimeContext: true,
|
|
499
|
+
plugin: true
|
|
500
|
+
})
|
|
501
|
+
const packageDefinitions = [
|
|
502
|
+
{
|
|
503
|
+
name: '@fastify/static',
|
|
504
|
+
options: [
|
|
505
|
+
{
|
|
506
|
+
path: 'root',
|
|
507
|
+
value: 'public',
|
|
508
|
+
type: 'path'
|
|
509
|
+
}
|
|
510
|
+
]
|
|
511
|
+
}
|
|
512
|
+
]
|
|
513
|
+
svc.addPackage(packageDefinitions[0])
|
|
514
|
+
await svc.prepare()
|
|
515
|
+
|
|
516
|
+
const platformaticConfigFile = svc.getFileObject('platformatic.json')
|
|
517
|
+
const contents = JSON.parse(platformaticConfigFile.contents)
|
|
518
|
+
|
|
519
|
+
assert.deepEqual(contents.plugins, {
|
|
520
|
+
packages: [
|
|
521
|
+
{
|
|
522
|
+
name: '@fastify/static',
|
|
523
|
+
options: {
|
|
524
|
+
root: '{PLT_ROOT}/public'
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
]
|
|
528
|
+
})
|
|
529
|
+
}
|
|
530
|
+
{
|
|
531
|
+
// with relative path type and name
|
|
532
|
+
const svc = new BaseGenerator({
|
|
533
|
+
module: '@platformatic/service'
|
|
534
|
+
})
|
|
535
|
+
svc.setConfig({
|
|
536
|
+
isRuntimeContext: true,
|
|
537
|
+
plugin: true,
|
|
538
|
+
serviceName: 'my-service'
|
|
539
|
+
})
|
|
540
|
+
const packageDefinitions = [
|
|
541
|
+
{
|
|
542
|
+
name: '@fastify/static',
|
|
543
|
+
options: [
|
|
544
|
+
{
|
|
545
|
+
path: 'root',
|
|
546
|
+
value: 'public',
|
|
547
|
+
type: 'path',
|
|
548
|
+
name: 'FST_PLUGIN_STATIC_ROOT'
|
|
549
|
+
}
|
|
550
|
+
]
|
|
551
|
+
}
|
|
552
|
+
]
|
|
553
|
+
svc.addPackage(packageDefinitions[0])
|
|
554
|
+
await svc.prepare()
|
|
555
|
+
|
|
556
|
+
const platformaticConfigFile = svc.getFileObject('platformatic.json')
|
|
557
|
+
const contents = JSON.parse(platformaticConfigFile.contents)
|
|
558
|
+
|
|
559
|
+
assert.deepEqual(contents.plugins, {
|
|
560
|
+
packages: [
|
|
561
|
+
{
|
|
562
|
+
name: '@fastify/static',
|
|
563
|
+
options: {
|
|
564
|
+
root: '{PLT_ROOT}/{PLT_MY_SERVICE_FST_PLUGIN_STATIC_ROOT}'
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
]
|
|
568
|
+
})
|
|
569
|
+
}
|
|
570
|
+
})
|
|
571
|
+
|
|
407
572
|
describe('runtime context', () => {
|
|
408
573
|
test('should set config.envPrefix correctly', async (t) => {
|
|
409
574
|
const bg = new BaseGenerator({
|