@platformatic/generators 1.13.0 → 1.13.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 +10 -2
- package/lib/base-generator.js +5 -0
- package/lib/errors.js +2 -1
- package/lib/utils.d.ts +7 -0
- package/lib/utils.js +33 -1
- package/package.json +1 -1
- package/test/base-generator.test.js +19 -0
- package/test/utils.test.js +64 -2
package/lib/base-generator.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseLogger } from 'pino'
|
|
2
2
|
import { FileGenerator } from './file-generator'
|
|
3
|
-
|
|
3
|
+
import { PackageConfiguration } from './utils'
|
|
4
4
|
export namespace BaseGenerator {
|
|
5
5
|
export type BaseGeneratorOptions = FileGenerator.FileGeneratorOptions & {
|
|
6
6
|
type?: 'service' | 'db' | 'composer'
|
|
@@ -24,6 +24,11 @@ export namespace BaseGenerator {
|
|
|
24
24
|
type Dependency = {
|
|
25
25
|
[key: string]: string
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
type PackageDefinition = {
|
|
29
|
+
name: string,
|
|
30
|
+
options: PackageConfiguration
|
|
31
|
+
}
|
|
27
32
|
type BaseGeneratorConfig = Record<string, any> & {
|
|
28
33
|
port?: number
|
|
29
34
|
hostname?: string
|
|
@@ -37,7 +42,7 @@ export namespace BaseGenerator {
|
|
|
37
42
|
env?: KeyValue,
|
|
38
43
|
isRuntimeContext?: boolean,
|
|
39
44
|
serviceName?: string,
|
|
40
|
-
envPrefix?: string
|
|
45
|
+
envPrefix?: string
|
|
41
46
|
}
|
|
42
47
|
|
|
43
48
|
type WhereClause = {
|
|
@@ -71,6 +76,7 @@ export namespace BaseGenerator {
|
|
|
71
76
|
config: BaseGeneratorConfig
|
|
72
77
|
questions: Array<object>
|
|
73
78
|
|
|
79
|
+
packages: PackageConfiguration[]
|
|
74
80
|
constructor(opts?: BaseGeneratorOptions)
|
|
75
81
|
|
|
76
82
|
setConfig(config?: BaseGeneratorConfig): void
|
|
@@ -82,6 +88,8 @@ export namespace BaseGenerator {
|
|
|
82
88
|
getFastifyVersion(): Promise<string>
|
|
83
89
|
getPlatformaticVersion(): Promise<string>
|
|
84
90
|
|
|
91
|
+
addPackage(pkg: PackageDefinition): void
|
|
92
|
+
|
|
85
93
|
prepare(): Promise<GeneratorMetadata>
|
|
86
94
|
run(): Promise<GeneratorMetadata>
|
|
87
95
|
addQuestion(question: any, where?: WhereClause): Promise<void>
|
package/lib/base-generator.js
CHANGED
|
@@ -29,6 +29,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
29
29
|
this.inquirer = null
|
|
30
30
|
this.targetDirectory = opts.targetDirectory || null
|
|
31
31
|
this.config = this.getDefaultConfig()
|
|
32
|
+
this.packages = []
|
|
32
33
|
}
|
|
33
34
|
|
|
34
35
|
getDefaultConfig () {
|
|
@@ -415,6 +416,10 @@ class BaseGenerator extends FileGenerator {
|
|
|
415
416
|
return metadata
|
|
416
417
|
}
|
|
417
418
|
|
|
419
|
+
addPackage (pkg) {
|
|
420
|
+
this.packages.push(pkg)
|
|
421
|
+
}
|
|
422
|
+
|
|
418
423
|
// implement in the subclass
|
|
419
424
|
async _beforePrepare () {}
|
|
420
425
|
async _afterPrepare () {}
|
package/lib/errors.js
CHANGED
|
@@ -7,5 +7,6 @@ const ERROR_PREFIX = 'PLT_GEN'
|
|
|
7
7
|
module.exports = {
|
|
8
8
|
NoQuestionsError: createError(`${ERROR_PREFIX}_NO_QUESTIONS_ERROR`, 'No questions added.'),
|
|
9
9
|
PrepareError: createError(`${ERROR_PREFIX}_PREPARE_ERROR`, 'Error while generating the files: %s.'),
|
|
10
|
-
MissingEnvVariable: createError(`${ERROR_PREFIX}_MISSING_ENV_VAR`, 'Env variable %s is defined in config file %s, but not in config.env object.')
|
|
10
|
+
MissingEnvVariable: createError(`${ERROR_PREFIX}_MISSING_ENV_VAR`, 'Env variable %s is defined in config file %s, but not in config.env object.'),
|
|
11
|
+
WrongTypeError: createError(`${ERROR_PREFIX}_WRONG_TYPE`, 'Invalid value type. Accepted values are \'string\', \'number\' and \'boolean\', found \'%s\'.')
|
|
11
12
|
}
|
package/lib/utils.d.ts
CHANGED
|
@@ -2,6 +2,12 @@ type Env = {
|
|
|
2
2
|
[key: string]: string
|
|
3
3
|
}
|
|
4
4
|
|
|
5
|
+
export type PackageConfiguration = {
|
|
6
|
+
type: 'number' | 'string' | 'boolean'
|
|
7
|
+
path: string
|
|
8
|
+
value: number | string | boolean
|
|
9
|
+
}
|
|
10
|
+
|
|
5
11
|
export namespace GeneratorUtils {
|
|
6
12
|
export function safeMkdir(dir: string): Promise<void>
|
|
7
13
|
export function stripVersion(version: string): string
|
|
@@ -9,4 +15,5 @@ export namespace GeneratorUtils {
|
|
|
9
15
|
export function addPrefixToEnv(env: Env, prefix: string): Env
|
|
10
16
|
export function envObjectToString(env: Env): string
|
|
11
17
|
export function extractEnvVariablesFromText(text: string): string[]
|
|
18
|
+
export function getPackageConfigurationObject(config: PackageConfiguration[]): object
|
|
12
19
|
}
|
package/lib/utils.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { mkdir } = require('node:fs/promises')
|
|
4
|
+
const { WrongTypeError } = require('./errors')
|
|
4
5
|
|
|
5
6
|
async function safeMkdir (dir) {
|
|
6
7
|
try {
|
|
@@ -58,10 +59,41 @@ function extractEnvVariablesFromText (text) {
|
|
|
58
59
|
}
|
|
59
60
|
return []
|
|
60
61
|
}
|
|
61
|
-
|
|
62
|
+
function getPackageConfigurationObject (config) {
|
|
63
|
+
const output = {}
|
|
64
|
+
let current = output
|
|
65
|
+
for (const param of config) {
|
|
66
|
+
const props = param.path.split('.')
|
|
67
|
+
props.forEach((prop, idx) => {
|
|
68
|
+
if (idx === props.length - 1) {
|
|
69
|
+
switch (param.type) {
|
|
70
|
+
case 'string' :
|
|
71
|
+
current[prop] = param.value.toString()
|
|
72
|
+
break
|
|
73
|
+
case 'number':
|
|
74
|
+
current[prop] = parseInt(param.value)
|
|
75
|
+
break
|
|
76
|
+
case 'boolean':
|
|
77
|
+
current[prop] = (param.value === 'true')
|
|
78
|
+
break
|
|
79
|
+
default:
|
|
80
|
+
throw new WrongTypeError(param.type)
|
|
81
|
+
}
|
|
82
|
+
current = output
|
|
83
|
+
} else {
|
|
84
|
+
if (!current[prop]) {
|
|
85
|
+
current[prop] = {}
|
|
86
|
+
}
|
|
87
|
+
current = current[prop]
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
return output
|
|
92
|
+
}
|
|
62
93
|
module.exports = {
|
|
63
94
|
addPrefixToEnv,
|
|
64
95
|
convertServiceNameToPrefix,
|
|
96
|
+
getPackageConfigurationObject,
|
|
65
97
|
envObjectToString,
|
|
66
98
|
extractEnvVariablesFromText,
|
|
67
99
|
safeMkdir,
|
package/package.json
CHANGED
|
@@ -361,6 +361,25 @@ test('should throw if there is a missing env variable', async () => {
|
|
|
361
361
|
}
|
|
362
362
|
})
|
|
363
363
|
|
|
364
|
+
test('should add package', async () => {
|
|
365
|
+
const bg = new BaseGenerator()
|
|
366
|
+
|
|
367
|
+
const packageDefinition = {
|
|
368
|
+
name: '@my/package',
|
|
369
|
+
options: [
|
|
370
|
+
{
|
|
371
|
+
path: 'foobar',
|
|
372
|
+
type: 'string',
|
|
373
|
+
value: 'foobar'
|
|
374
|
+
}
|
|
375
|
+
]
|
|
376
|
+
}
|
|
377
|
+
bg.addPackage(packageDefinition)
|
|
378
|
+
|
|
379
|
+
assert.equal(bg.packages.length, 1)
|
|
380
|
+
assert.deepEqual(bg.packages[0], packageDefinition)
|
|
381
|
+
})
|
|
382
|
+
|
|
364
383
|
describe('runtime context', () => {
|
|
365
384
|
test('should set config.envPrefix correctly', async (t) => {
|
|
366
385
|
const bg = new BaseGenerator()
|
package/test/utils.test.js
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { test, describe } = require('node:test')
|
|
4
4
|
const assert = require('node:assert')
|
|
5
|
-
const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, envObjectToString } = require('../lib/utils')
|
|
6
|
-
const { extractEnvVariablesFromText } = require('../lib/utils')
|
|
5
|
+
const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, envObjectToString, extractEnvVariablesFromText, getPackageConfigurationObject } = require('../lib/utils')
|
|
7
6
|
|
|
8
7
|
describe('utils', () => {
|
|
9
8
|
describe('stripVersion', async () => {
|
|
@@ -86,4 +85,67 @@ describe('utils', () => {
|
|
|
86
85
|
assert.deepEqual(env, [])
|
|
87
86
|
})
|
|
88
87
|
})
|
|
88
|
+
|
|
89
|
+
describe('getPackageConfigurationObject', async () => {
|
|
90
|
+
const input = [
|
|
91
|
+
{
|
|
92
|
+
path: 'prefix',
|
|
93
|
+
value: '/foo',
|
|
94
|
+
type: 'string'
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
path: 'foo.fooOption1',
|
|
98
|
+
value: 'value1',
|
|
99
|
+
type: 'string'
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
path: 'foo.fooOption2',
|
|
103
|
+
value: 'value2',
|
|
104
|
+
type: 'string'
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
path: 'foobar',
|
|
108
|
+
value: '123',
|
|
109
|
+
type: 'number'
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
path: 'boolean.truthy',
|
|
113
|
+
value: 'true',
|
|
114
|
+
type: 'boolean'
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
path: 'boolean.falsey',
|
|
118
|
+
value: 'false',
|
|
119
|
+
type: 'boolean'
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
const config = getPackageConfigurationObject(input)
|
|
123
|
+
assert.deepEqual(config, {
|
|
124
|
+
prefix: '/foo',
|
|
125
|
+
foo: {
|
|
126
|
+
fooOption1: 'value1',
|
|
127
|
+
fooOption2: 'value2'
|
|
128
|
+
},
|
|
129
|
+
foobar: 123,
|
|
130
|
+
boolean: {
|
|
131
|
+
truthy: true,
|
|
132
|
+
falsey: false
|
|
133
|
+
}
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
// should throw
|
|
137
|
+
try {
|
|
138
|
+
getPackageConfigurationObject([
|
|
139
|
+
{
|
|
140
|
+
path: 'wrong',
|
|
141
|
+
type: 'object',
|
|
142
|
+
value: {}
|
|
143
|
+
}
|
|
144
|
+
])
|
|
145
|
+
assert.fail()
|
|
146
|
+
} catch (err) {
|
|
147
|
+
assert.equal(err.code, 'PLT_GEN_WRONG_TYPE')
|
|
148
|
+
assert.equal(err.message, 'Invalid value type. Accepted values are \'string\', \'number\' and \'boolean\', found \'object\'.')
|
|
149
|
+
}
|
|
150
|
+
})
|
|
89
151
|
})
|