@fragment-dev/cli
Version:
134 lines (131 loc) • 3.82 kB
JavaScript
import {
inquirer_default
} from "./chunk-JRTPSPNZ.js";
import {
AuthenticatedCommand
} from "./chunk-W7HG2VEH.js";
import {
require_lib
} from "./chunk-UD22EIAP.js";
import {
require_source
} from "./chunk-M5OAS5QZ.js";
import {
__toESM,
init_cjs_shims
} from "./chunk-7GH3YGSC.js";
// src/commands/get-schema.ts
init_cjs_shims();
import fs from "fs";
var import_core = __toESM(require_lib(), 1);
import path from "path";
var import_chalk = __toESM(require_source(), 1);
var GetSchema = class _GetSchema extends AuthenticatedCommand {
static {
this.description = "Retrieve a schema from the API for local use";
}
static {
this.examples = ["<%= config.bin %> <%= command.id %>"];
}
static {
this.flags = {
key: import_core.Flags.string({
char: "k",
description: "Key of schema (e.g. template-schema)"
}),
version: import_core.Flags.integer({
description: "Schema version to retrieve (e.g. 2). Defaults to latest available version."
}),
output: import_core.Flags.string({
char: "o",
description: "Path to the schema file (e.g. /home/user/code/schema.jsonc). Defaults to the current directory and the schema key as the file name."
})
};
}
async downloadSchema({
key,
version,
destination: destinationParam
}) {
let destination = destinationParam;
const res = await this.sdk.GetSchema({
key,
version
});
this.assert(res, { message: `Could not find Schema ${key}` });
const logLineParts = [
`Could not find schema ${key}`,
this.flags.version ? `(version: ${version})` : ""
];
const { schema } = res;
if (!schema) {
this.logToStderr(`${logLineParts.join(" ")}.`);
this.exit(1);
}
const schemaKey = schema.key;
const schemaJson = schema.version.json;
if (!destination) {
destination = `${schemaKey}.jsonc`;
}
fs.writeFileSync(destination, `${JSON.stringify(schemaJson, null, 2)}
`);
this.log(`Schema written to ${import_chalk.default.yellow(destination)}.`);
}
async listSchemas() {
this.log("Fetching schemas...");
const [{ workspace }, { schemas }] = await Promise.all([
this.sdk.GetWorkspace(),
this.sdk.ListSchemas()
]);
this.assert(schemas, { message: "No schemas found" });
if (schemas.nodes.length === 0) {
this.log(
`No schemas found in workspace ${import_chalk.default.yellow(workspace.name)} (ID: ${workspace.id}).`
);
this.exit(1);
}
const { schemaKey } = await inquirer_default.prompt([
{
type: "list",
name: "schemaKey",
message: "Choose a schema to download",
choices: schemas.nodes.map((schema) => ({
name: schema.name === schema.key ? schema.key : `${schema.name} (Key: ${schema.key})`,
value: schema.key
}))
}
]);
return schemaKey;
}
async run() {
const { flags } = await this.parse(_GetSchema);
let destination;
if (flags.output) {
const dirname = path.dirname(flags.output);
if (!fs.existsSync(dirname)) {
this.logToStderr(
`Output directory ${dirname} passed to --output does not exist. Please create the directory and rerun.`
);
this.exit(1);
}
const destinationFilename = path.basename(flags.output);
if (path.extname(destinationFilename) !== ".jsonc") {
this.logToStderr(`Filename extension should be .jsonc.`);
this.exit(1);
}
destination = this.flags.output;
}
let schemaKey = flags.key;
if (!schemaKey) {
schemaKey = await this.listSchemas();
}
await this.downloadSchema({
key: schemaKey,
version: flags.version,
destination
});
}
};
export {
GetSchema
};