Skip to content

Commit

Permalink
Improve suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushSaini101 committed Jul 8, 2024
1 parent 139e914 commit 6cf2345
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 37 deletions.
9 changes: 5 additions & 4 deletions assets/create-template/templates/default/asyncapi.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
asyncapi: 2.6.0

asyncapi: 3.0.0
info:
title: Temperature Service
version: 1.0.0
Expand All @@ -12,22 +11,24 @@ servers:

channels:
temperature/changed:
description: Updates the bedroom temperature in the database when the temperatures drops or goes up.
description: Updates the bedroom temperature in the database when the temperature drops or goes up.
publish:
operationId: temperatureChange
message:
description: Message that is being sent when the temperature in the bedroom changes.
contentType: application/json
payload:
type: object
additionalProperties: false
properties:
temperatureId:
type: string

components:
schemas:
temperatureId:
type: object
additionalProperties: false
properties:
temperatureId:
type: string
type: string
16 changes: 8 additions & 8 deletions assets/create-template/templates/default/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "myTemplate",
"generator": {
"renderer": "react",
"supportedProtocols": [ ]
},
"dependencies": {
"@asyncapi/generator-react-sdk": "^0.2.25"
}
"name": "myTemplate",
"generator": {
"renderer": "react",
"supportedProtocols": []
},
"dependencies": {
"@asyncapi/generator-react-sdk": "^1.0.20"
}
}
21 changes: 0 additions & 21 deletions assets/create-template/templates/default/template/package.json.js

This file was deleted.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"@asyncapi/bundler": "^0.5.2",
"@asyncapi/converter": "^1.4.19",
"@asyncapi/diff": "^0.4.1",
"@asyncapi/modelina-cli": "^4.0.0-next.48",
"@asyncapi/generator": "^1.17.25",
"@asyncapi/modelina-cli": "^4.0.0-next.48",
"@asyncapi/openapi-schema-parser": "^3.0.22",
"@asyncapi/optimizer": "^1.0.2",
"@asyncapi/parser": "^3.1.0",
Expand All @@ -31,10 +31,11 @@
"chalk": "^4.1.0",
"chokidar": "^3.5.2",
"fast-levenshtein": "^3.0.0",
"fs-extra": "^11.1.0",
"fs-extra": "^11.2.0",
"indent-string": "^4.0.0",
"inquirer": "^8.2.0",
"js-yaml": "^4.1.0",
"jsonfile": "^6.1.0",
"lodash.template": "^4.4.0",
"node-fetch": "^2.0.0",
"oclif": "^4.2.0",
Expand Down
33 changes: 31 additions & 2 deletions src/commands/new/template.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { promises as fPromises } from 'fs';
import Command from '../../core/base';
import { resolve, join } from 'path';
import {load } from '../../core/models/SpecificationFile';
import { load } from '../../core/models/SpecificationFile';
import fs from 'fs-extra';
import { templateFlags } from '../../core/flags/new/template.flags';
import { cyan, gray } from 'picocolors';
import jsonfile from 'jsonfile';
import path from 'path';

export const successMessage = (projectName: string) =>
`🎉 Your template is succesfully created
Expand Down Expand Up @@ -38,10 +40,15 @@ export default class template extends Command {
const {
name: projectName,
template: templateName,
renderer: rendererName
} = flags;

const PROJECT_DIRECTORY = join(process.cwd(), projectName);

if (rendererName!=='nunjucks' && rendererName!=='react') {
this.error('Invalid flag check the flag name of renderer');
}

const templateDirectory = resolve(
__dirname,
'../../../assets/create-template/templates/',
Expand Down Expand Up @@ -74,7 +81,7 @@ export default class template extends Command {
}

try {
await fs.copy(templateDirectory, PROJECT_DIRECTORY);
await copyAndModify(templateDirectory, PROJECT_DIRECTORY,rendererName, projectName);
this.log(successMessage(projectName));
} catch (err) {
this.error(
Expand All @@ -86,3 +93,25 @@ export default class template extends Command {
}
}
}

async function copyAndModify(templateDirectory:string, PROJECT_DIRECTORY:string, rendererName:string, projectName:string) {
const packageJsonPath = path.join(templateDirectory, 'package.json');
try {
await fs.copy(templateDirectory, PROJECT_DIRECTORY, {
filter: (src) => {
return !src.endsWith('package.json');
}
});
const packageData = await jsonfile.readFile(packageJsonPath);
if ((packageData.generator && 'renderer' in packageData.generator)) {
packageData.generator.renderer = rendererName;
}
if (packageData.name) {
packageData.name = projectName;
}

await fs.writeJSON(`${PROJECT_DIRECTORY}/package.json`, packageData, { spaces: 2 });
} catch (err) {
console.error('Error:', err);
}
}
4 changes: 4 additions & 0 deletions src/core/flags/new/template.flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ export const templateFlags = () => {
description:
'Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)',
}),
renderer: Flags.string({
default: 'react',
description: 'Creating a template for particular engine, Its value can be either react or nunjucks.'
})
};
};

0 comments on commit 6cf2345

Please sign in to comment.