@platformatic/generators 3.0.0-alpha.5 → 3.0.0-alpha.6
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.js +3 -11
- package/lib/base-generator.d.ts +1 -1
- package/lib/base-generator.js +42 -40
- package/lib/create-gitignore.js +3 -8
- package/lib/errors.js +15 -10
- package/lib/file-generator.js +4 -8
- package/lib/import-generator.js +21 -25
- package/lib/utils.d.ts +3 -2
- package/lib/utils.js +21 -35
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@ Generates a Platformatic app programmatically.
|
|
|
7
7
|
The base class `BaseGenerator` is basically a file writer with some default behaviours.
|
|
8
8
|
|
|
9
9
|
It should be instantiated with an `options` object having this properties
|
|
10
|
-
- `type`: `"db" | "service" | "
|
|
10
|
+
- `type`: `"db" | "service" | "gateway"`
|
|
11
11
|
- `logger`: A pino-like logger object. If not provided a fake logger with no output will be used
|
|
12
12
|
- `questions`: An array of custom questions object to provide to `inquirer`. Default to `[]`
|
|
13
13
|
|
package/eslint.config.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import neostandard from 'neostandard'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export default neostandard({ ts: true })
|
package/index.js
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const { ImportGenerator } = require('./lib/import-generator')
|
|
5
|
-
const utils = require('./lib/utils')
|
|
6
|
-
|
|
7
|
-
module.exports = {
|
|
8
|
-
BaseGenerator,
|
|
9
|
-
ImportGenerator,
|
|
10
|
-
...utils
|
|
11
|
-
}
|
|
1
|
+
export { BaseGenerator } from './lib/base-generator.js'
|
|
2
|
+
export { ImportGenerator } from './lib/import-generator.js'
|
|
3
|
+
export * from './lib/utils.js'
|
package/lib/base-generator.d.ts
CHANGED
package/lib/base-generator.js
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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,
|
|
7
10
|
extractEnvVariablesFromText,
|
|
11
|
+
flattenObject,
|
|
12
|
+
getApplicationTemplateFromSchemaUrl,
|
|
13
|
+
getLatestNpmVersion,
|
|
8
14
|
getPackageConfigurationObject,
|
|
9
15
|
PLT_ROOT,
|
|
10
|
-
getLatestNpmVersion,
|
|
11
16
|
stripVersion
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
const { FileGenerator } = require('./file-generator')
|
|
15
|
-
const { PrepareError, MissingEnvVariable, ModuleNeeded } = require('./errors')
|
|
16
|
-
const { generateGitignore } = require('./create-gitignore')
|
|
17
|
-
const { getServiceTemplateFromSchemaUrl } = require('./utils')
|
|
18
|
-
const { flattenObject } = require('./utils')
|
|
19
|
-
const { envStringToObject } = require('./utils')
|
|
17
|
+
} from './utils.js'
|
|
18
|
+
|
|
20
19
|
/* c8 ignore start */
|
|
21
20
|
const fakeLogger = {
|
|
22
21
|
info: () => {},
|
|
@@ -27,7 +26,7 @@ const fakeLogger = {
|
|
|
27
26
|
}
|
|
28
27
|
/* c8 ignore start */
|
|
29
28
|
|
|
30
|
-
const DEFAULT_SERVICES_PATH = '
|
|
29
|
+
const DEFAULT_SERVICES_PATH = 'applications'
|
|
31
30
|
|
|
32
31
|
class BaseGenerator extends FileGenerator {
|
|
33
32
|
constructor (opts = {}) {
|
|
@@ -63,7 +62,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
63
62
|
dependencies: {},
|
|
64
63
|
devDependencies: {},
|
|
65
64
|
isRuntimeContext: false,
|
|
66
|
-
|
|
65
|
+
applicationName: '',
|
|
67
66
|
envPrefix: '',
|
|
68
67
|
env: {},
|
|
69
68
|
defaultEnv: {},
|
|
@@ -150,12 +149,12 @@ class BaseGenerator extends FileGenerator {
|
|
|
150
149
|
}
|
|
151
150
|
|
|
152
151
|
if (this.config.isRuntimeContext) {
|
|
153
|
-
if (!this.config.
|
|
154
|
-
this.config.
|
|
152
|
+
if (!this.config.applicationName) {
|
|
153
|
+
this.config.applicationName = generateDashedName()
|
|
155
154
|
}
|
|
156
155
|
// set envPrefix
|
|
157
|
-
if (this.config.
|
|
158
|
-
this.config.envPrefix =
|
|
156
|
+
if (this.config.applicationName && !this.config.envPrefix) {
|
|
157
|
+
this.config.envPrefix = convertApplicationNameToPrefix(this.config.applicationName)
|
|
159
158
|
}
|
|
160
159
|
}
|
|
161
160
|
this.setEnvVars(this.config.env)
|
|
@@ -291,7 +290,10 @@ class BaseGenerator extends FileGenerator {
|
|
|
291
290
|
contents.plugins = {}
|
|
292
291
|
}
|
|
293
292
|
contents.plugins.packages = this.packages.map(packageDefinition => {
|
|
294
|
-
const packageConfigOutput = getPackageConfigurationObject(
|
|
293
|
+
const packageConfigOutput = getPackageConfigurationObject(
|
|
294
|
+
packageDefinition.options,
|
|
295
|
+
this.config.applicationName
|
|
296
|
+
)
|
|
295
297
|
if (Object.keys(packageConfigOutput.env).length > 0) {
|
|
296
298
|
const envForPackages = {}
|
|
297
299
|
Object.entries(packageConfigOutput.env).forEach(kv => {
|
|
@@ -323,7 +325,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
323
325
|
if (this.pkgData) {
|
|
324
326
|
return this.pkgData
|
|
325
327
|
}
|
|
326
|
-
const currentPackageJsonPath = join(
|
|
328
|
+
const currentPackageJsonPath = join(import.meta.dirname, '..', 'package.json')
|
|
327
329
|
this.pkgData = JSON.parse(await readFile(currentPackageJsonPath, 'utf8'))
|
|
328
330
|
return this.pkgData
|
|
329
331
|
}
|
|
@@ -340,14 +342,13 @@ class BaseGenerator extends FileGenerator {
|
|
|
340
342
|
|
|
341
343
|
async generatePackageJson () {
|
|
342
344
|
const template = {
|
|
343
|
-
name: `${this.config.
|
|
345
|
+
name: `${this.config.applicationName}`,
|
|
344
346
|
scripts: {
|
|
345
347
|
start: 'platformatic start',
|
|
346
|
-
test: '
|
|
348
|
+
test: 'node --test'
|
|
347
349
|
},
|
|
348
350
|
devDependencies: {
|
|
349
351
|
fastify: `^${this.fastifyVersion}`,
|
|
350
|
-
borp: `${this.pkgData.devDependencies.borp}`,
|
|
351
352
|
...this.config.devDependencies
|
|
352
353
|
},
|
|
353
354
|
dependencies: {
|
|
@@ -359,7 +360,7 @@ class BaseGenerator extends FileGenerator {
|
|
|
359
360
|
}
|
|
360
361
|
|
|
361
362
|
if (this.config.typescript) {
|
|
362
|
-
const typescriptVersion = JSON.parse(await readFile(join(
|
|
363
|
+
const typescriptVersion = JSON.parse(await readFile(join(import.meta.dirname, '..', 'package.json'), 'utf-8'))
|
|
363
364
|
.devDependencies.typescript
|
|
364
365
|
template.devDependencies.typescript = typescriptVersion
|
|
365
366
|
}
|
|
@@ -417,17 +418,17 @@ class BaseGenerator extends FileGenerator {
|
|
|
417
418
|
this.packages.push(pkg)
|
|
418
419
|
}
|
|
419
420
|
|
|
420
|
-
async loadFromDir (
|
|
421
|
+
async loadFromDir (applicationName, runtimeRootPath) {
|
|
421
422
|
const runtimePkgConfigFileData = JSON.parse(await readFile(join(runtimeRootPath, this.runtimeConfig), 'utf-8'))
|
|
422
|
-
const
|
|
423
|
-
const
|
|
424
|
-
await readFile(join(runtimeRootPath,
|
|
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')
|
|
425
426
|
)
|
|
426
427
|
const runtimeEnv = envStringToObject(await readFile(join(runtimeRootPath, '.env'), 'utf-8'))
|
|
427
|
-
const
|
|
428
|
+
const applicationNamePrefix = convertApplicationNameToPrefix(applicationName)
|
|
428
429
|
const plugins = []
|
|
429
|
-
if (
|
|
430
|
-
for (const pkg of
|
|
430
|
+
if (applicationPkgJsonFileData.plugins && applicationPkgJsonFileData.plugins.packages) {
|
|
431
|
+
for (const pkg of applicationPkgJsonFileData.plugins.packages) {
|
|
431
432
|
const flattened = flattenObject(pkg)
|
|
432
433
|
const output = {
|
|
433
434
|
name: flattened.name,
|
|
@@ -438,9 +439,9 @@ class BaseGenerator extends FileGenerator {
|
|
|
438
439
|
.filter(([key, value]) => key.indexOf('options.') === 0 && flattened[key].startsWith('{PLT_'))
|
|
439
440
|
.forEach(([key, value]) => {
|
|
440
441
|
const runtimeEnvVarKey = value.replace(/[{}]/g, '')
|
|
441
|
-
const
|
|
442
|
+
const applicationEnvVarKey = runtimeEnvVarKey.replace(`PLT_${applicationNamePrefix}_`, '')
|
|
442
443
|
const option = {
|
|
443
|
-
name:
|
|
444
|
+
name: applicationEnvVarKey,
|
|
444
445
|
path: key.replace('options.', ''),
|
|
445
446
|
type: 'string',
|
|
446
447
|
value: runtimeEnv[runtimeEnvVarKey]
|
|
@@ -454,8 +455,8 @@ class BaseGenerator extends FileGenerator {
|
|
|
454
455
|
}
|
|
455
456
|
|
|
456
457
|
return {
|
|
457
|
-
name:
|
|
458
|
-
template:
|
|
458
|
+
name: applicationName,
|
|
459
|
+
template: getApplicationTemplateFromSchemaUrl(applicationPkgJsonFileData.$schema),
|
|
459
460
|
fields: [],
|
|
460
461
|
plugins
|
|
461
462
|
}
|
|
@@ -480,5 +481,6 @@ function serializeEnvVars (envVars) {
|
|
|
480
481
|
return envVarsString
|
|
481
482
|
}
|
|
482
483
|
|
|
483
|
-
|
|
484
|
-
|
|
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 = {
|
|
@@ -13,7 +12,7 @@ const fakeLogger = {
|
|
|
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
|
|
@@ -99,6 +98,3 @@ class FileGenerator {
|
|
|
99
98
|
this.files = []
|
|
100
99
|
}
|
|
101
100
|
}
|
|
102
|
-
|
|
103
|
-
module.exports = FileGenerator
|
|
104
|
-
module.exports.FileGenerator = FileGenerator
|
package/lib/import-generator.js
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
|
|
1
|
+
import { findConfigurationFileRecursive, safeRemove } from '@platformatic/foundation'
|
|
2
|
+
import { spawnSync } from 'node:child_process'
|
|
3
|
+
import { readFile, readdir, stat } from 'node:fs/promises'
|
|
4
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
5
|
+
import { BaseGenerator } from './base-generator.js'
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
const { BaseGenerator } = require('./base-generator')
|
|
5
|
-
const { spawnSync } = require('node:child_process')
|
|
6
|
-
const { stat, readFile, readdir } = require('node:fs/promises')
|
|
7
|
-
const { join, dirname, resolve, relative } = require('node:path')
|
|
8
|
-
|
|
9
|
-
class ImportGenerator extends BaseGenerator {
|
|
7
|
+
export class ImportGenerator extends BaseGenerator {
|
|
10
8
|
constructor (options = {}) {
|
|
11
|
-
const {
|
|
9
|
+
const { applicationName, module, version, parent: runtime, ...opts } = options
|
|
12
10
|
super({ ...opts, module })
|
|
13
11
|
|
|
14
12
|
this.runtime = runtime
|
|
15
13
|
this.setConfig({
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
applicationName,
|
|
15
|
+
applicationPathEnvName: `PLT_APPLICATION_${applicationName.toUpperCase().replaceAll(/[^A-Z0-9_]/g, '_')}_PATH`,
|
|
18
16
|
module,
|
|
19
17
|
version
|
|
20
18
|
})
|
|
@@ -83,9 +81,9 @@ class ImportGenerator extends BaseGenerator {
|
|
|
83
81
|
}
|
|
84
82
|
|
|
85
83
|
async _afterWriteFiles (runtime) {
|
|
86
|
-
// No need for an empty folder in the
|
|
84
|
+
// No need for an empty folder in the applications folder
|
|
87
85
|
if (this.config.operation === 'import') {
|
|
88
|
-
await safeRemove(join(runtime.
|
|
86
|
+
await safeRemove(join(runtime.applicationsBasePath, this.config.applicationName))
|
|
89
87
|
}
|
|
90
88
|
}
|
|
91
89
|
|
|
@@ -168,9 +166,9 @@ class ImportGenerator extends BaseGenerator {
|
|
|
168
166
|
/* c8 ignore next - else */
|
|
169
167
|
let env = envObject?.contents ?? ''
|
|
170
168
|
|
|
171
|
-
// Find which key is being used for the manual
|
|
169
|
+
// Find which key is being used for the manual applications
|
|
172
170
|
let key
|
|
173
|
-
for (const candidate of new Set([runtime.
|
|
171
|
+
for (const candidate of new Set([runtime.applicationsFolder, 'applications', 'services', 'web'])) {
|
|
174
172
|
if (Array.isArray(config[candidate])) {
|
|
175
173
|
key = candidate
|
|
176
174
|
break
|
|
@@ -178,23 +176,23 @@ class ImportGenerator extends BaseGenerator {
|
|
|
178
176
|
}
|
|
179
177
|
|
|
180
178
|
/* c8 ignore next - else */
|
|
181
|
-
key ??= runtime.
|
|
182
|
-
const
|
|
179
|
+
key ??= runtime.applicationsFolder ?? 'applications'
|
|
180
|
+
const applications = config[key] ?? []
|
|
183
181
|
|
|
184
|
-
if (!
|
|
185
|
-
|
|
186
|
-
id: this.config.
|
|
187
|
-
path: `{${this.config.
|
|
182
|
+
if (!applications.some(application => application.id === this.config.applicationName)) {
|
|
183
|
+
applications.push({
|
|
184
|
+
id: this.config.applicationName,
|
|
185
|
+
path: `{${this.config.applicationPathEnvName}}`,
|
|
188
186
|
url: this.config.gitUrl
|
|
189
187
|
})
|
|
190
188
|
}
|
|
191
189
|
|
|
192
|
-
config[key] =
|
|
190
|
+
config[key] = applications
|
|
193
191
|
|
|
194
192
|
if (env.length > 0) {
|
|
195
193
|
env += '\n'
|
|
196
194
|
}
|
|
197
|
-
env += `${this.config.
|
|
195
|
+
env += `${this.config.applicationPathEnvName}=${this.config.applicationPath}`
|
|
198
196
|
|
|
199
197
|
runtime.updateRuntimeConfig(config)
|
|
200
198
|
runtime.updateRuntimeEnv(env)
|
|
@@ -228,5 +226,3 @@ class ImportGenerator extends BaseGenerator {
|
|
|
228
226
|
}
|
|
229
227
|
}
|
|
230
228
|
}
|
|
231
|
-
|
|
232
|
-
module.exports = { ImportGenerator }
|
package/lib/utils.d.ts
CHANGED
|
@@ -10,12 +10,13 @@ export type PackageConfiguration = {
|
|
|
10
10
|
|
|
11
11
|
export namespace GeneratorUtils {
|
|
12
12
|
export function stripVersion (version: string): string
|
|
13
|
-
export function
|
|
13
|
+
export function convertApplicationNameToPrefix (applicationName: string): string
|
|
14
|
+
export function addPrefixToServiceName (applicationName: string, prefix: string): string
|
|
14
15
|
export function envObjectToString (env: Env): string
|
|
15
16
|
export function envStringToObject (env: string): Env
|
|
16
17
|
export function extractEnvVariablesFromText (text: string): string[]
|
|
17
18
|
export function getPackageConfigurationObject (config: PackageConfiguration[]): object
|
|
18
19
|
export function flattenObject (obj: object): object
|
|
19
|
-
export function
|
|
20
|
+
export function getApplicationTemplateFromSchemaUrl (schemaUrl: string): string
|
|
20
21
|
export const PLT_ROOT: string
|
|
21
22
|
}
|
package/lib/utils.js
CHANGED
|
@@ -1,19 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
import { EOL } from 'node:os'
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { setTimeout } from 'timers/promises'
|
|
4
|
+
import { request } from 'undici'
|
|
5
|
+
import { WrongTypeError } from './errors.js'
|
|
2
6
|
|
|
3
|
-
const
|
|
4
|
-
const { join } = require('node:path')
|
|
5
|
-
const { request } = require('undici')
|
|
6
|
-
const { setTimeout } = require('timers/promises')
|
|
7
|
-
const PLT_ROOT = 'PLT_ROOT'
|
|
8
|
-
const { EOL } = require('node:os')
|
|
9
|
-
const { createDirectory } = require('@platformatic/foundation')
|
|
7
|
+
export const PLT_ROOT = 'PLT_ROOT'
|
|
10
8
|
|
|
11
9
|
/**
|
|
12
10
|
* Strip all extra characters from a simple semver version string
|
|
13
11
|
* @param {string} version
|
|
14
12
|
* @returns string
|
|
15
13
|
*/
|
|
16
|
-
function stripVersion (version) {
|
|
14
|
+
export function stripVersion (version) {
|
|
17
15
|
const match = version.match(/(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)/)
|
|
18
16
|
if (match) {
|
|
19
17
|
return match[0]
|
|
@@ -22,11 +20,11 @@ function stripVersion (version) {
|
|
|
22
20
|
return version
|
|
23
21
|
}
|
|
24
22
|
|
|
25
|
-
function
|
|
26
|
-
return
|
|
23
|
+
export function convertApplicationNameToPrefix (applicationName) {
|
|
24
|
+
return applicationName.replace(/-/g, '_').toUpperCase()
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
function addPrefixToString (input, prefix) {
|
|
27
|
+
export function addPrefixToString (input, prefix) {
|
|
30
28
|
if (!prefix) {
|
|
31
29
|
return input
|
|
32
30
|
}
|
|
@@ -40,7 +38,7 @@ function addPrefixToString (input, prefix) {
|
|
|
40
38
|
}
|
|
41
39
|
}
|
|
42
40
|
|
|
43
|
-
function envObjectToString (env) {
|
|
41
|
+
export function envObjectToString (env) {
|
|
44
42
|
const output = []
|
|
45
43
|
Object.entries(env).forEach(kv => {
|
|
46
44
|
output.push(`${kv[0]}=${kv[1]}`)
|
|
@@ -48,7 +46,7 @@ function envObjectToString (env) {
|
|
|
48
46
|
return output.join(EOL)
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
function envStringToObject (envString) {
|
|
49
|
+
export function envStringToObject (envString) {
|
|
52
50
|
const output = {}
|
|
53
51
|
const split = envString.split(/\r?\n/)
|
|
54
52
|
split
|
|
@@ -61,14 +59,16 @@ function envStringToObject (envString) {
|
|
|
61
59
|
})
|
|
62
60
|
return output
|
|
63
61
|
}
|
|
64
|
-
|
|
62
|
+
|
|
63
|
+
export function extractEnvVariablesFromText (text) {
|
|
65
64
|
const match = text.match(/\{[a-zA-Z0-9-_]*\}/g)
|
|
66
65
|
if (match) {
|
|
67
66
|
return match.map(found => found.replace('{', '').replace('}', '')).filter(found => found !== '')
|
|
68
67
|
}
|
|
69
68
|
return []
|
|
70
69
|
}
|
|
71
|
-
|
|
70
|
+
|
|
71
|
+
export function getPackageConfigurationObject (config, applicationName = '') {
|
|
72
72
|
const output = {
|
|
73
73
|
config: {},
|
|
74
74
|
env: {}
|
|
@@ -100,7 +100,7 @@ function getPackageConfigurationObject (config, serviceName = '') {
|
|
|
100
100
|
if (!param.name) {
|
|
101
101
|
current[prop] = value
|
|
102
102
|
} else {
|
|
103
|
-
const key = addPrefixToString(param.name,
|
|
103
|
+
const key = addPrefixToString(param.name, convertApplicationNameToPrefix(applicationName))
|
|
104
104
|
// If it's a path, we need to add it to the env only the relative part of the path
|
|
105
105
|
if (isPath) {
|
|
106
106
|
current[prop] = `${join(`{${PLT_ROOT}}`, `{${key}}`)}`
|
|
@@ -122,7 +122,7 @@ function getPackageConfigurationObject (config, serviceName = '') {
|
|
|
122
122
|
return output
|
|
123
123
|
}
|
|
124
124
|
|
|
125
|
-
async function getLatestNpmVersion (pkg) {
|
|
125
|
+
export async function getLatestNpmVersion (pkg) {
|
|
126
126
|
const npmCall = request(`https://registry.npmjs.org/${pkg}`)
|
|
127
127
|
const timeout = setTimeout(1000, null)
|
|
128
128
|
const res = await Promise.race([npmCall, timeout])
|
|
@@ -136,6 +136,7 @@ async function getLatestNpmVersion (pkg) {
|
|
|
136
136
|
}
|
|
137
137
|
return null
|
|
138
138
|
}
|
|
139
|
+
|
|
139
140
|
/**
|
|
140
141
|
* Flatten a deep-nested object to a single level depth one
|
|
141
142
|
* i.e from
|
|
@@ -155,7 +156,7 @@ async function getLatestNpmVersion (pkg) {
|
|
|
155
156
|
* @param {Object} ob
|
|
156
157
|
* @returns Object
|
|
157
158
|
*/
|
|
158
|
-
function flattenObject (ob) {
|
|
159
|
+
export function flattenObject (ob) {
|
|
159
160
|
const result = {}
|
|
160
161
|
for (const i in ob) {
|
|
161
162
|
if (typeof ob[i] === 'object' && !Array.isArray(ob[i])) {
|
|
@@ -170,7 +171,7 @@ function flattenObject (ob) {
|
|
|
170
171
|
return result
|
|
171
172
|
}
|
|
172
173
|
|
|
173
|
-
function
|
|
174
|
+
export function getApplicationTemplateFromSchemaUrl (schemaUrl) {
|
|
174
175
|
const splitted = schemaUrl.split('/')
|
|
175
176
|
|
|
176
177
|
/* c8 ignore next 3 - Legacy interface */
|
|
@@ -179,18 +180,3 @@ function getServiceTemplateFromSchemaUrl (schemaUrl) {
|
|
|
179
180
|
}
|
|
180
181
|
return `@platformatic/${splitted[splitted.length - 2]}`
|
|
181
182
|
}
|
|
182
|
-
|
|
183
|
-
module.exports = {
|
|
184
|
-
addPrefixToString,
|
|
185
|
-
convertServiceNameToPrefix,
|
|
186
|
-
getPackageConfigurationObject,
|
|
187
|
-
envObjectToString,
|
|
188
|
-
envStringToObject,
|
|
189
|
-
extractEnvVariablesFromText,
|
|
190
|
-
flattenObject,
|
|
191
|
-
getServiceTemplateFromSchemaUrl,
|
|
192
|
-
createDirectory,
|
|
193
|
-
stripVersion,
|
|
194
|
-
PLT_ROOT,
|
|
195
|
-
getLatestNpmVersion
|
|
196
|
-
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platformatic/generators",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.6",
|
|
4
4
|
"description": "Main classes and utils for generators.",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"types": "index.d.ts",
|
|
6
8
|
"keywords": [],
|
|
7
9
|
"author": "Platformatic Inc. <oss@platformatic.dev> (https://platformatic.dev)",
|
|
8
10
|
"license": "Apache-2.0",
|
|
@@ -13,12 +15,12 @@
|
|
|
13
15
|
"fastify": "^5.0.0",
|
|
14
16
|
"pino": "^9.9.0",
|
|
15
17
|
"undici": "^7.0.0",
|
|
16
|
-
"@platformatic/foundation": "3.0.0-alpha.
|
|
18
|
+
"@platformatic/foundation": "3.0.0-alpha.6"
|
|
17
19
|
},
|
|
18
20
|
"devDependencies": {
|
|
19
21
|
"@types/inquirer": "^9.0.7",
|
|
20
|
-
"borp": "^0.20.0",
|
|
21
22
|
"c8": "^10.0.0",
|
|
23
|
+
"cleaner-spec-reporter": "^0.5.0",
|
|
22
24
|
"eslint": "9",
|
|
23
25
|
"neostandard": "^0.12.0",
|
|
24
26
|
"tsd": "^0.32.0",
|
|
@@ -29,6 +31,7 @@
|
|
|
29
31
|
},
|
|
30
32
|
"scripts": {
|
|
31
33
|
"lint": "eslint",
|
|
32
|
-
"test": "
|
|
34
|
+
"test": "node --test --test-reporter=cleaner-spec-reporter --test-concurrency=1 --test-timeout=2000000 test/*.test.js test/**/*.test.js",
|
|
35
|
+
"posttest": "tsd"
|
|
33
36
|
}
|
|
34
37
|
}
|