@platformatic/generators 1.20.2 → 1.21.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.
@@ -35,6 +35,8 @@ stackable.configManagerConfig = {
35
35
  stackable[Symbol.for('skip-override')] = true
36
36
 
37
37
  module.exports = stackable
38
+ module.exports.schema = schema
39
+ module.exports.Generator = Generator
38
40
  `
39
41
  }
40
42
 
@@ -73,6 +75,7 @@ stackable.configManagerConfig = {
73
75
  stackable[Symbol.for('skip-override')] = true
74
76
 
75
77
  export default stackable
78
+ export { Generator, schema }
76
79
  `
77
80
  }
78
81
 
@@ -89,6 +92,51 @@ declare module 'fastify' {
89
92
  platformatic: PlatformaticApp<${stackableConfigType}>
90
93
  }
91
94
  }
95
+
96
+ export { PlatformaticApp, ${stackableConfigType} }
97
+ `
98
+ }
99
+
100
+ function getJsGlobalTypesTemplateFile (stackableName) {
101
+ const stackableConfigType = pascalCase(stackableName + 'Config')
102
+
103
+ return `\
104
+ 'use strict'
105
+
106
+ function generateGlobalTypesFile (npmPackageName) {
107
+ return \`\
108
+ import { FastifyInstance } from 'fastify'
109
+ import { ${stackableConfigType}, PlatformaticApp } from '\${npmPackageName}'
110
+
111
+ declare module 'fastify' {
112
+ interface FastifyInstance {
113
+ platformatic: PlatformaticApp<${stackableConfigType}>
114
+ }
115
+ }
116
+ \`
117
+ }
118
+
119
+ module.exports = {
120
+ generateGlobalTypesFile
121
+ }
122
+ `
123
+ }
124
+
125
+ function getTsGlobalTypesTemplateFile (stackableName) {
126
+ const stackableConfigType = pascalCase(stackableName + 'Config')
127
+
128
+ return `\
129
+ export function generateGlobalTypesFile (npmPackageName: string): string {
130
+ return \`import { FastifyInstance } from 'fastify'
131
+ import { ${stackableConfigType}, PlatformaticApp } from '\${npmPackageName}'
132
+
133
+ declare module 'fastify' {
134
+ interface FastifyInstance {
135
+ platformatic: PlatformaticApp<${stackableConfigType}>
136
+ }
137
+ }
138
+ \`
139
+ }
92
140
  `
93
141
  }
94
142
 
@@ -98,8 +146,11 @@ function getJsStackableGeneratorFile (stackableName) {
98
146
  return `\
99
147
  'use strict'
100
148
 
149
+ const { join } = require('node:path')
150
+ const { readFile } = require('node:fs/promises')
101
151
  const { Generator: ServiceGenerator } = require('@platformatic/service')
102
152
  const { schema } = require('./schema')
153
+ const { generateGlobalTypesFile } = require('./templates/types')
103
154
 
104
155
  class ${stackableGeneratorType} extends ServiceGenerator {
105
156
  getDefaultConfig () {
@@ -110,10 +161,25 @@ class ${stackableGeneratorType} extends ServiceGenerator {
110
161
  return Object.assign({}, defaultBaseConfig, defaultConfig)
111
162
  }
112
163
 
164
+ getConfigFieldsDefinitions () {
165
+ const serviceConfigFieldsDefs = super.getConfigFieldsDefinitions()
166
+ return [
167
+ ...serviceConfigFieldsDefs,
168
+ {
169
+ var: 'PLT_GREETING_TEXT',
170
+ label: 'What should the stackable greeting say?',
171
+ default: 'Hello world!',
172
+ type: 'string'
173
+ }
174
+ ]
175
+ }
176
+
113
177
  async _getConfigFileContents () {
114
178
  const baseConfig = await super._getConfigFileContents()
179
+ const packageJson = await this.getStackablePackageJson()
115
180
  const config = {
116
181
  $schema: './stackable.schema.json',
182
+ module: packageJson.name,
117
183
  greeting: {
118
184
  text: '{PLT_GREETING_TEXT}'
119
185
  }
@@ -128,15 +194,48 @@ class ${stackableGeneratorType} extends ServiceGenerator {
128
194
  PLT_GREETING_TEXT: this.config.greeting ?? 'Hello world!',
129
195
  ...this.config.env
130
196
  }
197
+
198
+ const packageJson = await this.getStackablePackageJson()
199
+
200
+ this.config.dependencies = {
201
+ [packageJson.name]: \`^\${packageJson.version}\`
202
+ }
131
203
  }
132
204
 
133
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
+
134
213
  this.addFile({
135
214
  path: '',
136
215
  file: 'stackable.schema.json',
137
216
  contents: JSON.stringify(schema, null, 2)
138
217
  })
139
218
  }
219
+
220
+ async getStackablePackageJson () {
221
+ if (this._packageJson === null) {
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
+ }
140
239
  }
141
240
 
142
241
  module.exports = ${stackableGeneratorType}
@@ -148,11 +247,21 @@ function getTsStackableGeneratorFile (stackableName) {
148
247
  const stackableGeneratorType = pascalCase(stackableName + 'Generator')
149
248
 
150
249
  return `\
250
+ import { join } from 'node:path'
251
+ import { readFile } from 'node:fs/promises'
151
252
  import { Generator as ServiceGenerator } from '@platformatic/service'
152
253
  import { BaseGenerator } from '@platformatic/generators'
153
254
  import { schema } from './schema'
255
+ import { generateGlobalTypesFile } from './templates/types'
256
+
257
+ type PackageJson = {
258
+ name: string
259
+ version: string
260
+ }
154
261
 
155
262
  class ${stackableGeneratorType} extends ServiceGenerator {
263
+ private _packageJson: PackageJson | null = null
264
+
156
265
  getDefaultConfig (): BaseGenerator.JSONValue {
157
266
  const defaultBaseConfig = super.getDefaultConfig()
158
267
  const defaultConfig = {
@@ -161,10 +270,25 @@ class ${stackableGeneratorType} extends ServiceGenerator {
161
270
  return Object.assign({}, defaultBaseConfig, defaultConfig)
162
271
  }
163
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
+
164
286
  async _getConfigFileContents (): Promise<BaseGenerator.JSONValue> {
165
287
  const baseConfig = await super._getConfigFileContents()
288
+ const packageJson = await this.getStackablePackageJson()
166
289
  const config = {
167
290
  $schema: './stackable.schema.json',
291
+ module: packageJson.name,
168
292
  greeting: {
169
293
  text: '{PLT_GREETING_TEXT}'
170
294
  }
@@ -179,15 +303,48 @@ class ${stackableGeneratorType} extends ServiceGenerator {
179
303
  PLT_GREETING_TEXT: this.config.greeting ?? 'Hello world!',
180
304
  ...this.config.env
181
305
  }
306
+
307
+ const packageJson = await this.getStackablePackageJson()
308
+
309
+ this.config.dependencies = {
310
+ [packageJson.name]: \`^\${packageJson.version}\`
311
+ }
182
312
  }
183
313
 
184
314
  async _afterPrepare () {
315
+ const packageJson = await this.getStackablePackageJson()
316
+ this.addFile({
317
+ path: '',
318
+ file: 'global.d.ts',
319
+ contents: generateGlobalTypesFile(packageJson.name)
320
+ })
321
+
185
322
  this.addFile({
186
323
  path: '',
187
324
  file: 'stackable.schema.json',
188
325
  contents: JSON.stringify(schema, null, 2)
189
326
  })
190
327
  }
328
+
329
+ async getStackablePackageJson (): Promise<PackageJson> {
330
+ if (this._packageJson === null) {
331
+ const packageJsonPath = join(__dirname, '..', '..', 'package.json')
332
+ const packageJsonFile = await readFile(packageJsonPath, 'utf8')
333
+ const packageJson = JSON.parse(packageJsonFile)
334
+
335
+ if (!packageJson.name) {
336
+ throw new Error('Missing package name in package.json')
337
+ }
338
+
339
+ if (!packageJson.version) {
340
+ throw new Error('Missing package version in package.json')
341
+ }
342
+
343
+ this._packageJson = packageJson
344
+ return packageJson
345
+ }
346
+ return this._packageJson
347
+ }
191
348
  }
192
349
 
193
350
  export default ${stackableGeneratorType}
@@ -211,6 +368,7 @@ const ${schemaVarName} = {
211
368
  title: '${schemaTitle}',
212
369
  properties: {
213
370
  ...schema.schema.properties,
371
+ module: { type: 'string' },
214
372
  greeting: {
215
373
  type: 'object',
216
374
  properties: {
@@ -246,6 +404,7 @@ const ${schemaVarName} = {
246
404
  title: '${schemaTitle}',
247
405
  properties: {
248
406
  ...schema.schema.properties,
407
+ module: { type: 'string' },
249
408
  greeting: {
250
409
  type: 'object',
251
410
  properties: {
@@ -303,6 +462,11 @@ function generateStackableFiles (typescript, stackableName) {
303
462
  file: 'generator.ts',
304
463
  contents: getTsStackableGeneratorFile(stackableName)
305
464
  },
465
+ {
466
+ path: 'lib/templates',
467
+ file: 'types.ts',
468
+ contents: getTsGlobalTypesTemplateFile(stackableName)
469
+ },
306
470
  {
307
471
  path: 'lib',
308
472
  file: 'schema.ts',
@@ -331,6 +495,11 @@ function generateStackableFiles (typescript, stackableName) {
331
495
  file: 'generator.js',
332
496
  contents: getJsStackableGeneratorFile(stackableName)
333
497
  },
498
+ {
499
+ path: 'lib/templates',
500
+ file: 'types.js',
501
+ contents: getJsGlobalTypesTemplateFile(stackableName)
502
+ },
334
503
  {
335
504
  path: 'lib',
336
505
  file: 'schema.js',
@@ -226,6 +226,7 @@ class StackableGenerator extends FileGenerator {
226
226
 
227
227
  return {
228
228
  name: npmPackageName,
229
+ version: '0.0.1',
229
230
  main: 'dist/index.js',
230
231
  bin: {
231
232
  [createStackableCommand]: './dist/cli/create.js',
@@ -255,6 +256,7 @@ class StackableGenerator extends FileGenerator {
255
256
 
256
257
  return {
257
258
  name: npmPackageName,
259
+ version: '0.0.1',
258
260
  main: 'index.js',
259
261
  bin: {
260
262
  [createStackableCommand]: './cli/create.js',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/generators",
3
- "version": "1.20.2",
3
+ "version": "1.21.0",
4
4
  "description": "Main classes and utils for generators.",
5
5
  "main": "index.js",
6
6
  "keywords": [],
@@ -18,7 +18,6 @@
18
18
  "@types/inquirer": "^9.0.7",
19
19
  "borp": "^0.9.0",
20
20
  "c8": "^9.1.0",
21
- "glob": "^10.3.10",
22
21
  "snazzy": "^9.0.0",
23
22
  "standard": "^17.1.0",
24
23
  "typescript": "^5.3.3"
package/test/runner.js DELETED
@@ -1,26 +0,0 @@
1
- 'use strict'
2
-
3
- const { tap, spec } = require('node:test/reporters')
4
- const { run } = require('node:test')
5
- const { join } = require('node:path')
6
- const glob = require('glob').globSync
7
-
8
- /* eslint-disable new-cap */
9
- const reporter = process.stdout.isTTY ? new spec() : tap
10
-
11
- const files = [
12
- ...glob('*.test.{js,mjs}', { cwd: __dirname }),
13
- ...glob('*/*.test.{js,mjs}', { cwd: __dirname })
14
- ].map(file => join(__dirname, file))
15
-
16
- const stream = run({
17
- files,
18
- concurrency: 1,
19
- timeout: 30000
20
- })
21
-
22
- stream.on('test:fail', () => {
23
- process.exitCode = 1
24
- })
25
-
26
- stream.compose(reporter).pipe(process.stdout)