@platformatic/generators 2.0.0-alpha.2 → 2.0.0-alpha.21
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/eslint.config.js +3 -0
- package/index.d.ts +1 -1
- package/index.js +1 -3
- package/index.test-d.ts +4 -4
- package/lib/base-generator.d.ts +25 -25
- package/lib/base-generator.js +31 -30
- package/lib/create-gitignore.js +2 -2
- package/lib/create-plugin.d.ts +4 -4
- package/lib/create-plugin.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/file-generator.d.ts +14 -14
- package/lib/file-generator.js +4 -4
- package/lib/utils.d.ts +8 -9
- package/lib/utils.js +18 -23
- package/package.json +12 -11
- package/lib/create-stackable-cli.js +0 -161
- package/lib/create-stackable-files.js +0 -518
- package/lib/create-stackable-plugin.js +0 -49
- package/lib/create-stackable-tests.js +0 -395
- package/lib/stackable-generator.js +0 -317
- package/test/base-generator.test.js +0 -973
- package/test/file-generator.test.js +0 -140
- package/test/fixtures/sample-runtime/.env +0 -9
- package/test/fixtures/sample-runtime/platformatic.json +0 -18
- package/test/fixtures/sample-runtime/services/no-plugin/platformatic.json +0 -7
- package/test/fixtures/sample-runtime/services/rival/platformatic.json +0 -33
- package/test/helpers.js +0 -64
- package/test/stackable-generator.test.js +0 -114
- package/test/utils.test.js +0 -223
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
function getJsStackablePluginFile () {
|
|
4
|
-
return `\
|
|
5
|
-
/// <reference path="../index.d.ts" />
|
|
6
|
-
'use strict'
|
|
7
|
-
/** @param {import('fastify').FastifyInstance} fastify */
|
|
8
|
-
module.exports = async function (fastify, opts) {
|
|
9
|
-
const config = fastify.platformatic.config
|
|
10
|
-
const greeting = config.greeting
|
|
11
|
-
fastify.log.info({ greeting }, 'Loading stackable greeting plugin.')
|
|
12
|
-
fastify.decorate('greeting', greeting)
|
|
13
|
-
}
|
|
14
|
-
`
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function getTsStackablePluginFile () {
|
|
18
|
-
return `\
|
|
19
|
-
// eslint-disable-next-line
|
|
20
|
-
/// <reference path="../index.d.ts" />
|
|
21
|
-
import { FastifyInstance, FastifyPluginOptions } from 'fastify'
|
|
22
|
-
|
|
23
|
-
export default async function (fastify: FastifyInstance, opts: FastifyPluginOptions) {
|
|
24
|
-
const config = fastify.platformatic.config
|
|
25
|
-
const greeting = config.greeting
|
|
26
|
-
fastify.log.info({ greeting }, 'Loading stackable greeting plugin.')
|
|
27
|
-
fastify.decorate('greeting', greeting)
|
|
28
|
-
}
|
|
29
|
-
`
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function generateStackablePlugins (typescript) {
|
|
33
|
-
if (typescript) {
|
|
34
|
-
return [{
|
|
35
|
-
path: 'plugins',
|
|
36
|
-
file: 'example.ts',
|
|
37
|
-
contents: getTsStackablePluginFile()
|
|
38
|
-
}]
|
|
39
|
-
}
|
|
40
|
-
return [{
|
|
41
|
-
path: 'plugins',
|
|
42
|
-
file: 'example.js',
|
|
43
|
-
contents: getJsStackablePluginFile()
|
|
44
|
-
}]
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
module.exports = {
|
|
48
|
-
generateStackablePlugins
|
|
49
|
-
}
|
|
@@ -1,395 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { kebabCase } = require('change-case-all')
|
|
4
|
-
|
|
5
|
-
function getJsStackableIndexTestFile (stackableName) {
|
|
6
|
-
const configType = kebabCase(stackableName + '-app')
|
|
7
|
-
|
|
8
|
-
return `\
|
|
9
|
-
'use strict'
|
|
10
|
-
|
|
11
|
-
const test = require('node:test')
|
|
12
|
-
const assert = require('node:assert')
|
|
13
|
-
const stackable = require('../index')
|
|
14
|
-
const { schema } = require('../lib/schema')
|
|
15
|
-
const { Generator } = require('../lib/generator')
|
|
16
|
-
|
|
17
|
-
test('should export stackable interface', async () => {
|
|
18
|
-
assert.strictEqual(typeof stackable, 'function')
|
|
19
|
-
assert.strictEqual(stackable.configType, '${configType}')
|
|
20
|
-
assert.deepStrictEqual(stackable.schema, schema)
|
|
21
|
-
assert.deepStrictEqual(stackable.Generator, Generator)
|
|
22
|
-
assert.ok(stackable.configManagerConfig)
|
|
23
|
-
assert.ok(typeof stackable.transformConfig, 'function')
|
|
24
|
-
})
|
|
25
|
-
`
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function getTsStackableIndexTestFile (stackableName) {
|
|
29
|
-
const configType = kebabCase(stackableName + '-app')
|
|
30
|
-
|
|
31
|
-
return `\
|
|
32
|
-
import test from 'node:test'
|
|
33
|
-
import assert from 'node:assert'
|
|
34
|
-
import stackable from '../index'
|
|
35
|
-
import { schema } from '../lib/schema'
|
|
36
|
-
import { Generator } from '../lib/generator'
|
|
37
|
-
|
|
38
|
-
test('should export stackable interface', async () => {
|
|
39
|
-
assert.strictEqual(typeof stackable, 'function')
|
|
40
|
-
assert.strictEqual(stackable.configType, '${configType}')
|
|
41
|
-
assert.deepStrictEqual(stackable.schema, schema)
|
|
42
|
-
assert.deepStrictEqual(stackable.Generator, Generator)
|
|
43
|
-
assert.ok(stackable.configManagerConfig)
|
|
44
|
-
assert.ok(typeof stackable.transformConfig, 'function')
|
|
45
|
-
})
|
|
46
|
-
`
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function getJsStackableSchemaTestFile (stackableName) {
|
|
50
|
-
const schemaId = kebabCase(stackableName)
|
|
51
|
-
|
|
52
|
-
return `\
|
|
53
|
-
'use strict'
|
|
54
|
-
|
|
55
|
-
const test = require('node:test')
|
|
56
|
-
const assert = require('node:assert')
|
|
57
|
-
const { schema } = require('../index')
|
|
58
|
-
|
|
59
|
-
test('should export stackable schema', async () => {
|
|
60
|
-
assert.strictEqual(schema.$id, '${schemaId}')
|
|
61
|
-
assert.strictEqual(typeof schema.version, 'string')
|
|
62
|
-
|
|
63
|
-
assert.deepStrictEqual(schema.properties.greeting, {
|
|
64
|
-
type: 'object',
|
|
65
|
-
properties: {
|
|
66
|
-
text: {
|
|
67
|
-
type: 'string'
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
required: ['text'],
|
|
71
|
-
additionalProperties: false
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
`
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function getTsStackableSchemaTestFile (stackableName) {
|
|
78
|
-
const schemaId = kebabCase(stackableName)
|
|
79
|
-
|
|
80
|
-
return `\
|
|
81
|
-
import test from 'node:test'
|
|
82
|
-
import assert from 'node:assert'
|
|
83
|
-
import { schema } from '../index'
|
|
84
|
-
|
|
85
|
-
test('should export stackable schema', async () => {
|
|
86
|
-
assert.strictEqual(schema.$id, '${schemaId}')
|
|
87
|
-
assert.strictEqual(typeof schema.version, 'string')
|
|
88
|
-
|
|
89
|
-
assert.deepStrictEqual(schema.properties.greeting, {
|
|
90
|
-
type: 'object',
|
|
91
|
-
properties: {
|
|
92
|
-
text: {
|
|
93
|
-
type: 'string'
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
required: ['text'],
|
|
97
|
-
additionalProperties: false
|
|
98
|
-
})
|
|
99
|
-
})
|
|
100
|
-
`
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function getJsStackableGeneratorTestFile () {
|
|
104
|
-
return `\
|
|
105
|
-
'use strict'
|
|
106
|
-
|
|
107
|
-
const test = require('node:test')
|
|
108
|
-
const assert = require('node:assert')
|
|
109
|
-
const { tmpdir } = require('node:os')
|
|
110
|
-
const { join } = require('node:path')
|
|
111
|
-
const { readFile, readdir, mkdtemp, rm } = require('node:fs/promises')
|
|
112
|
-
const { Generator } = require('../index')
|
|
113
|
-
const stackablePackageJson = require('../package.json')
|
|
114
|
-
|
|
115
|
-
test('should return a default Generator config', async () => {
|
|
116
|
-
const generator = new Generator()
|
|
117
|
-
const defaultConfig = generator.getDefaultConfig()
|
|
118
|
-
|
|
119
|
-
assert.strictEqual(defaultConfig.hostname, '0.0.0.0')
|
|
120
|
-
assert.strictEqual(defaultConfig.port, 3042)
|
|
121
|
-
assert.strictEqual(defaultConfig.greeting, 'Hello world!')
|
|
122
|
-
assert.deepStrictEqual(defaultConfig.env, {})
|
|
123
|
-
assert.deepStrictEqual(defaultConfig.dependencies, {})
|
|
124
|
-
assert.deepStrictEqual(defaultConfig.devDependencies, {})
|
|
125
|
-
})
|
|
126
|
-
|
|
127
|
-
test('should return Generator config fields definitions', async () => {
|
|
128
|
-
const generator = new Generator()
|
|
129
|
-
const configFieldsDefs = generator.getConfigFieldsDefinitions()
|
|
130
|
-
|
|
131
|
-
const hostnameField = configFieldsDefs.find(
|
|
132
|
-
field => field.var === 'PLT_SERVER_HOSTNAME'
|
|
133
|
-
)
|
|
134
|
-
assert.deepStrictEqual(hostnameField, {
|
|
135
|
-
var: 'PLT_SERVER_HOSTNAME',
|
|
136
|
-
label: 'What is the hostname?',
|
|
137
|
-
default: '0.0.0.0',
|
|
138
|
-
type: 'string',
|
|
139
|
-
configValue: 'hostname'
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
const portField = configFieldsDefs.find(
|
|
143
|
-
field => field.var === 'PORT'
|
|
144
|
-
)
|
|
145
|
-
assert.deepStrictEqual(portField, {
|
|
146
|
-
var: 'PORT',
|
|
147
|
-
label: 'Which port do you want to use?',
|
|
148
|
-
default: 3042,
|
|
149
|
-
type: 'number',
|
|
150
|
-
configValue: 'port'
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
const greetingField = configFieldsDefs.find(
|
|
154
|
-
field => field.var === 'PLT_GREETING_TEXT'
|
|
155
|
-
)
|
|
156
|
-
assert.deepStrictEqual(greetingField, {
|
|
157
|
-
var: 'PLT_GREETING_TEXT',
|
|
158
|
-
label: 'What should the stackable greeting say?',
|
|
159
|
-
default: 'Hello world!',
|
|
160
|
-
type: 'string'
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
|
|
164
|
-
test('should generate a stackable app', async (t) => {
|
|
165
|
-
const testDir = await mkdtemp(join(tmpdir(), 'stackable-'))
|
|
166
|
-
t.after(() => rm(testDir, { recursive: true, force: true }))
|
|
167
|
-
|
|
168
|
-
const generator = new Generator()
|
|
169
|
-
|
|
170
|
-
generator.setConfig({
|
|
171
|
-
serviceName: 'stackable-app',
|
|
172
|
-
targetDirectory: testDir
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
await generator.prepare()
|
|
176
|
-
await generator.writeFiles()
|
|
177
|
-
|
|
178
|
-
const files = await readdir(testDir)
|
|
179
|
-
assert.deepStrictEqual(files.sort(), [
|
|
180
|
-
'.env',
|
|
181
|
-
'.env.sample',
|
|
182
|
-
'.gitignore',
|
|
183
|
-
'global.d.ts',
|
|
184
|
-
'package.json',
|
|
185
|
-
'platformatic.json',
|
|
186
|
-
'stackable.schema.json'
|
|
187
|
-
])
|
|
188
|
-
|
|
189
|
-
const packageJson = require(join(testDir, 'package.json'))
|
|
190
|
-
assert.strictEqual(packageJson.name, 'stackable-app')
|
|
191
|
-
|
|
192
|
-
const envFile = await readFile(join(testDir, '.env'), 'utf8')
|
|
193
|
-
const envVars = envFile.split('\\n').filter(Boolean)
|
|
194
|
-
assert.deepStrictEqual(envVars.sort(), [
|
|
195
|
-
'PLT_GREETING_TEXT=Hello world!',
|
|
196
|
-
'PLT_SERVER_HOSTNAME=0.0.0.0',
|
|
197
|
-
'PLT_SERVER_LOGGER_LEVEL=info',
|
|
198
|
-
'PLT_TYPESCRIPT=false',
|
|
199
|
-
'PORT=3042'
|
|
200
|
-
])
|
|
201
|
-
|
|
202
|
-
const stackableConfig = require(join(testDir, 'platformatic.json'))
|
|
203
|
-
const stackableName = stackablePackageJson.name
|
|
204
|
-
const stackableVersion = stackablePackageJson.version
|
|
205
|
-
|
|
206
|
-
assert.deepStrictEqual(stackableConfig, {
|
|
207
|
-
$schema: './stackable.schema.json',
|
|
208
|
-
module: \`\${stackableName}@\${stackableVersion}\`,
|
|
209
|
-
server: {
|
|
210
|
-
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
211
|
-
port: '{PORT}',
|
|
212
|
-
logger: {
|
|
213
|
-
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
214
|
-
}
|
|
215
|
-
},
|
|
216
|
-
service: {
|
|
217
|
-
openapi: true
|
|
218
|
-
},
|
|
219
|
-
greeting: {
|
|
220
|
-
text: '{PLT_GREETING_TEXT}'
|
|
221
|
-
},
|
|
222
|
-
watch: true
|
|
223
|
-
})
|
|
224
|
-
})
|
|
225
|
-
`
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function getTsStackableGeneratorTestFile () {
|
|
229
|
-
return `\
|
|
230
|
-
import test from 'node:test'
|
|
231
|
-
import assert from 'node:assert'
|
|
232
|
-
import { tmpdir } from 'node:os'
|
|
233
|
-
import { join } from 'node:path'
|
|
234
|
-
import { readFile, readdir, mkdtemp, rm } from 'node:fs/promises'
|
|
235
|
-
import { readFileSync } from 'node:fs'
|
|
236
|
-
import { Generator } from '../index'
|
|
237
|
-
|
|
238
|
-
const stackablePackageJsonPath = require.resolve('../../package.json')
|
|
239
|
-
const stackablePackageJson = JSON.parse(readFileSync(stackablePackageJsonPath, 'utf8'))
|
|
240
|
-
|
|
241
|
-
test('should return a default Generator config', async () => {
|
|
242
|
-
const generator = new Generator()
|
|
243
|
-
const defaultConfig = generator.getDefaultConfig()
|
|
244
|
-
|
|
245
|
-
assert.strictEqual(defaultConfig.hostname, '0.0.0.0')
|
|
246
|
-
assert.strictEqual(defaultConfig.port, 3042)
|
|
247
|
-
assert.strictEqual(defaultConfig.greeting, 'Hello world!')
|
|
248
|
-
assert.deepStrictEqual(defaultConfig.env, {})
|
|
249
|
-
assert.deepStrictEqual(defaultConfig.dependencies, {})
|
|
250
|
-
assert.deepStrictEqual(defaultConfig.devDependencies, {})
|
|
251
|
-
})
|
|
252
|
-
|
|
253
|
-
test('should return Generator config fields definitions', async () => {
|
|
254
|
-
const generator = new Generator()
|
|
255
|
-
const configFieldsDefs = generator.getConfigFieldsDefinitions()
|
|
256
|
-
|
|
257
|
-
const hostnameField = configFieldsDefs.find(
|
|
258
|
-
field => field.var === 'PLT_SERVER_HOSTNAME'
|
|
259
|
-
)
|
|
260
|
-
assert.deepStrictEqual(hostnameField, {
|
|
261
|
-
var: 'PLT_SERVER_HOSTNAME',
|
|
262
|
-
label: 'What is the hostname?',
|
|
263
|
-
default: '0.0.0.0',
|
|
264
|
-
type: 'string',
|
|
265
|
-
configValue: 'hostname'
|
|
266
|
-
})
|
|
267
|
-
|
|
268
|
-
const portField = configFieldsDefs.find(
|
|
269
|
-
field => field.var === 'PORT'
|
|
270
|
-
)
|
|
271
|
-
assert.deepStrictEqual(portField, {
|
|
272
|
-
var: 'PORT',
|
|
273
|
-
label: 'Which port do you want to use?',
|
|
274
|
-
default: 3042,
|
|
275
|
-
type: 'number',
|
|
276
|
-
configValue: 'port'
|
|
277
|
-
})
|
|
278
|
-
|
|
279
|
-
const greetingField = configFieldsDefs.find(
|
|
280
|
-
field => field.var === 'PLT_GREETING_TEXT'
|
|
281
|
-
)
|
|
282
|
-
assert.deepStrictEqual(greetingField, {
|
|
283
|
-
var: 'PLT_GREETING_TEXT',
|
|
284
|
-
label: 'What should the stackable greeting say?',
|
|
285
|
-
default: 'Hello world!',
|
|
286
|
-
type: 'string'
|
|
287
|
-
})
|
|
288
|
-
})
|
|
289
|
-
|
|
290
|
-
test('should generate a stackable app', async (t) => {
|
|
291
|
-
const testDir = await mkdtemp(join(tmpdir(), 'stackable-'))
|
|
292
|
-
t.after(() => rm(testDir, { recursive: true, force: true }))
|
|
293
|
-
|
|
294
|
-
const generator = new Generator()
|
|
295
|
-
|
|
296
|
-
generator.setConfig({
|
|
297
|
-
serviceName: 'stackable-app',
|
|
298
|
-
targetDirectory: testDir
|
|
299
|
-
})
|
|
300
|
-
|
|
301
|
-
await generator.prepare()
|
|
302
|
-
await generator.writeFiles()
|
|
303
|
-
|
|
304
|
-
const files = await readdir(testDir)
|
|
305
|
-
assert.deepStrictEqual(files.sort(), [
|
|
306
|
-
'.env',
|
|
307
|
-
'.env.sample',
|
|
308
|
-
'.gitignore',
|
|
309
|
-
'global.d.ts',
|
|
310
|
-
'package.json',
|
|
311
|
-
'platformatic.json',
|
|
312
|
-
'stackable.schema.json'
|
|
313
|
-
])
|
|
314
|
-
|
|
315
|
-
const packageJson = require(join(testDir, 'package.json'))
|
|
316
|
-
assert.strictEqual(packageJson.name, 'stackable-app')
|
|
317
|
-
|
|
318
|
-
const envFile = await readFile(join(testDir, '.env'), 'utf8')
|
|
319
|
-
const envVars = envFile.split('\\n').filter(Boolean)
|
|
320
|
-
assert.deepStrictEqual(envVars.sort(), [
|
|
321
|
-
'PLT_GREETING_TEXT=Hello world!',
|
|
322
|
-
'PLT_SERVER_HOSTNAME=0.0.0.0',
|
|
323
|
-
'PLT_SERVER_LOGGER_LEVEL=info',
|
|
324
|
-
'PLT_TYPESCRIPT=false',
|
|
325
|
-
'PORT=3042'
|
|
326
|
-
])
|
|
327
|
-
|
|
328
|
-
const stackableConfig = require(join(testDir, 'platformatic.json'))
|
|
329
|
-
const stackableName = stackablePackageJson.name
|
|
330
|
-
const stackableVersion = stackablePackageJson.version
|
|
331
|
-
|
|
332
|
-
assert.deepStrictEqual(stackableConfig, {
|
|
333
|
-
$schema: './stackable.schema.json',
|
|
334
|
-
module: \`\${stackableName}@\${stackableVersion}\`,
|
|
335
|
-
server: {
|
|
336
|
-
hostname: '{PLT_SERVER_HOSTNAME}',
|
|
337
|
-
port: '{PORT}',
|
|
338
|
-
logger: {
|
|
339
|
-
level: '{PLT_SERVER_LOGGER_LEVEL}'
|
|
340
|
-
}
|
|
341
|
-
},
|
|
342
|
-
service: {
|
|
343
|
-
openapi: true
|
|
344
|
-
},
|
|
345
|
-
greeting: {
|
|
346
|
-
text: '{PLT_GREETING_TEXT}'
|
|
347
|
-
},
|
|
348
|
-
watch: true
|
|
349
|
-
})
|
|
350
|
-
})
|
|
351
|
-
`
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
function generateStackableTests (typescript, stackableName) {
|
|
355
|
-
if (typescript) {
|
|
356
|
-
return [
|
|
357
|
-
{
|
|
358
|
-
path: 'test',
|
|
359
|
-
file: 'index.test.ts',
|
|
360
|
-
contents: getTsStackableIndexTestFile(stackableName)
|
|
361
|
-
},
|
|
362
|
-
{
|
|
363
|
-
path: 'test',
|
|
364
|
-
file: 'schema.test.ts',
|
|
365
|
-
contents: getTsStackableSchemaTestFile(stackableName)
|
|
366
|
-
},
|
|
367
|
-
{
|
|
368
|
-
path: 'test',
|
|
369
|
-
file: 'generator.test.ts',
|
|
370
|
-
contents: getTsStackableGeneratorTestFile()
|
|
371
|
-
}
|
|
372
|
-
]
|
|
373
|
-
}
|
|
374
|
-
return [
|
|
375
|
-
{
|
|
376
|
-
path: 'test',
|
|
377
|
-
file: 'index.test.js',
|
|
378
|
-
contents: getJsStackableIndexTestFile(stackableName)
|
|
379
|
-
},
|
|
380
|
-
{
|
|
381
|
-
path: 'test',
|
|
382
|
-
file: 'schema.test.js',
|
|
383
|
-
contents: getJsStackableSchemaTestFile(stackableName)
|
|
384
|
-
},
|
|
385
|
-
{
|
|
386
|
-
path: 'test',
|
|
387
|
-
file: 'generator.test.js',
|
|
388
|
-
contents: getJsStackableGeneratorTestFile()
|
|
389
|
-
}
|
|
390
|
-
]
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
module.exports = {
|
|
394
|
-
generateStackableTests
|
|
395
|
-
}
|