@platformatic/generators 3.0.0-alpha.1 → 3.0.0-alpha.3

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/index.d.ts CHANGED
@@ -1,5 +1,2 @@
1
- import { BaseGenerator } from './lib/base-generator'
2
- import { generatePlugins, generateTests } from './lib/create-plugin'
3
- import { FileGenerator } from './lib/file-generator'
4
-
5
- export { BaseGenerator, FileGenerator, generatePlugins, generateTests }
1
+ export * from './lib/base-generator'
2
+ export * from './lib/file-generator'
package/index.js CHANGED
@@ -2,12 +2,10 @@
2
2
 
3
3
  const { BaseGenerator } = require('./lib/base-generator')
4
4
  const { ImportGenerator } = require('./lib/import-generator')
5
- const { generateTests } = require('./lib/create-plugin')
6
5
  const utils = require('./lib/utils')
7
6
 
8
7
  module.exports = {
9
8
  BaseGenerator,
10
9
  ImportGenerator,
11
- generateTests,
12
10
  ...utils
13
11
  }
package/index.test-d.ts CHANGED
@@ -1,22 +1,16 @@
1
1
  import { expectAssignable } from 'tsd'
2
2
  import { BaseGenerator } from './lib/base-generator'
3
- import { generatePlugins, generateTests } from './index'
4
- import { FileGenerator } from './lib/file-generator'
5
3
 
6
4
  expectAssignable<BaseGenerator.ConfigFieldDefinition>({
7
5
  label: 'PLT_TESTING',
8
6
  var: 'testing',
9
7
  default: 'hello world',
10
8
  type: 'string',
11
- configValue: 'someConfigValue',
9
+ configValue: 'someConfigValue'
12
10
  })
13
11
 
14
12
  expectAssignable<BaseGenerator.ConfigField>({
15
13
  var: 'testing',
16
14
  configValue: 'someConfigValue',
17
- value: 'asd123',
15
+ value: 'asd123'
18
16
  })
19
-
20
- expectAssignable<FileGenerator.FileObject[]>(generatePlugins(true))
21
-
22
- expectAssignable<FileGenerator.FileObject[]>(generateTests(true, '@platformatic/service'))
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { generateDashedName } = require('@platformatic/utils')
3
+ const { generateDashedName } = require('@platformatic/foundation')
4
4
  const { readFile } = require('node:fs/promises')
5
5
  const {
6
6
  convertServiceNameToPrefix,
@@ -12,7 +12,6 @@ const {
12
12
  } = require('./utils')
13
13
  const { join } = require('node:path')
14
14
  const { FileGenerator } = require('./file-generator')
15
- const { generateTests, generatePlugins } = require('./create-plugin')
16
15
  const { PrepareError, MissingEnvVariable, ModuleNeeded } = require('./errors')
17
16
  const { generateGitignore } = require('./create-gitignore')
18
17
  const { getServiceTemplateFromSchemaUrl } = require('./utils')
@@ -42,12 +41,17 @@ class BaseGenerator extends FileGenerator {
42
41
  this.config = this.getDefaultConfig()
43
42
  this.packages = []
44
43
  this.module = opts.module
44
+ this.runtime = null
45
45
  this.runtimeConfig = opts.runtimeConfig ?? 'platformatic.json'
46
46
  if (!this.module) {
47
47
  throw ModuleNeeded()
48
48
  }
49
49
  }
50
50
 
51
+ setRuntime (runtime) {
52
+ this.runtime = runtime
53
+ }
54
+
51
55
  getDefaultConfig () {
52
56
  return {
53
57
  port: 3042,
@@ -215,24 +219,6 @@ class BaseGenerator extends FileGenerator {
215
219
 
216
220
  await this.generateEnv()
217
221
 
218
- if (this.config.typescript) {
219
- // create tsconfig.json
220
- this.addFile({
221
- path: '',
222
- file: 'tsconfig.json',
223
- contents: JSON.stringify(this.getTsConfig(), null, 2)
224
- })
225
- }
226
-
227
- if (this.config.plugin) {
228
- // create plugin
229
- this.files.push(...generatePlugins(this.config.typescript))
230
- if (this.config.tests) {
231
- // create tests
232
- this.files.push(...generateTests(this.config.typescript, this.module))
233
- }
234
- }
235
-
236
222
  this.files.push(generateGitignore())
237
223
 
238
224
  await this._afterPrepare()
@@ -274,30 +260,6 @@ class BaseGenerator extends FileGenerator {
274
260
  return true
275
261
  }
276
262
 
277
- getTsConfig () {
278
- return {
279
- compilerOptions: {
280
- module: 'commonjs',
281
- esModuleInterop: true,
282
- target: 'es2020',
283
- sourceMap: true,
284
- pretty: true,
285
- noEmitOnError: true,
286
- incremental: true,
287
- strict: true,
288
- outDir: 'dist',
289
- skipLibCheck: true
290
- },
291
- watchOptions: {
292
- watchFile: 'fixedPollingInterval',
293
- watchDirectory: 'fixedPollingInterval',
294
- fallbackPolling: 'dynamicPriority',
295
- synchronousWatchDirectory: true,
296
- excludeDirectories: ['**/node_modules', 'dist']
297
- }
298
- }
299
- }
300
-
301
263
  async prepareQuestions () {
302
264
  if (!this.config.isRuntimeContext) {
303
265
  if (!this.config.targetDirectory) {
@@ -309,19 +271,6 @@ class BaseGenerator extends FileGenerator {
309
271
  })
310
272
  }
311
273
 
312
- if (!this.config.skipTypescript) {
313
- this.questions.push({
314
- type: 'list',
315
- name: 'typescript',
316
- message: 'Do you want to use TypeScript?',
317
- default: false,
318
- choices: [
319
- { name: 'yes', value: true },
320
- { name: 'no', value: false }
321
- ]
322
- })
323
- }
324
-
325
274
  // port
326
275
  if (!this.config.skipPort) {
327
276
  this.questions.push({
@@ -405,15 +354,13 @@ class BaseGenerator extends FileGenerator {
405
354
  ...this.config.dependencies
406
355
  },
407
356
  engines: {
408
- node: '>=22.16.0'
357
+ node: '>=22.18.0'
409
358
  }
410
359
  }
411
360
 
412
361
  if (this.config.typescript) {
413
362
  const typescriptVersion = JSON.parse(await readFile(join(__dirname, '..', 'package.json'), 'utf-8'))
414
363
  .devDependencies.typescript
415
- template.scripts.clean = 'rm -fr ./dist'
416
- template.scripts.build = 'platformatic compile'
417
364
  template.devDependencies.typescript = typescriptVersion
418
365
  }
419
366
  return template
@@ -1,5 +1,5 @@
1
1
  'use strict'
2
- const { createDirectory } = require('@platformatic/utils')
2
+ const { createDirectory } = require('@platformatic/foundation')
3
3
  const { dirname, join, isAbsolute } = require('node:path')
4
4
  const { writeFile, readFile } = require('node:fs/promises')
5
5
 
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { safeRemove, findConfigurationFileRecursive } = require('@platformatic/utils')
3
+ const { safeRemove, findConfigurationFileRecursive } = require('@platformatic/foundation')
4
4
  const { BaseGenerator } = require('./base-generator')
5
5
  const { spawnSync } = require('node:child_process')
6
6
  const { stat, readFile, readdir } = require('node:fs/promises')
package/lib/utils.js CHANGED
@@ -6,7 +6,7 @@ const { request } = require('undici')
6
6
  const { setTimeout } = require('timers/promises')
7
7
  const PLT_ROOT = 'PLT_ROOT'
8
8
  const { EOL } = require('node:os')
9
- const { createDirectory } = require('@platformatic/utils')
9
+ const { createDirectory } = require('@platformatic/foundation')
10
10
 
11
11
  /**
12
12
  * Strip all extra characters from a simple semver version string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platformatic/generators",
3
- "version": "3.0.0-alpha.1",
3
+ "version": "3.0.0-alpha.3",
4
4
  "description": "Main classes and utils for generators.",
5
5
  "main": "index.js",
6
6
  "keywords": [],
@@ -11,9 +11,9 @@
11
11
  "change-case-all": "^2.1.0",
12
12
  "execa": "^9.6.0",
13
13
  "fastify": "^5.0.0",
14
- "pino": "^9.0.0",
14
+ "pino": "^9.9.0",
15
15
  "undici": "^7.0.0",
16
- "@platformatic/utils": "3.0.0-alpha.1"
16
+ "@platformatic/foundation": "3.0.0-alpha.3"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@types/inquirer": "^9.0.7",
@@ -24,6 +24,9 @@
24
24
  "tsd": "^0.32.0",
25
25
  "typescript": "^5.5.4"
26
26
  },
27
+ "engines": {
28
+ "node": ">=22.18.0"
29
+ },
27
30
  "scripts": {
28
31
  "lint": "eslint",
29
32
  "test": "pnpm run lint && borp --timeout=1200000 -C -X fixtures -X test --concurrency=1 && tsd"
@@ -1,13 +0,0 @@
1
- import { FileGenerator } from './file-generator'
2
-
3
- type HelperCustomization = {
4
- pre: string
5
- post: string
6
- config: string
7
- requires: string
8
- }
9
-
10
- export function generatePluginWithTypesSupport (typescript: boolean): FileGenerator.FileObject
11
- export function generateRouteWithTypesSupport (typescript: boolean): FileGenerator.FileObject
12
- export function generateTests (typescript: boolean, type: string, customization?: HelperCustomization): Array<FileGenerator.FileObject>
13
- export function generatePlugins (typescript: boolean): Array<FileGenerator.FileObject>
@@ -1,251 +0,0 @@
1
- 'use strict'
2
-
3
- const { join } = require('path')
4
-
5
- const JS_PLUGIN_WITH_TYPES_SUPPORT = `\
6
- 'use strict'
7
- /** @param {import('fastify').FastifyInstance} fastify */
8
- module.exports = async function (fastify, opts) {
9
- fastify.decorate('example', 'foobar')
10
- }
11
- `
12
-
13
- const TS_PLUGIN_WITH_TYPES_SUPPORT = `\
14
- import { FastifyInstance, FastifyPluginOptions } from 'fastify'
15
-
16
- export default async function (fastify: FastifyInstance, opts: FastifyPluginOptions) {
17
- fastify.decorate('example', 'foobar')
18
- }
19
- `
20
-
21
- const JS_ROUTES_WITH_TYPES_SUPPORT = `\
22
- 'use strict'
23
- /** @param {import('fastify').FastifyInstance} fastify */
24
- module.exports = async function (fastify, opts) {
25
- fastify.get('/example', async (request, reply) => {
26
- return { hello: fastify.example }
27
- })
28
- }
29
- `
30
-
31
- const TS_ROUTES_WITH_TYPES_SUPPORT = `\
32
- import { FastifyInstance, FastifyPluginOptions } from 'fastify'
33
-
34
- declare module 'fastify' {
35
- interface FastifyInstance {
36
- example: string
37
- }
38
- }
39
-
40
- export default async function (fastify: FastifyInstance, opts: FastifyPluginOptions) {
41
- fastify.get('/example', async (request, reply) => {
42
- return { hello: fastify.example }
43
- })
44
- }
45
- `
46
-
47
- function testHelperJS (mod, customization = { pre: '', post: '', config: '' }) {
48
- return `\
49
- 'use strict'
50
-
51
- const { join } = require('node:path')
52
- const { readFile } = require('node:fs/promises')
53
- const { buildServer } = require('${mod}')
54
- ${customization.requires || ''}
55
-
56
- async function getServer (t) {
57
- ${customization.pre || ''}
58
- const config = JSON.parse(await readFile(join(__dirname, '..', 'platformatic.json'), 'utf8'))
59
- // Add your config customizations here. For example you want to set
60
- // all things that are set in the config file to read from an env variable
61
- config.server ||= {}
62
- config.server.logger ||= {}
63
- config.watch = false
64
- ${customization.config || ''}
65
- // Add your config customizations here
66
- const server = await buildServer(config)
67
- t.after(() => server.close())
68
- ${customization.post || ''}
69
- return server
70
- }
71
-
72
- module.exports.getServer = getServer
73
- `
74
- }
75
-
76
- const TEST_ROUTES_JS = `\
77
- 'use strict'
78
-
79
- const test = require('node:test')
80
- const assert = require('node:assert')
81
- const { getServer } = require('../helper')
82
-
83
- test('example', async (t) => {
84
- const server = await getServer(t)
85
- const res = await server.inject({
86
- method: 'GET',
87
- url: '/example'
88
- })
89
-
90
- assert.strictEqual(res.statusCode, 200)
91
- assert.deepStrictEqual(res.json(), {
92
- hello: 'foobar'
93
- })
94
- })
95
- `
96
-
97
- const TEST_PLUGIN_JS = `\
98
- 'use strict'
99
-
100
- const test = require('node:test')
101
- const assert = require('node:assert')
102
- const { getServer } = require('../helper')
103
-
104
- test('example decorator', async (t) => {
105
- const server = await getServer(t)
106
-
107
- assert.strictEqual(server.example, 'foobar')
108
- })
109
- `
110
-
111
- function testHelperTS (mod, customizations = { pre: '', post: '', config: '', requires: '' }) {
112
- return `\
113
- import { join } from 'node:path'
114
- import { readFile } from 'node:fs/promises'
115
- import { buildServer } from '${mod}'
116
- import { test } from 'node:test'
117
- ${customizations.requires}
118
-
119
- type testfn = Parameters<typeof test>[0]
120
- type TestContext = Parameters<Exclude<testfn, undefined>>[0]
121
-
122
- export async function getServer (t: TestContext) {
123
- ${customizations.pre}
124
- // We go up two folder because this files executes in the dist folder
125
- const config = JSON.parse(await readFile(join(__dirname, '..', '..', 'platformatic.json'), 'utf8'))
126
- // Add your config customizations here. For example you want to set
127
- // all things that are set in the config file to read from an env variable
128
- config.server ||= {}
129
- config.server.logger ||= {}
130
- config.server.logger.level = 'warn'
131
- config.watch = false
132
- ${customizations.config}
133
- // Add your config customizations here
134
- const server = await buildServer(config)
135
- t.after(() => server.close())
136
- ${customizations.post}
137
- return server
138
- }
139
- `
140
- }
141
-
142
- const TEST_ROUTES_TS = `\
143
- import test from 'node:test'
144
- import assert from 'node:assert'
145
- import { getServer } from '../helper'
146
-
147
- test('root', async (t) => {
148
- const server = await getServer(t)
149
- const res = await server.inject({
150
- method: 'GET',
151
- url: '/example'
152
- })
153
-
154
- assert.strictEqual(res.statusCode, 200)
155
- assert.deepStrictEqual(res.json(), {
156
- hello: 'foobar'
157
- })
158
- })
159
- `
160
-
161
- const TEST_PLUGIN_TS = `\
162
- import test from 'node:test'
163
- import assert from 'node:assert'
164
- import { getServer } from '../helper'
165
-
166
- test('example decorator', async (t) => {
167
- const server = await getServer(t)
168
-
169
- assert.strictEqual(server.example, 'foobar')
170
- })
171
- `
172
-
173
- function generatePluginWithTypesSupport (typescript) {
174
- const pluginTemplate = typescript
175
- ? TS_PLUGIN_WITH_TYPES_SUPPORT
176
- : JS_PLUGIN_WITH_TYPES_SUPPORT
177
- const pluginName = typescript
178
- ? 'example.ts'
179
- : 'example.js'
180
- return {
181
- path: 'plugins',
182
- file: pluginName,
183
- contents: pluginTemplate,
184
- }
185
- }
186
-
187
- function generateRouteWithTypesSupport (typescript) {
188
- const routesTemplate = typescript
189
- ? TS_ROUTES_WITH_TYPES_SUPPORT
190
- : JS_ROUTES_WITH_TYPES_SUPPORT
191
- const routesName = typescript
192
- ? 'root.ts'
193
- : 'root.js'
194
- return {
195
- path: 'routes',
196
- file: routesName,
197
- contents: routesTemplate,
198
- }
199
- }
200
-
201
- function generateTests (typescript, mod, customizations) {
202
- const output = []
203
- if (typescript) {
204
- output.push({
205
- path: 'test',
206
- file: 'helper.ts',
207
- contents: testHelperTS(mod, customizations),
208
- })
209
- output.push({
210
- path: join('test', 'plugins'),
211
- file: 'example.test.ts',
212
- contents: TEST_PLUGIN_TS,
213
- })
214
- output.push({
215
- path: join('test', 'routes'),
216
- file: 'root.test.ts',
217
- contents: TEST_ROUTES_TS,
218
- })
219
- } else {
220
- output.push({
221
- path: 'test',
222
- file: 'helper.js',
223
- contents: testHelperJS(mod, customizations),
224
- })
225
- output.push({
226
- path: join('test', 'plugins'),
227
- file: 'example.test.js',
228
- contents: TEST_PLUGIN_JS,
229
- })
230
- output.push({
231
- path: join('test', 'routes'),
232
- file: 'root.test.js',
233
- contents: TEST_ROUTES_JS,
234
- })
235
- }
236
- return output
237
- }
238
-
239
- function generatePlugins (typescript) {
240
- const files = []
241
- files.push(generatePluginWithTypesSupport(typescript))
242
- files.push(generateRouteWithTypesSupport(typescript))
243
- return files
244
- }
245
-
246
- module.exports = {
247
- generatePluginWithTypesSupport,
248
- generateRouteWithTypesSupport,
249
- generatePlugins,
250
- generateTests,
251
- }