@platformatic/generators 1.13.5 → 1.13.7
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.js +1 -0
- package/lib/utils.d.ts +1 -0
- package/lib/utils.js +38 -7
- package/package.json +1 -1
- package/test/utils.test.js +53 -14
package/lib/base-generator.js
CHANGED
package/lib/utils.d.ts
CHANGED
|
@@ -13,6 +13,7 @@ export namespace GeneratorUtils {
|
|
|
13
13
|
export function stripVersion(version: string): string
|
|
14
14
|
export function convertServiceNameToPrefix(serviceName: string): string
|
|
15
15
|
export function addPrefixToEnv(env: Env, prefix: string): Env
|
|
16
|
+
export function addPrefixToEnv(input: string, prefix: string): string
|
|
16
17
|
export function envObjectToString(env: Env): string
|
|
17
18
|
export function extractEnvVariablesFromText(text: string): string[]
|
|
18
19
|
export function getPackageConfigurationObject(config: PackageConfiguration[]): object
|
package/lib/utils.js
CHANGED
|
@@ -29,11 +29,30 @@ function convertServiceNameToPrefix (serviceName) {
|
|
|
29
29
|
return serviceName.replace(/-/g, '_').toUpperCase()
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
function addPrefixToString (input, prefix) {
|
|
33
|
+
if (!prefix) {
|
|
34
|
+
return input
|
|
35
|
+
}
|
|
36
|
+
const prefixRegExp = new RegExp(`^PLT_${prefix}_`)
|
|
37
|
+
if (!input.match(prefixRegExp)) {
|
|
38
|
+
// strip PLT_ if needed
|
|
39
|
+
input = input.replace(/^PLT_/, '')
|
|
40
|
+
return [`PLT_${prefix}_${input}`]
|
|
41
|
+
} else {
|
|
42
|
+
return input
|
|
43
|
+
}
|
|
44
|
+
}
|
|
32
45
|
function addPrefixToEnv (env, prefix) {
|
|
33
46
|
const newEnv = {}
|
|
47
|
+
if (!prefix) {
|
|
48
|
+
// return original env
|
|
49
|
+
return env
|
|
50
|
+
}
|
|
34
51
|
const prefixRegExp = new RegExp(`^PLT_${prefix}_`)
|
|
35
52
|
Object.entries(env).forEach((kv) => {
|
|
36
53
|
if (!kv[0].match(prefixRegExp)) {
|
|
54
|
+
// strip PLT_ if needed
|
|
55
|
+
kv[0] = kv[0].replace(/^PLT_/, '')
|
|
37
56
|
newEnv[`PLT_${prefix}_${kv[0]}`] = kv[1]
|
|
38
57
|
} else {
|
|
39
58
|
newEnv[kv[0]] = kv[1]
|
|
@@ -59,27 +78,38 @@ function extractEnvVariablesFromText (text) {
|
|
|
59
78
|
}
|
|
60
79
|
return []
|
|
61
80
|
}
|
|
62
|
-
function getPackageConfigurationObject (config) {
|
|
63
|
-
const output = {
|
|
64
|
-
|
|
81
|
+
function getPackageConfigurationObject (config, serviceName = '') {
|
|
82
|
+
const output = {
|
|
83
|
+
config: {},
|
|
84
|
+
env: {}
|
|
85
|
+
}
|
|
86
|
+
let current = output.config
|
|
65
87
|
for (const param of config) {
|
|
66
88
|
const props = param.path.split('.')
|
|
67
89
|
props.forEach((prop, idx) => {
|
|
68
90
|
if (idx === props.length - 1) {
|
|
91
|
+
let value
|
|
69
92
|
switch (param.type) {
|
|
70
93
|
case 'string' :
|
|
71
|
-
|
|
94
|
+
value = param.value.toString()
|
|
72
95
|
break
|
|
73
96
|
case 'number':
|
|
74
|
-
|
|
97
|
+
value = parseInt(param.value)
|
|
75
98
|
break
|
|
76
99
|
case 'boolean':
|
|
77
|
-
|
|
100
|
+
value = (param.value === 'true')
|
|
78
101
|
break
|
|
79
102
|
default:
|
|
80
103
|
throw new WrongTypeError(param.type)
|
|
81
104
|
}
|
|
82
|
-
|
|
105
|
+
if (!param.name) {
|
|
106
|
+
current[prop] = value
|
|
107
|
+
} else {
|
|
108
|
+
const key = addPrefixToString(param.name, convertServiceNameToPrefix(serviceName))
|
|
109
|
+
current[prop] = `{${key}}`
|
|
110
|
+
output.env[key] = value
|
|
111
|
+
}
|
|
112
|
+
current = output.config
|
|
83
113
|
} else {
|
|
84
114
|
if (!current[prop]) {
|
|
85
115
|
current[prop] = {}
|
|
@@ -92,6 +122,7 @@ function getPackageConfigurationObject (config) {
|
|
|
92
122
|
}
|
|
93
123
|
module.exports = {
|
|
94
124
|
addPrefixToEnv,
|
|
125
|
+
addPrefixToString,
|
|
95
126
|
convertServiceNameToPrefix,
|
|
96
127
|
getPackageConfigurationObject,
|
|
97
128
|
envObjectToString,
|
package/package.json
CHANGED
package/test/utils.test.js
CHANGED
|
@@ -2,7 +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, extractEnvVariablesFromText, getPackageConfigurationObject } = require('../lib/utils')
|
|
5
|
+
const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, envObjectToString, extractEnvVariablesFromText, getPackageConfigurationObject, addPrefixToString } = require('../lib/utils')
|
|
6
6
|
|
|
7
7
|
describe('utils', () => {
|
|
8
8
|
describe('stripVersion', async () => {
|
|
@@ -34,18 +34,38 @@ describe('utils', () => {
|
|
|
34
34
|
})
|
|
35
35
|
|
|
36
36
|
describe('addPrefixToEnv', () => {
|
|
37
|
-
test('Should convert env and add prefix, if
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
37
|
+
test('Should convert env and add prefix, if needed', async (t) => {
|
|
38
|
+
{
|
|
39
|
+
const testEnv = {
|
|
40
|
+
FOO: 'bar',
|
|
41
|
+
PLT_MY_SERVICE_NAME: 'service',
|
|
42
|
+
DATABASE_URL: 'foobar',
|
|
43
|
+
PLT_ANOTHER_VALUE: 'anotherValue'
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
assert.deepEqual(addPrefixToEnv(testEnv, 'MY_SERVICE'), {
|
|
47
|
+
PLT_MY_SERVICE_FOO: 'bar',
|
|
48
|
+
PLT_MY_SERVICE_NAME: 'service',
|
|
49
|
+
PLT_MY_SERVICE_DATABASE_URL: 'foobar',
|
|
50
|
+
PLT_MY_SERVICE_ANOTHER_VALUE: 'anotherValue'
|
|
51
|
+
})
|
|
42
52
|
}
|
|
53
|
+
{
|
|
54
|
+
// empty service name, return same env
|
|
55
|
+
const testEnv = {
|
|
56
|
+
FOO: 'bar',
|
|
57
|
+
PLT_MY_SERVICE_NAME: 'service',
|
|
58
|
+
DATABASE_URL: 'foobar',
|
|
59
|
+
PLT_ANOTHER_VALUE: 'anotherValue'
|
|
60
|
+
}
|
|
43
61
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
62
|
+
assert.deepEqual(addPrefixToEnv(testEnv, ''), {
|
|
63
|
+
FOO: 'bar',
|
|
64
|
+
PLT_MY_SERVICE_NAME: 'service',
|
|
65
|
+
DATABASE_URL: 'foobar',
|
|
66
|
+
PLT_ANOTHER_VALUE: 'anotherValue'
|
|
67
|
+
})
|
|
68
|
+
}
|
|
49
69
|
})
|
|
50
70
|
})
|
|
51
71
|
|
|
@@ -103,6 +123,12 @@ describe('utils', () => {
|
|
|
103
123
|
value: 'value2',
|
|
104
124
|
type: 'string'
|
|
105
125
|
},
|
|
126
|
+
{
|
|
127
|
+
path: 'foo.fooOption3',
|
|
128
|
+
value: 'value3',
|
|
129
|
+
type: 'string',
|
|
130
|
+
name: 'THE_FOO_OPTION_3'
|
|
131
|
+
},
|
|
106
132
|
{
|
|
107
133
|
path: 'foobar',
|
|
108
134
|
value: '123',
|
|
@@ -119,12 +145,13 @@ describe('utils', () => {
|
|
|
119
145
|
type: 'boolean'
|
|
120
146
|
}
|
|
121
147
|
]
|
|
122
|
-
const
|
|
123
|
-
assert.deepEqual(config, {
|
|
148
|
+
const output = getPackageConfigurationObject(input)
|
|
149
|
+
assert.deepEqual(output.config, {
|
|
124
150
|
prefix: '/foo',
|
|
125
151
|
foo: {
|
|
126
152
|
fooOption1: 'value1',
|
|
127
|
-
fooOption2: 'value2'
|
|
153
|
+
fooOption2: 'value2',
|
|
154
|
+
fooOption3: '{THE_FOO_OPTION_3}'
|
|
128
155
|
},
|
|
129
156
|
foobar: 123,
|
|
130
157
|
boolean: {
|
|
@@ -133,6 +160,10 @@ describe('utils', () => {
|
|
|
133
160
|
}
|
|
134
161
|
})
|
|
135
162
|
|
|
163
|
+
assert.deepEqual(output.env, {
|
|
164
|
+
THE_FOO_OPTION_3: 'value3'
|
|
165
|
+
})
|
|
166
|
+
|
|
136
167
|
// should throw
|
|
137
168
|
try {
|
|
138
169
|
getPackageConfigurationObject([
|
|
@@ -148,4 +179,12 @@ describe('utils', () => {
|
|
|
148
179
|
assert.equal(err.message, 'Invalid value type. Accepted values are \'string\', \'number\' and \'boolean\', found \'object\'.')
|
|
149
180
|
}
|
|
150
181
|
})
|
|
182
|
+
|
|
183
|
+
describe('addPrefixToString', () => {
|
|
184
|
+
test('should add prefix to string', async () => {
|
|
185
|
+
assert.equal(addPrefixToString('PLT_SERVICE_FOO', 'SERVICE'), 'PLT_SERVICE_FOO')
|
|
186
|
+
assert.equal(addPrefixToString('FOO', 'SERVICE'), 'PLT_SERVICE_FOO')
|
|
187
|
+
assert.equal(addPrefixToString('FOO', ''), 'FOO')
|
|
188
|
+
})
|
|
189
|
+
})
|
|
151
190
|
})
|