dpkit
Version:
Fast TypeScript data management framework built on top of the Data Package standard and Polars DataFrames
43 lines (35 loc) • 1.01 kB
text/typescript
import { loadPackage } from "@dpkit/all"
import type { Session } from "./session.ts"
export async function selectResource(
session: Session,
options: { package?: string; resource?: string },
) {
if (!options.package) {
session.terminate("Please provide a path argument or a package option")
}
const dataPackage = await session.task(
"Loading package",
loadPackage(options.package),
)
const resourceName =
options.resource ??
(await session.select({
message: "Select resource",
options: dataPackage.resources.map(resource => ({
label: resource.name,
value: resource.name,
})),
}))
const resource = dataPackage.resources.find(
resource => resource.name === resourceName,
)
if (!resource) {
if (typeof resourceName !== "string") {
session.terminate("Resource selection cancelled")
}
session.terminate(
`Resource "${resourceName}" is not found in the provided data package`,
)
}
return resource
}