@settlemint/btp-sdk-cli
Advanced tools
Comparing version 0.3.2-prfa8007a to 0.3.2-prfc7de4c
#!/usr/bin/env node | ||
import { cancel, select, isCancel, intro, outro, spinner, password, text } from '@clack/prompts'; | ||
import { cancel, intro, outro, spinner, select, isCancel, password, text } from '@clack/prompts'; | ||
import { Command } from '@commander-js/extra-typings'; | ||
import dotenv from 'dotenv'; | ||
import { magentaBright, inverse, greenBright, redBright } from 'yoctocolors'; | ||
import { cosmiconfig } from 'cosmiconfig'; | ||
import { writeFileSync, readFileSync, existsSync } from 'node:fs'; | ||
import path from 'node:path'; | ||
import { cosmiconfig } from 'cosmiconfig'; | ||
import { merge } from 'ts-deepmerge'; | ||
@@ -82,17 +82,17 @@ import { z } from 'zod'; | ||
var printCancel = (msg) => cancel(inverse(redBright(msg))); | ||
var promptPassword = async (options) => { | ||
const passwordResult = await password(options); | ||
if (isCancel(passwordResult)) { | ||
var handleCancellation = (result) => { | ||
if (isCancel(result)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
return passwordResult; | ||
}; | ||
var promptPassword = async (options) => { | ||
const result = await password(options); | ||
handleCancellation(result); | ||
return result; | ||
}; | ||
var promptText = async (options) => { | ||
const textResult = await text(options); | ||
if (isCancel(textResult)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
return textResult; | ||
const result = await text(options); | ||
handleCancellation(result); | ||
return result; | ||
}; | ||
@@ -106,2 +106,24 @@ var printSpinner = async (options) => { | ||
}; | ||
var promptSelect = async ({ | ||
items, | ||
message, | ||
allowNone = false | ||
}) => { | ||
if (items.length === 0 && !allowNone) { | ||
return void 0; | ||
} | ||
const options = items.map((item) => ({ | ||
value: item, | ||
label: item.name | ||
})); | ||
if (allowNone) { | ||
options.unshift({ value: void 0, label: "None" }); | ||
} | ||
const result = await select({ | ||
message, | ||
options | ||
}); | ||
handleCancellation(result); | ||
return result; | ||
}; | ||
function findProjectRoot(startDir) { | ||
@@ -119,25 +141,59 @@ let currentDir = startDir; | ||
// src/lib/config.ts | ||
var ConfigSchema = z.object({ | ||
var MinimalConfigSchema = z.object({ | ||
instance: z.string() | ||
}); | ||
var RequiredConfigSchema = MinimalConfigSchema.extend({ | ||
workspace: z.string(), | ||
application: z.string() | ||
}); | ||
var OptionalConfigSchema = RequiredConfigSchema.extend({ | ||
portal: z.string().optional(), | ||
graph: z.string().optional(), | ||
hasura: z.string().optional(), | ||
node: z.string().optional() | ||
}); | ||
var EnvSchema = z.object({ | ||
BTP_PAT_TOKEN: z.string() | ||
}); | ||
var ConfigEnvSchema = ConfigSchema.extend({ | ||
var MinimalConfigEnvSchema = OptionalConfigSchema.extend({ | ||
pat: z.string() | ||
}); | ||
async function config() { | ||
const config2 = await parseConfig(); | ||
var RequiredConfigEnvSchema = OptionalConfigSchema.extend({ | ||
pat: z.string() | ||
}); | ||
var OptionalConfigEnvSchema = OptionalConfigSchema.extend({ | ||
pat: z.string() | ||
}); | ||
async function config(type = "optional") { | ||
const config2 = await parseConfig(type); | ||
if (!config2) { | ||
throw new Error("No config found"); | ||
} | ||
return ConfigEnvSchema.parse({ ...config2, pat: process.env.BTP_PAT_TOKEN }); | ||
switch (type) { | ||
case "minimal": | ||
return MinimalConfigEnvSchema.parse({ ...config2, pat: process.env.BTP_PAT_TOKEN }); | ||
case "required": | ||
return RequiredConfigEnvSchema.parse({ ...config2, pat: process.env.BTP_PAT_TOKEN }); | ||
case "optional": | ||
return OptionalConfigEnvSchema.parse({ ...config2, pat: process.env.BTP_PAT_TOKEN }); | ||
default: | ||
throw new Error(`Invalid config type: ${type}`); | ||
} | ||
} | ||
async function parseConfig() { | ||
async function parseConfig(type = "optional") { | ||
const explorer = cosmiconfig("btp"); | ||
const result = await explorer.search(); | ||
if (result) { | ||
return ConfigSchema.parse(result.config); | ||
if (!result) { | ||
return void 0; | ||
} | ||
return void 0; | ||
switch (type) { | ||
case "minimal": | ||
return MinimalConfigSchema.parse(result.config); | ||
case "required": | ||
return RequiredConfigSchema.parse(result.config); | ||
case "optional": | ||
return OptionalConfigSchema.parse(result.config); | ||
default: | ||
throw new Error(`Invalid config type: ${type}`); | ||
} | ||
} | ||
@@ -147,6 +203,6 @@ async function createConfig(config2) { | ||
const preConfiguredConfig = merge(defaultConfig, config2); | ||
const validatedPreConfiguredConfig = ConfigSchema.parse(preConfiguredConfig); | ||
const validatedPreConfiguredConfig = OptionalConfigSchema.parse(preConfiguredConfig); | ||
const existingConfig = await parseConfig(); | ||
const mergedConfig = existingConfig ? merge(existingConfig, validatedPreConfiguredConfig) : preConfiguredConfig; | ||
const validatedMergedConfig = ConfigSchema.parse(mergedConfig); | ||
const validatedMergedConfig = OptionalConfigSchema.parse(mergedConfig); | ||
const projectRoot = findProjectRoot(process.cwd()); | ||
@@ -178,3 +234,3 @@ const configPath = path.join(projectRoot, ".btprc.json"); | ||
async function getServices() { | ||
const { instance, pat } = await config(); | ||
const { instance, pat } = await config("minimal"); | ||
const result = await fetch(`${instance}/cm/sdk/services`, { | ||
@@ -271,76 +327,32 @@ headers: { | ||
}); | ||
const selectWorkspaceResult = await select({ | ||
message: "Select a workspace", | ||
options: services.map((service) => ({ | ||
value: service, | ||
label: service.name | ||
})) | ||
}); | ||
if (isCancel(selectWorkspaceResult)) { | ||
printCancel("Cancelled"); | ||
const workspace = await promptSelect({ items: services, message: "Select a workspace" }); | ||
if (!workspace) { | ||
printCancel("No workspace selected"); | ||
process.exit(0); | ||
} | ||
const selectApplicationResult = await select({ | ||
message: "Select an application", | ||
options: selectWorkspaceResult.applications.map((service) => ({ | ||
value: service, | ||
label: service.name | ||
})) | ||
}); | ||
if (isCancel(selectApplicationResult)) { | ||
printCancel("Cancelled"); | ||
const application = await promptSelect({ items: workspace.applications, message: "Select an application" }); | ||
if (!application) { | ||
printCancel("No application selected"); | ||
process.exit(0); | ||
} | ||
if (selectApplicationResult.portals.length > 0) { | ||
const selectPortalResult = await select({ | ||
message: "Select a Smart Contract Set Portal instance", | ||
options: selectApplicationResult.portals.map((service) => ({ | ||
value: service, | ||
label: service.name | ||
})) | ||
}); | ||
if (isCancel(selectPortalResult)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
} | ||
if (selectApplicationResult.graphs.length > 0) { | ||
const selectGraphResult = await select({ | ||
message: "Select a The Graph instance", | ||
options: selectApplicationResult.graphs.map((service) => ({ | ||
value: service, | ||
label: service.name | ||
})) | ||
}); | ||
if (isCancel(selectGraphResult)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
} | ||
if (selectApplicationResult.hasuras.length > 0) { | ||
const selectHasuraResult = await select({ | ||
message: "Select an Hasura instance", | ||
options: selectApplicationResult.hasuras.map((service) => ({ | ||
value: service, | ||
label: service.name | ||
})) | ||
}); | ||
if (isCancel(selectHasuraResult)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
} | ||
if (selectApplicationResult.nodes.length > 0) { | ||
const selectNodeResult = await select({ | ||
message: "Select a blockchain node or loadbalancer", | ||
options: selectApplicationResult.nodes.map((service) => ({ | ||
value: service, | ||
label: service.name | ||
})) | ||
}); | ||
if (isCancel(selectNodeResult)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
} | ||
const portal = await promptSelect({ | ||
items: application.portals, | ||
message: "Select a Smart Contract Set Portal instance", | ||
allowNone: true | ||
}); | ||
const graph = await promptSelect({ | ||
items: application.graphs, | ||
message: "Select a The Graph instance", | ||
allowNone: true | ||
}); | ||
const hasura = await promptSelect({ | ||
items: application.hasuras, | ||
message: "Select an Hasura instance", | ||
allowNone: true | ||
}); | ||
const node = await promptSelect({ | ||
items: application.nodes, | ||
message: "Select a blockchain node or loadbalancer", | ||
allowNone: true | ||
}); | ||
printOutro("You're all set!"); | ||
@@ -347,0 +359,0 @@ } catch (error) { |
{ | ||
"name": "@settlemint/btp-sdk-cli", | ||
"version": "0.3.2-prfa8007a", | ||
"version": "0.3.2-prfc7de4c", | ||
"main": "./dist/index.js", | ||
@@ -5,0 +5,0 @@ "module": "./dist/index.js", |
Sorry, the diff of this file is not supported yet
45808
369
4