@platformatic/generators 2.0.0-alpha.8 → 2.0.0

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,518 +0,0 @@
1
- 'use strict'
2
-
3
- const { pascalCase, camelCase, capitalCase, kebabCase } = require('change-case-all')
4
-
5
- function getJsStackableIndexFile (stackableName) {
6
- return `\
7
- 'use strict'
8
-
9
- const { platformaticService } = require('@platformatic/service')
10
- const { schema } = require('./lib/schema')
11
- const { Generator } = require('./lib/generator')
12
- const { version } = require('./package.json')
13
-
14
- async function stackable (fastify, opts) {
15
- await fastify.register(platformaticService, opts)
16
- await fastify.register(require('./plugins/example'), opts)
17
- }
18
- stackable.configType = '${kebabCase(stackableName + '-app')}'
19
- stackable.schema = schema
20
- stackable.Generator = Generator
21
- stackable.configManagerConfig = {
22
- version,
23
- schema,
24
- allowToWatch: ['.env'],
25
- schemaOptions: {
26
- useDefaults: true,
27
- coerceTypes: true,
28
- allErrors: true,
29
- strict: false
30
- },
31
- async transformConfig () {}
32
- }
33
-
34
- // break Fastify encapsulation
35
- stackable[Symbol.for('skip-override')] = true
36
-
37
- module.exports = stackable
38
- `
39
- }
40
-
41
- function getTsStackableIndexFile (stackableName) {
42
- const stackableConfigType = pascalCase(stackableName + 'Config')
43
-
44
- return `\
45
- import { platformaticService, Stackable } from '@platformatic/service'
46
- import { schema } from './lib/schema'
47
- import { Generator } from './lib/generator'
48
- import { ${stackableConfigType} } from './config'
49
- import { readFileSync } from 'node:fs'
50
-
51
- const { version } = JSON.parse(readFileSync('package.json', 'utf8'))
52
-
53
- const stackable: Stackable<${stackableConfigType}> = async function (fastify, opts) {
54
- await fastify.register(platformaticService, opts)
55
- await fastify.register(require('./plugins/example'), opts)
56
- }
57
-
58
- stackable.configType = '${kebabCase(stackableName + '-app')}'
59
- stackable.schema = schema
60
- stackable.Generator = Generator
61
- stackable.configManagerConfig = {
62
- version,
63
- schema,
64
- allowToWatch: ['.env'],
65
- schemaOptions: {
66
- useDefaults: true,
67
- coerceTypes: true,
68
- allErrors: true,
69
- strict: false
70
- },
71
- async transformConfig () {}
72
- }
73
-
74
- // break Fastify encapsulation
75
- // @ts-ignore
76
- stackable[Symbol.for('skip-override')] = true
77
-
78
- export default stackable
79
- export { Generator, schema }
80
- `
81
- }
82
-
83
- function getStackableIndexTypesFile (stackableName) {
84
- const stackableConfigType = pascalCase(stackableName + 'Config')
85
-
86
- return `\
87
- import { FastifyInstance } from 'fastify'
88
- import { PlatformaticApp } from '@platformatic/service'
89
- import { ${stackableConfigType} } from './config'
90
-
91
- declare module 'fastify' {
92
- interface FastifyInstance {
93
- platformatic: PlatformaticApp<${stackableConfigType}>
94
- }
95
- }
96
-
97
- export { PlatformaticApp, ${stackableConfigType} }
98
- `
99
- }
100
-
101
- function getJsGlobalTypesTemplateFile (stackableName) {
102
- const stackableConfigType = pascalCase(stackableName + 'Config')
103
-
104
- return `\
105
- 'use strict'
106
-
107
- function generateGlobalTypesFile (npmPackageName) {
108
- return \`\
109
- import { FastifyInstance } from 'fastify'
110
- import { ${stackableConfigType}, PlatformaticApp } from '\${npmPackageName}'
111
-
112
- declare module 'fastify' {
113
- interface FastifyInstance {
114
- platformatic: PlatformaticApp<${stackableConfigType}>
115
- }
116
- }
117
- \`
118
- }
119
-
120
- module.exports = {
121
- generateGlobalTypesFile
122
- }
123
- `
124
- }
125
-
126
- function getTsGlobalTypesTemplateFile (stackableName) {
127
- const stackableConfigType = pascalCase(stackableName + 'Config')
128
-
129
- return `\
130
- export function generateGlobalTypesFile (npmPackageName: string): string {
131
- return \`import { FastifyInstance } from 'fastify'
132
- import { ${stackableConfigType}, PlatformaticApp } from '\${npmPackageName}'
133
-
134
- declare module 'fastify' {
135
- interface FastifyInstance {
136
- platformatic: PlatformaticApp<${stackableConfigType}>
137
- }
138
- }
139
- \`
140
- }
141
- `
142
- }
143
-
144
- function getJsStackableGeneratorFile (stackableName) {
145
- const stackableGeneratorType = pascalCase(stackableName + 'Generator')
146
-
147
- return `\
148
- 'use strict'
149
-
150
- const { join } = require('node:path')
151
- const { readFile } = require('node:fs/promises')
152
- const { Generator: ServiceGenerator } = require('@platformatic/service')
153
- const { schema } = require('./schema')
154
- const { generateGlobalTypesFile } = require('./templates/types')
155
-
156
- class ${stackableGeneratorType} extends ServiceGenerator {
157
- getDefaultConfig () {
158
- const defaultBaseConfig = super.getDefaultConfig()
159
- const defaultConfig = {
160
- greeting: 'Hello world!'
161
- }
162
- return Object.assign({}, defaultBaseConfig, defaultConfig)
163
- }
164
-
165
- getConfigFieldsDefinitions () {
166
- const serviceConfigFieldsDefs = super.getConfigFieldsDefinitions()
167
- return [
168
- ...serviceConfigFieldsDefs,
169
- {
170
- var: 'PLT_GREETING_TEXT',
171
- label: 'What should the stackable greeting say?',
172
- default: 'Hello world!',
173
- type: 'string'
174
- }
175
- ]
176
- }
177
-
178
- async _getConfigFileContents () {
179
- const baseConfig = await super._getConfigFileContents()
180
- const packageJson = await this.getStackablePackageJson()
181
- const config = {
182
- $schema: './stackable.schema.json',
183
- module: \`\${packageJson.name}@\${packageJson.version}\`,
184
- greeting: {
185
- text: \`{\${this.getEnvVarName('PLT_GREETING_TEXT')}}\`
186
- }
187
- }
188
- return Object.assign({}, baseConfig, config)
189
- }
190
-
191
- async _beforePrepare () {
192
- super._beforePrepare()
193
-
194
- this.addEnvVars({
195
- PLT_GREETING_TEXT: this.config.greeting ?? 'Hello world!'
196
- }, { overwrite: false, default: true })
197
-
198
- const packageJson = await this.getStackablePackageJson()
199
-
200
- this.config.dependencies = {
201
- [packageJson.name]: \`^\${packageJson.version}\`
202
- }
203
- }
204
-
205
- async _afterPrepare () {
206
- const packageJson = await this.getStackablePackageJson()
207
- this.addFile({
208
- path: '',
209
- file: 'global.d.ts',
210
- contents: generateGlobalTypesFile(packageJson.name)
211
- })
212
-
213
- this.addFile({
214
- path: '',
215
- file: 'stackable.schema.json',
216
- contents: JSON.stringify(schema, null, 2)
217
- })
218
- }
219
-
220
- async getStackablePackageJson () {
221
- if (!this._packageJson) {
222
- const packageJsonPath = join(__dirname, '..', 'package.json')
223
- const packageJsonFile = await readFile(packageJsonPath, 'utf8')
224
- const packageJson = JSON.parse(packageJsonFile)
225
-
226
- if (!packageJson.name) {
227
- throw new Error('Missing package name in package.json')
228
- }
229
-
230
- if (!packageJson.version) {
231
- throw new Error('Missing package version in package.json')
232
- }
233
-
234
- this._packageJson = packageJson
235
- return packageJson
236
- }
237
- return this._packageJson
238
- }
239
- }
240
-
241
- module.exports = ${stackableGeneratorType}
242
- module.exports.Generator = ${stackableGeneratorType}
243
- `
244
- }
245
-
246
- function getTsStackableGeneratorFile (stackableName) {
247
- const stackableGeneratorType = pascalCase(stackableName + 'Generator')
248
-
249
- return `\
250
- import { join } from 'node:path'
251
- import { readFile } from 'node:fs/promises'
252
- import { Generator as ServiceGenerator } from '@platformatic/service'
253
- import { BaseGenerator } from '@platformatic/generators'
254
- import { schema } from './schema'
255
- import { generateGlobalTypesFile } from './templates/types'
256
-
257
- type PackageJson = {
258
- name: string
259
- version: string
260
- }
261
-
262
- class ${stackableGeneratorType} extends ServiceGenerator {
263
- private _packageJson: PackageJson | null = null
264
-
265
- getDefaultConfig (): { [x: string]: BaseGenerator.JSONValue } {
266
- const defaultBaseConfig = super.getDefaultConfig()
267
- const defaultConfig = {
268
- greeting: 'Hello world!'
269
- }
270
- return Object.assign({}, defaultBaseConfig, defaultConfig)
271
- }
272
-
273
- getConfigFieldsDefinitions (): BaseGenerator.ConfigFieldDefinition[] {
274
- const serviceConfigFieldsDefs = super.getConfigFieldsDefinitions()
275
- return [
276
- ...serviceConfigFieldsDefs,
277
- {
278
- var: 'PLT_GREETING_TEXT',
279
- label: 'What should the stackable greeting say?',
280
- default: 'Hello world!',
281
- type: 'string'
282
- }
283
- ]
284
- }
285
-
286
- async _getConfigFileContents (): Promise<{ [x: string]: BaseGenerator.JSONValue }> {
287
- const baseConfig = await super._getConfigFileContents()
288
- const packageJson = await this.getStackablePackageJson()
289
- const config = {
290
- $schema: './stackable.schema.json',
291
- module: \`\${packageJson.name}@\${packageJson.version}\`,
292
- greeting: {
293
- text: \`{\${this.getEnvVarName('PLT_GREETING_TEXT')}}\`
294
- }
295
- }
296
- return Object.assign({}, baseConfig, config)
297
- }
298
-
299
- async _beforePrepare (): Promise<void> {
300
- await super._beforePrepare()
301
-
302
- this.addEnvVars({
303
- PLT_GREETING_TEXT: this.config.greeting ?? 'Hello world!'
304
- }, { overwrite: false, default: true })
305
-
306
- const packageJson = await this.getStackablePackageJson()
307
-
308
- this.config.dependencies = {
309
- [packageJson.name]: \`^\${packageJson.version}\`
310
- }
311
- }
312
-
313
- async _afterPrepare (): Promise<void> {
314
- const packageJson = await this.getStackablePackageJson()
315
- this.addFile({
316
- path: '',
317
- file: 'global.d.ts',
318
- contents: generateGlobalTypesFile(packageJson.name)
319
- })
320
-
321
- this.addFile({
322
- path: '',
323
- file: 'stackable.schema.json',
324
- contents: JSON.stringify(schema, null, 2)
325
- })
326
- }
327
-
328
- async getStackablePackageJson (): Promise<PackageJson> {
329
- if (!this._packageJson) {
330
- const packageJsonPath = join(__dirname, '..', '..', 'package.json')
331
- const packageJsonFile = await readFile(packageJsonPath, 'utf8')
332
- const packageJson: Partial<PackageJson> = JSON.parse(packageJsonFile)
333
-
334
- if (packageJson.name === undefined || packageJson.name === null) {
335
- throw new Error('Missing package name in package.json')
336
- }
337
-
338
- if (packageJson.version === undefined || packageJson.version === null) {
339
- throw new Error('Missing package version in package.json')
340
- }
341
-
342
- this._packageJson = packageJson as PackageJson
343
- return packageJson as PackageJson
344
- }
345
- return this._packageJson
346
- }
347
- }
348
-
349
- export default ${stackableGeneratorType}
350
- export { ${stackableGeneratorType} as Generator }
351
- `
352
- }
353
-
354
- function getJsStackableSchemaFile (stackableName) {
355
- const schemaId = kebabCase(stackableName)
356
- const schemaTitle = capitalCase(stackableName + 'Config')
357
- const schemaVarName = camelCase(stackableName + 'Schema')
358
-
359
- return `\
360
- 'use strict'
361
-
362
- const { schema } = require('@platformatic/service')
363
- const { version } = require('../package.json')
364
-
365
- const ${schemaVarName} = {
366
- ...schema,
367
- $id: '${schemaId}',
368
- title: '${schemaTitle}',
369
- version,
370
- properties: {
371
- ...schema.properties,
372
- module: { type: 'string' },
373
- greeting: {
374
- type: 'object',
375
- properties: {
376
- text: {
377
- type: 'string'
378
- }
379
- },
380
- required: ['text'],
381
- additionalProperties: false
382
- }
383
- }
384
- }
385
-
386
- module.exports.schema = ${schemaVarName}
387
-
388
- if (require.main === module) {
389
- console.log(JSON.stringify(${schemaVarName}, null, 2))
390
- }
391
- `
392
- }
393
-
394
- function getTsStackableSchemaFile (stackableName) {
395
- const schemaId = kebabCase(stackableName)
396
- const schemaTitle = capitalCase(stackableName + 'Config')
397
- const schemaVarName = camelCase(stackableName + 'Schema')
398
-
399
- return `\
400
- import { schema } from '@platformatic/service'
401
- import { readFileSync } from 'node:fs'
402
-
403
- const { version } = JSON.parse(readFileSync('package.json', 'utf8'))
404
-
405
- const ${schemaVarName} = {
406
- ...schema,
407
- $id: '${schemaId}',
408
- title: '${schemaTitle}',
409
- version,
410
- properties: {
411
- ...schema.properties,
412
- module: { type: 'string' },
413
- greeting: {
414
- type: 'object',
415
- properties: {
416
- text: {
417
- type: 'string'
418
- }
419
- },
420
- required: ['text'],
421
- additionalProperties: false
422
- }
423
- }
424
- }
425
-
426
- export { ${schemaVarName} as schema }
427
-
428
- if (require.main === module) {
429
- console.log(JSON.stringify(${schemaVarName}, null, 2))
430
- }
431
- `
432
- }
433
-
434
- function getStackableConfigTypesFile (stackableName) {
435
- const stackableConfigType = pascalCase(stackableName + 'Config')
436
-
437
- return `\
438
- // Use npm run build:config to generate this file from the Stackable schema
439
- export interface ${stackableConfigType} {
440
- greeting?: {
441
- text: string;
442
- };
443
- }
444
- `
445
- }
446
-
447
- function generateStackableFiles (typescript, stackableName) {
448
- if (typescript) {
449
- return [
450
- {
451
- path: '',
452
- file: 'index.ts',
453
- contents: getTsStackableIndexFile(stackableName),
454
- },
455
- {
456
- path: '',
457
- file: 'index.d.ts',
458
- contents: getStackableIndexTypesFile(stackableName),
459
- },
460
- {
461
- path: '',
462
- file: 'config.d.ts',
463
- contents: getStackableConfigTypesFile(stackableName),
464
- },
465
- {
466
- path: 'lib',
467
- file: 'generator.ts',
468
- contents: getTsStackableGeneratorFile(stackableName),
469
- },
470
- {
471
- path: 'lib/templates',
472
- file: 'types.ts',
473
- contents: getTsGlobalTypesTemplateFile(stackableName),
474
- },
475
- {
476
- path: 'lib',
477
- file: 'schema.ts',
478
- contents: getTsStackableSchemaFile(stackableName),
479
- },
480
- ]
481
- }
482
- return [
483
- {
484
- path: '',
485
- file: 'index.js',
486
- contents: getJsStackableIndexFile(stackableName),
487
- },
488
- {
489
- path: '',
490
- file: 'index.d.ts',
491
- contents: getStackableIndexTypesFile(stackableName),
492
- },
493
- {
494
- path: '',
495
- file: 'config.d.ts',
496
- contents: getStackableConfigTypesFile(stackableName),
497
- },
498
- {
499
- path: 'lib',
500
- file: 'generator.js',
501
- contents: getJsStackableGeneratorFile(stackableName),
502
- },
503
- {
504
- path: 'lib/templates',
505
- file: 'types.js',
506
- contents: getJsGlobalTypesTemplateFile(stackableName),
507
- },
508
- {
509
- path: 'lib',
510
- file: 'schema.js',
511
- contents: getJsStackableSchemaFile(stackableName),
512
- },
513
- ]
514
- }
515
-
516
- module.exports = {
517
- generateStackableFiles,
518
- }
@@ -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
- }