@platformatic/generators 3.4.1 → 3.5.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.
- 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/base-generator.js
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { generateDashedName } from '@platformatic/foundation'
|
|
2
|
+
import { readFile } from 'node:fs/promises'
|
|
3
|
+
import { join } from 'node:path'
|
|
4
|
+
import { generateGitignore } from './create-gitignore.js'
|
|
5
|
+
import { MissingEnvVariable, ModuleNeeded, PrepareError } from './errors.js'
|
|
6
|
+
import { FileGenerator } from './file-generator.js'
|
|
7
|
+
import {
|
|
8
|
+
convertApplicationNameToPrefix,
|
|
9
|
+
envStringToObject,
|
|
6
10
|
extractEnvVariablesFromText,
|
|
11
|
+
flattenObject,
|
|
12
|
+
getApplicationTemplateFromSchemaUrl,
|
|
13
|
+
getLatestNpmVersion,
|
|
7
14
|
getPackageConfigurationObject,
|
|
8
15
|
PLT_ROOT,
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const { join } = require('node:path')
|
|
13
|
-
const { FileGenerator } = require('./file-generator')
|
|
14
|
-
const { generateTests, generatePlugins } = require('./create-plugin')
|
|
15
|
-
const { PrepareError, MissingEnvVariable, ModuleNeeded } = require('./errors')
|
|
16
|
-
const { generateGitignore } = require('./create-gitignore')
|
|
17
|
-
const generateName = require('boring-name-generator')
|
|
18
|
-
const { getServiceTemplateFromSchemaUrl } = require('./utils')
|
|
19
|
-
const { flattenObject } = require('./utils')
|
|
20
|
-
const { envStringToObject } = require('./utils')
|
|
16
|
+
stripVersion
|
|
17
|
+
} from './utils.js'
|
|
18
|
+
|
|
21
19
|
/* c8 ignore start */
|
|
22
20
|
const fakeLogger = {
|
|
23
21
|
info: () => {},
|
|
24
22
|
warn: () => {},
|
|
25
23
|
debug: () => {},
|
|
26
24
|
trace: () => {},
|
|
27
|
-
error: () => {}
|
|
25
|
+
error: () => {}
|
|
28
26
|
}
|
|
29
27
|
/* c8 ignore start */
|
|
30
28
|
|
|
29
|
+
const DEFAULT_SERVICES_PATH = 'applications'
|
|
30
|
+
|
|
31
31
|
class BaseGenerator extends FileGenerator {
|
|
32
32
|
constructor (opts = {}) {
|
|
33
33
|
super(opts)
|
|
@@ -40,11 +40,17 @@ class BaseGenerator extends FileGenerator {
|
|
|
40
40
|
this.config = this.getDefaultConfig()
|
|
41
41
|
this.packages = []
|
|
42
42
|
this.module = opts.module
|
|
43
|
+
this.runtime = null
|
|
44
|
+
this.runtimeConfig = opts.runtimeConfig ?? 'platformatic.json'
|
|
43
45
|
if (!this.module) {
|
|
44
46
|
throw ModuleNeeded()
|
|
45
47
|
}
|
|
46
48
|
}
|
|
47
49
|
|
|
50
|
+
setRuntime (runtime) {
|
|
51
|
+
this.runtime = runtime
|
|
52
|
+
}
|
|
53
|
+
|
|
48
54
|
getDefaultConfig () {
|
|
49
55
|
return {
|
|
50
56
|
port: 3042,
|
|
@@ -56,11 +62,11 @@ class BaseGenerator extends FileGenerator {
|
|
|
56
62
|
dependencies: {},
|
|
57
63
|
devDependencies: {},
|
|
58
64
|
isRuntimeContext: false,
|
|
59
|
-
|
|
65
|
+
applicationName: '',
|
|
60
66
|
envPrefix: '',
|
|
61
67
|
env: {},
|
|
62
68
|
defaultEnv: {},
|
|
63
|
-
isUpdating: false
|
|
69
|
+
isUpdating: false
|
|
64
70
|
}
|
|
65
71
|
}
|
|
66
72
|
|
|
@@ -71,9 +77,11 @@ class BaseGenerator extends FileGenerator {
|
|
|
71
77
|
setConfigFields (fields) {
|
|
72
78
|
const availableConfigFields = this.getConfigFieldsDefinitions()
|
|
73
79
|
function shouldHandleConfigField (field) {
|
|
74
|
-
return
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
return (
|
|
81
|
+
availableConfigFields.filter(f => {
|
|
82
|
+
return f.configValue === field.configValue && f.var === field.var
|
|
83
|
+
}).length > 0
|
|
84
|
+
)
|
|
77
85
|
}
|
|
78
86
|
for (const field of fields) {
|
|
79
87
|
if (shouldHandleConfigField(field)) {
|
|
@@ -137,16 +145,16 @@ class BaseGenerator extends FileGenerator {
|
|
|
137
145
|
this.config = {
|
|
138
146
|
...this.getDefaultConfig(),
|
|
139
147
|
...oldConfig,
|
|
140
|
-
...config
|
|
148
|
+
...config
|
|
141
149
|
}
|
|
142
150
|
|
|
143
151
|
if (this.config.isRuntimeContext) {
|
|
144
|
-
if (!this.config.
|
|
145
|
-
this.config.
|
|
152
|
+
if (!this.config.applicationName) {
|
|
153
|
+
this.config.applicationName = generateDashedName()
|
|
146
154
|
}
|
|
147
155
|
// set envPrefix
|
|
148
|
-
if (this.config.
|
|
149
|
-
this.config.envPrefix =
|
|
156
|
+
if (this.config.applicationName && !this.config.envPrefix) {
|
|
157
|
+
this.config.envPrefix = convertApplicationNameToPrefix(this.config.applicationName)
|
|
150
158
|
}
|
|
151
159
|
}
|
|
152
160
|
this.setEnvVars(this.config.env)
|
|
@@ -163,7 +171,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
163
171
|
const newConfig = await this.inquirer.prompt(this.questions)
|
|
164
172
|
this.setConfig({
|
|
165
173
|
...this.config,
|
|
166
|
-
...newConfig
|
|
174
|
+
...newConfig
|
|
167
175
|
})
|
|
168
176
|
}
|
|
169
177
|
}
|
|
@@ -175,8 +183,8 @@ class BaseGenerator extends FileGenerator {
|
|
|
175
183
|
if (this.config.isUpdating) {
|
|
176
184
|
// only the packages options may have changed, let's update those
|
|
177
185
|
await this.generateConfigFile()
|
|
178
|
-
const generatedConfigFile = JSON.parse(this.getFileObject(
|
|
179
|
-
const fileFromDisk = await this.loadFile({ file:
|
|
186
|
+
const generatedConfigFile = JSON.parse(this.getFileObject(this.runtimeConfig, '').contents)
|
|
187
|
+
const fileFromDisk = await this.loadFile({ file: this.runtimeConfig, path: '' })
|
|
180
188
|
const currentConfigFile = JSON.parse(fileFromDisk.contents)
|
|
181
189
|
if (currentConfigFile.plugins) {
|
|
182
190
|
if (generatedConfigFile.plugins && generatedConfigFile.plugins.packages) {
|
|
@@ -189,8 +197,8 @@ class BaseGenerator extends FileGenerator {
|
|
|
189
197
|
this.reset()
|
|
190
198
|
this.addFile({
|
|
191
199
|
path: '',
|
|
192
|
-
file:
|
|
193
|
-
contents: JSON.stringify(currentConfigFile, null, 2)
|
|
200
|
+
file: this.runtimeConfig,
|
|
201
|
+
contents: JSON.stringify(currentConfigFile, null, 2)
|
|
194
202
|
})
|
|
195
203
|
} else {
|
|
196
204
|
await this.getFastifyVersion()
|
|
@@ -203,31 +211,13 @@ class BaseGenerator extends FileGenerator {
|
|
|
203
211
|
this.addFile({
|
|
204
212
|
path: '',
|
|
205
213
|
file: 'package.json',
|
|
206
|
-
contents: JSON.stringify(template, null, 2)
|
|
214
|
+
contents: JSON.stringify(template, null, 2)
|
|
207
215
|
})
|
|
208
216
|
|
|
209
217
|
await this.generateConfigFile()
|
|
210
218
|
|
|
211
219
|
await this.generateEnv()
|
|
212
220
|
|
|
213
|
-
if (this.config.typescript) {
|
|
214
|
-
// create tsconfig.json
|
|
215
|
-
this.addFile({
|
|
216
|
-
path: '',
|
|
217
|
-
file: 'tsconfig.json',
|
|
218
|
-
contents: JSON.stringify(this.getTsConfig(), null, 2),
|
|
219
|
-
})
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
if (this.config.plugin) {
|
|
223
|
-
// create plugin
|
|
224
|
-
this.files.push(...generatePlugins(this.config.typescript))
|
|
225
|
-
if (this.config.tests) {
|
|
226
|
-
// create tests
|
|
227
|
-
this.files.push(...generateTests(this.config.typescript, this.module))
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
221
|
this.files.push(generateGitignore())
|
|
232
222
|
|
|
233
223
|
await this._afterPrepare()
|
|
@@ -236,7 +226,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
236
226
|
}
|
|
237
227
|
return {
|
|
238
228
|
targetDirectory: this.targetDirectory,
|
|
239
|
-
env: this.config.env
|
|
229
|
+
env: this.config.env
|
|
240
230
|
}
|
|
241
231
|
} catch (err) {
|
|
242
232
|
if (err.code?.startsWith('PLT_GEN')) {
|
|
@@ -251,7 +241,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
251
241
|
|
|
252
242
|
checkEnvVariablesInConfigFile () {
|
|
253
243
|
const excludedEnvs = [PLT_ROOT]
|
|
254
|
-
const configFileName =
|
|
244
|
+
const configFileName = this.runtimeConfig
|
|
255
245
|
const fileObject = this.getFileObject(configFileName)
|
|
256
246
|
const envVars = extractEnvVariablesFromText(fileObject.contents)
|
|
257
247
|
const envKeys = Object.keys(this.config.env)
|
|
@@ -269,30 +259,6 @@ class BaseGenerator extends FileGenerator {
|
|
|
269
259
|
return true
|
|
270
260
|
}
|
|
271
261
|
|
|
272
|
-
getTsConfig () {
|
|
273
|
-
return {
|
|
274
|
-
compilerOptions: {
|
|
275
|
-
module: 'commonjs',
|
|
276
|
-
esModuleInterop: true,
|
|
277
|
-
target: 'es2020',
|
|
278
|
-
sourceMap: true,
|
|
279
|
-
pretty: true,
|
|
280
|
-
noEmitOnError: true,
|
|
281
|
-
incremental: true,
|
|
282
|
-
strict: true,
|
|
283
|
-
outDir: 'dist',
|
|
284
|
-
skipLibCheck: true,
|
|
285
|
-
},
|
|
286
|
-
watchOptions: {
|
|
287
|
-
watchFile: 'fixedPollingInterval',
|
|
288
|
-
watchDirectory: 'fixedPollingInterval',
|
|
289
|
-
fallbackPolling: 'dynamicPriority',
|
|
290
|
-
synchronousWatchDirectory: true,
|
|
291
|
-
excludeDirectories: ['**/node_modules', 'dist'],
|
|
292
|
-
},
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
262
|
async prepareQuestions () {
|
|
297
263
|
if (!this.config.isRuntimeContext) {
|
|
298
264
|
if (!this.config.targetDirectory) {
|
|
@@ -300,48 +266,44 @@ class BaseGenerator extends FileGenerator {
|
|
|
300
266
|
this.questions.push({
|
|
301
267
|
type: 'input',
|
|
302
268
|
name: 'targetDirectory',
|
|
303
|
-
message: 'Where would you like to create your project?'
|
|
269
|
+
message: 'Where would you like to create your project?'
|
|
304
270
|
})
|
|
305
271
|
}
|
|
306
272
|
|
|
307
|
-
// typescript
|
|
308
|
-
this.questions.push({
|
|
309
|
-
type: 'list',
|
|
310
|
-
name: 'typescript',
|
|
311
|
-
message: 'Do you want to use TypeScript?',
|
|
312
|
-
default: false,
|
|
313
|
-
choices: [{ name: 'yes', value: true }, { name: 'no', value: false }],
|
|
314
|
-
})
|
|
315
|
-
|
|
316
273
|
// port
|
|
317
|
-
this.
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
274
|
+
if (!this.config.skipPort) {
|
|
275
|
+
this.questions.push({
|
|
276
|
+
type: 'input',
|
|
277
|
+
name: 'port',
|
|
278
|
+
message: 'What port do you want to use?'
|
|
279
|
+
})
|
|
280
|
+
}
|
|
322
281
|
}
|
|
323
282
|
}
|
|
324
283
|
|
|
325
284
|
async generateConfigFile () {
|
|
326
|
-
const configFileName =
|
|
285
|
+
const configFileName = this.runtimeConfig
|
|
327
286
|
const contents = await this._getConfigFileContents()
|
|
328
287
|
// handle packages
|
|
329
288
|
if (this.packages.length > 0) {
|
|
330
289
|
if (!contents.plugins) {
|
|
331
290
|
contents.plugins = {}
|
|
332
291
|
}
|
|
333
|
-
contents.plugins.packages = this.packages.map(
|
|
334
|
-
const packageConfigOutput = getPackageConfigurationObject(
|
|
292
|
+
contents.plugins.packages = this.packages.map(packageDefinition => {
|
|
293
|
+
const packageConfigOutput = getPackageConfigurationObject(
|
|
294
|
+
packageDefinition.options,
|
|
295
|
+
this.config.applicationName
|
|
296
|
+
)
|
|
335
297
|
if (Object.keys(packageConfigOutput.env).length > 0) {
|
|
336
298
|
const envForPackages = {}
|
|
337
|
-
Object.entries(packageConfigOutput.env).forEach(
|
|
299
|
+
Object.entries(packageConfigOutput.env).forEach(kv => {
|
|
338
300
|
envForPackages[kv[0]] = kv[1]
|
|
339
301
|
})
|
|
340
302
|
this.addEnvVars(envForPackages)
|
|
341
303
|
}
|
|
342
304
|
return {
|
|
343
305
|
name: packageDefinition.name,
|
|
344
|
-
options: packageConfigOutput.config
|
|
306
|
+
options: packageConfigOutput.config
|
|
345
307
|
}
|
|
346
308
|
})
|
|
347
309
|
}
|
|
@@ -349,8 +311,10 @@ class BaseGenerator extends FileGenerator {
|
|
|
349
311
|
this.addFile({
|
|
350
312
|
path: '',
|
|
351
313
|
file: configFileName,
|
|
352
|
-
contents: JSON.stringify(contents, null, 2)
|
|
314
|
+
contents: JSON.stringify(contents, null, 2)
|
|
353
315
|
})
|
|
316
|
+
|
|
317
|
+
return contents
|
|
354
318
|
}
|
|
355
319
|
|
|
356
320
|
/**
|
|
@@ -361,7 +325,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
361
325
|
if (this.pkgData) {
|
|
362
326
|
return this.pkgData
|
|
363
327
|
}
|
|
364
|
-
const currentPackageJsonPath = join(
|
|
328
|
+
const currentPackageJsonPath = join(import.meta.dirname, '..', 'package.json')
|
|
365
329
|
this.pkgData = JSON.parse(await readFile(currentPackageJsonPath, 'utf8'))
|
|
366
330
|
return this.pkgData
|
|
367
331
|
}
|
|
@@ -378,57 +342,61 @@ class BaseGenerator extends FileGenerator {
|
|
|
378
342
|
|
|
379
343
|
async generatePackageJson () {
|
|
380
344
|
const template = {
|
|
381
|
-
name: `${this.config.
|
|
345
|
+
name: `${this.config.applicationName}`,
|
|
382
346
|
scripts: {
|
|
383
347
|
start: 'platformatic start',
|
|
384
|
-
test: '
|
|
348
|
+
test: 'node --test'
|
|
385
349
|
},
|
|
386
350
|
devDependencies: {
|
|
387
351
|
fastify: `^${this.fastifyVersion}`,
|
|
388
|
-
|
|
389
|
-
...this.config.devDependencies,
|
|
352
|
+
...this.config.devDependencies
|
|
390
353
|
},
|
|
391
354
|
dependencies: {
|
|
392
|
-
...this.config.dependencies
|
|
355
|
+
...this.config.dependencies
|
|
393
356
|
},
|
|
394
357
|
engines: {
|
|
395
|
-
node: '
|
|
396
|
-
}
|
|
358
|
+
node: '>=22.19.0'
|
|
359
|
+
}
|
|
397
360
|
}
|
|
398
361
|
|
|
399
362
|
if (this.config.typescript) {
|
|
400
|
-
const typescriptVersion = JSON.parse(await readFile(join(
|
|
401
|
-
|
|
402
|
-
template.scripts.build = 'platformatic compile'
|
|
363
|
+
const typescriptVersion = JSON.parse(await readFile(join(import.meta.dirname, '..', 'package.json'), 'utf-8'))
|
|
364
|
+
.devDependencies.typescript
|
|
403
365
|
template.devDependencies.typescript = typescriptVersion
|
|
404
366
|
}
|
|
405
367
|
return template
|
|
406
368
|
}
|
|
407
369
|
|
|
408
370
|
async generateEnv () {
|
|
409
|
-
if (
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
file: '.env',
|
|
413
|
-
contents: serializeEnvVars(this.config.env),
|
|
414
|
-
})
|
|
371
|
+
if (this.config.isRuntimeContext) {
|
|
372
|
+
return
|
|
373
|
+
}
|
|
415
374
|
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
375
|
+
const serializedEnv = serializeEnvVars(this.config.env)
|
|
376
|
+
|
|
377
|
+
this.addFile({
|
|
378
|
+
path: '',
|
|
379
|
+
file: '.env',
|
|
380
|
+
contents: serializedEnv
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
const emptyEnvVars = {}
|
|
384
|
+
for (const envVarName of Object.keys(this.config.env)) {
|
|
385
|
+
if (!this.config.defaultEnv[envVarName]) {
|
|
386
|
+
emptyEnvVars[envVarName] = ''
|
|
421
387
|
}
|
|
388
|
+
}
|
|
422
389
|
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
}),
|
|
390
|
+
this.addFile({
|
|
391
|
+
path: '',
|
|
392
|
+
file: '.env.sample',
|
|
393
|
+
contents: serializeEnvVars({
|
|
394
|
+
...this.config.defaultEnv,
|
|
395
|
+
...emptyEnvVars
|
|
430
396
|
})
|
|
431
|
-
}
|
|
397
|
+
})
|
|
398
|
+
|
|
399
|
+
return serializedEnv
|
|
432
400
|
}
|
|
433
401
|
|
|
434
402
|
async run () {
|
|
@@ -450,31 +418,33 @@ class BaseGenerator extends FileGenerator {
|
|
|
450
418
|
this.packages.push(pkg)
|
|
451
419
|
}
|
|
452
420
|
|
|
453
|
-
async loadFromDir (
|
|
454
|
-
const runtimePkgConfigFileData = JSON.parse(await readFile(join(runtimeRootPath,
|
|
455
|
-
const
|
|
456
|
-
const
|
|
421
|
+
async loadFromDir (applicationName, runtimeRootPath) {
|
|
422
|
+
const runtimePkgConfigFileData = JSON.parse(await readFile(join(runtimeRootPath, this.runtimeConfig), 'utf-8'))
|
|
423
|
+
const applicationsPath = runtimePkgConfigFileData.autoload?.path ?? DEFAULT_SERVICES_PATH
|
|
424
|
+
const applicationPkgJsonFileData = JSON.parse(
|
|
425
|
+
await readFile(join(runtimeRootPath, applicationsPath, applicationName, 'platformatic.json'), 'utf-8')
|
|
426
|
+
)
|
|
457
427
|
const runtimeEnv = envStringToObject(await readFile(join(runtimeRootPath, '.env'), 'utf-8'))
|
|
458
|
-
const
|
|
428
|
+
const applicationNamePrefix = convertApplicationNameToPrefix(applicationName)
|
|
459
429
|
const plugins = []
|
|
460
|
-
if (
|
|
461
|
-
for (const pkg of
|
|
430
|
+
if (applicationPkgJsonFileData.plugins && applicationPkgJsonFileData.plugins.packages) {
|
|
431
|
+
for (const pkg of applicationPkgJsonFileData.plugins.packages) {
|
|
462
432
|
const flattened = flattenObject(pkg)
|
|
463
433
|
const output = {
|
|
464
434
|
name: flattened.name,
|
|
465
|
-
options: []
|
|
435
|
+
options: []
|
|
466
436
|
}
|
|
467
437
|
if (pkg.options) {
|
|
468
438
|
Object.entries(flattened)
|
|
469
439
|
.filter(([key, value]) => key.indexOf('options.') === 0 && flattened[key].startsWith('{PLT_'))
|
|
470
440
|
.forEach(([key, value]) => {
|
|
471
441
|
const runtimeEnvVarKey = value.replace(/[{}]/g, '')
|
|
472
|
-
const
|
|
442
|
+
const applicationEnvVarKey = runtimeEnvVarKey.replace(`PLT_${applicationNamePrefix}_`, '')
|
|
473
443
|
const option = {
|
|
474
|
-
name:
|
|
444
|
+
name: applicationEnvVarKey,
|
|
475
445
|
path: key.replace('options.', ''),
|
|
476
446
|
type: 'string',
|
|
477
|
-
value: runtimeEnv[runtimeEnvVarKey]
|
|
447
|
+
value: runtimeEnv[runtimeEnvVarKey]
|
|
478
448
|
}
|
|
479
449
|
output.options.push(option)
|
|
480
450
|
})
|
|
@@ -485,10 +455,10 @@ class BaseGenerator extends FileGenerator {
|
|
|
485
455
|
}
|
|
486
456
|
|
|
487
457
|
return {
|
|
488
|
-
name:
|
|
489
|
-
template:
|
|
458
|
+
name: applicationName,
|
|
459
|
+
template: getApplicationTemplateFromSchemaUrl(applicationPkgJsonFileData.$schema),
|
|
490
460
|
fields: [],
|
|
491
|
-
plugins
|
|
461
|
+
plugins
|
|
492
462
|
}
|
|
493
463
|
}
|
|
494
464
|
|
|
@@ -497,7 +467,9 @@ class BaseGenerator extends FileGenerator {
|
|
|
497
467
|
async postInstallActions () {}
|
|
498
468
|
async _beforePrepare () {}
|
|
499
469
|
async _afterPrepare () {}
|
|
500
|
-
async _getConfigFileContents () {
|
|
470
|
+
async _getConfigFileContents () {
|
|
471
|
+
return {}
|
|
472
|
+
}
|
|
501
473
|
}
|
|
502
474
|
|
|
503
475
|
function serializeEnvVars (envVars) {
|
|
@@ -509,5 +481,6 @@ function serializeEnvVars (envVars) {
|
|
|
509
481
|
return envVarsString
|
|
510
482
|
}
|
|
511
483
|
|
|
512
|
-
|
|
513
|
-
|
|
484
|
+
export default BaseGenerator
|
|
485
|
+
const _BaseGenerator = BaseGenerator
|
|
486
|
+
export { _BaseGenerator as BaseGenerator }
|
package/lib/create-gitignore.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
1
|
const gitignore = `\
|
|
4
2
|
dist
|
|
5
3
|
.DS_Store
|
|
@@ -29,14 +27,11 @@ tags
|
|
|
29
27
|
# clinicjs
|
|
30
28
|
.clinic/
|
|
31
29
|
`
|
|
32
|
-
|
|
30
|
+
|
|
31
|
+
export function generateGitignore () {
|
|
33
32
|
return {
|
|
34
33
|
path: '',
|
|
35
34
|
file: '.gitignore',
|
|
36
|
-
contents: gitignore
|
|
35
|
+
contents: gitignore
|
|
37
36
|
}
|
|
38
37
|
}
|
|
39
|
-
|
|
40
|
-
module.exports = {
|
|
41
|
-
generateGitignore,
|
|
42
|
-
}
|
package/lib/errors.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import createError from '@fastify/error'
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
export const ERROR_PREFIX = 'PLT_GEN'
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
module
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
export const ModuleNeeded = createError(
|
|
6
|
+
`${ERROR_PREFIX}_PREPARE_ERROR`,
|
|
7
|
+
'The module which the package will be published to must be specified'
|
|
8
|
+
)
|
|
9
|
+
export const PrepareError = createError(`${ERROR_PREFIX}_PREPARE_ERROR`, 'Error while generating the files: %s.')
|
|
10
|
+
export const MissingEnvVariable = createError(
|
|
11
|
+
`${ERROR_PREFIX}_MISSING_ENV_VAR`,
|
|
12
|
+
'Env variable %s is defined in config file %s, but not in config.env object.'
|
|
13
|
+
)
|
|
14
|
+
export const WrongTypeError = createError(
|
|
15
|
+
`${ERROR_PREFIX}_WRONG_TYPE`,
|
|
16
|
+
"Invalid value type. Accepted values are 'string', 'number' and 'boolean', found '%s'."
|
|
17
|
+
)
|
package/lib/file-generator.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const { writeFile, readFile } = require('node:fs/promises')
|
|
1
|
+
import { createDirectory } from '@platformatic/foundation'
|
|
2
|
+
import { readFile, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { dirname, isAbsolute, join } from 'node:path'
|
|
5
4
|
|
|
6
5
|
/* c8 ignore start */
|
|
7
6
|
const fakeLogger = {
|
|
@@ -9,11 +8,11 @@ const fakeLogger = {
|
|
|
9
8
|
warn: () => {},
|
|
10
9
|
debug: () => {},
|
|
11
10
|
trace: () => {},
|
|
12
|
-
error: () => {}
|
|
11
|
+
error: () => {}
|
|
13
12
|
}
|
|
14
13
|
/* c8 ignore start */
|
|
15
14
|
|
|
16
|
-
class FileGenerator {
|
|
15
|
+
export class FileGenerator {
|
|
17
16
|
constructor (opts = {}) {
|
|
18
17
|
this.files = []
|
|
19
18
|
this.logger = opts.logger || fakeLogger
|
|
@@ -31,15 +30,16 @@ class FileGenerator {
|
|
|
31
30
|
return this.getFileObject(file, path)
|
|
32
31
|
}
|
|
33
32
|
|
|
34
|
-
addFile ({ path, file, contents, options = {} }) {
|
|
33
|
+
addFile ({ path, file, contents, options = {}, tags }) {
|
|
35
34
|
const fileObject = this.getFileObject(file, path)
|
|
36
35
|
if (path.startsWith('/')) {
|
|
37
36
|
path = path.substring(1)
|
|
38
37
|
}
|
|
39
38
|
if (fileObject) {
|
|
40
39
|
fileObject.contents = contents
|
|
40
|
+
fileObject.tags = tags ?? []
|
|
41
41
|
} else {
|
|
42
|
-
this.files.push({ path, file, contents, options })
|
|
42
|
+
this.files.push({ path, file, contents, options, tags: tags ?? [] })
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
@@ -61,29 +61,35 @@ class FileGenerator {
|
|
|
61
61
|
}
|
|
62
62
|
await createDirectory(this.targetDirectory)
|
|
63
63
|
for (const fileToWrite of this.files) {
|
|
64
|
-
if (fileToWrite.contents.length === 0) {
|
|
64
|
+
if (fileToWrite.contents.length === 0 && !fileToWrite.file.match(/^\..+keep$/)) {
|
|
65
65
|
continue
|
|
66
66
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
|
|
68
|
+
let fullFilePath = join(fileToWrite.path, fileToWrite.file)
|
|
69
|
+
|
|
70
|
+
if (!isAbsolute(fullFilePath)) {
|
|
71
|
+
fullFilePath = join(this.targetDirectory, fullFilePath)
|
|
70
72
|
}
|
|
71
|
-
|
|
73
|
+
|
|
74
|
+
await createDirectory(dirname(fullFilePath))
|
|
72
75
|
await writeFile(fullFilePath, fileToWrite.contents, fileToWrite.options)
|
|
76
|
+
|
|
73
77
|
this.logger.info(`${fullFilePath} written!`)
|
|
74
78
|
}
|
|
75
79
|
}
|
|
76
80
|
|
|
77
81
|
getFileObject (name, path = '') {
|
|
78
|
-
const output = this.files.find(
|
|
82
|
+
const output = this.files.find(file => {
|
|
79
83
|
return file.path === path && file.file === name
|
|
80
84
|
})
|
|
81
|
-
if (!output) {
|
|
85
|
+
if (!output) {
|
|
86
|
+
return null
|
|
87
|
+
}
|
|
82
88
|
return output
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
listFiles () {
|
|
86
|
-
return this.files.map(
|
|
92
|
+
return this.files.map(fileObject => {
|
|
87
93
|
return join(fileObject.path, fileObject.file)
|
|
88
94
|
})
|
|
89
95
|
}
|
|
@@ -92,6 +98,3 @@ class FileGenerator {
|
|
|
92
98
|
this.files = []
|
|
93
99
|
}
|
|
94
100
|
}
|
|
95
|
-
|
|
96
|
-
module.exports = FileGenerator
|
|
97
|
-
module.exports.FileGenerator = FileGenerator
|