Skip to content
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
79 changes: 52 additions & 27 deletions src/helpers/installPackagesAndFetchDocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,83 @@ export async function installPackagesAndFetchDocs(
let lastDoc = "";
for (const pkg of selectedPackages) {
const s = spinner();
const installCmd = runtime === "deno"
? `deno add jsr:${pkg}`
: runtime === "bun"
? `bun add ${pkg}`
: `npm install ${pkg}`;

if (!options.silent) {
s.start(`Installing ${pkg} via ${runtime}...`);
const pkgsToInstall = [pkg];
if (
pkg === "@nshiab/simple-data-analysis" ||
pkg === "@nshiab/journalism-dataviz"
) {
pkgsToInstall.push("@observablehq/plot");
}

try {
await new Promise((resolve, reject) => {
commandRunner.exec(installCmd, (error) => {
if (error) {
reject(error);
} else {
resolve(void 0);
}
});
});
for (const p of pkgsToInstall) {
const isObservablePlot = p === "@observablehq/plot";
const installCmd = runtime === "deno"
? (isObservablePlot ? "deno add npm:" + p : "deno add jsr:" + p)
: runtime === "bun"
? "bun add " + p
: "npm install " + p;

if (!options.silent) {
s.stop(`✅ ${pkg} installed!`);
s.start("Installing " + p + " via " + runtime + "...");
}

try {
await new Promise((resolve, reject) => {
commandRunner.exec(installCmd, (error) => {
if (error) {
reject(error);
} else {
resolve(void 0);
}
});
});
if (!options.silent) {
s.stop("✅ " + p + " installed!");
}
} catch (error) {
if (!options.silent) {
s.stop("❌ Failed to install " + p + ".");
}
throw error;
}
}

const sFetch = spinner();
// Documentation logic
if (pkg === "@observablehq/plot") {
continue;
}

const sFetch = spinner();
try {
if (!options.silent) {
sFetch.start(`Fetching documentation for ${pkg}...`);
sFetch.start("Fetching documentation for " + pkg + "...");
}
const repoName = pkg.split("/")[1];
const url =
`https://raw.githubusercontent.com/nshiab/${repoName}/refs/heads/main/llm.md`;
const url = "https://raw.githubusercontent.com/nshiab/" + repoName +
"/refs/heads/main/llm.md";

const response = await fetch(url);
if (response.ok) {
const docContent = await response.text();
writeFileSync(join("docs", `${repoName}.md`), docContent);
writeFileSync(join("docs", repoName + ".md"), docContent);
if (!options.silent) {
sFetch.stop(`✅ Documentation for ${pkg} saved!`);
sFetch.stop("✅ Documentation for " + pkg + " saved!");
}
lastDoc = docContent;
} else {
if (!options.silent) {
sFetch.stop(
`⚠️ No documentation (llm.md) found for ${pkg}. Skipping doc fetch.`,
"⚠️ No documentation (llm.md) found for " + pkg +
". Skipping doc fetch.",
);
}
}
} catch (error) {
if (!options.silent) {
s.stop(`❌ Failed to install ${pkg}.`);
sFetch.stop("❌ Failed to fetch documentation for " + pkg + ".");
}
throw error;
console.error(error);
}
}
return lastDoc;
Expand Down
8 changes: 8 additions & 0 deletions test/installPackagesAndFetchDocs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ Deno.test("installPackagesAndFetchDocs - should try to install packages and fetc
execStub.calls[0].args[0],
"deno add jsr:@nshiab/simple-data-analysis",
);
assertEquals(
execStub.calls[1].args[0],
"deno add npm:@observablehq/plot",
);
} finally {
execStub.restore();
globalThis.fetch = originalFetch;
Expand Down Expand Up @@ -95,6 +99,10 @@ Deno.test("installPackagesAndFetchDocs - should use npm install for node runtime
execStub.calls[0].args[0],
"npm install @nshiab/simple-data-analysis",
);
assertEquals(
execStub.calls[1].args[0],
"npm install @observablehq/plot",
);
} finally {
runtimeStub.restore();
execStub.restore();
Expand Down