@platformatic/generators 2.0.0-alpha.1 → 2.0.0-alpha.11
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 -31
- 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 -37
- 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
package/eslint.config.js
ADDED
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const { BaseGenerator } = require('./lib/base-generator')
|
|
4
|
-
const { StackableGenerator } = require('./lib/stackable-generator')
|
|
5
4
|
const { generateTests } = require('./lib/create-plugin')
|
|
6
5
|
const utils = require('./lib/utils')
|
|
7
6
|
module.exports = {
|
|
8
7
|
BaseGenerator,
|
|
9
|
-
StackableGenerator,
|
|
10
8
|
generateTests,
|
|
11
|
-
...utils
|
|
9
|
+
...utils,
|
|
12
10
|
}
|
package/index.test-d.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import { expectAssignable } from 'tsd'
|
|
2
|
-
import { BaseGenerator } from './lib/base-generator'
|
|
2
|
+
import { BaseGenerator } from './lib/base-generator'
|
|
3
3
|
import { generatePlugins, generateTests } from './index'
|
|
4
|
-
import { FileGenerator } from './lib/file-generator'
|
|
4
|
+
import { FileGenerator } from './lib/file-generator'
|
|
5
5
|
|
|
6
6
|
expectAssignable<BaseGenerator.ConfigFieldDefinition>({
|
|
7
7
|
label: 'PLT_TESTING',
|
|
8
8
|
var: 'testing',
|
|
9
9
|
default: 'hello world',
|
|
10
10
|
type: 'string',
|
|
11
|
-
configValue: 'someConfigValue'
|
|
11
|
+
configValue: 'someConfigValue',
|
|
12
12
|
})
|
|
13
13
|
|
|
14
14
|
expectAssignable<BaseGenerator.ConfigField>({
|
|
15
15
|
var: 'testing',
|
|
16
16
|
configValue: 'someConfigValue',
|
|
17
|
-
value: 'asd123'
|
|
17
|
+
value: 'asd123',
|
|
18
18
|
})
|
|
19
19
|
|
|
20
20
|
expectAssignable<FileGenerator.FileObject[]>(generatePlugins(true))
|
package/lib/base-generator.d.ts
CHANGED
|
@@ -81,42 +81,42 @@ export namespace BaseGenerator {
|
|
|
81
81
|
questions: Array<object>
|
|
82
82
|
|
|
83
83
|
packages: PackageConfiguration[]
|
|
84
|
-
constructor(opts?: BaseGeneratorOptions)
|
|
84
|
+
constructor (opts?: BaseGeneratorOptions)
|
|
85
|
+
|
|
86
|
+
setConfig (config?: BaseGeneratorConfig): void
|
|
85
87
|
|
|
86
|
-
setConfig(config?: BaseGeneratorConfig): void
|
|
87
|
-
|
|
88
88
|
getEnvVarName (envVarName: string): string
|
|
89
89
|
addEnvVars (envVars: Env, opts: AddEnvVarOptions): void
|
|
90
90
|
addEnvVar (envVarName: string, envVarValue: EnvVarValue, opts: AddEnvVarOptions): void
|
|
91
91
|
getEnvVar (envVarName: string): EnvVarValue
|
|
92
|
-
setEnvVars(env?: Env): void
|
|
92
|
+
setEnvVars (env?: Env): void
|
|
93
93
|
|
|
94
|
-
getDefaultConfig(): { [x: string]: JSONValue }
|
|
94
|
+
getDefaultConfig (): { [x: string]: JSONValue }
|
|
95
95
|
|
|
96
|
-
getFastifyVersion(): Promise<string>
|
|
97
|
-
getPlatformaticVersion(): Promise<string>
|
|
96
|
+
getFastifyVersion (): Promise<string>
|
|
97
|
+
getPlatformaticVersion (): Promise<string>
|
|
98
98
|
|
|
99
|
-
addPackage(pkg: PackageDefinition): Promise<void>
|
|
99
|
+
addPackage (pkg: PackageDefinition): Promise<void>
|
|
100
100
|
|
|
101
|
-
loadFromDir(dir: string): Promise<void>
|
|
102
|
-
prepare(): Promise<GeneratorMetadata>
|
|
103
|
-
run(): Promise<GeneratorMetadata>
|
|
104
|
-
addQuestion(question: any, where?: WhereClause): Promise<void>
|
|
105
|
-
removeQuestion(variableName: string): void
|
|
106
|
-
getTSConfig(): { [x: string]: JSONValue }
|
|
101
|
+
loadFromDir (dir: string): Promise<void>
|
|
102
|
+
prepare (): Promise<GeneratorMetadata>
|
|
103
|
+
run (): Promise<GeneratorMetadata>
|
|
104
|
+
addQuestion (question: any, where?: WhereClause): Promise<void>
|
|
105
|
+
removeQuestion (variableName: string): void
|
|
106
|
+
getTSConfig (): { [x: string]: JSONValue }
|
|
107
107
|
|
|
108
|
-
getConfigFieldsDefinitions(): ConfigFieldDefinition[]
|
|
109
|
-
setConfigFields(fields: ConfigField[]): void
|
|
108
|
+
getConfigFieldsDefinitions (): ConfigFieldDefinition[]
|
|
109
|
+
setConfigFields (fields: ConfigField[]): void
|
|
110
110
|
|
|
111
|
-
generateConfigFile(): Promise<void>
|
|
112
|
-
readPackageJsonFile(): Promise<JSONValue>
|
|
113
|
-
generatePackageJson(): Promise<{ [x: string]: JSONValue }>
|
|
114
|
-
getConfigFileName(): string
|
|
115
|
-
checkEnvVariablesInConfigFile(): boolean
|
|
116
|
-
_beforePrepare(): Promise<void>
|
|
117
|
-
_afterPrepare(): Promise<void | JSONValue>
|
|
118
|
-
_getConfigFileContents(): Promise<{ [x: string]: BaseGenerator.JSONValue }>
|
|
111
|
+
generateConfigFile (): Promise<void>
|
|
112
|
+
readPackageJsonFile (): Promise<JSONValue>
|
|
113
|
+
generatePackageJson (): Promise<{ [x: string]: JSONValue }>
|
|
114
|
+
getConfigFileName (): string
|
|
115
|
+
checkEnvVariablesInConfigFile (): boolean
|
|
116
|
+
_beforePrepare (): Promise<void>
|
|
117
|
+
_afterPrepare (): Promise<void | JSONValue>
|
|
118
|
+
_getConfigFileContents (): Promise<{ [x: string]: BaseGenerator.JSONValue }>
|
|
119
119
|
|
|
120
|
-
postInstallActions(): Promise<void>
|
|
120
|
+
postInstallActions (): Promise<void>
|
|
121
121
|
}
|
|
122
122
|
}
|
package/lib/base-generator.js
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
const { readFile } = require('node:fs/promises')
|
|
4
4
|
const {
|
|
5
|
-
stripVersion,
|
|
6
5
|
convertServiceNameToPrefix,
|
|
7
6
|
extractEnvVariablesFromText,
|
|
8
7
|
getPackageConfigurationObject,
|
|
9
8
|
PLT_ROOT,
|
|
10
|
-
getLatestNpmVersion
|
|
9
|
+
getLatestNpmVersion,
|
|
11
10
|
} = require('./utils')
|
|
12
11
|
const { join } = require('node:path')
|
|
13
12
|
const { FileGenerator } = require('./file-generator')
|
|
@@ -24,7 +23,7 @@ const fakeLogger = {
|
|
|
24
23
|
warn: () => {},
|
|
25
24
|
debug: () => {},
|
|
26
25
|
trace: () => {},
|
|
27
|
-
error: () => {}
|
|
26
|
+
error: () => {},
|
|
28
27
|
}
|
|
29
28
|
/* c8 ignore start */
|
|
30
29
|
|
|
@@ -60,7 +59,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
60
59
|
envPrefix: '',
|
|
61
60
|
env: {},
|
|
62
61
|
defaultEnv: {},
|
|
63
|
-
isUpdating: false
|
|
62
|
+
isUpdating: false,
|
|
64
63
|
}
|
|
65
64
|
}
|
|
66
65
|
|
|
@@ -137,7 +136,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
137
136
|
this.config = {
|
|
138
137
|
...this.getDefaultConfig(),
|
|
139
138
|
...oldConfig,
|
|
140
|
-
...config
|
|
139
|
+
...config,
|
|
141
140
|
}
|
|
142
141
|
|
|
143
142
|
if (this.config.isRuntimeContext) {
|
|
@@ -163,7 +162,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
163
162
|
const newConfig = await this.inquirer.prompt(this.questions)
|
|
164
163
|
this.setConfig({
|
|
165
164
|
...this.config,
|
|
166
|
-
...newConfig
|
|
165
|
+
...newConfig,
|
|
167
166
|
})
|
|
168
167
|
}
|
|
169
168
|
}
|
|
@@ -190,7 +189,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
190
189
|
this.addFile({
|
|
191
190
|
path: '',
|
|
192
191
|
file: 'platformatic.json',
|
|
193
|
-
contents: JSON.stringify(currentConfigFile, null, 2)
|
|
192
|
+
contents: JSON.stringify(currentConfigFile, null, 2),
|
|
194
193
|
})
|
|
195
194
|
} else {
|
|
196
195
|
await this.getFastifyVersion()
|
|
@@ -203,7 +202,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
203
202
|
this.addFile({
|
|
204
203
|
path: '',
|
|
205
204
|
file: 'package.json',
|
|
206
|
-
contents: JSON.stringify(template, null, 2)
|
|
205
|
+
contents: JSON.stringify(template, null, 2),
|
|
207
206
|
})
|
|
208
207
|
|
|
209
208
|
await this.generateConfigFile()
|
|
@@ -215,7 +214,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
215
214
|
this.addFile({
|
|
216
215
|
path: '',
|
|
217
216
|
file: 'tsconfig.json',
|
|
218
|
-
contents: JSON.stringify(this.getTsConfig(), null, 2)
|
|
217
|
+
contents: JSON.stringify(this.getTsConfig(), null, 2),
|
|
219
218
|
})
|
|
220
219
|
}
|
|
221
220
|
|
|
@@ -236,7 +235,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
236
235
|
}
|
|
237
236
|
return {
|
|
238
237
|
targetDirectory: this.targetDirectory,
|
|
239
|
-
env: this.config.env
|
|
238
|
+
env: this.config.env,
|
|
240
239
|
}
|
|
241
240
|
} catch (err) {
|
|
242
241
|
if (err.code?.startsWith('PLT_GEN')) {
|
|
@@ -280,15 +279,16 @@ class BaseGenerator extends FileGenerator {
|
|
|
280
279
|
noEmitOnError: true,
|
|
281
280
|
incremental: true,
|
|
282
281
|
strict: true,
|
|
283
|
-
outDir: 'dist'
|
|
282
|
+
outDir: 'dist',
|
|
283
|
+
skipLibCheck: true,
|
|
284
284
|
},
|
|
285
285
|
watchOptions: {
|
|
286
286
|
watchFile: 'fixedPollingInterval',
|
|
287
287
|
watchDirectory: 'fixedPollingInterval',
|
|
288
288
|
fallbackPolling: 'dynamicPriority',
|
|
289
289
|
synchronousWatchDirectory: true,
|
|
290
|
-
excludeDirectories: ['**/node_modules', 'dist']
|
|
291
|
-
}
|
|
290
|
+
excludeDirectories: ['**/node_modules', 'dist'],
|
|
291
|
+
},
|
|
292
292
|
}
|
|
293
293
|
}
|
|
294
294
|
|
|
@@ -299,7 +299,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
299
299
|
this.questions.push({
|
|
300
300
|
type: 'input',
|
|
301
301
|
name: 'targetDirectory',
|
|
302
|
-
message: 'Where would you like to create your project?'
|
|
302
|
+
message: 'Where would you like to create your project?',
|
|
303
303
|
})
|
|
304
304
|
}
|
|
305
305
|
|
|
@@ -309,14 +309,14 @@ class BaseGenerator extends FileGenerator {
|
|
|
309
309
|
name: 'typescript',
|
|
310
310
|
message: 'Do you want to use TypeScript?',
|
|
311
311
|
default: false,
|
|
312
|
-
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }]
|
|
312
|
+
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }],
|
|
313
313
|
})
|
|
314
314
|
|
|
315
315
|
// port
|
|
316
316
|
this.questions.push({
|
|
317
317
|
type: 'input',
|
|
318
318
|
name: 'port',
|
|
319
|
-
message: 'What port do you want to use?'
|
|
319
|
+
message: 'What port do you want to use?',
|
|
320
320
|
})
|
|
321
321
|
}
|
|
322
322
|
}
|
|
@@ -340,7 +340,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
340
340
|
}
|
|
341
341
|
return {
|
|
342
342
|
name: packageDefinition.name,
|
|
343
|
-
options: packageConfigOutput.config
|
|
343
|
+
options: packageConfigOutput.config,
|
|
344
344
|
}
|
|
345
345
|
})
|
|
346
346
|
}
|
|
@@ -348,7 +348,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
348
348
|
this.addFile({
|
|
349
349
|
path: '',
|
|
350
350
|
file: configFileName,
|
|
351
|
-
contents: JSON.stringify(contents, null, 2)
|
|
351
|
+
contents: JSON.stringify(contents, null, 2),
|
|
352
352
|
})
|
|
353
353
|
}
|
|
354
354
|
|
|
@@ -367,12 +367,12 @@ class BaseGenerator extends FileGenerator {
|
|
|
367
367
|
|
|
368
368
|
async getFastifyVersion () {
|
|
369
369
|
const pkgData = await this.readPackageJsonFile()
|
|
370
|
-
this.fastifyVersion =
|
|
370
|
+
this.fastifyVersion = pkgData.dependencies.fastify
|
|
371
371
|
}
|
|
372
372
|
|
|
373
373
|
async getPlatformaticVersion () {
|
|
374
374
|
const pkgData = await this.readPackageJsonFile()
|
|
375
|
-
this.platformaticVersion =
|
|
375
|
+
this.platformaticVersion = pkgData.version
|
|
376
376
|
}
|
|
377
377
|
|
|
378
378
|
async generatePackageJson () {
|
|
@@ -380,19 +380,19 @@ class BaseGenerator extends FileGenerator {
|
|
|
380
380
|
name: `${this.config.serviceName}`,
|
|
381
381
|
scripts: {
|
|
382
382
|
start: 'platformatic start',
|
|
383
|
-
test: 'borp'
|
|
383
|
+
test: 'borp',
|
|
384
384
|
},
|
|
385
385
|
devDependencies: {
|
|
386
386
|
fastify: `^${this.fastifyVersion}`,
|
|
387
387
|
borp: `${this.pkgData.devDependencies.borp}`,
|
|
388
|
-
...this.config.devDependencies
|
|
388
|
+
...this.config.devDependencies,
|
|
389
389
|
},
|
|
390
390
|
dependencies: {
|
|
391
|
-
...this.config.dependencies
|
|
391
|
+
...this.config.dependencies,
|
|
392
392
|
},
|
|
393
393
|
engines: {
|
|
394
|
-
node: '^18.8.0 || >=20.6.0'
|
|
395
|
-
}
|
|
394
|
+
node: '^18.8.0 || >=20.6.0',
|
|
395
|
+
},
|
|
396
396
|
}
|
|
397
397
|
|
|
398
398
|
if (this.config.typescript) {
|
|
@@ -409,7 +409,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
409
409
|
this.addFile({
|
|
410
410
|
path: '',
|
|
411
411
|
file: '.env',
|
|
412
|
-
contents: serializeEnvVars(this.config.env)
|
|
412
|
+
contents: serializeEnvVars(this.config.env),
|
|
413
413
|
})
|
|
414
414
|
|
|
415
415
|
const emptyEnvVars = {}
|
|
@@ -424,8 +424,8 @@ class BaseGenerator extends FileGenerator {
|
|
|
424
424
|
file: '.env.sample',
|
|
425
425
|
contents: serializeEnvVars({
|
|
426
426
|
...this.config.defaultEnv,
|
|
427
|
-
...emptyEnvVars
|
|
428
|
-
})
|
|
427
|
+
...emptyEnvVars,
|
|
428
|
+
}),
|
|
429
429
|
})
|
|
430
430
|
}
|
|
431
431
|
}
|
|
@@ -461,7 +461,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
461
461
|
const flattened = flattenObject(pkg)
|
|
462
462
|
const output = {
|
|
463
463
|
name: flattened.name,
|
|
464
|
-
options: []
|
|
464
|
+
options: [],
|
|
465
465
|
}
|
|
466
466
|
if (pkg.options) {
|
|
467
467
|
Object.entries(flattened)
|
|
@@ -473,7 +473,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
473
473
|
name: serviceEnvVarKey,
|
|
474
474
|
path: key.replace('options.', ''),
|
|
475
475
|
type: 'string',
|
|
476
|
-
value: runtimeEnv[runtimeEnvVarKey]
|
|
476
|
+
value: runtimeEnv[runtimeEnvVarKey],
|
|
477
477
|
}
|
|
478
478
|
output.options.push(option)
|
|
479
479
|
})
|
|
@@ -487,7 +487,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
487
487
|
name: serviceName,
|
|
488
488
|
template: getServiceTemplateFromSchemaUrl(servicePkgJsonFileData.$schema),
|
|
489
489
|
fields: [],
|
|
490
|
-
plugins
|
|
490
|
+
plugins,
|
|
491
491
|
}
|
|
492
492
|
}
|
|
493
493
|
|
package/lib/create-gitignore.js
CHANGED
package/lib/create-plugin.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ type HelperCustomization = {
|
|
|
7
7
|
requires: string
|
|
8
8
|
}
|
|
9
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>
|
|
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>
|
package/lib/create-plugin.js
CHANGED
|
@@ -184,7 +184,7 @@ function generatePluginWithTypesSupport (typescript) {
|
|
|
184
184
|
return {
|
|
185
185
|
path: 'plugins',
|
|
186
186
|
file: pluginName,
|
|
187
|
-
contents: pluginTemplate
|
|
187
|
+
contents: pluginTemplate,
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
190
|
|
|
@@ -198,7 +198,7 @@ function generateRouteWithTypesSupport (typescript) {
|
|
|
198
198
|
return {
|
|
199
199
|
path: 'routes',
|
|
200
200
|
file: routesName,
|
|
201
|
-
contents: routesTemplate
|
|
201
|
+
contents: routesTemplate,
|
|
202
202
|
}
|
|
203
203
|
}
|
|
204
204
|
|
|
@@ -208,33 +208,33 @@ function generateTests (typescript, mod, customizations) {
|
|
|
208
208
|
output.push({
|
|
209
209
|
path: 'test',
|
|
210
210
|
file: 'helper.ts',
|
|
211
|
-
contents: testHelperTS(mod, customizations)
|
|
211
|
+
contents: testHelperTS(mod, customizations),
|
|
212
212
|
})
|
|
213
213
|
output.push({
|
|
214
214
|
path: join('test', 'plugins'),
|
|
215
215
|
file: 'example.test.ts',
|
|
216
|
-
contents: TEST_PLUGIN_TS
|
|
216
|
+
contents: TEST_PLUGIN_TS,
|
|
217
217
|
})
|
|
218
218
|
output.push({
|
|
219
219
|
path: join('test', 'routes'),
|
|
220
220
|
file: 'root.test.ts',
|
|
221
|
-
contents: TEST_ROUTES_TS
|
|
221
|
+
contents: TEST_ROUTES_TS,
|
|
222
222
|
})
|
|
223
223
|
} else {
|
|
224
224
|
output.push({
|
|
225
225
|
path: 'test',
|
|
226
226
|
file: 'helper.js',
|
|
227
|
-
contents: testHelperJS(mod, customizations)
|
|
227
|
+
contents: testHelperJS(mod, customizations),
|
|
228
228
|
})
|
|
229
229
|
output.push({
|
|
230
230
|
path: join('test', 'plugins'),
|
|
231
231
|
file: 'example.test.js',
|
|
232
|
-
contents: TEST_PLUGIN_JS
|
|
232
|
+
contents: TEST_PLUGIN_JS,
|
|
233
233
|
})
|
|
234
234
|
output.push({
|
|
235
235
|
path: join('test', 'routes'),
|
|
236
236
|
file: 'root.test.js',
|
|
237
|
-
contents: TEST_ROUTES_JS
|
|
237
|
+
contents: TEST_ROUTES_JS,
|
|
238
238
|
})
|
|
239
239
|
}
|
|
240
240
|
return output
|
|
@@ -251,5 +251,5 @@ module.exports = {
|
|
|
251
251
|
generatePluginWithTypesSupport,
|
|
252
252
|
generateRouteWithTypesSupport,
|
|
253
253
|
generatePlugins,
|
|
254
|
-
generateTests
|
|
254
|
+
generateTests,
|
|
255
255
|
}
|
package/lib/errors.js
CHANGED
|
@@ -8,5 +8,5 @@ module.exports = {
|
|
|
8
8
|
ModuleNeeded: createError(`${ERROR_PREFIX}_PREPARE_ERROR`, 'The module which the package will be published to must be specified'),
|
|
9
9
|
PrepareError: createError(`${ERROR_PREFIX}_PREPARE_ERROR`, 'Error while generating the files: %s.'),
|
|
10
10
|
MissingEnvVariable: createError(`${ERROR_PREFIX}_MISSING_ENV_VAR`, 'Env variable %s is defined in config file %s, but not in config.env object.'),
|
|
11
|
-
WrongTypeError: createError(`${ERROR_PREFIX}_WRONG_TYPE`, 'Invalid value type. Accepted values are \'string\', \'number\' and \'boolean\', found \'%s\'.')
|
|
11
|
+
WrongTypeError: createError(`${ERROR_PREFIX}_WRONG_TYPE`, 'Invalid value type. Accepted values are \'string\', \'number\' and \'boolean\', found \'%s\'.'),
|
|
12
12
|
}
|
package/lib/file-generator.d.ts
CHANGED
|
@@ -4,27 +4,27 @@ export namespace FileGenerator {
|
|
|
4
4
|
export type FileGeneratorOptions = {
|
|
5
5
|
logger?: BaseLogger
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
export type FileObject = {
|
|
9
9
|
path: string,
|
|
10
10
|
file: string,
|
|
11
11
|
contents: string
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
|
|
14
14
|
export class FileGenerator {
|
|
15
15
|
files: FileObject[]
|
|
16
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
|
|
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
28
|
}
|
|
29
|
-
|
|
29
|
+
|
|
30
30
|
}
|
package/lib/file-generator.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use strict'
|
|
2
|
-
const {
|
|
2
|
+
const { createDirectory } = require('@platformatic/utils')
|
|
3
3
|
const { join } = require('node:path')
|
|
4
4
|
const { writeFile, readFile } = require('node:fs/promises')
|
|
5
5
|
|
|
@@ -9,7 +9,7 @@ const fakeLogger = {
|
|
|
9
9
|
warn: () => {},
|
|
10
10
|
debug: () => {},
|
|
11
11
|
trace: () => {},
|
|
12
|
-
error: () => {}
|
|
12
|
+
error: () => {},
|
|
13
13
|
}
|
|
14
14
|
/* c8 ignore start */
|
|
15
15
|
|
|
@@ -59,14 +59,14 @@ class FileGenerator {
|
|
|
59
59
|
if (!this.targetDirectory) {
|
|
60
60
|
throw new Error('No target directory set.')
|
|
61
61
|
}
|
|
62
|
-
await
|
|
62
|
+
await createDirectory(this.targetDirectory)
|
|
63
63
|
for (const fileToWrite of this.files) {
|
|
64
64
|
if (fileToWrite.contents.length === 0) {
|
|
65
65
|
continue
|
|
66
66
|
}
|
|
67
67
|
const baseDir = join(this.targetDirectory, fileToWrite.path)
|
|
68
68
|
if (fileToWrite.path !== '') {
|
|
69
|
-
await
|
|
69
|
+
await createDirectory(baseDir)
|
|
70
70
|
}
|
|
71
71
|
const fullFilePath = join(baseDir, fileToWrite.file)
|
|
72
72
|
await writeFile(fullFilePath, fileToWrite.contents, fileToWrite.options)
|
package/lib/utils.d.ts
CHANGED
|
@@ -9,14 +9,13 @@ export type PackageConfiguration = {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
export namespace GeneratorUtils {
|
|
12
|
-
export function
|
|
13
|
-
export function
|
|
14
|
-
export function
|
|
15
|
-
export function
|
|
16
|
-
export function
|
|
17
|
-
export function
|
|
18
|
-
export function
|
|
19
|
-
export function
|
|
20
|
-
export function getServiceTemplateFromSchemaUrl(schemaUrl: string): string
|
|
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
|
|
21
20
|
export const PLT_ROOT: string
|
|
22
21
|
}
|
package/lib/utils.js
CHANGED
|
@@ -1,33 +1,12 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
const { mkdir } = require('node:fs/promises')
|
|
4
3
|
const { WrongTypeError } = require('./errors')
|
|
5
4
|
const { join } = require('node:path')
|
|
6
5
|
const { request } = require('undici')
|
|
7
6
|
const { setTimeout } = require('timers/promises')
|
|
8
7
|
const PLT_ROOT = 'PLT_ROOT'
|
|
9
8
|
const { EOL } = require('node:os')
|
|
10
|
-
|
|
11
|
-
try {
|
|
12
|
-
await mkdir(dir, { recursive: true })
|
|
13
|
-
/* c8 ignore next 3 */
|
|
14
|
-
} catch (err) {
|
|
15
|
-
// do nothing
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Strip all extra characters from a simple semver version string
|
|
21
|
-
* @param {string} version
|
|
22
|
-
* @returns string
|
|
23
|
-
*/
|
|
24
|
-
function stripVersion (version) {
|
|
25
|
-
const match = version.match(/(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)/)
|
|
26
|
-
if (match) {
|
|
27
|
-
return match[0]
|
|
28
|
-
}
|
|
29
|
-
return version
|
|
30
|
-
}
|
|
9
|
+
const { createDirectory } = require('@platformatic/utils')
|
|
31
10
|
|
|
32
11
|
function convertServiceNameToPrefix (serviceName) {
|
|
33
12
|
return serviceName.replace(/-/g, '_').toUpperCase()
|
|
@@ -49,7 +28,7 @@ function addPrefixToString (input, prefix) {
|
|
|
49
28
|
|
|
50
29
|
function envObjectToString (env) {
|
|
51
30
|
const output = []
|
|
52
|
-
Object.entries(env).forEach(
|
|
31
|
+
Object.entries(env).forEach(kv => {
|
|
53
32
|
output.push(`${kv[0]}=${kv[1]}`)
|
|
54
33
|
})
|
|
55
34
|
return output.join(EOL)
|
|
@@ -57,12 +36,12 @@ function envObjectToString (env) {
|
|
|
57
36
|
|
|
58
37
|
function envStringToObject (envString) {
|
|
59
38
|
const output = {}
|
|
60
|
-
const split = envString.split(
|
|
39
|
+
const split = envString.split(/\r?\n/)
|
|
61
40
|
split
|
|
62
|
-
.filter(
|
|
41
|
+
.filter(line => {
|
|
63
42
|
return line.trim() !== '' && line.indexOf('#') !== 0
|
|
64
43
|
})
|
|
65
|
-
.forEach(
|
|
44
|
+
.forEach(line => {
|
|
66
45
|
const kv = line.split('=')
|
|
67
46
|
output[kv[0]] = kv[1]
|
|
68
47
|
})
|
|
@@ -71,16 +50,14 @@ function envStringToObject (envString) {
|
|
|
71
50
|
function extractEnvVariablesFromText (text) {
|
|
72
51
|
const match = text.match(/\{[a-zA-Z0-9-_]*\}/g)
|
|
73
52
|
if (match) {
|
|
74
|
-
return match
|
|
75
|
-
.map((found) => found.replace('{', '').replace('}', ''))
|
|
76
|
-
.filter((found) => found !== '')
|
|
53
|
+
return match.map(found => found.replace('{', '').replace('}', '')).filter(found => found !== '')
|
|
77
54
|
}
|
|
78
55
|
return []
|
|
79
56
|
}
|
|
80
57
|
function getPackageConfigurationObject (config, serviceName = '') {
|
|
81
58
|
const output = {
|
|
82
59
|
config: {},
|
|
83
|
-
env: {}
|
|
60
|
+
env: {},
|
|
84
61
|
}
|
|
85
62
|
let current = output.config
|
|
86
63
|
for (const param of config) {
|
|
@@ -90,14 +67,14 @@ function getPackageConfigurationObject (config, serviceName = '') {
|
|
|
90
67
|
let value
|
|
91
68
|
let isPath = false
|
|
92
69
|
switch (param.type) {
|
|
93
|
-
case 'string'
|
|
70
|
+
case 'string':
|
|
94
71
|
value = param.value.toString()
|
|
95
72
|
break
|
|
96
73
|
case 'number':
|
|
97
74
|
value = parseInt(param.value)
|
|
98
75
|
break
|
|
99
76
|
case 'boolean':
|
|
100
|
-
value =
|
|
77
|
+
value = param.value === 'true'
|
|
101
78
|
break
|
|
102
79
|
case 'path':
|
|
103
80
|
value = `${join(`{${PLT_ROOT}}`, param.value)}`
|
|
@@ -167,7 +144,7 @@ async function getLatestNpmVersion (pkg) {
|
|
|
167
144
|
function flattenObject (ob) {
|
|
168
145
|
const result = {}
|
|
169
146
|
for (const i in ob) {
|
|
170
|
-
if (
|
|
147
|
+
if (typeof ob[i] === 'object' && !Array.isArray(ob[i])) {
|
|
171
148
|
const temp = flattenObject(ob[i])
|
|
172
149
|
for (const j in temp) {
|
|
173
150
|
result[i + '.' + j] = temp[j]
|
|
@@ -181,8 +158,13 @@ function flattenObject (ob) {
|
|
|
181
158
|
|
|
182
159
|
function getServiceTemplateFromSchemaUrl (schemaUrl) {
|
|
183
160
|
const splitted = schemaUrl.split('/')
|
|
184
|
-
|
|
161
|
+
|
|
162
|
+
if (schemaUrl.startsWith('https://platformatic.dev/schemas')) {
|
|
163
|
+
return `@platformatic/${splitted[splitted.length - 1]}`
|
|
164
|
+
}
|
|
165
|
+
return `@platformatic/${splitted[splitted.length - 2]}`
|
|
185
166
|
}
|
|
167
|
+
|
|
186
168
|
module.exports = {
|
|
187
169
|
addPrefixToString,
|
|
188
170
|
convertServiceNameToPrefix,
|
|
@@ -192,8 +174,7 @@ module.exports = {
|
|
|
192
174
|
extractEnvVariablesFromText,
|
|
193
175
|
flattenObject,
|
|
194
176
|
getServiceTemplateFromSchemaUrl,
|
|
195
|
-
|
|
196
|
-
stripVersion,
|
|
177
|
+
createDirectory,
|
|
197
178
|
PLT_ROOT,
|
|
198
|
-
getLatestNpmVersion
|
|
179
|
+
getLatestNpmVersion,
|
|
199
180
|
}
|