@platformatic/generators
Advanced tools
Comparing version 1.13.0 to 1.13.1
import { BaseLogger } from 'pino' | ||
import { FileGenerator } from './file-generator' | ||
import { PackageConfiguration } from './utils' | ||
export namespace BaseGenerator { | ||
@@ -27,2 +27,7 @@ export type BaseGeneratorOptions = FileGenerator.FileGeneratorOptions & { | ||
} | ||
type PackageDefinition = { | ||
name: string, | ||
options: PackageConfiguration | ||
} | ||
type BaseGeneratorConfig = Record<string, any> & { | ||
@@ -41,3 +46,3 @@ port?: number | ||
serviceName?: string, | ||
envPrefix?: string | ||
envPrefix?: string | ||
} | ||
@@ -76,2 +81,3 @@ | ||
packages: PackageConfiguration[] | ||
constructor(opts?: BaseGeneratorOptions) | ||
@@ -88,2 +94,4 @@ | ||
addPackage(pkg: PackageDefinition): void | ||
prepare(): Promise<GeneratorMetadata> | ||
@@ -90,0 +98,0 @@ run(): Promise<GeneratorMetadata> |
@@ -32,2 +32,3 @@ 'use strict' | ||
this.config = this.getDefaultConfig() | ||
this.packages = [] | ||
} | ||
@@ -419,2 +420,6 @@ | ||
addPackage (pkg) { | ||
this.packages.push(pkg) | ||
} | ||
// implement in the subclass | ||
@@ -421,0 +426,0 @@ async _beforePrepare () {} |
@@ -10,3 +10,4 @@ 'use strict' | ||
PrepareError: createError(`${ERROR_PREFIX}_PREPARE_ERROR`, 'Error while generating the files: %s.'), | ||
MissingEnvVariable: createError(`${ERROR_PREFIX}_MISSING_ENV_VAR`, 'Env variable %s is defined in config file %s, but not in config.env object.') | ||
MissingEnvVariable: createError(`${ERROR_PREFIX}_MISSING_ENV_VAR`, 'Env variable %s is defined in config file %s, but not in config.env object.'), | ||
WrongTypeError: createError(`${ERROR_PREFIX}_WRONG_TYPE`, 'Invalid value type. Accepted values are \'string\', \'number\' and \'boolean\', found \'%s\'.') | ||
} |
@@ -5,2 +5,8 @@ type Env = { | ||
export type PackageConfiguration = { | ||
type: 'number' | 'string' | 'boolean' | ||
path: string | ||
value: number | string | boolean | ||
} | ||
export namespace GeneratorUtils { | ||
@@ -13,2 +19,3 @@ export function safeMkdir(dir: string): Promise<void> | ||
export function extractEnvVariablesFromText(text: string): string[] | ||
export function getPackageConfigurationObject(config: PackageConfiguration[]): object | ||
} |
'use strict' | ||
const { mkdir } = require('node:fs/promises') | ||
const { WrongTypeError } = require('./errors') | ||
@@ -61,6 +62,37 @@ async function safeMkdir (dir) { | ||
} | ||
function getPackageConfigurationObject (config) { | ||
const output = {} | ||
let current = output | ||
for (const param of config) { | ||
const props = param.path.split('.') | ||
props.forEach((prop, idx) => { | ||
if (idx === props.length - 1) { | ||
switch (param.type) { | ||
case 'string' : | ||
current[prop] = param.value.toString() | ||
break | ||
case 'number': | ||
current[prop] = parseInt(param.value) | ||
break | ||
case 'boolean': | ||
current[prop] = (param.value === 'true') | ||
break | ||
default: | ||
throw new WrongTypeError(param.type) | ||
} | ||
current = output | ||
} else { | ||
if (!current[prop]) { | ||
current[prop] = {} | ||
} | ||
current = current[prop] | ||
} | ||
}) | ||
} | ||
return output | ||
} | ||
module.exports = { | ||
addPrefixToEnv, | ||
convertServiceNameToPrefix, | ||
getPackageConfigurationObject, | ||
envObjectToString, | ||
@@ -67,0 +99,0 @@ extractEnvVariablesFromText, |
{ | ||
"name": "@platformatic/generators", | ||
"version": "1.13.0", | ||
"version": "1.13.1", | ||
"description": "Main classes and utils for generators.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -364,2 +364,21 @@ 'use strict' | ||
test('should add package', async () => { | ||
const bg = new BaseGenerator() | ||
const packageDefinition = { | ||
name: '@my/package', | ||
options: [ | ||
{ | ||
path: 'foobar', | ||
type: 'string', | ||
value: 'foobar' | ||
} | ||
] | ||
} | ||
bg.addPackage(packageDefinition) | ||
assert.equal(bg.packages.length, 1) | ||
assert.deepEqual(bg.packages[0], packageDefinition) | ||
}) | ||
describe('runtime context', () => { | ||
@@ -366,0 +385,0 @@ test('should set config.envPrefix correctly', async (t) => { |
@@ -5,4 +5,3 @@ 'use strict' | ||
const assert = require('node:assert') | ||
const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, envObjectToString } = require('../lib/utils') | ||
const { extractEnvVariablesFromText } = require('../lib/utils') | ||
const { stripVersion, convertServiceNameToPrefix, addPrefixToEnv, envObjectToString, extractEnvVariablesFromText, getPackageConfigurationObject } = require('../lib/utils') | ||
@@ -90,2 +89,65 @@ describe('utils', () => { | ||
}) | ||
describe('getPackageConfigurationObject', async () => { | ||
const input = [ | ||
{ | ||
path: 'prefix', | ||
value: '/foo', | ||
type: 'string' | ||
}, | ||
{ | ||
path: 'foo.fooOption1', | ||
value: 'value1', | ||
type: 'string' | ||
}, | ||
{ | ||
path: 'foo.fooOption2', | ||
value: 'value2', | ||
type: 'string' | ||
}, | ||
{ | ||
path: 'foobar', | ||
value: '123', | ||
type: 'number' | ||
}, | ||
{ | ||
path: 'boolean.truthy', | ||
value: 'true', | ||
type: 'boolean' | ||
}, | ||
{ | ||
path: 'boolean.falsey', | ||
value: 'false', | ||
type: 'boolean' | ||
} | ||
] | ||
const config = getPackageConfigurationObject(input) | ||
assert.deepEqual(config, { | ||
prefix: '/foo', | ||
foo: { | ||
fooOption1: 'value1', | ||
fooOption2: 'value2' | ||
}, | ||
foobar: 123, | ||
boolean: { | ||
truthy: true, | ||
falsey: false | ||
} | ||
}) | ||
// should throw | ||
try { | ||
getPackageConfigurationObject([ | ||
{ | ||
path: 'wrong', | ||
type: 'object', | ||
value: {} | ||
} | ||
]) | ||
assert.fail() | ||
} catch (err) { | ||
assert.equal(err.code, 'PLT_GEN_WRONG_TYPE') | ||
assert.equal(err.message, 'Invalid value type. Accepted values are \'string\', \'number\' and \'boolean\', found \'object\'.') | ||
} | ||
}) | ||
}) |
63813
1673