@platformatic/generators 1.15.0 → 1.16.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.
@@ -6,7 +6,7 @@ export namespace BaseGenerator {
6
6
  module: string
7
7
  inquirer?: object
8
8
  }
9
-
9
+
10
10
  export type Env = {
11
11
  [key: string]: string | number | boolean
12
12
  }
@@ -14,13 +14,13 @@ export namespace BaseGenerator {
14
14
  [key: string]: string | number | undefined | null | boolean | object
15
15
  }
16
16
  type JSONValue =
17
- | string
18
- | number
19
- | boolean
20
- | { [x: string]: JSONValue }
21
- | object
22
- | Array<JSONValue>
23
-
17
+ | string
18
+ | number
19
+ | boolean
20
+ | { [x: string]: JSONValue }
21
+ | object
22
+ | Array<JSONValue>
23
+
24
24
  type Dependency = {
25
25
  [key: string]: string
26
26
  }
@@ -34,7 +34,7 @@ export namespace BaseGenerator {
34
34
  hostname?: string
35
35
  plugin?: boolean
36
36
  dependencies?: Dependency
37
- devDependencies?: Dependency
37
+ devDependencies?: Dependency
38
38
  typescript?: boolean
39
39
  initGitRepository?: boolean
40
40
  staticWorkspaceGitHubActions?: boolean
@@ -44,12 +44,12 @@ export namespace BaseGenerator {
44
44
  serviceName?: string,
45
45
  envPrefix?: string
46
46
  }
47
-
47
+
48
48
  type WhereClause = {
49
49
  before?: string
50
50
  after?: string
51
51
  }
52
-
52
+
53
53
  type GeneratorMetadata = {
54
54
  targetDirectory: string
55
55
  env: KeyValue
@@ -59,7 +59,7 @@ export namespace BaseGenerator {
59
59
  label: string
60
60
  var: string
61
61
  default: string
62
- type: 'number' | 'string' | 'boolean'
62
+ type: 'number' | 'string' | 'boolean' | 'path'
63
63
  configValue?: 'string'
64
64
 
65
65
  }
@@ -72,22 +72,22 @@ export namespace BaseGenerator {
72
72
  logger: BaseLogger
73
73
  platformaticVersion: string
74
74
  fastifyVersion: string
75
-
75
+
76
76
  config: BaseGeneratorConfig
77
77
  questions: Array<object>
78
-
78
+
79
79
  packages: PackageConfiguration[]
80
80
  constructor(opts?: BaseGeneratorOptions)
81
-
81
+
82
82
  setConfig(config?: BaseGeneratorConfig): void
83
- setEnv(env?: Env ): void
84
-
83
+ setEnv(env?: Env): void
84
+
85
85
  getDefaultConfig(): JSONValue
86
- getDefaultEnv(): Env
87
-
86
+ getDefaultEnv(): Env
87
+
88
88
  getFastifyVersion(): Promise<string>
89
89
  getPlatformaticVersion(): Promise<string>
90
-
90
+
91
91
  addPackage(pkg: PackageDefinition): void
92
92
 
93
93
  prepare(): Promise<GeneratorMetadata>
@@ -95,7 +95,7 @@ export namespace BaseGenerator {
95
95
  addQuestion(question: any, where?: WhereClause): Promise<void>
96
96
  removeQuestion(variableName: string): void
97
97
  getTSConfig(): JSONValue
98
-
98
+
99
99
  getConfigFieldsDefinitions(): ConfigFieldDefinition[]
100
100
  setConfigFields(fields: ConfigField[]): void
101
101
 
@@ -1,7 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { readFile } = require('node:fs/promises')
4
- const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, extractEnvVariablesFromText, getPackageConfigurationObject } = require('./utils')
4
+ const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, extractEnvVariablesFromText, getPackageConfigurationObject, PLT_ROOT } = require('./utils')
5
5
  const { join } = require('node:path')
6
6
  const { FileGenerator } = require('./file-generator')
7
7
  const { generateTests, generatePlugins } = require('./create-plugin')
@@ -203,16 +203,20 @@ class BaseGenerator extends FileGenerator {
203
203
  }
204
204
 
205
205
  checkEnvVariablesInConfigFile () {
206
+ const excludedEnvs = [PLT_ROOT]
206
207
  const configFileName = 'platformatic.json'
207
208
  const fileOjbect = this.getFileObject(configFileName)
208
209
  const envVars = extractEnvVariablesFromText(fileOjbect.contents)
209
210
  const envKeys = Object.keys(this.config.env)
210
211
  if (envVars.length > 0) {
211
- envVars.forEach((ev) => {
212
+ for (const ev of envVars) {
213
+ if (excludedEnvs.includes(ev)) {
214
+ continue
215
+ }
212
216
  if (!envKeys.includes(ev)) {
213
217
  throw new MissingEnvVariable(ev, configFileName)
214
218
  }
215
- })
219
+ }
216
220
  }
217
221
 
218
222
  return true
@@ -59,10 +59,11 @@ ${customization.requires || ''}
59
59
 
60
60
  async function getServer (t) {
61
61
  ${customization.pre || ''}
62
- const config = JSON.parse(await readFile(join(__dirname, '..', 'platformatic.${mod}.json'), 'utf8'))
62
+ const config = JSON.parse(await readFile(join(__dirname, '..', 'platformatic.json'), 'utf8'))
63
63
  // Add your config customizations here. For example you want to set
64
64
  // all things that are set in the config file to read from an env variable
65
- config.server.logger.level = 'warn'
65
+ config.server ||= {}
66
+ config.server.logger ||= {}
66
67
  config.watch = false
67
68
  ${customization.config || ''}
68
69
  // Add your config customizations here
@@ -125,9 +126,11 @@ type TestContext = Parameters<Exclude<testfn, undefined>>[0]
125
126
  export async function getServer (t: TestContext) {
126
127
  ${customizations.pre}
127
128
  // We go up two folder because this files executes in the dist folder
128
- const config = JSON.parse(await readFile(join(__dirname, '..', '..', 'platformatic'), 'utf8'))
129
+ const config = JSON.parse(await readFile(join(__dirname, '..', '..', 'platformatic.json'), 'utf8'))
129
130
  // Add your config customizations here. For example you want to set
130
131
  // all things that are set in the config file to read from an env variable
132
+ config.server ||= {}
133
+ config.server.logger ||= {}
131
134
  config.server.logger.level = 'warn'
132
135
  config.watch = false
133
136
  ${customizations.config}
package/lib/utils.d.ts CHANGED
@@ -3,7 +3,7 @@ type Env = {
3
3
  }
4
4
 
5
5
  export type PackageConfiguration = {
6
- type: 'number' | 'string' | 'boolean'
6
+ type: 'number' | 'string' | 'boolean' | 'path'
7
7
  path: string
8
8
  value: number | string | boolean
9
9
  }
@@ -17,4 +17,5 @@ export namespace GeneratorUtils {
17
17
  export function envObjectToString(env: Env): string
18
18
  export function extractEnvVariablesFromText(text: string): string[]
19
19
  export function getPackageConfigurationObject(config: PackageConfiguration[]): object
20
+ export const PLT_ROOT: string
20
21
  }
package/lib/utils.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  const { mkdir } = require('node:fs/promises')
4
4
  const { WrongTypeError } = require('./errors')
5
+ const { join } = require('node:path')
6
+ const PLT_ROOT = 'PLT_ROOT'
5
7
 
6
8
  async function safeMkdir (dir) {
7
9
  try {
@@ -89,6 +91,7 @@ function getPackageConfigurationObject (config, serviceName = '') {
89
91
  props.forEach((prop, idx) => {
90
92
  if (idx === props.length - 1) {
91
93
  let value
94
+ let isPath = false
92
95
  switch (param.type) {
93
96
  case 'string' :
94
97
  value = param.value.toString()
@@ -99,6 +102,10 @@ function getPackageConfigurationObject (config, serviceName = '') {
99
102
  case 'boolean':
100
103
  value = (param.value === 'true')
101
104
  break
105
+ case 'path':
106
+ value = `${join(`{${PLT_ROOT}}`, param.value)}`
107
+ isPath = true
108
+ break
102
109
  default:
103
110
  throw new WrongTypeError(param.type)
104
111
  }
@@ -106,7 +113,13 @@ function getPackageConfigurationObject (config, serviceName = '') {
106
113
  current[prop] = value
107
114
  } else {
108
115
  const key = addPrefixToString(param.name, convertServiceNameToPrefix(serviceName))
109
- current[prop] = `{${key}}`
116
+ // If it's a path, we need to add it to the env only the relative part of the path
117
+ if (isPath) {
118
+ current[prop] = `${join(`{${PLT_ROOT}}`, `{${key}}`)}`
119
+ value = param.value
120
+ } else {
121
+ current[prop] = `{${key}}`
122
+ }
110
123
  output.env[key] = value
111
124
  }
112
125
  current = output.config
@@ -128,5 +141,6 @@ module.exports = {
128
141
  envObjectToString,
129
142
  extractEnvVariablesFromText,
130
143
  safeMkdir,
131
- stripVersion
144
+ stripVersion,
145
+ PLT_ROOT
132
146
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/generators",
3
- "version": "1.15.0",
3
+ "version": "1.16.0",
4
4
  "description": "Main classes and utils for generators.",
5
5
  "main": "index.js",
6
6
  "keywords": [],
@@ -295,8 +295,8 @@ test('should generate javascript plugin, routes and tests', async (t) => {
295
295
  assert.ok(bg.getFileObject('example.js', 'plugins'))
296
296
  assert.ok(bg.getFileObject('root.js', 'routes'))
297
297
 
298
- assert.ok(bg.getFileObject('root.test.js', 'test/routes'))
299
- assert.ok(bg.getFileObject('example.test.js', 'test/plugins'))
298
+ assert.ok(bg.getFileObject('root.test.js', join('test', 'routes')))
299
+ assert.ok(bg.getFileObject('example.test.js', join('test', 'plugins')))
300
300
  })
301
301
 
302
302
  test('should generate tsConfig file and typescript files', async (t) => {
@@ -335,8 +335,8 @@ test('should generate tsConfig file and typescript files', async (t) => {
335
335
  assert.ok(bg.getFileObject('example.ts', 'plugins'))
336
336
  assert.ok(bg.getFileObject('root.ts', 'routes'))
337
337
 
338
- assert.ok(bg.getFileObject('root.test.ts', 'test/routes'))
339
- assert.ok(bg.getFileObject('example.test.ts', 'test/plugins'))
338
+ assert.ok(bg.getFileObject('root.test.ts', join('test', 'routes')))
339
+ assert.ok(bg.getFileObject('example.test.ts', join('test', 'plugins')))
340
340
  })
341
341
 
342
342
  test('should throw if preapare fails', async (t) => {
@@ -488,6 +488,85 @@ test('support packages', async (t) => {
488
488
  ]
489
489
  })
490
490
  }
491
+
492
+ {
493
+ // with relative path type but no name
494
+ const svc = new BaseGenerator({
495
+ module: '@platformatic/service'
496
+ })
497
+ svc.setConfig({
498
+ isRuntimeContext: true,
499
+ plugin: true
500
+ })
501
+ const packageDefinitions = [
502
+ {
503
+ name: '@fastify/static',
504
+ options: [
505
+ {
506
+ path: 'root',
507
+ value: 'public',
508
+ type: 'path'
509
+ }
510
+ ]
511
+ }
512
+ ]
513
+ svc.addPackage(packageDefinitions[0])
514
+ await svc.prepare()
515
+
516
+ const platformaticConfigFile = svc.getFileObject('platformatic.json')
517
+ const contents = JSON.parse(platformaticConfigFile.contents)
518
+
519
+ assert.deepEqual(contents.plugins, {
520
+ packages: [
521
+ {
522
+ name: '@fastify/static',
523
+ options: {
524
+ root: join('{PLT_ROOT}', 'public')
525
+ }
526
+ }
527
+ ]
528
+ })
529
+ }
530
+ {
531
+ // with relative path type and name
532
+ const svc = new BaseGenerator({
533
+ module: '@platformatic/service'
534
+ })
535
+ svc.setConfig({
536
+ isRuntimeContext: true,
537
+ plugin: true,
538
+ serviceName: 'my-service'
539
+ })
540
+ const packageDefinitions = [
541
+ {
542
+ name: '@fastify/static',
543
+ options: [
544
+ {
545
+ path: 'root',
546
+ value: 'public',
547
+ type: 'path',
548
+ name: 'FST_PLUGIN_STATIC_ROOT'
549
+ }
550
+ ]
551
+ }
552
+ ]
553
+ svc.addPackage(packageDefinitions[0])
554
+ await svc.prepare()
555
+
556
+ const platformaticConfigFile = svc.getFileObject('platformatic.json')
557
+ const contents = JSON.parse(platformaticConfigFile.contents)
558
+
559
+ assert.deepEqual(contents.plugins, {
560
+ packages: [
561
+ {
562
+ name: '@fastify/static',
563
+ options: {
564
+ root: join('{PLT_ROOT}', '{PLT_MY_SERVICE_FST_PLUGIN_STATIC_ROOT}')
565
+ }
566
+ }
567
+ ]
568
+ })
569
+ }
491
570
  })
492
571
 
493
572
  describe('runtime context', () => {
@@ -39,9 +39,9 @@ describe('FileGenerator', () => {
39
39
  fg.addFile({ path: '/anotherpath', file: 'foobar.txt', contents: 'foobar' })
40
40
 
41
41
  assert.deepEqual(fg.listFiles(), [
42
- 'path/helloworld.txt',
43
- 'path/foobar.txt',
44
- 'anotherpath/foobar.txt'
42
+ join('path', 'helloworld.txt'),
43
+ join('path', 'foobar.txt'),
44
+ join('anotherpath', 'foobar.txt')
45
45
  ])
46
46
  })
47
47
  test('should append file content', async (t) => {
package/test/runner.js CHANGED
@@ -2,16 +2,16 @@
2
2
 
3
3
  const { tap, spec } = require('node:test/reporters')
4
4
  const { run } = require('node:test')
5
- const path = require('node:path')
5
+ const { join } = require('node:path')
6
6
  const glob = require('glob').globSync
7
7
 
8
8
  /* eslint-disable new-cap */
9
9
  const reporter = process.stdout.isTTY ? new spec() : tap
10
10
 
11
11
  const files = [
12
- ...glob(path.join(__dirname, '*.test.js')),
13
- ...glob(path.join(__dirname, 'cli', '*.test.mjs'))
14
- ]
12
+ ...glob('*.test.{js,mjs}', { cwd: __dirname }),
13
+ ...glob('*/*.test.{js,mjs}', { cwd: __dirname })
14
+ ].map(file => join(__dirname, file))
15
15
 
16
16
  const stream = run({
17
17
  files,