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

Fix genezio link #938

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 0 additions & 1 deletion src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,6 @@ async function handleSdk(

reportSuccessForSdk(sdkLanguage, sdkResponse, GenezioCommand.deploy, {
name: configuration.name,
region: configuration.region,
stage: options.stage || "prod",
});
}
Expand Down
16 changes: 12 additions & 4 deletions src/commands/link.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UserError } from "../errors.js";
import {
deleteLinkPathForProject,
deleteAllLinkPaths,
Expand All @@ -6,12 +7,20 @@ import {
import { log } from "../utils/logging.js";
import yamlConfigIOController from "../yamlProjectConfiguration/v2.js";

async function getProjectName(): Promise<string> {
const projectConfiguration = await yamlConfigIOController.read().catch((_error) => {
throw new UserError(
"Command execution failed. Please ensure you are running this command from a directory containing 'genezio.yaml' or provide the [projectName] argument.",
);
});
return projectConfiguration.name;
}

export async function linkCommand(projectName: string | undefined) {
const cwd = process.cwd();
let name = projectName;
if (!name) {
const projectConfiguration = await yamlConfigIOController.read();
name = projectConfiguration.name;
name = await getProjectName();
}

await setLinkPathForProject(name, cwd);
Expand All @@ -26,8 +35,7 @@ export async function unlinkCommand(unlinkAll: boolean, projectName: string | un
}
let name = projectName;
if (!name) {
const projectConfiguration = await yamlConfigIOController.read();
name = projectConfiguration.name;
name = await getProjectName();
}

await deleteLinkPathForProject(name);
Expand Down
19 changes: 9 additions & 10 deletions src/commands/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { watchPackage } from "../generateSdk/sdkMonitor.js";
import { NodeJsBundler } from "../bundlers/node/nodeJsBundler.js";
import { KotlinBundler } from "../bundlers/kotlin/localKotlinBundler.js";
import { reportSuccessForSdk } from "../generateSdk/sdkSuccessReport.js";
import { getLinkPathsForProject } from "../utils/linkDatabase.js";

type ClassProcess = {
process: ChildProcess;
Expand Down Expand Up @@ -311,7 +312,6 @@ export async function startLocalEnvironment(options: GenezioLocalOptions) {
);
const watcherTimeout = await handleSdk(
yamlProjectConfiguration.name,
yamlProjectConfiguration.region,
yamlProjectConfiguration.frontend,
sdk,
options,
Expand Down Expand Up @@ -344,6 +344,7 @@ export async function startLocalEnvironment(options: GenezioLocalOptions) {
}

function logChangeDetection() {
// eslint-disable-next-line no-console
console.clear();
log.info("\x1b[36m%s\x1b[0m", "Change detected, reloading...");
}
Expand Down Expand Up @@ -764,7 +765,6 @@ async function listenForChanges() {
*/
async function handleSdk(
projectName: string,
projectRegion: string,
frontends: YamlFrontend[] | undefined,
sdk: SdkGeneratorResponse,
options: GenezioLocalOptions,
Expand All @@ -781,6 +781,12 @@ async function handleSdk(
? path.join(frontendPath, frontends[0].sdk?.path)
: path.join(frontendPath, "sdk");
}
} else {
const linkedPaths = await getLinkPathsForProject(projectName);
if (linkedPaths.length > 0) {
const linkedPath = linkedPaths[0];
sdkPath = path.join(linkedPath, "sdk");
}
}

const classUrls = sdk.files.map((c) => ({
Expand All @@ -800,21 +806,14 @@ async function handleSdk(
});

if (sdkFolderPath) {
const timeout = await watchPackage(
sdkLanguage,
projectName,
projectRegion,
frontends,
sdkFolderPath,
);
const timeout = await watchPackage(sdkLanguage, projectName, frontends, sdkFolderPath);
if (timeout) {
nodeJsWatcher = timeout;
}
}

reportSuccessForSdk(sdkLanguage, sdk, GenezioCommand.local, {
name: projectName,
region: projectRegion,
stage: "local",
});

Expand Down
16 changes: 4 additions & 12 deletions src/generateSdk/sdkMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,20 @@ const POLLING_INTERVAL = 2000;
export async function watchPackage(
language: Language,
projectName: string,
projectRegion: string,
frontend: YamlFrontend[] | undefined,
sdkPath: string,
): Promise<NodeJS.Timeout | undefined> {
switch (language) {
case Language.js:
case Language.ts:
return watchNodeModules(projectName, projectRegion, frontend, sdkPath);
return watchNodeModules(projectName, frontend, sdkPath);
default:
return;
}
}

async function watchNodeModules(
projectName: string,
projectRegion: string,
frontends: YamlFrontend[] | undefined,
sdkPath: string,
): Promise<NodeJS.Timeout | undefined> {
Expand All @@ -48,7 +46,7 @@ async function watchNodeModules(
}
}

const linkPaths = await getLinkPathsForProject(projectName, projectRegion);
const linkPaths = await getLinkPathsForProject(projectName);
for (const linkPath of linkPaths) {
watchPaths.push(path.join(linkPath, nodeModulesSdkDirectoryPath));
watchPaths.push(path.join(linkPath, "node_modules", ".package-lock.json"));
Expand Down Expand Up @@ -79,12 +77,7 @@ async function watchNodeModules(
const res: Result = compareSync(genezioSdkPath, watchPath, options);
if (!res.same) {
debugLogger.debug(`[WATCH_NODE_MODULES] Rewriting the SDK to node_modules...`);
await writeSdkToNodeModules(
projectName,
projectRegion,
frontends ?? [],
sdkPath,
);
await writeSdkToNodeModules(projectName, frontends ?? [], sdkPath);
}
}
}
Expand All @@ -93,7 +86,6 @@ async function watchNodeModules(

async function writeSdkToNodeModules(
projectName: string,
projectRegion: string,
frontends: YamlFrontend[],
originSdkPath: string,
) {
Expand All @@ -115,7 +107,7 @@ async function writeSdkToNodeModules(
// A frontend can be explicitly declared in the genezio.yaml file or it can be linked to the project
const frontendPaths = (frontends || [])
.map((f) => f.path)
.concat(await getLinkPathsForProject(projectName, projectRegion));
.concat(await getLinkPathsForProject(projectName));
for (const frontendPath of frontendPaths) {
const to = path.join(frontendPath, "node_modules", "@genezio-sdk", `${projectName}`);

Expand Down
24 changes: 7 additions & 17 deletions src/genezio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
GenezioCreateInteractiveOptions,
GenezioDeleteOptions,
GenezioDeployOptions,
GenezioLinkOptions,
GenezioListOptions,
GenezioLocalOptions,
GenezioSdkOptions,
Expand Down Expand Up @@ -372,43 +371,34 @@ program

program
.command("link")
.option(
"--projectName <projectName>",
"The name of the project that you want to communicate with.",
)
.argument("[projectName]", "The name of the project that you want to communicate with.")
.summary("Links the genezio generated SDK in the current working directory")
.description(
"Linking a client with a deployed project will enable `genezio local` to figure out where to generate the SDK to call the backend methods.\n\
This command is useful when the project has dedicated repositories for the backend and the frontend.",
)
.action(async (options: GenezioLinkOptions) => {
await linkCommand(options.projectName).catch((error) => {
.action(async (projectName: string) => {
await linkCommand(projectName).catch((error) => {
logError(error);
log.error(
"Error: Command execution failed. Please ensure you are running this command from a directory containing 'genezio.yaml' or provide the '--projectName <name>' and '--region <region>' flags.",
);
exit(1);
});
});

program
.command("unlink")
.option("--all", "Remove all links.", false)
.option(
"--projectName <projectName>",
.argument(
"[projectName]",
"The name of the project that you want to communicate with. If --all is used, this option is ignored.",
)
.summary("Unlink the generated SDK from a client.")
.description(
"Clear the previously set path for your frontend app, which is useful when managing a project with multiple repositories.\n\
This reset allows 'genezio local' to stop automatically generating the SDK in that location.",
)
.action(async (options: GenezioUnlinkOptions) => {
await unlinkCommand(options.all, options.projectName).catch((error) => {
.action(async (projectName: string, options: GenezioUnlinkOptions) => {
await unlinkCommand(options.all, projectName).catch((error) => {
logError(error);
log.error(
"Error: Command execution failed. Please ensure you are running this command from a directory containing 'genezio.yaml' or provide the '--projectName <name>' and '--region <region>' flags.",
);
exit(1);
});
});
Expand Down
5 changes: 0 additions & 5 deletions src/models/commandOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ export interface GenezioSdkOptions extends BaseOptions {
packageVersion: string;
}

export interface GenezioLinkOptions extends BaseOptions {
projectName?: string;
}

export interface GenezioUnlinkOptions extends BaseOptions {
all: boolean;
projectName?: string;
}

export interface GenezioCreateInteractiveOptions extends BaseOptions {
Expand Down
7 changes: 2 additions & 5 deletions src/utils/linkDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ async function getLinkContent(): Promise<Map<string, string[]>> {
}
}

export async function getLinkPathsForProject(
projectName: string,
region: string,
): Promise<string[]> {
export async function getLinkPathsForProject(projectName: string): Promise<string[]> {
const content = await getLinkContent();
const key = `${projectName}:${region}`;
const key = `${projectName}`;
return content.get(key) || [];
}

Expand Down
1 change: 0 additions & 1 deletion src/utils/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export enum GenezioCommand {

export type ProjectPrimaryKeys = {
name: string;
region: string;
stage?: string;
};

Expand Down
Loading