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

Use CLI wrapper to initialize project #1012

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
54 changes: 6 additions & 48 deletions packages/create/src/recipes/_edgedb/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "node:path";
import debug from "debug";

import type { BaseOptions, Recipe } from "../types.js";
import { copyTemplateFiles, execInLoginShell } from "../../utils.js";
import { copyTemplateFiles, execInPackageManager } from "../../utils.js";

const logger = debug("@edgedb/create:recipe:edgedb");

Expand All @@ -28,59 +28,17 @@ const recipe: Recipe<EdgeDBOptions> = {
{ initializeProject }: EdgeDBOptions
) {
logger("Running edgedb recipe");
logger("Checking for existing EdgeDB CLI");

const spinner = p.spinner();

if (initializeProject) {
let edgedbCliVersion: string | null = null;
let shouldInstallCli: boolean | symbol = true;
try {
const { stdout } = await execInLoginShell("edgedb --version");
edgedbCliVersion = stdout.trim();
logger(edgedbCliVersion);
} catch (error) {
logger("No EdgeDB CLI detected");
shouldInstallCli = await p.confirm({
message:
"The EdgeDB CLI is required to initialize a project. Install now?",
initialValue: true,
});
}

if (edgedbCliVersion === null) {
if (shouldInstallCli === false) {
logger("User declined to install EdgeDB CLI");
throw new Error("EdgeDB CLI is required");
}

logger("Installing EdgeDB CLI");

spinner.start("Installing EdgeDB CLI");
const { stdout, stderr } = await execInLoginShell(
"curl --proto '=https' --tlsv1.2 -sSf https://sh.edgedb.com | sh -s -- -y"
);
logger({ stdout, stderr });
spinner.stop("EdgeDB CLI installed");
}

try {
const { stdout } = await execInLoginShell("edgedb --version");
edgedbCliVersion = stdout.trim();
logger(edgedbCliVersion);
} catch (error) {
logger("EdgeDB CLI could not be installed.");
logger(error);
throw new Error("EdgeDB CLI could not be installed.");
}

spinner.start("Initializing EdgeDB project");
try {
await execInLoginShell("edgedb project init --non-interactive", {
await execInPackageManager("edgedb project init --non-interactive", {
cwd: projectDir,
});
const { stdout, stderr } = await execInLoginShell(
"edgedb query 'select sys::get_version_as_str()'",
const { stdout, stderr } = await execInPackageManager(
`edgedb query "select sys::get_version_as_str()"`,
{ cwd: projectDir }
);
const serverVersion = JSON.parse(stdout.trim());
Expand Down Expand Up @@ -122,10 +80,10 @@ const recipe: Recipe<EdgeDBOptions> = {
logger("Creating and applying initial migration");
spinner.start("Creating and applying initial migration");
try {
await execInLoginShell("edgedb migration create", {
await execInPackageManager("edgedb migration create", {
cwd: projectDir,
});
await execInLoginShell("edgedb migrate", { cwd: projectDir });
await execInPackageManager("edgedb migrate", { cwd: projectDir });
spinner.stop("Initial migration created and applied");
} catch (error) {
logger(error);
Expand Down
2 changes: 1 addition & 1 deletion packages/create/src/recipes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export const recipes: Recipe<any>[] = [
remix,
sveltekit,
// init
_edgedbInit,
_install,
_edgedbInit,
];
25 changes: 25 additions & 0 deletions packages/create/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,28 @@ stdout: ${stdout}`
});
});
}

export async function execInPackageManager(
cmd: string,
options?: SpawnOptionsWithoutStdio
): Promise<{ stdout: string; stderr: string }> {
const packageManager = getPackageManager();
let command;
switch (packageManager) {
case "yarn":
command = `yarn exec -- ${cmd}`;
break;
case "bun":
command = `bun run --bun ${cmd}`;
break;
case "pnpm":
command = `pnpm exec ${cmd}`;
break;
case "npm":
command = `npm exec -- ${cmd}`;
break;
default:
command = cmd;
}
return execInLoginShell(command, options);
}