Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve glee command #890

Merged
merged 65 commits into from
Feb 1, 2024
Merged
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
14cbd0c
handle the case if the file is not present.
AyushNautiyalDeveloper Sep 19, 2023
03c0146
Merge pull request #1 from asyncapi/master
ayushnau Sep 21, 2023
0c7dc9a
Merge branch 'master' into filehandle
ayushnau Sep 21, 2023
972cef0
remove the lint error
AyushNautiyalDeveloper Sep 26, 2023
013599d
Merge branch 'master' of github.com-ayushnau:ayushnau/cli into fileha…
AyushNautiyalDeveloper Sep 26, 2023
0bb6b5d
made the required changes.
AyushNautiyalDeveloper Sep 30, 2023
002952f
Merge branch 'master' into filehandle
Souvikns Oct 4, 2023
c673263
Merge pull request #2 from asyncapi/master
ayushnau Oct 5, 2023
3750452
Added the latest version of asyncapi in the for generator template of…
AyushNautiyalDeveloper Oct 5, 2023
6665cd9
Merge branch 'master' into master
Souvikns Oct 9, 2023
16d845c
Merge branch 'master' into master
Souvikns Oct 9, 2023
84162fc
Merge branch 'master' into filehandle
Souvikns Oct 9, 2023
29b2df3
added the hook .
AyushNautiyalDeveloper Oct 13, 2023
9b42245
fixed bug
AyushNautiyalDeveloper Oct 13, 2023
6a53b61
added the semicolon
AyushNautiyalDeveloper Oct 13, 2023
a1afe8b
ran lint command
AyushNautiyalDeveloper Oct 13, 2023
6a82b6c
Added the test
AyushNautiyalDeveloper Oct 14, 2023
2a28d11
Merge branch 'master' of github.com-ayushnau:ayushnau/cli into fileha…
AyushNautiyalDeveloper Oct 18, 2023
51d1a6f
fixed the bug of help command.
AyushNautiyalDeveloper Oct 18, 2023
fa80b7c
Merge branch 'master' into filehandle
Souvikns Oct 19, 2023
f8cdf92
made small change.
AyushNautiyalDeveloper Oct 19, 2023
aabac41
Merge branch 'filehandle' of github.com-ayushnau:ayushnau/cli into fi…
AyushNautiyalDeveloper Oct 19, 2023
0fc7b9f
all the operation function are not created.
AyushNautiyalDeveloper Nov 18, 2023
98a25ec
Merge branch 'master' into filehandle
AyushNautiyalDeveloper Nov 18, 2023
aa01244
Merge pull request #3 from asyncapi/master
ayushnau Nov 25, 2023
7e0803f
added the file generation code.
AyushNautiyalDeveloper Dec 3, 2023
5b3d0d6
Added the types for the fuctions.
AyushNautiyalDeveloper Dec 3, 2023
e78d376
added the glee command changes.
AyushNautiyalDeveloper Dec 3, 2023
fe226df
Merge pull request #4 from asyncapi/master
ayushnau Dec 3, 2023
9b71196
add the type in the createTypes.
AyushNautiyalDeveloper Dec 3, 2023
332272a
Merge branch 'master' into filehandle
AyushNautiyalDeveloper Dec 4, 2023
a1b94b8
added the package.json file.
AyushNautiyalDeveloper Dec 4, 2023
25b03ce
Merge branch 'master' into filehandle
ayushnau Dec 4, 2023
4b35b4c
Merge pull request #5 from asyncapi/master
ayushnau Dec 11, 2023
286d695
Merge branch 'master' into filehandle and resolve the issue.
AyushNautiyalDeveloper Dec 11, 2023
ed68eae
Merge remote-tracking branch 'refs/remotes/origin/filehandle' into fi…
AyushNautiyalDeveloper Dec 11, 2023
d8dd534
integrated the glee template with the command.
AyushNautiyalDeveloper Dec 20, 2023
d7f3cb7
integrated the glee template.
AyushNautiyalDeveloper Dec 23, 2023
f81c02c
Added the support for both template and local
AyushNautiyalDeveloper Dec 31, 2023
2cc23ec
Merge pull request #6 from asyncapi/master
ayushnau Jan 9, 2024
10d051e
Merge branch 'master' into filehandle
AyushNautiyalDeveloper Jan 9, 2024
9b906a0
Added the -f flag support for the glee command.
AyushNautiyalDeveloper Jan 12, 2024
456f94d
wrapped the file case with try statement.
AyushNautiyalDeveloper Jan 12, 2024
ce3b64b
removed the unwated files.
AyushNautiyalDeveloper Jan 12, 2024
3fb37b5
Added correct package.lock json file.
AyushNautiyalDeveloper Jan 12, 2024
5331813
added correct version of package-lock.json file.
AyushNautiyalDeveloper Jan 12, 2024
5126039
correctly formatted the glee file.
AyushNautiyalDeveloper Jan 12, 2024
985b99a
Added the correct strict inquality operator.
AyushNautiyalDeveloper Jan 12, 2024
b35ff1d
added the correct formatting .
AyushNautiyalDeveloper Jan 12, 2024
3f13a1d
Refactored the code.
AyushNautiyalDeveloper Jan 16, 2024
4db28fd
removed the console.logs.
AyushNautiyalDeveloper Jan 16, 2024
79e36d9
fixed the lint issue.
AyushNautiyalDeveloper Jan 16, 2024
093cac3
Merge pull request #7 from asyncapi/master
ayushnau Jan 18, 2024
b81b9e8
Merge branch 'master' into filehandle
AyushNautiyalDeveloper Jan 18, 2024
5fd4080
fixed the file path bug.
AyushNautiyalDeveloper Jan 18, 2024
8866706
Added correct message for completing generating the project.
AyushNautiyalDeveloper Jan 18, 2024
1336bd1
Added the validation for the file.
AyushNautiyalDeveloper Jan 20, 2024
cd37b67
Fixed the bug of not showing error when the generator is throwing err…
AyushNautiyalDeveloper Jan 26, 2024
7e9de8b
linted the text.
AyushNautiyalDeveloper Jan 26, 2024
79c7aad
Merge pull request #8 from asyncapi/master
ayushnau Jan 26, 2024
cbeba16
Merge branch 'master' into filehandle
AyushNautiyalDeveloper Jan 26, 2024
c5cc299
Fixed the issue of remote server not present.
AyushNautiyalDeveloper Jan 31, 2024
be9e5a1
linted the code..
AyushNautiyalDeveloper Jan 31, 2024
6a4d4a1
added the correct path.
AyushNautiyalDeveloper Feb 1, 2024
1f7065b
Merge branch 'master' into filehandle
Souvikns Feb 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 218 additions & 26 deletions src/commands/new/glee.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,247 @@
import {Flags} from '@oclif/core';
import { Flags } from '@oclif/core';
import { promises as fPromises } from 'fs';
import Command from '../../base';
import { resolve, join } from 'path';
import path, { resolve, join } from 'path';
import fs from 'fs-extra';
import { Specification, load } from '../../models/SpecificationFile';
import yaml from 'js-yaml';
import { prompt } from 'inquirer';
// eslint-disable-next-line
// @ts-ignore
import Generator from '@asyncapi/generator';

export default class NewGlee extends Command {
static description = 'Creates a new Glee project';

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you reset the formatting changes? it makes it hard to review.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this was my default setting.

protected commandName = 'glee';

static flags = {
help: Flags.help({ char: 'h' }),
name: Flags.string({ char: 'n', description: 'name of the project', default: 'project' }),
template: Flags.string({ char: 't', description: 'name of the template', default: 'default' }),
name: Flags.string({
char: 'n',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unable to regenerate the glee project as it is missing the --force-write flag.

Screenshot 2024-01-23 at 7 38 56 PM Screenshot 2024-01-23 at 7 39 03 PM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is a requirement by the generator. you can't generate on a git repository with upstaged changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but can't we pass in the flag from our CLI, since generate fromTemplate does that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Souvikns I think it is best to expose --force-write in asyncapi new glee command. and leave it to the user if they want to force write or not. 🤷

@ayushnau can you add --force-write flag to asyncapi new glee command and then pass it on to the generator if user passes it? 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok sure will do that.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KhudaDad414 Added the required changes. please review.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/rtm

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@KhudaDad414 i have Added force-write flag. please review.

Copy link
Member

@peter-rr peter-rr Jan 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AyushNautiyalDeveloper
--force-write flag addition LGTM 👍

description: 'Name of the Project',
default: 'project',
}),
template: Flags.string({
char: 't',
description: 'Name of the Template',
default: 'default',
}),
file: Flags.string({
char: 'f',
description:
'The path to the AsyncAPI file for generating a Glee project.',
}),
'force-write': Flags.boolean({
default: false,
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)',
}),
};

async run() {
const { flags } = await this.parse(NewGlee); // NOSONAR
async getFilteredServers(serversObject: any) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want to remove the template support. Users should still be able to create a glee project from one of the templates.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

console.log({ serversObject });
const servers = Object.keys(serversObject);

const projectName = flags.name;
const templateName = flags.template;
const localServers = await prompt([
{
name: 'LOCAL_SERVERS',
message:
'Select all of the servers that you want glee to set up and run a server for (local servers):',
type: 'checkbox',
choices() {
return servers;
},
},
]);

const PROJECT_DIRECTORY = join(process.cwd(), projectName);
const GLEE_TEMPLATES_DIRECTORY = resolve(__dirname, '../../../assets/create-glee-app/templates/', templateName);
return servers.filter(
(server) => !localServers.LOCAL_SERVERS.includes(server)
);
}
async createTemporaryFile(
asyncapiInput: Specification,
filteredRemoteServers: string[],
file: any
) {
const asyncapiObject = asyncapiInput.toJson();
asyncapiObject['x-remoteServers'] = filteredRemoteServers;

delete asyncapiObject.filePath;
delete asyncapiObject.kind;

const updatedAsyncApiContent = yaml.dump(asyncapiObject, {
lineWidth: -1,
});

const currentFileDirectory = path.join(__dirname, file);

fs.writeFileSync(currentFileDirectory, updatedAsyncApiContent);
return { currentFileDirectory, updatedAsyncApiContent };
}
async validateFile(file: any, projectName: any, PROJECT_DIRECTORY: any) {
try {
await fPromises.mkdir(PROJECT_DIRECTORY);
const validExtensions = ['.yaml', '.yml', '.json'];
const fileExtension = path.extname(file);

if (!validExtensions.includes(fileExtension)) {
throw new Error(
'CLI Support only yml, yaml, and json extension for file'
);
}

if (
fs.existsSync(PROJECT_DIRECTORY) &&
fs.readdirSync(PROJECT_DIRECTORY).length > 0
) {
throw new Error(
`Unable to create the project. We tried to use "${projectName}" as the directory of your new project but it already exists (${PROJECT_DIRECTORY}). Please specify a different name for the new project. For example, run the following command instead:\n\n asyncapi new ${this.commandName} -f ${file} --name ${projectName}-1\n`
);
}
} catch (error: any) {
this.error(error.message);
}
}

async handleGenerateProjectWithFile(
file: any,
CURRENT_GLEE_TEMPLATE: any,
projectName: string,
forceWrite: boolean
) {
const PROJECT_DIRECTORY = path.join(process.cwd(), projectName);
await this.validateFile(file, projectName, PROJECT_DIRECTORY);

try {
console.log(file);
const asyncapiInput = (await load(file)) || (await load());
console.log(asyncapiInput);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ayushnau
this load logic will not work if the file is not in the current directory. for example, if I run it with:
asyncapi new glee -f ./some/random/folder/asyncapi.yml
generator will not be able to find the file. 🤔

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it is working.


const serversObject = asyncapiInput.toJson()?.servers;
let filteredRemoteServers: any[] = [];
if (serversObject) {
filteredRemoteServers = await this.getFilteredServers(serversObject);
}

const temporaryFileDirectory = 'asyncapi.yaml';
const { currentFileDirectory, updatedAsyncApiContent } =
await this.createTemporaryFile(
asyncapiInput,
filteredRemoteServers,
temporaryFileDirectory
);

const generator = new Generator(
CURRENT_GLEE_TEMPLATE,
PROJECT_DIRECTORY,
{ forceWrite }
);
await generator.generateFromString(updatedAsyncApiContent);
fs.unlinkSync(currentFileDirectory);

this.log(
`Success! Created ${projectName} at ${PROJECT_DIRECTORY}\n\nNext steps:\n\n cd ${projectName}\n npm install --ignore-scripts\n\nImplement the function in functions and auth folder and run the project with:\n npm run dev`
);
} catch (err: any) {
switch (err.code) {
case 'EEXIST':
this.error(`Unable to create the project. We tried to use "${projectName}" as the directory of your new project but it already exists (${PROJECT_DIRECTORY}). Please specify a different name for the new project. For example, run the following command instead:\n\n asyncapi new ${this.commandName} --name ${projectName}-1\n`);
break;
case 'EACCES':
this.error(`Unable to create the project. We tried to access the "${PROJECT_DIRECTORY}" directory but it was not possible due to file access permissions. Please check the write permissions of your current working directory ("${process.cwd()}").`);
this.error(
`Unable to create the project. We tried to access the "${PROJECT_DIRECTORY}" directory but it was not possible due to file access permissions. Please check the write permissions of your current working directory ("${process.cwd()}").`
);
break;
case 'EPERM':
this.error(`Unable to create the project. We tried to create the "${PROJECT_DIRECTORY}" directory but the operation requires elevated privileges. Please check the privileges for your current user.`);
this.error(
`Unable to create the project. We tried to create the "${PROJECT_DIRECTORY}" directory but the operation requires elevated privileges. Please check the privileges for your current user.`
);
break;
default:
this.error(`Unable to create the project. Please check the following message for further info about the error:\n\n${err}`);
this.error(
`Unable to create the project. Please check the following message for further info about the error:\n\n${err}`
);
}
}
}
async run() {
const { flags } = await this.parse(NewGlee); // NOSONAR

try {
await fs.copy(GLEE_TEMPLATES_DIRECTORY, PROJECT_DIRECTORY);
await fPromises.rename(`${PROJECT_DIRECTORY}/env`, `${PROJECT_DIRECTORY}/.env`);
await fPromises.rename(`${PROJECT_DIRECTORY}/gitignore`, `${PROJECT_DIRECTORY}/.gitignore`);
await fPromises.rename(`${PROJECT_DIRECTORY}/README-template.md`, `${PROJECT_DIRECTORY}/README.md`);
this.log(`Your project "${projectName}" has been created successfully!\n\nNext steps:\n\n cd ${projectName}\n npm install\n npm run dev\n\nAlso, you can already open the project in your favorite editor and start tweaking it.`);
} catch (err) {
this.error(`Unable to create the project. Please check the following message for further info about the error:\n\n${err}`);
const {
name: projectName,
template: templateName,
file,
'force-write': forceWrite,
} = flags;

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

const GLEE_TEMPLATES_DIRECTORY = resolve(
__dirname,
'../../../assets/create-glee-app/templates/',
templateName
);

const CURRENT_GLEE_TEMPLATE =
'https://github.com/KhudaDad414/glee-generator-template';

if (file && templateName && templateName !== 'default') {
this.error('You cannot use both --t and --f in the same command.');
}

if (file) {
console.log('file running');
await this.handleGenerateProjectWithFile(
file,
CURRENT_GLEE_TEMPLATE,
projectName,
forceWrite
);
} else {
try {
await fPromises.mkdir(PROJECT_DIRECTORY);
} catch (err: any) {
switch (err.code) {
case 'EEXIST':
this.error(
`Unable to create the project. We tried to use "${projectName}" as the directory of your new project but it already exists (${PROJECT_DIRECTORY}). Please specify a different name for the new project. For example, run the following command instead:\n\n asyncapi new ${this.commandName} --name ${projectName}-1\n`
);
break;
case 'EACCES':
this.error(
`Unable to create the project. We tried to access the "${PROJECT_DIRECTORY}" directory but it was not possible due to file access permissions. Please check the write permissions of your current working directory ("${process.cwd()}").`
);
break;
case 'EPERM':
this.error(
`Unable to create the project. We tried to create the "${PROJECT_DIRECTORY}" directory but the operation requires elevated privileges. Please check the privileges for your current user.`
);
break;
default:
this.error(
`Unable to create the project. Please check the following message for further info about the error:\n\n${err}`
);
}
}

try {
await fs.copy(GLEE_TEMPLATES_DIRECTORY, PROJECT_DIRECTORY);
await fPromises.rename(
`${PROJECT_DIRECTORY}/env`,
`${PROJECT_DIRECTORY}/.env`
);
await fPromises.rename(
`${PROJECT_DIRECTORY}/gitignore`,
`${PROJECT_DIRECTORY}/.gitignore`
);
await fPromises.rename(
`${PROJECT_DIRECTORY}/README-template.md`,
`${PROJECT_DIRECTORY}/README.md`
);
this.log(
`Your project "${projectName}" has been created successfully!\n\nNext steps:\n\n cd ${projectName}\n npm install\n npm run dev\n\nAlso, you can already open the project in your favorite editor and start tweaking it.`
);
} catch (err) {
this.error(
`Unable to create the project. Please check the following message for further info about the error:\n\n${err}`
);
}
}
}
}
Loading