@platformatic/generators 3.4.1 → 3.5.1
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/README.md +1 -1
- package/eslint.config.js +2 -2
- package/index.d.ts +158 -6
- package/index.js +3 -10
- package/index.test-d.ts +5 -11
- package/lib/base-generator.js +119 -146
- package/lib/create-gitignore.js +3 -8
- package/lib/errors.js +15 -10
- package/lib/file-generator.js +22 -19
- package/lib/import-generator.js +228 -0
- package/lib/utils.js +24 -36
- package/package.json +25 -11
- package/tsconfig.json +22 -0
- package/lib/base-generator.d.ts +0 -122
- package/lib/create-plugin.d.ts +0 -13
- package/lib/create-plugin.js +0 -255
- package/lib/file-generator.d.ts +0 -30
- package/lib/utils.d.ts +0 -21
package/lib/create-plugin.js
DELETED
|
@@ -1,255 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { join } = require('path')
|
|
4
|
-
|
|
5
|
-
const JS_PLUGIN_WITH_TYPES_SUPPORT = `\
|
|
6
|
-
/// <reference path="../global.d.ts" />
|
|
7
|
-
'use strict'
|
|
8
|
-
/** @param {import('fastify').FastifyInstance} fastify */
|
|
9
|
-
module.exports = async function (fastify, opts) {
|
|
10
|
-
fastify.decorate('example', 'foobar')
|
|
11
|
-
}
|
|
12
|
-
`
|
|
13
|
-
|
|
14
|
-
const TS_PLUGIN_WITH_TYPES_SUPPORT = `\
|
|
15
|
-
/// <reference path="../global.d.ts" />
|
|
16
|
-
import { FastifyInstance, FastifyPluginOptions } from 'fastify'
|
|
17
|
-
|
|
18
|
-
export default async function (fastify: FastifyInstance, opts: FastifyPluginOptions) {
|
|
19
|
-
fastify.decorate('example', 'foobar')
|
|
20
|
-
}
|
|
21
|
-
`
|
|
22
|
-
|
|
23
|
-
const JS_ROUTES_WITH_TYPES_SUPPORT = `\
|
|
24
|
-
/// <reference path="../global.d.ts" />
|
|
25
|
-
'use strict'
|
|
26
|
-
/** @param {import('fastify').FastifyInstance} fastify */
|
|
27
|
-
module.exports = async function (fastify, opts) {
|
|
28
|
-
fastify.get('/example', async (request, reply) => {
|
|
29
|
-
return { hello: fastify.example }
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
`
|
|
33
|
-
|
|
34
|
-
const TS_ROUTES_WITH_TYPES_SUPPORT = `\
|
|
35
|
-
/// <reference path="../global.d.ts" />
|
|
36
|
-
import { FastifyInstance, FastifyPluginOptions } from 'fastify'
|
|
37
|
-
|
|
38
|
-
declare module 'fastify' {
|
|
39
|
-
interface FastifyInstance {
|
|
40
|
-
example: string
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export default async function (fastify: FastifyInstance, opts: FastifyPluginOptions) {
|
|
45
|
-
fastify.get('/example', async (request, reply) => {
|
|
46
|
-
return { hello: fastify.example }
|
|
47
|
-
})
|
|
48
|
-
}
|
|
49
|
-
`
|
|
50
|
-
|
|
51
|
-
function testHelperJS (mod, customization = { pre: '', post: '', config: '' }) {
|
|
52
|
-
return `\
|
|
53
|
-
'use strict'
|
|
54
|
-
|
|
55
|
-
const { join } = require('node:path')
|
|
56
|
-
const { readFile } = require('node:fs/promises')
|
|
57
|
-
const { buildServer } = require('${mod}')
|
|
58
|
-
${customization.requires || ''}
|
|
59
|
-
|
|
60
|
-
async function getServer (t) {
|
|
61
|
-
${customization.pre || ''}
|
|
62
|
-
const config = JSON.parse(await readFile(join(__dirname, '..', 'platformatic.json'), 'utf8'))
|
|
63
|
-
// Add your config customizations here. For example you want to set
|
|
64
|
-
// all things that are set in the config file to read from an env variable
|
|
65
|
-
config.server ||= {}
|
|
66
|
-
config.server.logger ||= {}
|
|
67
|
-
config.watch = false
|
|
68
|
-
${customization.config || ''}
|
|
69
|
-
// Add your config customizations here
|
|
70
|
-
const server = await buildServer(config)
|
|
71
|
-
t.after(() => server.close())
|
|
72
|
-
${customization.post || ''}
|
|
73
|
-
return server
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
module.exports.getServer = getServer
|
|
77
|
-
`
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
const TEST_ROUTES_JS = `\
|
|
81
|
-
'use strict'
|
|
82
|
-
|
|
83
|
-
const test = require('node:test')
|
|
84
|
-
const assert = require('node:assert')
|
|
85
|
-
const { getServer } = require('../helper')
|
|
86
|
-
|
|
87
|
-
test('example', async (t) => {
|
|
88
|
-
const server = await getServer(t)
|
|
89
|
-
const res = await server.inject({
|
|
90
|
-
method: 'GET',
|
|
91
|
-
url: '/example'
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
assert.strictEqual(res.statusCode, 200)
|
|
95
|
-
assert.deepStrictEqual(res.json(), {
|
|
96
|
-
hello: 'foobar'
|
|
97
|
-
})
|
|
98
|
-
})
|
|
99
|
-
`
|
|
100
|
-
|
|
101
|
-
const TEST_PLUGIN_JS = `\
|
|
102
|
-
'use strict'
|
|
103
|
-
|
|
104
|
-
const test = require('node:test')
|
|
105
|
-
const assert = require('node:assert')
|
|
106
|
-
const { getServer } = require('../helper')
|
|
107
|
-
|
|
108
|
-
test('example decorator', async (t) => {
|
|
109
|
-
const server = await getServer(t)
|
|
110
|
-
|
|
111
|
-
assert.strictEqual(server.example, 'foobar')
|
|
112
|
-
})
|
|
113
|
-
`
|
|
114
|
-
|
|
115
|
-
function testHelperTS (mod, customizations = { pre: '', post: '', config: '', requires: '' }) {
|
|
116
|
-
return `\
|
|
117
|
-
import { join } from 'node:path'
|
|
118
|
-
import { readFile } from 'node:fs/promises'
|
|
119
|
-
import { buildServer } from '${mod}'
|
|
120
|
-
import { test } from 'node:test'
|
|
121
|
-
${customizations.requires}
|
|
122
|
-
|
|
123
|
-
type testfn = Parameters<typeof test>[0]
|
|
124
|
-
type TestContext = Parameters<Exclude<testfn, undefined>>[0]
|
|
125
|
-
|
|
126
|
-
export async function getServer (t: TestContext) {
|
|
127
|
-
${customizations.pre}
|
|
128
|
-
// We go up two folder because this files executes in the dist folder
|
|
129
|
-
const config = JSON.parse(await readFile(join(__dirname, '..', '..', 'platformatic.json'), 'utf8'))
|
|
130
|
-
// Add your config customizations here. For example you want to set
|
|
131
|
-
// all things that are set in the config file to read from an env variable
|
|
132
|
-
config.server ||= {}
|
|
133
|
-
config.server.logger ||= {}
|
|
134
|
-
config.server.logger.level = 'warn'
|
|
135
|
-
config.watch = false
|
|
136
|
-
${customizations.config}
|
|
137
|
-
// Add your config customizations here
|
|
138
|
-
const server = await buildServer(config)
|
|
139
|
-
t.after(() => server.close())
|
|
140
|
-
${customizations.post}
|
|
141
|
-
return server
|
|
142
|
-
}
|
|
143
|
-
`
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const TEST_ROUTES_TS = `\
|
|
147
|
-
import test from 'node:test'
|
|
148
|
-
import assert from 'node:assert'
|
|
149
|
-
import { getServer } from '../helper'
|
|
150
|
-
|
|
151
|
-
test('root', async (t) => {
|
|
152
|
-
const server = await getServer(t)
|
|
153
|
-
const res = await server.inject({
|
|
154
|
-
method: 'GET',
|
|
155
|
-
url: '/example'
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
assert.strictEqual(res.statusCode, 200)
|
|
159
|
-
assert.deepStrictEqual(res.json(), {
|
|
160
|
-
hello: 'foobar'
|
|
161
|
-
})
|
|
162
|
-
})
|
|
163
|
-
`
|
|
164
|
-
|
|
165
|
-
const TEST_PLUGIN_TS = `\
|
|
166
|
-
import test from 'node:test'
|
|
167
|
-
import assert from 'node:assert'
|
|
168
|
-
import { getServer } from '../helper'
|
|
169
|
-
|
|
170
|
-
test('example decorator', async (t) => {
|
|
171
|
-
const server = await getServer(t)
|
|
172
|
-
|
|
173
|
-
assert.strictEqual(server.example, 'foobar')
|
|
174
|
-
})
|
|
175
|
-
`
|
|
176
|
-
|
|
177
|
-
function generatePluginWithTypesSupport (typescript) {
|
|
178
|
-
const pluginTemplate = typescript
|
|
179
|
-
? TS_PLUGIN_WITH_TYPES_SUPPORT
|
|
180
|
-
: JS_PLUGIN_WITH_TYPES_SUPPORT
|
|
181
|
-
const pluginName = typescript
|
|
182
|
-
? 'example.ts'
|
|
183
|
-
: 'example.js'
|
|
184
|
-
return {
|
|
185
|
-
path: 'plugins',
|
|
186
|
-
file: pluginName,
|
|
187
|
-
contents: pluginTemplate,
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function generateRouteWithTypesSupport (typescript) {
|
|
192
|
-
const routesTemplate = typescript
|
|
193
|
-
? TS_ROUTES_WITH_TYPES_SUPPORT
|
|
194
|
-
: JS_ROUTES_WITH_TYPES_SUPPORT
|
|
195
|
-
const routesName = typescript
|
|
196
|
-
? 'root.ts'
|
|
197
|
-
: 'root.js'
|
|
198
|
-
return {
|
|
199
|
-
path: 'routes',
|
|
200
|
-
file: routesName,
|
|
201
|
-
contents: routesTemplate,
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function generateTests (typescript, mod, customizations) {
|
|
206
|
-
const output = []
|
|
207
|
-
if (typescript) {
|
|
208
|
-
output.push({
|
|
209
|
-
path: 'test',
|
|
210
|
-
file: 'helper.ts',
|
|
211
|
-
contents: testHelperTS(mod, customizations),
|
|
212
|
-
})
|
|
213
|
-
output.push({
|
|
214
|
-
path: join('test', 'plugins'),
|
|
215
|
-
file: 'example.test.ts',
|
|
216
|
-
contents: TEST_PLUGIN_TS,
|
|
217
|
-
})
|
|
218
|
-
output.push({
|
|
219
|
-
path: join('test', 'routes'),
|
|
220
|
-
file: 'root.test.ts',
|
|
221
|
-
contents: TEST_ROUTES_TS,
|
|
222
|
-
})
|
|
223
|
-
} else {
|
|
224
|
-
output.push({
|
|
225
|
-
path: 'test',
|
|
226
|
-
file: 'helper.js',
|
|
227
|
-
contents: testHelperJS(mod, customizations),
|
|
228
|
-
})
|
|
229
|
-
output.push({
|
|
230
|
-
path: join('test', 'plugins'),
|
|
231
|
-
file: 'example.test.js',
|
|
232
|
-
contents: TEST_PLUGIN_JS,
|
|
233
|
-
})
|
|
234
|
-
output.push({
|
|
235
|
-
path: join('test', 'routes'),
|
|
236
|
-
file: 'root.test.js',
|
|
237
|
-
contents: TEST_ROUTES_JS,
|
|
238
|
-
})
|
|
239
|
-
}
|
|
240
|
-
return output
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function generatePlugins (typescript) {
|
|
244
|
-
const files = []
|
|
245
|
-
files.push(generatePluginWithTypesSupport(typescript))
|
|
246
|
-
files.push(generateRouteWithTypesSupport(typescript))
|
|
247
|
-
return files
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
module.exports = {
|
|
251
|
-
generatePluginWithTypesSupport,
|
|
252
|
-
generateRouteWithTypesSupport,
|
|
253
|
-
generatePlugins,
|
|
254
|
-
generateTests,
|
|
255
|
-
}
|
package/lib/file-generator.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { BaseLogger } from 'pino'
|
|
2
|
-
|
|
3
|
-
export namespace FileGenerator {
|
|
4
|
-
export type FileGeneratorOptions = {
|
|
5
|
-
logger?: BaseLogger
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export type FileObject = {
|
|
9
|
-
path: string,
|
|
10
|
-
file: string,
|
|
11
|
-
contents: string
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export class FileGenerator {
|
|
15
|
-
files: FileObject[]
|
|
16
|
-
targetDirectory: string
|
|
17
|
-
|
|
18
|
-
constructor (opts?: FileGeneratorOptions)
|
|
19
|
-
|
|
20
|
-
setTargetDirectory (dir: string): void
|
|
21
|
-
addFile (file: FileObject): void
|
|
22
|
-
appendfile (file: FileObject): void
|
|
23
|
-
reset (): void
|
|
24
|
-
writeFiles (): Promise<void>
|
|
25
|
-
listFiles (): FileObject
|
|
26
|
-
loadFile (): Promise<FileObject>
|
|
27
|
-
getFileObject (file: string, path?: string): FileObject
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
}
|
package/lib/utils.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
type Env = {
|
|
2
|
-
[key: string]: string
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
export type PackageConfiguration = {
|
|
6
|
-
type: 'number' | 'string' | 'boolean' | 'path'
|
|
7
|
-
path: string
|
|
8
|
-
value: number | string | boolean
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export namespace GeneratorUtils {
|
|
12
|
-
export function stripVersion (version: string): string
|
|
13
|
-
export function convertServiceNameToPrefix (serviceName: string): string
|
|
14
|
-
export function envObjectToString (env: Env): string
|
|
15
|
-
export function envStringToObject (env: string): Env
|
|
16
|
-
export function extractEnvVariablesFromText (text: string): string[]
|
|
17
|
-
export function getPackageConfigurationObject (config: PackageConfiguration[]): object
|
|
18
|
-
export function flattenObject (obj: object): object
|
|
19
|
-
export function getServiceTemplateFromSchemaUrl (schemaUrl: string): string
|
|
20
|
-
export const PLT_ROOT: string
|
|
21
|
-
}
|