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
Expose the EdgeDB CLI as npx edgedb
#931
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ad55134
Repurpose the `cli.ts` module for EdgeDB CLI
scotttrinh 81945ab
Prefer `https` over curl/iwr
scotttrinh a3e00ef
Get binary directory from temporary CLI
scotttrinh c769bc2
Update for `install-dir`
scotttrinh a87d821
Use a URL instead of strings
scotttrinh bc89528
Avoid building an intermediate URL object
scotttrinh 7679bcc
Simplify getting the cliLocation update debug
scotttrinh 08c45f4
Fix logic based on actual usage
scotttrinh 0a86879
Format and better error message
scotttrinh 83cc377
Add `mts` files to prettier check
scotttrinh e1d6bf0
Add explicit dependency on semver
scotttrinh 6f2839b
Use compatible stable version
scotttrinh 3cf5511
Check preconditions before opening fd
scotttrinh f93e76f
Ensure data is flushed after downloading
scotttrinh 7819a58
Break up temporary CLI and self-install steps
scotttrinh 5fec348
Slightly better error message
scotttrinh 3a6fb27
prettier
scotttrinh e873d0f
Only write messages to stdout/err if TTY
scotttrinh df325c3
Always output errors to stderr even if not TTY
scotttrinh File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,303 @@ | ||
#!/usr/bin/env node | ||
import { execSync, type ExecSyncOptions } from "node:child_process"; | ||
import { createWriteStream } from "node:fs"; | ||
import * as os from "node:os"; | ||
import * as fs from "node:fs/promises"; | ||
import * as path from "node:path"; | ||
import * as process from "node:process"; | ||
import * as semver from "semver"; | ||
import envPaths from "env-paths"; | ||
import Debug from "debug"; | ||
import which from "which"; | ||
|
||
const debug = Debug("edgedb:cli"); | ||
|
||
const IS_TTY = process.stdout.isTTY; | ||
const EDGEDB_PKG_ROOT = "https://packages.edgedb.com"; | ||
const CACHE_DIR = envPaths("edgedb").cache; | ||
const TEMPORARY_CLI_PATH = path.join(CACHE_DIR, "/edgedb-cli"); | ||
const CLI_LOCATION_CACHE_FILE_PATH = path.join(CACHE_DIR, "/cli-location"); | ||
|
||
interface Package { | ||
name: string; | ||
version: string; | ||
revision: string; | ||
installref: string; | ||
} | ||
|
||
try { | ||
await main(process.argv.slice(2)); | ||
process.exit(0); | ||
} catch (err) { | ||
console.error(err); | ||
if ( | ||
typeof err === "object" && | ||
err !== null && | ||
"code" in err && | ||
typeof err.code === "number" | ||
) { | ||
process.exit(err.code); | ||
} | ||
|
||
process.exit(1); | ||
} | ||
|
||
async function main(args: string[]) { | ||
debug("Starting main function with args:", args); | ||
const cliLocation = | ||
(await whichEdgeDbCli()) ?? | ||
(await getCliLocationFromCache()) ?? | ||
(await getCliLocationFromTempCli()) ?? | ||
(await selfInstallFromTempCli()) ?? | ||
null; | ||
|
||
if (cliLocation === null) { | ||
throw Error("Failed to find or install EdgeDB CLI."); | ||
} | ||
|
||
return runEdgeDbCli(args, cliLocation); | ||
} | ||
|
||
async function whichEdgeDbCli() { | ||
debug("Checking if CLI is in PATH..."); | ||
const location = await which("edgedb", { nothrow: true }); | ||
if (location) { | ||
debug(` - CLI found in PATH at: ${location}`); | ||
return location; | ||
} | ||
debug(" - No CLI found in PATH."); | ||
return null; | ||
} | ||
|
||
async function getCliLocationFromCache(): Promise<string | null> { | ||
debug("Checking CLI cache..."); | ||
try { | ||
const cachedBinaryPath = ( | ||
await fs.readFile(CLI_LOCATION_CACHE_FILE_PATH, { encoding: "utf8" }) | ||
).trim(); | ||
debug(" - CLI path in cache at:", cachedBinaryPath); | ||
|
||
try { | ||
await fs.access(cachedBinaryPath, fs.constants.F_OK); | ||
debug(" - CLI binary found in path:", cachedBinaryPath); | ||
return cachedBinaryPath; | ||
} catch (err) { | ||
debug(" - No CLI found in cache.", err); | ||
return null; | ||
} | ||
} catch (err) { | ||
debug(" - Cache file cannot be read.", err); | ||
return null; | ||
} | ||
} | ||
|
||
async function getCliLocationFromTempCli(): Promise<string | null> { | ||
debug("Installing temporary CLI to get install directory..."); | ||
await downloadCliPackage(); | ||
|
||
const installDir = getInstallDir(TEMPORARY_CLI_PATH); | ||
const binaryPath = path.join(installDir, "edgedb"); | ||
await fs.writeFile(CLI_LOCATION_CACHE_FILE_PATH, binaryPath, { | ||
encoding: "utf8", | ||
}); | ||
debug(" - CLI installed at:", binaryPath); | ||
|
||
try { | ||
debug(" - CLI binary found in path:", binaryPath); | ||
await fs.access(binaryPath, fs.constants.F_OK); | ||
return binaryPath; | ||
} catch { | ||
debug(" - CLI binary not found in path:", binaryPath); | ||
return null; | ||
} | ||
} | ||
|
||
async function selfInstallFromTempCli(): Promise<string | null> { | ||
debug("Self-installing EdgeDB CLI..."); | ||
runEdgeDbCli(["_self_install"], TEMPORARY_CLI_PATH); | ||
debug(" - CLI self-installed successfully."); | ||
return getCliLocationFromCache(); | ||
} | ||
|
||
async function downloadCliPackage() { | ||
if (IS_TTY) { | ||
console.log("No EdgeDB CLI found, downloading CLI package..."); | ||
} | ||
debug("Downloading CLI package..."); | ||
const cliPkg = await findPackage(); | ||
const downloadDir = path.dirname(TEMPORARY_CLI_PATH); | ||
await fs.mkdir(downloadDir, { recursive: true }).catch((error) => { | ||
if (error.code !== "EEXIST") throw error; | ||
}); | ||
const downloadUrl = new URL(cliPkg.installref, EDGEDB_PKG_ROOT); | ||
await downloadFile(downloadUrl, TEMPORARY_CLI_PATH); | ||
debug(" - CLI package downloaded to:", TEMPORARY_CLI_PATH); | ||
|
||
const fd = await fs.open(TEMPORARY_CLI_PATH, "r+"); | ||
await fd.chmod(0o755); | ||
await fd.datasync(); | ||
await fd.close(); | ||
} | ||
|
||
function runEdgeDbCli( | ||
args: string[], | ||
pathToCli: string | null, | ||
execOptions: ExecSyncOptions = { stdio: "inherit" } | ||
) { | ||
const cliCommand = pathToCli ?? "edgedb"; | ||
const command = `${cliCommand} ${args.join(" ")}`; | ||
debug(`Running EdgeDB CLI: ${command}`); | ||
return execSync(command, execOptions); | ||
} | ||
|
||
async function findPackage(): Promise<Package> { | ||
const arch = os.arch(); | ||
const platform = os.platform(); | ||
const includeCliPrereleases = true; | ||
const cliVersionRange = ">=4.1.1"; | ||
const libc = platform === "linux" ? "musl" : ""; | ||
const dist = getBaseDist(arch, platform, libc); | ||
|
||
debug(`Finding compatible package for ${dist}...`); | ||
const versionMap = await getVersionMap(dist); | ||
const pkg = await getMatchingPkg( | ||
versionMap, | ||
cliVersionRange, | ||
includeCliPrereleases | ||
); | ||
if (!pkg) { | ||
throw Error( | ||
`No compatible EdgeDB CLI package found for the current platform ${dist}` | ||
); | ||
} | ||
debug(" - Package found:", pkg); | ||
return pkg; | ||
} | ||
|
||
async function getVersionMap(dist: string): Promise<Map<string, Package>> { | ||
debug("Getting version map for distribution:", dist); | ||
const indexRequest = await fetch( | ||
new URL(`archive/.jsonindexes/${dist}.json`, EDGEDB_PKG_ROOT) | ||
); | ||
const index = (await indexRequest.json()) as { packages: Package[] }; | ||
const versionMap = new Map(); | ||
|
||
for (const pkg of index.packages) { | ||
if (pkg.name !== "edgedb-cli") { | ||
continue; | ||
} | ||
|
||
if ( | ||
!versionMap.has(pkg.version) || | ||
versionMap.get(pkg.version).revision < pkg.revision | ||
) { | ||
versionMap.set(pkg.version, pkg); | ||
} | ||
} | ||
|
||
return versionMap; | ||
} | ||
|
||
async function getMatchingPkg( | ||
versionMap: Map<string, Package>, | ||
cliVersionRange: string, | ||
includeCliPrereleases: boolean | ||
): Promise<Package | null> { | ||
debug("Getting matching version for range:", cliVersionRange); | ||
let matchingPkg: Package | null = null; | ||
for (const [version, pkg] of versionMap.entries()) { | ||
if ( | ||
semver.satisfies(version, cliVersionRange, { | ||
includePrerelease: includeCliPrereleases, | ||
}) | ||
) { | ||
if ( | ||
!matchingPkg || | ||
semver.compareBuild(version, matchingPkg.version) > 0 | ||
) { | ||
matchingPkg = pkg; | ||
} | ||
} | ||
} | ||
|
||
if (matchingPkg) { | ||
debug(" - Matching version found:", matchingPkg.version); | ||
return matchingPkg; | ||
} else { | ||
throw Error( | ||
"no published EdgeDB CLI version matches requested version " + | ||
`'${cliVersionRange}'` | ||
); | ||
} | ||
} | ||
|
||
async function downloadFile(url: string | URL, path: string) { | ||
debug("Downloading file from URL:", url); | ||
const response = await fetch(url); | ||
if (!response.ok || !response.body) { | ||
throw new Error(` - Download failed: ${response.statusText}`); | ||
} | ||
|
||
const fileStream = createWriteStream(path, { flush: true }); | ||
|
||
if (response.body) { | ||
for await (const chunk of streamReader(response.body)) { | ||
fileStream.write(chunk); | ||
} | ||
fileStream.end(); | ||
debug(" - File downloaded successfully."); | ||
} else { | ||
throw new Error(" - Download failed: no response body"); | ||
} | ||
} | ||
|
||
function getBaseDist(arch: string, platform: string, libc = ""): string { | ||
debug("Getting base distribution for:", arch, platform, libc); | ||
let distArch = ""; | ||
let distPlatform = ""; | ||
|
||
if (platform === "linux") { | ||
if (libc === "") { | ||
libc = "gnu"; | ||
} | ||
distPlatform = `unknown-linux-${libc}`; | ||
} else if (platform === "darwin") { | ||
distPlatform = "apple-darwin"; | ||
} else { | ||
throw Error(`This action cannot be run on ${platform}`); | ||
} | ||
|
||
if (arch === "x64") { | ||
distArch = "x86_64"; | ||
} else if (arch === "arm64") { | ||
distArch = "aarch64"; | ||
} else { | ||
throw Error(`This action does not support the ${arch} architecture`); | ||
} | ||
|
||
const dist = `${distArch}-${distPlatform}`; | ||
debug(" - Base distribution:", dist); | ||
return dist; | ||
} | ||
|
||
function getInstallDir(cliPath: string): string { | ||
debug("Getting install directory for CLI path:", cliPath); | ||
const installDir = runEdgeDbCli(["info", "--get", "'install-dir'"], cliPath, { | ||
stdio: "pipe", | ||
}) | ||
.toString() | ||
.trim(); | ||
debug(" - Install directory:", installDir); | ||
return installDir; | ||
} | ||
|
||
async function* streamReader(readableStream: ReadableStream<Uint8Array>) { | ||
debug("Reading stream..."); | ||
const reader = readableStream.getReader(); | ||
while (true) { | ||
const { done, value } = await reader.read(); | ||
if (done) break; | ||
yield value; | ||
} | ||
debug(" - Stream reading completed."); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"module": "node16", | ||
"moduleResolution": "node16", | ||
"outDir": "./dist", | ||
"declaration": false | ||
}, | ||
"include": ["src/cli.mts"] | ||
} | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of this logic is adapted from https://github.com/edgedb/setup-edgedb/blob/main/src/main.ts#L123 and ought to be centralized into a separate package to keep them in sync with each other, but there are some slight differences between the GitHub Actions runtime and a plain Node runtime, so I decided the easiest thing to do for now was to copy.
We can still look at making a common abstraction they can share (probably this logic without the side effects), but I don't want to block this effort on that.