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: add liara init command #200

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

Conversation

Mortza81
Copy link

@Mortza81 Mortza81 commented Dec 2, 2024

This command provides an interactive process to generate a basic liara.json configuration file.
The generated configuration file will include only the basic parameters to get started, such as application name, port, platform, and other necessary fields. However, any additional or advanced configurations (such as cron, healthCheck, etc.) will not be included by default and must be manually added after the file is created.

@smaEti smaEti requested a review from vashian December 2, 2024 11:31
@jarqvi jarqvi requested review from jarqvi and removed request for vashian December 3, 2024 08:54
@jarqvi
Copy link
Member

jarqvi commented Dec 3, 2024

related: #151

@jarqvi jarqvi requested review from vashian and removed request for jarqvi December 3, 2024 08:57
src/utils/getSupportedVersions.ts Show resolved Hide resolved
Comment on lines 65 to 70
export default function supportedVersions(platform: string) {
if (platform in versions) {
return versions[platform as keyof typeof versions];
}
return null;
}
Copy link
Member

Choose a reason for hiding this comment

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

I suggest avoiding the use of null here.

In JavaScript, undefined is the default for uninitialized or missing values, which helps align with the language's conventions. Using undefined simplifies checks and ensures consistency, reducing the need to handle both null and undefined separately.

I think when designing APIs, returning undefined for missing data is often more intuitive and aligns with JavaScript conventions.

export default function supportedVersions(platform: string) {
  return versions[platform as keyof typeof versions]
}

Test it and let me know what you think!

Copy link
Author

@Mortza81 Mortza81 Dec 4, 2024

Choose a reason for hiding this comment

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

You're right! Also we can simplify the null checks in the init.ts file (lines: 272, 227, 291). Since both null and undefined are considered falsy in JavaScript, we can replace the explicit null checks with a straightforward truthy/falsy evaluation.

For example:

if (platformVersion == null) {
    return null;
}

Can be simplified to:

if (!platformVersion) {
    return null;
}

If you agree with this approach, I’ll proceed with committing these changes.

src/base.ts Outdated
Comment on lines 328 to 338
async promptPort(platform: string): Promise<number> {
const { port } = (await inquirer.prompt({
name: 'port',
type: 'input',
default: getDefaultPort(platform),
message: 'Enter the port your app listens to:',
validate: validatePort,
})) as { port: number };

return port;
}
Copy link
Member

Choose a reason for hiding this comment

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

I noticed that the promptPort method is repeated in deploy.ts as well. Additionally, base.ts doesn’t seem like the appropriate place for a utility function like this.

Why not move promptPort to the utils directory? This would allow us to reuse it wherever needed and keep base.ts focused and simple.

Keeping base.ts as clean as possible is important for maintainability and readability.

Copy link
Author

@Mortza81 Mortza81 Dec 4, 2024

Choose a reason for hiding this comment

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

I tried to follow the approach that has been used so far. there is also promptNetwork() and promptProject() that is defined in base.ts. I suggest that we let them be in base.ts for now and move them later.

src/base.ts Outdated
Comment on lines 77 to 90
interface IMount {
name: string;
mountedTo: string;
}
export interface IGetDiskResponse {
disks: {
name: string;
size: number;
updatedAt: string;
createdAt: string;
filebrowserUrl: string;
}[];
mounts: IMount[];
}
Copy link
Member

Choose a reason for hiding this comment

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

Why not move these interfaces to the types directory?

}
return null;
} catch (error) {
throw error;
Copy link
Member

Choose a reason for hiding this comment

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

Appropriate errors should be thrown to inform the user.

@vashian
Copy link
Member

vashian commented Dec 9, 2024

IMO :

The typeof operator gives misleading results for null:

typeof null; // "object"

Handling both null and undefined leads to verbose checks. For example, default parameter values only work with undefined. If a parameter is explicitly set to null, the default value won't apply:

function greet(name = "Guest") {
  console.log(name);
}
greet(null); // Outputs: null

Default values in object destructuring also fail with null, and using null in nested destructuring can even cause runtime errors.

These are the main reasons I avoid using null. Since @mhemrg and I have different perspectives on this, I think it’s a good idea to ask for his review on the PR to ensure future consistency.

It includes only the essential settings; additional configurations must be added manually.
📚 For more details on each field and its usage, visit: https://docs.liara.ir/paas/liarajson/.

Afterwards, use liara deploy to deploy your project.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Afterwards, use liara deploy to deploy your project.
Afterwards, use liara deploy to deploy your app.

cron,
);

this.createLiaraJsonFile(configs);
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need an await for this?

Suggested change
this.createLiaraJsonFile(configs);
this.createLiaraJsonFile(configs);

@mhemrg
Copy link
Member

mhemrg commented Dec 11, 2024

The following pattern:

try {
  //
}
catch (error) {
  throw error;
}

Doesn't accomplish anything. You can remove them all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants