Skip to content
This repository was archived by the owner on Aug 21, 2025. It is now read-only.
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
16 changes: 8 additions & 8 deletions .trunk/trunk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ runtimes:
# This is the section where you manage your linters. (https://docs.trunk.io/check/configuration)
lint:
enabled:
- trivy@0.59.1
- trivy@0.60.0
- actionlint@1.7.7
- checkov@3.2.365
- eslint@9.19.0
- checkov@3.2.386
- eslint@9.22.0
- git-diff-check
- markdownlint@0.44.0
- osv-scanner@1.9.2
- prettier@3.4.2
- renovate@39.161.0
- osv-scanner@2.0.0
- prettier@3.5.3
- renovate@39.207.2
- shellcheck@0.10.0
- shfmt@3.6.0
- trufflehog@3.88.4
- yamllint@1.35.1
- trufflehog@3.88.17
- yamllint@1.36.2
actions:
enabled:
- trunk-announce
Expand Down
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"editor.formatOnSave": true,
"editor.defaultFormatter": "trunk.io",
"editor.trimAutoWhitespace": true,
"trunk.autoInit": false
"trunk.autoInit": false,
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
292 changes: 145 additions & 147 deletions src/commands/link/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@
*/

import { Command } from "@oclif/core";
import chalk from "chalk";
import * as fs from "../../util/fs.js";
import * as http from "node:http";
import { URL } from "node:url";
import open from "open";
import { execSync } from "child_process";
import path from "path";

import { ciStr } from "../../util/ci.js";
import { getProjectsByOrgReq, sendMapRepoAndFinishProjectCreationReq, sendCreateProjectReq, sendGetRepoIdReq } from "../../util/graphql.js";
import { confirmExistingProjectLink, confirmOverwriteCiHypFile, fileExists, getCiHypFilePath, getSettingsFilePath, getGitConfigFilePath, getGitRemoteUrl, getGithubWorkflowDir, promptProjectLinkSelection, promptProjectName, readSettingsJson, writeGithubInstallationIdToSettingsFile } from "../../util/index.js";

export default class LinkIndex extends Command {
static override hidden = true;

static override args = {};

static override description = "Link a repo with a Modus App to a Hypermode Project";
// static override description = "Link a repo with a Modus App to a Hypermode Project";
static override description = "Temporarily disabled during migration";

static override examples = ["<%= config.bin %> <%= command.id %>"];

Expand Down Expand Up @@ -100,135 +95,138 @@ export default class LinkIndex extends Command {
}

public async run(): Promise<void> {
// check if the directory has a .git/config with a remote named 'origin', if not, throw an error and ask them to set that up
const gitConfigFilePath = getGitConfigFilePath();

if (!(await fileExists(gitConfigFilePath))) {
throw new Error(chalk.red("No .git found in this directory. Please initialize a git repository with `git init`."));
}

// Check if the current branch is 'main'
let currentBranch = "";
try {
currentBranch = execSync("git symbolic-ref --short HEAD", { encoding: "utf-8" }).trim();
} catch (error) {
this.log(chalk.red("Unable to determine the current branch."));
throw error;
}

if (currentBranch !== "main") {
this.log(chalk.red("You must be on the 'main' branch to link your repository."));
this.log("Please switch to the 'main' branch:");
this.log(` > ${chalk.blue("git checkout main")}`);
this.log("or rename your current branch to 'main'.");
this.log(` > ${chalk.blue("git branch -m main")}`);
this.exit(1);
}

const remoteUrl = await getGitRemoteUrl(gitConfigFilePath);

if (!remoteUrl) {
this.log(chalk.red("`hyp link` requires a git remote to work"));
const gitRoot = execSync("git rev-parse --show-toplevel", { encoding: "utf-8" }).trim();
const projectName = path.basename(gitRoot);
this.log(`Please create a GitHub repository: https://github.com/new?name=${projectName}`);
this.log(`And push your code:`);
this.log(` > ${chalk.blue("git remote add origin <GIT_URL>)")}`);
this.log(` > ${chalk.blue("git push -u origin main")}`);

this.exit(1);
}

// check the .hypermode/settings.json and see if there is a installationId with a key for the github owner. if there is,
// continue, if not send them to github app installation page, and then go to callback server, and add installation id to settings.json

const settingsFilePath = getSettingsFilePath();
if (!(await fileExists(settingsFilePath))) {
this.log(chalk.red("Not logged in.") + " Log in with `hyp login`.");
return;
}

const settings = await readSettingsJson(settingsFilePath);

if (!settings.email || !settings.jwt || !settings.orgId) {
this.log(chalk.red("Not logged in.") + " Log in with `hyp login`.");
return;
}

const { gitOwner, repoName } = parseGitUrl(remoteUrl);

const repoFullName = `${gitOwner}/${repoName}`;

let installationId = null;

if (!settings.installationIds || !settings.installationIds[gitOwner]) {
installationId = await this.getUserInstallationThroughAuthFlow();
await writeGithubInstallationIdToSettingsFile(gitOwner, installationId);
} else {
installationId = settings.installationIds[gitOwner];
}

// call hypermode getRepoId with the installationId and the git url, if it returns a repoId, continue, if not, throw an error
const repoId = await sendGetRepoIdReq(settings.jwt, installationId, remoteUrl);

if (!repoId) {
throw new Error("No repoId found for the given installationId and gitUrl");
}

// get list of the projects for the user in this org, if any have no repoId, ask if they want to link it, or give option of none.
// If they pick an option, connect repo. If none, ask if they want to create a new project, prompt for name, and connect repoId to project
const projects = await getProjectsByOrgReq(settings.jwt, settings.orgId);

const projectsNoRepoId = projects.filter((project) => !project.repoId);

let selectedProject = null;

if (projectsNoRepoId.length > 0) {
const confirmExistingProject = await confirmExistingProjectLink();

if (confirmExistingProject) {
selectedProject = await promptProjectLinkSelection(projectsNoRepoId);
const completedProject = await sendMapRepoAndFinishProjectCreationReq(settings.jwt, selectedProject.id, repoId, repoFullName);

this.log(chalk.green("Successfully linked project " + completedProject.name + " to repo " + repoName + "! 🎉"));
} else {
const projectName = await promptProjectName(projects);
const newProject = await sendCreateProjectReq(settings.jwt, settings.orgId, projectName, repoId, repoFullName);

this.log(chalk.green("Successfully created project " + newProject.name + " and linked it to repo " + repoName + "! 🎉"));
}
} else {
const projectName = await promptProjectName(projects);
const newProject = await sendCreateProjectReq(settings.jwt, settings.orgId, projectName, repoId, repoFullName);

this.log(chalk.blueBright("Successfully created project " + newProject.name + " and linked it to repo " + repoFullName + "! Setting up CI workflow..."));
}

// add ci workflow to the repo if it doesn't already exist
const githubWorkflowDir = getGithubWorkflowDir();
const ciHypFilePath = getCiHypFilePath();

if (!(await fileExists(githubWorkflowDir))) {
// create the directory
await fs.mkdir(githubWorkflowDir, { recursive: true });
}

let shouldCreateCIFile = true;
if (await fileExists(ciHypFilePath)) {
// prompt if they want to replace it
const confirmOverwrite = await confirmOverwriteCiHypFile();
if (!confirmOverwrite) {
this.log(chalk.yellow("Skipping ci-modus-build.yml creation."));
shouldCreateCIFile = false;
}
}
this.error("Temporarily disabled during migration");
return;

// // check if the directory has a .git/config with a remote named 'origin', if not, throw an error and ask them to set that up
// const gitConfigFilePath = getGitConfigFilePath();

// if (!(await fileExists(gitConfigFilePath))) {
// throw new Error(chalk.red("No .git found in this directory. Please initialize a git repository with `git init`."));
// }

// // Check if the current branch is 'main'
// let currentBranch = "";
// try {
// currentBranch = execSync("git symbolic-ref --short HEAD", { encoding: "utf-8" }).trim();
// } catch (error) {
// this.log(chalk.red("Unable to determine the current branch."));
// throw error;
// }

// if (currentBranch !== "main") {
// this.log(chalk.red("You must be on the 'main' branch to link your repository."));
// this.log("Please switch to the 'main' branch:");
// this.log(` > ${chalk.blue("git checkout main")}`);
// this.log("or rename your current branch to 'main'.");
// this.log(` > ${chalk.blue("git branch -m main")}`);
// this.exit(1);
// }

// const remoteUrl = await getGitRemoteUrl(gitConfigFilePath);

// if (!remoteUrl) {
// this.log(chalk.red("`hyp link` requires a git remote to work"));
// const gitRoot = execSync("git rev-parse --show-toplevel", { encoding: "utf-8" }).trim();
// const projectName = path.basename(gitRoot);
// this.log(`Please create a GitHub repository: https://github.com/new?name=${projectName}`);
// this.log(`And push your code:`);
// this.log(` > ${chalk.blue("git remote add origin <GIT_URL>)")}`);
// this.log(` > ${chalk.blue("git push -u origin main")}`);

// this.exit(1);
// }

// // check the .hypermode/settings.json and see if there is a installationId with a key for the github owner. if there is,
// // continue, if not send them to github app installation page, and then go to callback server, and add installation id to settings.json

// const settingsFilePath = getSettingsFilePath();
// if (!(await fileExists(settingsFilePath))) {
// this.log(chalk.red("Not logged in.") + " Log in with `hyp login`.");
// return;
// }

// const settings = await readSettingsJson(settingsFilePath);

// if (!settings.email || !settings.apiKey || !settings.orgId) {
// this.log(chalk.red("Not logged in.") + " Log in with `hyp login`.");
// return;
// }

// const { gitOwner, repoName } = parseGitUrl(remoteUrl);

// const repoFullName = `${gitOwner}/${repoName}`;

// let installationId = null;

// if (!settings.installationIds || !settings.installationIds[gitOwner]) {
// installationId = await this.getUserInstallationThroughAuthFlow();
// await writeGithubInstallationIdToSettingsFile(gitOwner, installationId);
// } else {
// installationId = settings.installationIds[gitOwner];
// }

// // call hypermode getRepoId with the installationId and the git url, if it returns a repoId, continue, if not, throw an error
// const repoId = await sendGetRepoIdReq(settings.apiKey, installationId, remoteUrl);

// if (!repoId) {
// throw new Error("No repoId found for the given installationId and gitUrl");
// }

// // get list of the projects for the user in this org, if any have no repoId, ask if they want to link it, or give option of none.
// // If they pick an option, connect repo. If none, ask if they want to create a new project, prompt for name, and connect repoId to project
// const projects = await getProjectsByOrgReq(settings.apiKey, settings.orgId);

// const projectsNoRepoId = projects.filter((project) => !project.repoId);

// let selectedProject = null;

// if (projectsNoRepoId.length > 0) {
// const confirmExistingProject = await confirmExistingProjectLink();

// if (confirmExistingProject) {
// selectedProject = await promptProjectLinkSelection(projectsNoRepoId);
// const completedProject = await sendMapRepoAndFinishProjectCreationReq(settings.apiKey, selectedProject.id, repoId, repoFullName);

// this.log(chalk.green("Successfully linked project " + completedProject.name + " to repo " + repoName + "! 🎉"));
// } else {
// const projectName = await promptProjectName(projects);
// const newProject = await sendCreateProjectReq(settings.apiKey, settings.orgId, projectName, repoId, repoFullName);

// this.log(chalk.green("Successfully created project " + newProject.name + " and linked it to repo " + repoName + "! 🎉"));
// }
// } else {
// const projectName = await promptProjectName(projects);
// const newProject = await sendCreateProjectReq(settings.apiKey, settings.orgId, projectName, repoId, repoFullName);

// this.log(chalk.blueBright("Successfully created project " + newProject.name + " and linked it to repo " + repoFullName + "! Setting up CI workflow..."));
// }

// // add ci workflow to the repo if it doesn't already exist
// const githubWorkflowDir = getGithubWorkflowDir();
// const ciHypFilePath = getCiHypFilePath();

// if (!(await fileExists(githubWorkflowDir))) {
// // create the directory
// await fs.mkdir(githubWorkflowDir, { recursive: true });
// }

// let shouldCreateCIFile = true;
// if (await fileExists(ciHypFilePath)) {
// // prompt if they want to replace it
// const confirmOverwrite = await confirmOverwriteCiHypFile();
// if (!confirmOverwrite) {
// this.log(chalk.yellow("Skipping ci-modus-build.yml creation."));
// shouldCreateCIFile = false;
// }
// }

if (shouldCreateCIFile) {
await fs.writeFile(ciHypFilePath, ciStr, { flag: "w" });
this.log(chalk.green("Modus CI workflow added to your project. Commit this change to initiate a deployment to Hypermode."));
}

this.log(chalk.green("Linking complete! 🎉"));
// if (shouldCreateCIFile) {
// await fs.writeFile(ciHypFilePath, ciStr, { flag: "w" });
// this.log(chalk.green("Modus CI workflow added to your project. Commit this change to initiate a deployment to Hypermode."));
// }

// this.log(chalk.green("Linking complete! 🎉"));
}
}

Expand Down Expand Up @@ -284,16 +282,16 @@ const linkHTML = `<!-- src/commands/login/login.html -->
</html>
`;

function parseGitUrl(gitUrl: string) {
const regex = /^(?:git@|https:\/\/)([^:/]+)[:/]([^/]+)\/([^/]+?)(?:\.git)?$/;
const match = gitUrl.match(regex);
// function parseGitUrl(gitUrl: string) {
// const regex = /^(?:git@|https:\/\/)([^:/]+)[:/]([^/]+)\/([^/]+?)(?:\.git)?$/;
// const match = gitUrl.match(regex);

if (!match) {
throw new Error(`Invalid Git URL: ${gitUrl}`);
}
// if (!match) {
// throw new Error(`Invalid Git URL: ${gitUrl}`);
// }

const gitOwner = match[2];
const repoName = match[3];
// const gitOwner = match[2];
// const repoName = match[3];

return { gitOwner, repoName };
}
// return { gitOwner, repoName };
// }
Loading