import { existsSync, walkSync } from "../deno_ral/fs.ts";
import { basename, join } from "../deno_ral/path.ts";
const kExecutionFiles = [
"environment.yml",
"requirements.txt",
"renv.lock",
"Pipfile",
"Pipfile.lock",
"setup.py",
"Project.toml",
"REQUIRE",
"install.R",
"apt.txt",
"DESCRIPTION",
"postBuild",
"start",
"runtime.txt",
"default.nix",
"Dockerfile",
];
export function isReesEnvronmentFile(path: string) {
return kExecutionFiles.includes(basename(path));
}
export function codeSpacesUrl(repoUrl: string) {
const containerUrl = repoUrl.replace(
/(https?\:\/\/github.com)\/([a-zA-Z0-9-_\.]+?)\/([a-zA-Z0-9-_\.]+?)\//,
"$1/codespaces/new/$2/$3?resume=1",
);
return containerUrl;
}
export interface BinderOptions {
openFile?: string;
editor?: "vscode" | "rstudio" | "jupyterlab";
}
export function binderUrl(
organization: string,
repository: string,
binderOptions?: BinderOptions,
) {
const url = [`https://mybinder.org/v2/gh/${organization}/${repository}/HEAD`];
const params = [];
if (binderOptions && binderOptions.openFile) {
params.push(`labpath=${binderOptions.openFile}`);
}
if (binderOptions?.editor) {
switch (binderOptions.editor) {
case "rstudio":
params.push(`urlpath=rstudio`);
break;
case "vscode":
params.push(`urlpath=vscode`);
break;
case "jupyterlab":
default:
break;
}
}
if (params.length > 0) {
url.push("?");
url.push(params.join("&"));
}
return url.join("");
}
export function isDevContainerFile(relPath: string) {
if (relPath === join(".devcontainer", "devcontainer.json")) {
return true;
}
if (relPath === ".devcontainer.json") {
return true;
}
if (relPath.match(/^\.devcontainer[\/\\][^\/\\]+?[\/\\]devcontainer.json$/)) {
return true;
}
return false;
}
export function hasBinderCompatibleEnvironment(dir: string) {
for (const executionFile of kExecutionFiles) {
if (existsSync(join(dir, executionFile))) {
return true;
}
}
return false;
}
export function hasDevContainer(dir: string) {
if (existsSync(join(dir, ".devcontainer", "devcontainer.json"))) {
return true;
}
if (existsSync(join(dir, ".devcontainer.json"))) {
return true;
}
const containerRootDir = join(dir, ".devcontainer");
if (existsSync(containerRootDir)) {
for (
const walk of walkSync(containerRootDir, {
maxDepth: 1,
includeFiles: false,
includeDirs: true,
})
) {
if (existsSync(join(containerRootDir, walk.name, "devcontainer.json"))) {
return true;
}
}
}
return false;
}