Skip to content

Commit

Permalink
Add support for multiple projects in the .env file
Browse files Browse the repository at this point in the history
  • Loading branch information
AsaAyers committed Jun 15, 2023
1 parent 884b7ac commit 5799e9d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion AppExplorer/view-file.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function readCardData(fullPath: string, path: string, project: Proj

return ({
path,
gitPath: fsPath.relative(project.projectRoot, fullPath),
gitPath: fsPath.relative(project.root, fullPath),
remote,
commitHash,
projectName,
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ npm install
npm run dev
```

To explore your codebase, this uses an environment variable `REPO_ROOT` and `AppExplorer.json`.
To explore other projects add numbered environment variables to `.env`

```sh
echo '{"name":"OtherProject","root":"src"}' > ../other-project/AppExplorer.json
REPO_ROOT=../other-project npm run dev
REPO1="../project-a"
REPO2="../project-b"
```

## Project config
Expand Down
2 changes: 1 addition & 1 deletion app/lsp/Project.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export type Project = {
readonly name: string;
readonly root: string;
readonly projectRoot: string;
readonly remote: string;
// readonly registrations?: Array<Registration>;
plugins: Array<string>;
};
8 changes: 1 addition & 7 deletions app/lsp/lsp.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@ import path from "path";
import type { Params } from "@remix-run/react";
import { getProjects } from "./projects";
import { childProcess } from "./components/child_process.server";
import type { Project } from "./Project";
export { getTypescriptConnection } from "./ts.server";

export type Project = {
readonly name: string;
readonly root: string;
readonly projectRoot: string;
// readonly registrations?: Array<Registration>;
plugins: Array<string>;
};
export function launchLanguageServer(
command: string,
args: string[]
Expand Down
82 changes: 40 additions & 42 deletions app/lsp/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,67 @@ import path from "path";
import type { Project } from "./Project";
import invariant from "tiny-invariant";
import { fs } from "~/fs-promises.server";
import { getRemoteURL } from "~/models/git.server";

const LSPProjects: Record<Project["name"], Project> = {};

export async function getProjects() {
const projects = LSPProjects;

const project = await prepareProject(path.join(__dirname, "../"));
const root = path.join(__dirname, "../");
const project = await prepareProject(root);
if (project) {
LSPProjects[project.name] = project;
}

const REPO_ROOT = process.env.REPO_ROOT;
const r = await Object.keys(process.env)
.filter((key) => key.startsWith("REPO"))
.map((key) => process.env[key])
.reduce(async (acc, repo) => {
const tmp = await acc;

// if REPO_ROOT contains an AppExplorer.json, read the file and add it to LSPProjects
if (typeof REPO_ROOT === "string") {
const project = await prepareProject(REPO_ROOT);
if (project) {
LSPProjects[project.name] = project;
}
}
if (typeof repo === "string") {
const project = await prepareProject(path.join(root, repo));
if (project) {
tmp[project.name] = project;
}
}
return acc;
}, Promise.resolve(projects));

return projects;
return r;
}
async function prepareProject(gitRoot: string): Promise<Project | undefined> {
const appExplorerJsonPath = path.join(gitRoot, "AppExplorer.json");

let stat;
try {
stat = await fs.stat(appExplorerJsonPath);
stat = await fs.stat(gitRoot);
if (!stat.isDirectory()) {
return;
}
} catch (e) {
return;
}
if (stat.isFile()) {
const projectConfig = JSON.parse(
await fs.readFile(appExplorerJsonPath, "utf8")
);
invariant(
typeof projectConfig.name === "string",
"expected a name in AppExplorer.json"
);
invariant(
typeof projectConfig.root === "string",
"expected a root in AppExplorer.json"
);

const pluginFolder = path.resolve(gitRoot, "AppExplorer");

let plugins: Project["plugins"] = [];
try {
if ((await fs.stat(pluginFolder)).isDirectory()) {
plugins = await readPlugins(projectConfig.name, pluginFolder);
}
} catch (e) {
// ignore
}
const projectConfig = {
root: gitRoot,
name: path.basename(gitRoot),
};

const pluginFolder = path.resolve(gitRoot, "AppExplorer");

return {
name: projectConfig.name,
root: path.resolve(gitRoot, projectConfig.root),
projectRoot: gitRoot,
plugins,
};
let plugins: Project["plugins"] = [];
try {
if ((await fs.stat(pluginFolder)).isDirectory()) {
plugins = await readPlugins(projectConfig.name, pluginFolder);
}
} catch (e) {
// ignore
}
return {
name: projectConfig.name,
root: gitRoot,
remote: await getRemoteURL(gitRoot),
plugins,
};
}

async function readPlugins(projectName: string, pluginFolder: string) {
Expand Down
12 changes: 10 additions & 2 deletions app/models/git.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import * as path from "path";
export function getCommitHash(fullPath: string): string {
const hash = child.spawnSync("git", ["rev-parse", "--short", "HEAD"], {
encoding: "utf-8",
cwd: path.dirname(fullPath),
// TODO: Figure out why I had to add this .git folder here. Before I added
// it, it claimed it couldn't find a git repo.
cwd: path.dirname(fullPath) + "/.git",
});

return hash.stdout.trim();
Expand All @@ -15,7 +17,9 @@ export function getCommitHash(fullPath: string): string {
export function getRemoteURL(fullPath: string): string {
const hash = child.spawnSync("git", ["remote", "-v"], {
encoding: "utf-8",
cwd: path.dirname(fullPath),
// TODO: Figure out why I had to add this .git folder here. Before I added
// it, it claimed it couldn't find a git repo.
cwd: path.dirname(fullPath) + "/.git",
});

const github = String(hash.stdout).match(
Expand All @@ -26,5 +30,9 @@ export function getRemoteURL(fullPath: string): string {
return github[0];
}

if (hash.stderr) {
console.warn(`Unable to read remote URL for ${fullPath}\n`, hash.stderr);
}

return "https://example.com/unknown_remote";
}

0 comments on commit 5799e9d

Please sign in to comment.