@platformatic/generators 2.0.0-alpha.2 → 2.0.0-alpha.21
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/eslint.config.js +3 -0
- package/index.d.ts +1 -1
- package/index.js +1 -3
- package/index.test-d.ts +4 -4
- package/lib/base-generator.d.ts +25 -25
- package/lib/base-generator.js +31 -30
- package/lib/create-gitignore.js +2 -2
- package/lib/create-plugin.d.ts +4 -4
- package/lib/create-plugin.js +9 -9
- package/lib/errors.js +1 -1
- package/lib/file-generator.d.ts +14 -14
- package/lib/file-generator.js +4 -4
- package/lib/utils.d.ts +8 -9
- package/lib/utils.js +18 -23
- package/package.json +12 -11
- package/lib/create-stackable-cli.js +0 -161
- package/lib/create-stackable-files.js +0 -518
- package/lib/create-stackable-plugin.js +0 -49
- package/lib/create-stackable-tests.js +0 -395
- package/lib/stackable-generator.js +0 -317
- package/test/base-generator.test.js +0 -973
- package/test/file-generator.test.js +0 -140
- package/test/fixtures/sample-runtime/.env +0 -9
- package/test/fixtures/sample-runtime/platformatic.json +0 -18
- package/test/fixtures/sample-runtime/services/no-plugin/platformatic.json +0 -7
- package/test/fixtures/sample-runtime/services/rival/platformatic.json +0 -33
- package/test/helpers.js +0 -64
- package/test/stackable-generator.test.js +0 -114
- package/test/utils.test.js +0 -223
|
@@ -1,973 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { readFile, rm, cp } = require('node:fs/promises')
|
|
4
|
-
const { test, after, afterEach, describe } = require('node:test')
|
|
5
|
-
const assert = require('node:assert')
|
|
6
|
-
const { join } = require('node:path')
|
|
7
|
-
|
|
8
|
-
const { fakeLogger, getTempDir, moveToTmpdir, mockNpmJsRequestForPkgs, mockAgent } = require('./helpers')
|
|
9
|
-
const { BaseGenerator } = require('../lib/base-generator')
|
|
10
|
-
const { convertServiceNameToPrefix } = require('../lib/utils')
|
|
11
|
-
|
|
12
|
-
afterEach(async () => {
|
|
13
|
-
try {
|
|
14
|
-
await rm(join(__dirname, 'tmp'), { recursive: true })
|
|
15
|
-
} catch (err) {
|
|
16
|
-
// do nothing
|
|
17
|
-
}
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
test('should write file and dirs', async (t) => {
|
|
21
|
-
const dir = await getTempDir()
|
|
22
|
-
const gen = new BaseGenerator({
|
|
23
|
-
logger: fakeLogger,
|
|
24
|
-
module: '@platformatic/service'
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
gen.setConfig({
|
|
28
|
-
targetDirectory: dir,
|
|
29
|
-
serviceName: 'test-service'
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
await gen.run()
|
|
33
|
-
// check files are created
|
|
34
|
-
const packageJson = JSON.parse(await readFile(join(dir, 'package.json'), 'utf8'))
|
|
35
|
-
assert.ok(packageJson.scripts)
|
|
36
|
-
assert.ok(packageJson.dependencies)
|
|
37
|
-
assert.equal(packageJson.dependencies.platformatic, undefined)
|
|
38
|
-
assert.ok(packageJson.engines)
|
|
39
|
-
|
|
40
|
-
assert.equal(packageJson.name, 'test-service')
|
|
41
|
-
|
|
42
|
-
const configFile = JSON.parse(await readFile(join(dir, 'platformatic.json'), 'utf8'))
|
|
43
|
-
assert.deepEqual(configFile, {})
|
|
44
|
-
|
|
45
|
-
const gitignore = await readFile(join(dir, '.gitignore'), 'utf8')
|
|
46
|
-
assert.ok(gitignore.length > 0) // file is created and not empty
|
|
47
|
-
})
|
|
48
|
-
|
|
49
|
-
test('extended class should generate config', async (t) => {
|
|
50
|
-
class ServiceClass extends BaseGenerator {
|
|
51
|
-
constructor (opts) {
|
|
52
|
-
super({
|
|
53
|
-
...opts,
|
|
54
|
-
module: '@platformatic/service'
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
async _getConfigFileContents () {
|
|
59
|
-
// Implement when extending this class
|
|
60
|
-
return {
|
|
61
|
-
foo: 'bar'
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const svc = new ServiceClass({
|
|
67
|
-
logger: fakeLogger
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
await svc.prepare()
|
|
71
|
-
|
|
72
|
-
const configFile = svc.files[1]
|
|
73
|
-
assert.deepEqual(configFile, {
|
|
74
|
-
path: '',
|
|
75
|
-
file: 'platformatic.json',
|
|
76
|
-
contents: JSON.stringify({ foo: 'bar' }, null, 2),
|
|
77
|
-
options: {}
|
|
78
|
-
})
|
|
79
|
-
})
|
|
80
|
-
|
|
81
|
-
test('setConfig', async (t) => {
|
|
82
|
-
const bg = new BaseGenerator({
|
|
83
|
-
module: '@platformatic/service'
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
// should init the default config
|
|
87
|
-
await bg.prepare()
|
|
88
|
-
|
|
89
|
-
assert.deepEqual(bg.config, {
|
|
90
|
-
port: 3042,
|
|
91
|
-
hostname: '0.0.0.0',
|
|
92
|
-
plugin: false,
|
|
93
|
-
typescript: false,
|
|
94
|
-
initGitRepository: false,
|
|
95
|
-
env: {},
|
|
96
|
-
defaultEnv: {},
|
|
97
|
-
dependencies: {},
|
|
98
|
-
devDependencies: {},
|
|
99
|
-
isRuntimeContext: false,
|
|
100
|
-
serviceName: '',
|
|
101
|
-
envPrefix: '',
|
|
102
|
-
tests: false,
|
|
103
|
-
isUpdating: false
|
|
104
|
-
})
|
|
105
|
-
|
|
106
|
-
// should not have undefined properties
|
|
107
|
-
Object.entries(bg.config).forEach((kv) => {
|
|
108
|
-
assert.notStrictEqual(undefined, kv[1])
|
|
109
|
-
})
|
|
110
|
-
|
|
111
|
-
// partial config with defaults
|
|
112
|
-
bg.setConfig({
|
|
113
|
-
port: 3084
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
assert.deepEqual(bg.config, {
|
|
117
|
-
port: 3084, // this is the only custom value
|
|
118
|
-
hostname: '0.0.0.0',
|
|
119
|
-
plugin: false,
|
|
120
|
-
typescript: false,
|
|
121
|
-
initGitRepository: false,
|
|
122
|
-
env: {},
|
|
123
|
-
defaultEnv: {},
|
|
124
|
-
dependencies: {},
|
|
125
|
-
devDependencies: {},
|
|
126
|
-
isRuntimeContext: false,
|
|
127
|
-
serviceName: '',
|
|
128
|
-
envPrefix: '',
|
|
129
|
-
tests: false,
|
|
130
|
-
isUpdating: false
|
|
131
|
-
})
|
|
132
|
-
|
|
133
|
-
// reset config to defaults
|
|
134
|
-
bg.setConfig()
|
|
135
|
-
assert.deepEqual(bg.config, {
|
|
136
|
-
port: 3042,
|
|
137
|
-
hostname: '0.0.0.0',
|
|
138
|
-
plugin: false,
|
|
139
|
-
typescript: false,
|
|
140
|
-
initGitRepository: false,
|
|
141
|
-
env: {},
|
|
142
|
-
defaultEnv: {},
|
|
143
|
-
dependencies: {},
|
|
144
|
-
devDependencies: {},
|
|
145
|
-
isRuntimeContext: false,
|
|
146
|
-
serviceName: '',
|
|
147
|
-
envPrefix: '',
|
|
148
|
-
tests: false,
|
|
149
|
-
isUpdating: false
|
|
150
|
-
})
|
|
151
|
-
|
|
152
|
-
// update only some fields
|
|
153
|
-
bg.setConfig({
|
|
154
|
-
hostname: '123.123.123.123',
|
|
155
|
-
port: 3000
|
|
156
|
-
})
|
|
157
|
-
|
|
158
|
-
bg.setConfig({
|
|
159
|
-
port: 1234
|
|
160
|
-
})
|
|
161
|
-
|
|
162
|
-
assert.deepEqual(bg.config, {
|
|
163
|
-
port: 1234,
|
|
164
|
-
hostname: '123.123.123.123',
|
|
165
|
-
plugin: false,
|
|
166
|
-
typescript: false,
|
|
167
|
-
initGitRepository: false,
|
|
168
|
-
env: {},
|
|
169
|
-
defaultEnv: {},
|
|
170
|
-
dependencies: {},
|
|
171
|
-
devDependencies: {},
|
|
172
|
-
isRuntimeContext: false,
|
|
173
|
-
serviceName: '',
|
|
174
|
-
envPrefix: '',
|
|
175
|
-
tests: false,
|
|
176
|
-
isUpdating: false
|
|
177
|
-
})
|
|
178
|
-
})
|
|
179
|
-
|
|
180
|
-
test('should append env values', async (t) => {
|
|
181
|
-
const bg = new BaseGenerator({
|
|
182
|
-
module: '@platformatic/service'
|
|
183
|
-
})
|
|
184
|
-
// partial config with defaults
|
|
185
|
-
bg.setConfig({
|
|
186
|
-
env: {
|
|
187
|
-
FOO: 'bar'
|
|
188
|
-
}
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
await bg.prepare()
|
|
192
|
-
const dotEnvFile = bg.getFileObject('.env')
|
|
193
|
-
assert.equal(dotEnvFile.contents.trim(), 'FOO=bar')
|
|
194
|
-
|
|
195
|
-
const dotEnvSampleFile = bg.getFileObject('.env.sample')
|
|
196
|
-
assert.equal(dotEnvSampleFile.contents.trim(), 'FOO=')
|
|
197
|
-
})
|
|
198
|
-
|
|
199
|
-
test('should add a default env var to the .env.sample config', async (t) => {
|
|
200
|
-
const bg = new BaseGenerator({
|
|
201
|
-
module: '@platformatic/service'
|
|
202
|
-
})
|
|
203
|
-
// partial config with defaults
|
|
204
|
-
bg.setConfig({
|
|
205
|
-
env: {
|
|
206
|
-
FOO: 'bar'
|
|
207
|
-
}
|
|
208
|
-
})
|
|
209
|
-
|
|
210
|
-
bg.addEnvVars({
|
|
211
|
-
BAR: 'baz'
|
|
212
|
-
}, { overwrite: false, default: true })
|
|
213
|
-
|
|
214
|
-
await bg.prepare()
|
|
215
|
-
const dotEnvFile = bg.getFileObject('.env')
|
|
216
|
-
assert.equal(dotEnvFile.contents.trim(), 'FOO=bar\nBAR=baz')
|
|
217
|
-
|
|
218
|
-
const dotEnvSampleFile = bg.getFileObject('.env.sample')
|
|
219
|
-
assert.equal(dotEnvSampleFile.contents.trim(), 'BAR=baz\nFOO=')
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
test('should prepare the questions', async (t) => {
|
|
223
|
-
const bg = new BaseGenerator({
|
|
224
|
-
module: '@platformatic/service'
|
|
225
|
-
})
|
|
226
|
-
// partial config with defaults
|
|
227
|
-
bg.setConfig({
|
|
228
|
-
env: {
|
|
229
|
-
FOO: 'bar'
|
|
230
|
-
}
|
|
231
|
-
})
|
|
232
|
-
|
|
233
|
-
await bg.prepareQuestions()
|
|
234
|
-
assert.deepStrictEqual(bg.questions, [{
|
|
235
|
-
type: 'input',
|
|
236
|
-
name: 'targetDirectory',
|
|
237
|
-
message: 'Where would you like to create your project?'
|
|
238
|
-
}, {
|
|
239
|
-
type: 'list',
|
|
240
|
-
name: 'typescript',
|
|
241
|
-
message: 'Do you want to use TypeScript?',
|
|
242
|
-
default: false,
|
|
243
|
-
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
244
|
-
}, {
|
|
245
|
-
type: 'input',
|
|
246
|
-
name: 'port',
|
|
247
|
-
message: 'What port do you want to use?'
|
|
248
|
-
}])
|
|
249
|
-
})
|
|
250
|
-
|
|
251
|
-
test('should prepare the questions with a targetDirectory', async (t) => {
|
|
252
|
-
const bg = new BaseGenerator({
|
|
253
|
-
module: '@platformatic/service'
|
|
254
|
-
})
|
|
255
|
-
// partial config with defaults
|
|
256
|
-
bg.setConfig({
|
|
257
|
-
targetDirectory: './foo',
|
|
258
|
-
env: {
|
|
259
|
-
FOO: 'bar'
|
|
260
|
-
}
|
|
261
|
-
})
|
|
262
|
-
|
|
263
|
-
await bg.prepareQuestions()
|
|
264
|
-
assert.deepStrictEqual(bg.questions, [{
|
|
265
|
-
type: 'list',
|
|
266
|
-
name: 'typescript',
|
|
267
|
-
message: 'Do you want to use TypeScript?',
|
|
268
|
-
default: false,
|
|
269
|
-
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
270
|
-
}, {
|
|
271
|
-
type: 'input',
|
|
272
|
-
name: 'port',
|
|
273
|
-
message: 'What port do you want to use?'
|
|
274
|
-
}])
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
test('should prepare the questions in runtime context', async (t) => {
|
|
278
|
-
const bg = new BaseGenerator({
|
|
279
|
-
module: '@platformatic/service'
|
|
280
|
-
})
|
|
281
|
-
// partial config with defaults
|
|
282
|
-
bg.setConfig({
|
|
283
|
-
isRuntimeContext: true,
|
|
284
|
-
env: {
|
|
285
|
-
FOO: 'bar'
|
|
286
|
-
}
|
|
287
|
-
})
|
|
288
|
-
|
|
289
|
-
await bg.prepareQuestions()
|
|
290
|
-
assert.deepStrictEqual(bg.questions, [])
|
|
291
|
-
})
|
|
292
|
-
|
|
293
|
-
test('should return service metadata', async (t) => {
|
|
294
|
-
const bg = new BaseGenerator({
|
|
295
|
-
module: '@platformatic/service'
|
|
296
|
-
})
|
|
297
|
-
// partial config with defaults
|
|
298
|
-
bg.setConfig({
|
|
299
|
-
targetDirectory: '/foo/bar',
|
|
300
|
-
env: {
|
|
301
|
-
FOO: 'bar'
|
|
302
|
-
}
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
const metadata = await bg.prepare()
|
|
306
|
-
assert.deepEqual(metadata, {
|
|
307
|
-
targetDirectory: '/foo/bar',
|
|
308
|
-
env: {
|
|
309
|
-
FOO: 'bar'
|
|
310
|
-
}
|
|
311
|
-
})
|
|
312
|
-
})
|
|
313
|
-
|
|
314
|
-
test('should generate javascript plugin, routes and tests', async (t) => {
|
|
315
|
-
const bg = new BaseGenerator({
|
|
316
|
-
module: '@platformatic/service'
|
|
317
|
-
})
|
|
318
|
-
bg.setConfig({
|
|
319
|
-
plugin: true,
|
|
320
|
-
tests: true
|
|
321
|
-
})
|
|
322
|
-
await bg.prepare()
|
|
323
|
-
assert.ok(bg.getFileObject('example.js', 'plugins'))
|
|
324
|
-
assert.ok(bg.getFileObject('root.js', 'routes'))
|
|
325
|
-
|
|
326
|
-
assert.ok(bg.getFileObject('root.test.js', join('test', 'routes')))
|
|
327
|
-
assert.ok(bg.getFileObject('example.test.js', join('test', 'plugins')))
|
|
328
|
-
})
|
|
329
|
-
|
|
330
|
-
test('should generate tsConfig file and typescript files', async (t) => {
|
|
331
|
-
const bg = new BaseGenerator({
|
|
332
|
-
module: '@platformatic/service'
|
|
333
|
-
})
|
|
334
|
-
bg.setConfig({
|
|
335
|
-
typescript: true,
|
|
336
|
-
plugin: true,
|
|
337
|
-
tests: true
|
|
338
|
-
})
|
|
339
|
-
const template = {
|
|
340
|
-
compilerOptions: {
|
|
341
|
-
module: 'commonjs',
|
|
342
|
-
esModuleInterop: true,
|
|
343
|
-
target: 'es2020',
|
|
344
|
-
sourceMap: true,
|
|
345
|
-
pretty: true,
|
|
346
|
-
noEmitOnError: true,
|
|
347
|
-
incremental: true,
|
|
348
|
-
strict: true,
|
|
349
|
-
outDir: 'dist'
|
|
350
|
-
},
|
|
351
|
-
watchOptions: {
|
|
352
|
-
watchFile: 'fixedPollingInterval',
|
|
353
|
-
watchDirectory: 'fixedPollingInterval',
|
|
354
|
-
fallbackPolling: 'dynamicPriority',
|
|
355
|
-
synchronousWatchDirectory: true,
|
|
356
|
-
excludeDirectories: ['**/node_modules', 'dist']
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
await bg.prepare()
|
|
360
|
-
const tsConfigFile = bg.getFileObject('tsconfig.json')
|
|
361
|
-
assert.deepEqual(JSON.parse(tsConfigFile.contents), template)
|
|
362
|
-
|
|
363
|
-
assert.ok(bg.getFileObject('example.ts', 'plugins'))
|
|
364
|
-
assert.ok(bg.getFileObject('root.ts', 'routes'))
|
|
365
|
-
|
|
366
|
-
assert.ok(bg.getFileObject('root.test.ts', join('test', 'routes')))
|
|
367
|
-
assert.ok(bg.getFileObject('example.test.ts', join('test', 'plugins')))
|
|
368
|
-
})
|
|
369
|
-
|
|
370
|
-
test('should throw if prepare fails', async (t) => {
|
|
371
|
-
const bg = new BaseGenerator({
|
|
372
|
-
module: '@platformatic/service'
|
|
373
|
-
})
|
|
374
|
-
|
|
375
|
-
bg._beforePrepare = async () => {
|
|
376
|
-
throw new Error('beforePrepare error')
|
|
377
|
-
}
|
|
378
|
-
try {
|
|
379
|
-
await bg.prepare()
|
|
380
|
-
assert.fail()
|
|
381
|
-
} catch (err) {
|
|
382
|
-
assert.equal(err.code, 'PLT_GEN_PREPARE_ERROR')
|
|
383
|
-
assert.equal(err.message, 'Error while generating the files: beforePrepare error.')
|
|
384
|
-
}
|
|
385
|
-
})
|
|
386
|
-
|
|
387
|
-
test('should throw if there is a missing env variable', async () => {
|
|
388
|
-
const bg = new BaseGenerator({
|
|
389
|
-
module: '@platformatic/service'
|
|
390
|
-
})
|
|
391
|
-
|
|
392
|
-
bg._getConfigFileContents = async () => {
|
|
393
|
-
return {
|
|
394
|
-
FOO: '{FOO}',
|
|
395
|
-
BAR: '{BAR}'
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
bg.setConfig({
|
|
400
|
-
env: {
|
|
401
|
-
FOO: 'foobar'
|
|
402
|
-
}
|
|
403
|
-
})
|
|
404
|
-
|
|
405
|
-
try {
|
|
406
|
-
await bg.prepare()
|
|
407
|
-
assert.fail()
|
|
408
|
-
} catch (err) {
|
|
409
|
-
assert.equal(err.code, 'PLT_GEN_MISSING_ENV_VAR')
|
|
410
|
-
assert.equal(err.message, 'Env variable BAR is defined in config file platformatic.json, but not in config.env object.')
|
|
411
|
-
}
|
|
412
|
-
})
|
|
413
|
-
|
|
414
|
-
test('should add package', async () => {
|
|
415
|
-
const bg = new BaseGenerator({
|
|
416
|
-
module: '@platformatic/service'
|
|
417
|
-
})
|
|
418
|
-
|
|
419
|
-
const packageDefinition = {
|
|
420
|
-
name: '@my/package',
|
|
421
|
-
options: [
|
|
422
|
-
{
|
|
423
|
-
path: 'foobar',
|
|
424
|
-
type: 'string',
|
|
425
|
-
value: 'foobar'
|
|
426
|
-
}
|
|
427
|
-
]
|
|
428
|
-
}
|
|
429
|
-
await bg.addPackage(packageDefinition)
|
|
430
|
-
|
|
431
|
-
assert.equal(bg.packages.length, 1)
|
|
432
|
-
assert.deepEqual(bg.packages[0], packageDefinition)
|
|
433
|
-
})
|
|
434
|
-
|
|
435
|
-
test('support packages', async (t) => {
|
|
436
|
-
{
|
|
437
|
-
const svc = new BaseGenerator({
|
|
438
|
-
module: '@platformatic/service'
|
|
439
|
-
})
|
|
440
|
-
const packageDefinitions = [
|
|
441
|
-
{
|
|
442
|
-
name: '@fastify/compress',
|
|
443
|
-
options: [
|
|
444
|
-
{
|
|
445
|
-
path: 'threshold',
|
|
446
|
-
value: '1',
|
|
447
|
-
type: 'number'
|
|
448
|
-
},
|
|
449
|
-
{
|
|
450
|
-
path: 'foobar',
|
|
451
|
-
value: '123',
|
|
452
|
-
type: 'number',
|
|
453
|
-
name: 'FST_PLUGIN_STATIC_FOOBAR'
|
|
454
|
-
}
|
|
455
|
-
]
|
|
456
|
-
}
|
|
457
|
-
]
|
|
458
|
-
svc.setConfig({
|
|
459
|
-
isRuntimeContext: true,
|
|
460
|
-
serviceName: 'my-service'
|
|
461
|
-
})
|
|
462
|
-
await svc.addPackage(packageDefinitions[0])
|
|
463
|
-
await svc.prepare()
|
|
464
|
-
|
|
465
|
-
const platformaticConfigFile = svc.getFileObject('platformatic.json')
|
|
466
|
-
const contents = JSON.parse(platformaticConfigFile.contents)
|
|
467
|
-
|
|
468
|
-
assert.deepEqual(contents.plugins, {
|
|
469
|
-
packages: [
|
|
470
|
-
{
|
|
471
|
-
name: '@fastify/compress',
|
|
472
|
-
options: {
|
|
473
|
-
threshold: 1,
|
|
474
|
-
foobar: '{PLT_MY_SERVICE_FST_PLUGIN_STATIC_FOOBAR}'
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
]
|
|
478
|
-
})
|
|
479
|
-
|
|
480
|
-
assert.equal(svc.config.env.PLT_MY_SERVICE_FST_PLUGIN_STATIC_FOOBAR, 123)
|
|
481
|
-
|
|
482
|
-
const packageJsonFile = svc.getFileObject('package.json')
|
|
483
|
-
const packageJson = JSON.parse(packageJsonFile.contents)
|
|
484
|
-
assert.equal(packageJson.dependencies['@fastify/compress'], 'latest')
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
{
|
|
488
|
-
// with standard platformatic plugin
|
|
489
|
-
const svc = new BaseGenerator({
|
|
490
|
-
module: '@platformatic/service'
|
|
491
|
-
})
|
|
492
|
-
svc.setConfig({
|
|
493
|
-
plugin: true
|
|
494
|
-
})
|
|
495
|
-
const packageDefinitions = [
|
|
496
|
-
{
|
|
497
|
-
name: '@fastify/compress',
|
|
498
|
-
options: [
|
|
499
|
-
{
|
|
500
|
-
path: 'threshold',
|
|
501
|
-
value: '1',
|
|
502
|
-
type: 'number'
|
|
503
|
-
}
|
|
504
|
-
]
|
|
505
|
-
}
|
|
506
|
-
]
|
|
507
|
-
await svc.addPackage(packageDefinitions[0])
|
|
508
|
-
await svc.prepare()
|
|
509
|
-
|
|
510
|
-
const platformaticConfigFile = svc.getFileObject('platformatic.json')
|
|
511
|
-
const contents = JSON.parse(platformaticConfigFile.contents)
|
|
512
|
-
|
|
513
|
-
assert.deepEqual(contents.plugins, {
|
|
514
|
-
packages: [
|
|
515
|
-
{
|
|
516
|
-
name: '@fastify/compress',
|
|
517
|
-
options: {
|
|
518
|
-
threshold: 1
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
]
|
|
522
|
-
})
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
{
|
|
526
|
-
// with relative path type but no name
|
|
527
|
-
const svc = new BaseGenerator({
|
|
528
|
-
module: '@platformatic/service'
|
|
529
|
-
})
|
|
530
|
-
svc.setConfig({
|
|
531
|
-
isRuntimeContext: true,
|
|
532
|
-
plugin: true
|
|
533
|
-
})
|
|
534
|
-
const packageDefinitions = [
|
|
535
|
-
{
|
|
536
|
-
name: '@fastify/static',
|
|
537
|
-
options: [
|
|
538
|
-
{
|
|
539
|
-
path: 'root',
|
|
540
|
-
value: 'public',
|
|
541
|
-
type: 'path'
|
|
542
|
-
}
|
|
543
|
-
]
|
|
544
|
-
}
|
|
545
|
-
]
|
|
546
|
-
await svc.addPackage(packageDefinitions[0])
|
|
547
|
-
await svc.prepare()
|
|
548
|
-
|
|
549
|
-
const platformaticConfigFile = svc.getFileObject('platformatic.json')
|
|
550
|
-
const contents = JSON.parse(platformaticConfigFile.contents)
|
|
551
|
-
|
|
552
|
-
assert.deepEqual(contents.plugins, {
|
|
553
|
-
packages: [
|
|
554
|
-
{
|
|
555
|
-
name: '@fastify/static',
|
|
556
|
-
options: {
|
|
557
|
-
root: join('{PLT_ROOT}', 'public')
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
]
|
|
561
|
-
})
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
{
|
|
565
|
-
// with relative path type and name
|
|
566
|
-
const svc = new BaseGenerator({
|
|
567
|
-
module: '@platformatic/service'
|
|
568
|
-
})
|
|
569
|
-
svc.setConfig({
|
|
570
|
-
isRuntimeContext: true,
|
|
571
|
-
plugin: true,
|
|
572
|
-
serviceName: 'my-service'
|
|
573
|
-
})
|
|
574
|
-
const packageDefinitions = [
|
|
575
|
-
{
|
|
576
|
-
name: '@fastify/static',
|
|
577
|
-
options: [
|
|
578
|
-
{
|
|
579
|
-
path: 'root',
|
|
580
|
-
value: 'public',
|
|
581
|
-
type: 'path',
|
|
582
|
-
name: 'FST_PLUGIN_STATIC_ROOT'
|
|
583
|
-
}
|
|
584
|
-
]
|
|
585
|
-
}
|
|
586
|
-
]
|
|
587
|
-
await svc.addPackage(packageDefinitions[0])
|
|
588
|
-
await svc.prepare()
|
|
589
|
-
|
|
590
|
-
const platformaticConfigFile = svc.getFileObject('platformatic.json')
|
|
591
|
-
const contents = JSON.parse(platformaticConfigFile.contents)
|
|
592
|
-
|
|
593
|
-
assert.deepEqual(contents.plugins, {
|
|
594
|
-
packages: [
|
|
595
|
-
{
|
|
596
|
-
name: '@fastify/static',
|
|
597
|
-
options: {
|
|
598
|
-
root: join('{PLT_ROOT}', '{PLT_MY_SERVICE_FST_PLUGIN_STATIC_ROOT}')
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
]
|
|
602
|
-
})
|
|
603
|
-
}
|
|
604
|
-
|
|
605
|
-
{
|
|
606
|
-
mockNpmJsRequestForPkgs(['foobar'])
|
|
607
|
-
// should get the version from npm
|
|
608
|
-
// mockAgent
|
|
609
|
-
// .get('https://registry.npmjs.org')
|
|
610
|
-
// .intercept({
|
|
611
|
-
// method: 'GET',
|
|
612
|
-
// path: '/foobar'
|
|
613
|
-
// })
|
|
614
|
-
// .reply(200, {
|
|
615
|
-
// 'dist-tags': {
|
|
616
|
-
// latest: '1.42.0'
|
|
617
|
-
// }
|
|
618
|
-
// })
|
|
619
|
-
|
|
620
|
-
const svc = new BaseGenerator({
|
|
621
|
-
module: '@platformatic/service'
|
|
622
|
-
})
|
|
623
|
-
const packageDefinitions = [
|
|
624
|
-
{
|
|
625
|
-
name: 'foobar',
|
|
626
|
-
options: []
|
|
627
|
-
}
|
|
628
|
-
]
|
|
629
|
-
svc.setConfig({
|
|
630
|
-
isRuntimeContext: true,
|
|
631
|
-
serviceName: 'my-service'
|
|
632
|
-
})
|
|
633
|
-
await svc.addPackage(packageDefinitions[0])
|
|
634
|
-
await svc.prepare()
|
|
635
|
-
|
|
636
|
-
const packageJsonFile = svc.getFileObject('package.json')
|
|
637
|
-
const packageJson = JSON.parse(packageJsonFile.contents)
|
|
638
|
-
assert.equal(packageJson.dependencies.foobar, '1.42.0')
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
{
|
|
642
|
-
// should default to `latest` if getting the version from npm fails
|
|
643
|
-
mockAgent
|
|
644
|
-
.get('https://registry.npmjs.org')
|
|
645
|
-
.intercept({
|
|
646
|
-
method: 'GET',
|
|
647
|
-
path: '/foobar'
|
|
648
|
-
})
|
|
649
|
-
.reply(500, {
|
|
650
|
-
message: 'Internal Server Error'
|
|
651
|
-
})
|
|
652
|
-
|
|
653
|
-
const svc = new BaseGenerator({
|
|
654
|
-
module: '@platformatic/service'
|
|
655
|
-
})
|
|
656
|
-
const packageDefinitions = [
|
|
657
|
-
{
|
|
658
|
-
name: 'foobar',
|
|
659
|
-
options: []
|
|
660
|
-
}
|
|
661
|
-
]
|
|
662
|
-
svc.setConfig({
|
|
663
|
-
isRuntimeContext: true,
|
|
664
|
-
serviceName: 'my-service'
|
|
665
|
-
})
|
|
666
|
-
await svc.addPackage(packageDefinitions[0])
|
|
667
|
-
await svc.prepare()
|
|
668
|
-
|
|
669
|
-
const packageJsonFile = svc.getFileObject('package.json')
|
|
670
|
-
const packageJson = JSON.parse(packageJsonFile.contents)
|
|
671
|
-
assert.equal(packageJson.dependencies.foobar, 'latest')
|
|
672
|
-
}
|
|
673
|
-
|
|
674
|
-
{
|
|
675
|
-
// should set latest on timeout
|
|
676
|
-
mockAgent
|
|
677
|
-
.get('https://registry.npmjs.org')
|
|
678
|
-
.intercept({
|
|
679
|
-
method: 'GET',
|
|
680
|
-
path: '/foobarxxx'
|
|
681
|
-
})
|
|
682
|
-
.reply(200, {
|
|
683
|
-
'dist-tags': {
|
|
684
|
-
latest: '1.42.0'
|
|
685
|
-
}
|
|
686
|
-
}).delay(3000)
|
|
687
|
-
|
|
688
|
-
const svc = new BaseGenerator({
|
|
689
|
-
module: '@platformatic/service'
|
|
690
|
-
})
|
|
691
|
-
const packageName = 'foobarxxx'
|
|
692
|
-
const packageDefinitions = [
|
|
693
|
-
{
|
|
694
|
-
name: packageName,
|
|
695
|
-
options: []
|
|
696
|
-
}
|
|
697
|
-
]
|
|
698
|
-
svc.setConfig({
|
|
699
|
-
isRuntimeContext: true,
|
|
700
|
-
serviceName: 'my-service'
|
|
701
|
-
})
|
|
702
|
-
await svc.addPackage(packageDefinitions[0])
|
|
703
|
-
await svc.prepare()
|
|
704
|
-
|
|
705
|
-
const packageJsonFile = svc.getFileObject('package.json')
|
|
706
|
-
const packageJson = JSON.parse(packageJsonFile.contents)
|
|
707
|
-
assert.equal(packageJson.dependencies[packageName], 'latest')
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
{
|
|
711
|
-
// should default to `latest` if getting the version from npm fails
|
|
712
|
-
mockAgent
|
|
713
|
-
.get('https://registry.npmjs.org')
|
|
714
|
-
.intercept({
|
|
715
|
-
method: 'GET',
|
|
716
|
-
path: '/foobar'
|
|
717
|
-
})
|
|
718
|
-
.reply(500, {
|
|
719
|
-
message: 'Internal Server Error'
|
|
720
|
-
})
|
|
721
|
-
|
|
722
|
-
const svc = new BaseGenerator({
|
|
723
|
-
module: '@platformatic/service'
|
|
724
|
-
})
|
|
725
|
-
const packageDefinitions = [
|
|
726
|
-
{
|
|
727
|
-
name: 'foobar',
|
|
728
|
-
options: []
|
|
729
|
-
}
|
|
730
|
-
]
|
|
731
|
-
svc.setConfig({
|
|
732
|
-
isRuntimeContext: true,
|
|
733
|
-
serviceName: 'my-service'
|
|
734
|
-
})
|
|
735
|
-
await svc.addPackage(packageDefinitions[0])
|
|
736
|
-
await svc.prepare()
|
|
737
|
-
|
|
738
|
-
const packageJsonFile = svc.getFileObject('package.json')
|
|
739
|
-
const packageJson = JSON.parse(packageJsonFile.contents)
|
|
740
|
-
assert.equal(packageJson.dependencies.foobar, 'latest')
|
|
741
|
-
}
|
|
742
|
-
})
|
|
743
|
-
test('should load data from directory', async (t) => {
|
|
744
|
-
const runtimeDirectory = join(__dirname, 'fixtures', 'sample-runtime')
|
|
745
|
-
const bg = new BaseGenerator({
|
|
746
|
-
module: '@platformatic/service'
|
|
747
|
-
})
|
|
748
|
-
const data = await bg.loadFromDir('rival', runtimeDirectory)
|
|
749
|
-
const expected = {
|
|
750
|
-
name: 'rival',
|
|
751
|
-
template: '@platformatic/service',
|
|
752
|
-
fields: [],
|
|
753
|
-
plugins: [
|
|
754
|
-
{
|
|
755
|
-
name: '@fastify/oauth2',
|
|
756
|
-
options: [
|
|
757
|
-
{
|
|
758
|
-
path: 'name',
|
|
759
|
-
type: 'string',
|
|
760
|
-
value: 'googleOAuth2',
|
|
761
|
-
name: 'FST_PLUGIN_OAUTH2_NAME'
|
|
762
|
-
},
|
|
763
|
-
{
|
|
764
|
-
path: 'credentials.client.id',
|
|
765
|
-
type: 'string',
|
|
766
|
-
value: 'sample_client_id',
|
|
767
|
-
name: 'FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_ID'
|
|
768
|
-
},
|
|
769
|
-
{
|
|
770
|
-
path: 'credentials.client.secret',
|
|
771
|
-
type: 'string',
|
|
772
|
-
value: 'sample_client_secret',
|
|
773
|
-
name: 'FST_PLUGIN_OAUTH2_CREDENTIALS_CLIENT_SECRET'
|
|
774
|
-
},
|
|
775
|
-
{
|
|
776
|
-
path: 'startRedirectPath',
|
|
777
|
-
type: 'string',
|
|
778
|
-
value: '/login/google',
|
|
779
|
-
name: 'FST_PLUGIN_OAUTH2_REDIRECT_PATH'
|
|
780
|
-
},
|
|
781
|
-
{
|
|
782
|
-
path: 'callbackUri',
|
|
783
|
-
type: 'string',
|
|
784
|
-
value: 'http://localhost:3000/login/google/callback',
|
|
785
|
-
name: 'FST_PLUGIN_OAUTH2_CALLBACK_URI'
|
|
786
|
-
}
|
|
787
|
-
]
|
|
788
|
-
}]
|
|
789
|
-
}
|
|
790
|
-
assert.deepEqual(data, expected)
|
|
791
|
-
})
|
|
792
|
-
|
|
793
|
-
test('on update should just touch the packages configuration', async (t) => {
|
|
794
|
-
mockNpmJsRequestForPkgs(['@fastify/foo-plugin'])
|
|
795
|
-
const runtimeDirectory = join(__dirname, 'fixtures', 'sample-runtime', 'services', 'rival')
|
|
796
|
-
const dir = await moveToTmpdir(after)
|
|
797
|
-
await cp(runtimeDirectory, dir, { recursive: true })
|
|
798
|
-
|
|
799
|
-
const bg = new BaseGenerator({
|
|
800
|
-
module: '@platformatic/service',
|
|
801
|
-
targetDirectory: dir
|
|
802
|
-
})
|
|
803
|
-
bg.setConfig({
|
|
804
|
-
isUpdating: true
|
|
805
|
-
})
|
|
806
|
-
await bg.addPackage({
|
|
807
|
-
name: '@fastify/foo-plugin',
|
|
808
|
-
options: [
|
|
809
|
-
{
|
|
810
|
-
path: 'name',
|
|
811
|
-
type: 'string',
|
|
812
|
-
value: 'foobar',
|
|
813
|
-
name: 'FST_PLUGIN_FOO_FOOBAR'
|
|
814
|
-
}
|
|
815
|
-
]
|
|
816
|
-
})
|
|
817
|
-
await bg.prepare()
|
|
818
|
-
|
|
819
|
-
assert.equal(bg.files.length, 1)
|
|
820
|
-
assert.equal(bg.files[0].file, 'platformatic.json')
|
|
821
|
-
assert.equal(bg.files[0].path, '')
|
|
822
|
-
|
|
823
|
-
const configFileContents = JSON.parse(bg.files[0].contents)
|
|
824
|
-
assert.deepEqual(configFileContents.plugins.packages, [
|
|
825
|
-
{
|
|
826
|
-
name: '@fastify/foo-plugin',
|
|
827
|
-
options: {
|
|
828
|
-
name: '{FST_PLUGIN_FOO_FOOBAR}'
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
])
|
|
832
|
-
assert.deepEqual(bg.config.dependencies, {
|
|
833
|
-
'@fastify/foo-plugin': '1.42.0'
|
|
834
|
-
})
|
|
835
|
-
})
|
|
836
|
-
|
|
837
|
-
test('on update should just touch the packages configuration', async (t) => {
|
|
838
|
-
mockNpmJsRequestForPkgs(['@fastify/foo-plugin'])
|
|
839
|
-
const runtimeDirectory = join(__dirname, 'fixtures', 'sample-runtime', 'services', 'no-plugin')
|
|
840
|
-
const dir = await moveToTmpdir(after)
|
|
841
|
-
await cp(runtimeDirectory, dir, { recursive: true })
|
|
842
|
-
|
|
843
|
-
const bg = new BaseGenerator({
|
|
844
|
-
module: '@platformatic/service',
|
|
845
|
-
targetDirectory: dir
|
|
846
|
-
})
|
|
847
|
-
bg.setConfig({
|
|
848
|
-
isUpdating: true
|
|
849
|
-
})
|
|
850
|
-
await bg.addPackage({
|
|
851
|
-
name: '@fastify/foo-plugin',
|
|
852
|
-
options: [
|
|
853
|
-
{
|
|
854
|
-
path: 'name',
|
|
855
|
-
type: 'string',
|
|
856
|
-
value: 'foobar',
|
|
857
|
-
name: 'FST_PLUGIN_FOO_FOOBAR'
|
|
858
|
-
}
|
|
859
|
-
]
|
|
860
|
-
})
|
|
861
|
-
await bg.prepare()
|
|
862
|
-
|
|
863
|
-
assert.equal(bg.files.length, 1)
|
|
864
|
-
assert.equal(bg.files[0].file, 'platformatic.json')
|
|
865
|
-
assert.equal(bg.files[0].path, '')
|
|
866
|
-
|
|
867
|
-
const configFileContents = JSON.parse(bg.files[0].contents)
|
|
868
|
-
assert.equal(configFileContents.plugins, undefined)
|
|
869
|
-
assert.deepEqual(bg.config.dependencies, {
|
|
870
|
-
'@fastify/foo-plugin': '1.42.0'
|
|
871
|
-
})
|
|
872
|
-
})
|
|
873
|
-
|
|
874
|
-
describe('runtime context', () => {
|
|
875
|
-
test('should set config.envPrefix correctly', async (t) => {
|
|
876
|
-
const bg = new BaseGenerator({
|
|
877
|
-
module: '@platformatic/service'
|
|
878
|
-
})
|
|
879
|
-
|
|
880
|
-
bg.setConfig({
|
|
881
|
-
isRuntimeContext: true,
|
|
882
|
-
serviceName: 'sample-service'
|
|
883
|
-
})
|
|
884
|
-
|
|
885
|
-
assert.equal(bg.config.envPrefix, 'SAMPLE_SERVICE')
|
|
886
|
-
|
|
887
|
-
bg.setConfig({
|
|
888
|
-
isRuntimeContext: true,
|
|
889
|
-
serviceName: 'sample-service',
|
|
890
|
-
envPrefix: 'ANOTHER_PREFIX',
|
|
891
|
-
env: {
|
|
892
|
-
FOO: 'bar',
|
|
893
|
-
BAZ: 'baz'
|
|
894
|
-
}
|
|
895
|
-
})
|
|
896
|
-
|
|
897
|
-
assert.equal(bg.config.envPrefix, 'ANOTHER_PREFIX')
|
|
898
|
-
assert.deepEqual(bg.config.env, {
|
|
899
|
-
PLT_ANOTHER_PREFIX_FOO: 'bar',
|
|
900
|
-
PLT_ANOTHER_PREFIX_BAZ: 'baz'
|
|
901
|
-
})
|
|
902
|
-
})
|
|
903
|
-
|
|
904
|
-
test('should generate correct env file from config.env', async (t) => {
|
|
905
|
-
const bg = new BaseGenerator({
|
|
906
|
-
module: '@platformatic/service'
|
|
907
|
-
})
|
|
908
|
-
|
|
909
|
-
bg.setConfig({
|
|
910
|
-
isRuntimeContext: true,
|
|
911
|
-
serviceName: 'sample-service',
|
|
912
|
-
envPrefix: 'ANOTHER_PREFIX',
|
|
913
|
-
env: {
|
|
914
|
-
FOO: 'bar',
|
|
915
|
-
BAZ: 'baz'
|
|
916
|
-
}
|
|
917
|
-
})
|
|
918
|
-
|
|
919
|
-
const meta = await bg.prepare()
|
|
920
|
-
|
|
921
|
-
assert.deepEqual(meta.env, {
|
|
922
|
-
PLT_ANOTHER_PREFIX_FOO: 'bar',
|
|
923
|
-
PLT_ANOTHER_PREFIX_BAZ: 'baz'
|
|
924
|
-
})
|
|
925
|
-
})
|
|
926
|
-
|
|
927
|
-
test('should return service metadata', async (t) => {
|
|
928
|
-
const bg = new BaseGenerator({
|
|
929
|
-
module: '@platformatic/service'
|
|
930
|
-
})
|
|
931
|
-
// partial config with defaults
|
|
932
|
-
bg.setConfig({
|
|
933
|
-
targetDirectory: '/foo/bar',
|
|
934
|
-
isRuntimeContext: true,
|
|
935
|
-
serviceName: 'my-service',
|
|
936
|
-
env: {
|
|
937
|
-
FOO: 'bar'
|
|
938
|
-
}
|
|
939
|
-
})
|
|
940
|
-
|
|
941
|
-
const metadata = await bg.prepare()
|
|
942
|
-
assert.deepEqual(metadata, {
|
|
943
|
-
targetDirectory: '/foo/bar',
|
|
944
|
-
env: {
|
|
945
|
-
PLT_MY_SERVICE_FOO: 'bar'
|
|
946
|
-
}
|
|
947
|
-
})
|
|
948
|
-
})
|
|
949
|
-
|
|
950
|
-
test('should generate service name if not provided', async () => {
|
|
951
|
-
const bg = new BaseGenerator({
|
|
952
|
-
module: '@platformatic/service'
|
|
953
|
-
})
|
|
954
|
-
bg.setConfig({
|
|
955
|
-
targetDirectory: '/foo/bar',
|
|
956
|
-
isRuntimeContext: true,
|
|
957
|
-
env: {
|
|
958
|
-
FOO: 'bar'
|
|
959
|
-
}
|
|
960
|
-
})
|
|
961
|
-
|
|
962
|
-
const metadata = await bg.prepare()
|
|
963
|
-
|
|
964
|
-
assert.equal(bg.config.envPrefix, convertServiceNameToPrefix(bg.config.serviceName))
|
|
965
|
-
const envPrefix = bg.config.envPrefix
|
|
966
|
-
assert.deepEqual(metadata, {
|
|
967
|
-
targetDirectory: '/foo/bar',
|
|
968
|
-
env: {
|
|
969
|
-
[`PLT_${envPrefix}_FOO`]: 'bar'
|
|
970
|
-
}
|
|
971
|
-
})
|
|
972
|
-
})
|
|
973
|
-
})
|