Skip to content

Commit

Permalink
Merge pull request #108 from willosborne/improvements
Browse files Browse the repository at this point in the history
Improvements to CLI generate
  • Loading branch information
willosborne authored Apr 3, 2024
2 parents a1c8e58 + 214c5e4 commit abd55f0
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cli/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
export default {
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/src/**/*.spec.ts']
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['<rootDir>/src/**/*.spec.ts']
};
160 changes: 157 additions & 3 deletions cli/src/commands/generate/generate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { exportedForTesting } from './generate';
import { runGenerate } from './generate';
import { tmpdir } from 'node:os';
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs';
import path from 'node:path';

const { getPropertyValue } = exportedForTesting;
const {
getPropertyValue,
instantiateNodes,
instantiateRelationships,
initLogger
} = exportedForTesting;

describe('getPropertyValue', () => {
it('generates string placeholder name from variable', () => {
Expand Down Expand Up @@ -44,8 +51,155 @@ describe('getPropertyValue', () => {
}
});
});

it('generates array with single string placeholder', () => {
expect(getPropertyValue('key-name', {
'type': 'array'
}))
.toEqual([
'{{ KEY_NAME }}'
]);
});
});

function getSamplePatternNode(properties: any): any {
return {
properties: {
nodes: {
type: 'array',
prefixItems: [
{
properties: properties
}
]
}
}
};
}


describe('instantiateNodes', () => {
beforeEach(() => {
initLogger(false);
});

it('return instantiated node with array property', () => {
const pattern = getSamplePatternNode({
'property-name': {
type: 'array'
}
});
expect(instantiateNodes(pattern))
.toEqual(
[{
'property-name': [
'{{ PROPERTY_NAME }}'
]
}]
);
});

it('return instantiated node with string property', () => {
const pattern = getSamplePatternNode({
'property-name': {
type: 'string'
}
});

expect(instantiateNodes(pattern))
.toEqual([
{
'property-name': '{{ PROPERTY_NAME }}'
}
]);
});

it('return instantiated node with const property', () => {
const pattern = getSamplePatternNode({
'property-name': {
const: 'value here'
}
});

expect(instantiateNodes(pattern))
.toEqual([
{
'property-name': 'value here'
}
]);
});
});


function getSamplePatternRelationship(properties: any): any {
return {
properties: {
relationships: {
type: 'array',
prefixItems: [
{
properties: properties
}
]
}
}
};
}

describe('instantiateRelationships', () => {
beforeEach(() => {
initLogger(false);
});

it('return instantiated relationship with array property', () => {
const pattern = getSamplePatternRelationship({
'property-name': {
type: 'array'
}
});

expect(instantiateRelationships(pattern))
.toEqual(
[{
'property-name': [
'{{ PROPERTY_NAME }}'
]
}]
);
});

it('return instantiated relationship with string property', () => {
const pattern = getSamplePatternRelationship({
'property-name': {
type: 'string'
}
});

expect(instantiateRelationships(pattern))
.toEqual([
{
'property-name': '{{ PROPERTY_NAME }}'
}
]);
});

it('return instantiated relationship with const property', () => {
const pattern = getSamplePatternRelationship({
'property-name': {
const: 'value here'
}
});

expect(instantiateRelationships(pattern))
.toEqual([
{
'property-name': 'value here'
}
]);
});
});


describe('runGenerate', () => {
let tempDirectoryPath;
const testPath: string = 'test_fixtures/api-gateway.json';
Expand Down Expand Up @@ -73,7 +227,7 @@ describe('runGenerate', () => {
expect(existsSync(outPath))
.toBeTruthy();
});

it('instantiates to calm instantiation file', () => {
const outPath = path.join(tempDirectoryPath, 'output.json');
runGenerate(testPath, outPath, false);
Expand All @@ -85,7 +239,7 @@ describe('runGenerate', () => {
const parsed = JSON.parse(spec);
expect(parsed)
.toHaveProperty('nodes');
expect(parsed)
expect(parsed)
.toHaveProperty('relationships');
});
});
39 changes: 28 additions & 11 deletions cli/src/commands/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ function getPropertyValue(keyName: string, detail: any) : any {
if (propertyType === 'integer') {
return -1;
}
if (propertyType === 'array') {
return [
getStringPlaceholder(keyName)
];
}
}
}

function instantiateNodes(pattern: any): any {
const nodes = pattern?.properties?.nodes?.prefixItems;
if (!nodes) {
console.error('Warning: pattern has no nodes defined.');
logger.error('Warning: pattern has no nodes defined.');
if (pattern?.properties?.nodes?.items) {
logger.warn('Note: properties.relationships.items is deprecated: please use prefixItems instead.');
}
return [];
}
const outputNodes = [];
Expand All @@ -64,11 +72,14 @@ function instantiateNodes(pattern: any): any {
return outputNodes;
}

function getRelationships(pattern: any): any {
function instantiateRelationships(pattern: any): any {
const relationships = pattern?.properties?.relationships?.prefixItems;

if (!relationships) {
console.error('Warning: pattern has no relationships defined');
logger.error('Warning: pattern has no relationships defined');
if (pattern?.properties?.relationships?.items) {
logger.warn('Note: properties.relationships.items is deprecated: please use prefixItems instead.');
}
return [];
}

Expand All @@ -94,11 +105,7 @@ function getRelationships(pattern: any): any {
return outputRelationships;
}

export const exportedForTesting = {
getPropertyValue
};

export function runGenerate (patternPath: string, outputPath: string, debug: boolean): void {
function initLogger(debug: boolean): void {
const level = debug ? 'debug' : 'info';
logger = winston.createLogger({
transports: [
Expand All @@ -107,17 +114,27 @@ export function runGenerate (patternPath: string, outputPath: string, debug: boo
level: level,
format: winston.format.cli()
});
}

export const exportedForTesting = {
getPropertyValue,
instantiateNodes,
instantiateRelationships,
initLogger
};

export function runGenerate (patternPath: string, outputPath: string, debug: boolean): void {
initLogger(debug);

const pattern = loadFile(patternPath);
const outputNodes = instantiateNodes(pattern);
const relationshipNodes = getRelationships(pattern);

const relationshipNodes = instantiateRelationships(pattern);

const final = {
'nodes': outputNodes,
'relationships': relationshipNodes
'relationships': relationshipNodes,
};

const output = JSON.stringify(final, null, 2);
logger.debug('Generated instantiation: ' + output);

Expand Down

0 comments on commit abd55f0

Please sign in to comment.