@settlemint/btp-sdk-cli
Advanced tools
Comparing version 0.3.2-pr5004d47 to 0.3.2-pr907a874
#!/usr/bin/env node | ||
import { cancel, intro, outro } from '@clack/prompts'; | ||
import { cancel, intro, outro, password, isCancel, text } from '@clack/prompts'; | ||
import { Command } from '@commander-js/extra-typings'; | ||
import { inverse, magentaBright, greenBright } from 'yoctocolors'; | ||
import { magentaBright, inverse, greenBright, redBright } from 'yoctocolors'; | ||
import { writeFileSync, existsSync } from 'node:fs'; | ||
import path from 'node:path'; | ||
import { cosmiconfig } from 'cosmiconfig'; | ||
import { merge } from 'ts-deepmerge'; | ||
import { z } from 'zod'; | ||
@@ -55,7 +60,4 @@ // package.json | ||
"@commander-js/extra-typings": "12.1.0", | ||
"@inquirer/prompts": "5.3.2", | ||
boxen: "8.0.0", | ||
commander: "^12.1.0", | ||
cosmiconfig: "9.0.0", | ||
"reflect-metadata": "0.2.2", | ||
"ts-deepmerge": "7.0.1", | ||
@@ -67,2 +69,116 @@ yoctocolors: "2.1.1", | ||
}; | ||
var printAsciiArt = () => console.log( | ||
magentaBright(` | ||
_________ __ __ .__ _____ .__ __ | ||
/ _____/ _____/ |__/ |_| | ____ / \\ |__| _____/ |_ | ||
\\_____ \\_/ __ \\ __\\ __\\ | _/ __ \\ / \\ / \\| |/ \\ __\\ | ||
/ \\ ___/| | | | | |_\\ ___// Y \\ | | \\ | | ||
/_______ /\\___ >__| |__| |____/\\___ >____|__ /__|___| /__| | ||
\\/ \\/ \\/ \\/ \\/ | ||
`) | ||
); | ||
var printIntro = (msg) => intro(inverse(magentaBright(msg))); | ||
var printOutro = (msg) => outro(inverse(greenBright(msg))); | ||
var printCancel = (msg) => cancel(inverse(redBright(msg))); | ||
var promptPassword = async (options) => { | ||
const passwordResult = await password(options); | ||
if (isCancel(passwordResult)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
return passwordResult; | ||
}; | ||
var promptText = async (options) => { | ||
const textResult = await text(options); | ||
if (isCancel(textResult)) { | ||
printCancel("Cancelled"); | ||
process.exit(0); | ||
} | ||
return textResult; | ||
}; | ||
var ConfigSchema = z.object({ | ||
pat: z.string(), | ||
instance: z.string() | ||
}); | ||
async function parseConfig() { | ||
const explorer = cosmiconfig("btp"); | ||
const result = await explorer.search(); | ||
if (result) { | ||
return ConfigSchema.parse(result.config); | ||
} | ||
return void 0; | ||
} | ||
function findProjectRoot(startDir) { | ||
let currentDir = startDir; | ||
while (currentDir !== path.parse(currentDir).root) { | ||
if (existsSync(path.join(currentDir, "package.json"))) { | ||
return currentDir; | ||
} | ||
currentDir = path.dirname(currentDir); | ||
} | ||
throw new Error("Unable to find project root"); | ||
} | ||
async function createConfig(config) { | ||
const defaultConfig = { | ||
pat: "sm_pat_xxxxxxxxxxxxxxxx", | ||
instance: "https://console.settlemint.com" | ||
}; | ||
const preConfiguredConfig = merge(defaultConfig, config); | ||
const validatedPreConfiguredConfig = ConfigSchema.parse(preConfiguredConfig); | ||
const existingConfig = await parseConfig(); | ||
const mergedConfig = existingConfig ? merge(validatedPreConfiguredConfig, existingConfig) : preConfiguredConfig; | ||
const validatedMergedConfig = ConfigSchema.parse(mergedConfig); | ||
const projectRoot = findProjectRoot(process.cwd()); | ||
const configPath = path.join(projectRoot, ".btprc.json"); | ||
writeFileSync(configPath, JSON.stringify(validatedMergedConfig, null, 2)); | ||
return validatedMergedConfig; | ||
} | ||
// src/lib/instance.ts | ||
async function coerceInstanceUrl(url) { | ||
let envUrl = process.env.BTP_INSTANCE_URL || url; | ||
if (!validateInstanceUrl(envUrl)) { | ||
envUrl = await promptText({ | ||
message: "Enter the URL of your BTP instance", | ||
defaultValue: "https://console.settlemint.com", | ||
initialValue: "https://console.settlemint.com", | ||
placeholder: "https://console.settlemint.com", | ||
validate(value) { | ||
if (!validateInstanceUrl(value)) { | ||
return "Invalid BTP instance URL. Please enter a valid HTTPS URL."; | ||
} | ||
} | ||
}); | ||
} | ||
return envUrl; | ||
} | ||
function validateInstanceUrl(url) { | ||
try { | ||
const parsedUrl = new URL(url ?? ""); | ||
return parsedUrl.protocol === "https:"; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
// src/lib/pat-token.ts | ||
async function coercePatToken(pat) { | ||
let envPat = process.env.BTP_PAT_TOKEN || pat; | ||
if (!validatePatToken(envPat)) { | ||
envPat = await promptPassword({ | ||
message: "Enter a Personal Access Token for authentication", | ||
validate(value) { | ||
if (!validatePatToken(value)) { | ||
return "Invalid Personal Access Token"; | ||
} | ||
} | ||
}); | ||
} | ||
return envPat; | ||
} | ||
function validatePatToken(pat) { | ||
return /^sm_pat_[a-f0-9]{16}$/.test(pat?.trim() ?? ""); | ||
} | ||
// src/commands/init.ts | ||
function initCommand() { | ||
@@ -73,4 +189,14 @@ return new Command("init").option("-p, --pat <key>", "Personal Access Token for authentication (BTP_PAT_TOKEN environment variable)").option( | ||
).description("Initializes the setup of the BTP SDK").action(async ({ pat, instance }) => { | ||
intro(inverse(magentaBright("Setting up the BTP SDK in your project"))); | ||
outro(inverse(greenBright("You're all set!"))); | ||
printAsciiArt(); | ||
printIntro("Setting up the BTP SDK in your project"); | ||
try { | ||
const personalAccessToken = await coercePatToken(pat); | ||
const instanceUrl = await coerceInstanceUrl(instance); | ||
await createConfig({ pat: personalAccessToken, instance: instanceUrl }); | ||
printOutro("You're all set!"); | ||
} catch (error) { | ||
printCancel(`Error: ${error.message}`); | ||
console.error(error.stack); | ||
process.exit(1); | ||
} | ||
}); | ||
@@ -77,0 +203,0 @@ } |
{ | ||
"name": "@settlemint/btp-sdk-cli", | ||
"version": "0.3.2-pr5004d47", | ||
"version": "0.3.2-pr907a874", | ||
"main": "./dist/index.js", | ||
@@ -49,7 +49,4 @@ "module": "./dist/index.js", | ||
"@commander-js/extra-typings": "12.1.0", | ||
"@inquirer/prompts": "5.3.2", | ||
"boxen": "8.0.0", | ||
"commander": "^12.1.0", | ||
"cosmiconfig": "9.0.0", | ||
"reflect-metadata": "0.2.2", | ||
"ts-deepmerge": "7.0.1", | ||
@@ -56,0 +53,0 @@ "yoctocolors": "2.1.1", |
Sorry, the diff of this file is not supported yet
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
27674
7
208
2
- Removed@inquirer/prompts@5.3.2
- Removedboxen@8.0.0
- Removedreflect-metadata@0.2.2
- Removed@inquirer/checkbox@2.5.0(transitive)
- Removed@inquirer/confirm@3.2.0(transitive)
- Removed@inquirer/core@9.2.1(transitive)
- Removed@inquirer/editor@2.2.0(transitive)
- Removed@inquirer/expand@2.3.0(transitive)
- Removed@inquirer/figures@1.0.10(transitive)
- Removed@inquirer/input@2.3.0(transitive)
- Removed@inquirer/number@1.1.0(transitive)
- Removed@inquirer/password@2.2.0(transitive)
- Removed@inquirer/prompts@5.3.2(transitive)
- Removed@inquirer/rawlist@2.3.0(transitive)
- Removed@inquirer/search@1.1.0(transitive)
- Removed@inquirer/select@2.5.0(transitive)
- Removed@inquirer/type@1.5.52.0.0(transitive)
- Removed@types/mute-stream@0.0.4(transitive)
- Removed@types/node@22.13.8(transitive)
- Removed@types/wrap-ansi@3.0.0(transitive)
- Removedansi-align@3.0.1(transitive)
- Removedansi-escapes@4.3.2(transitive)
- Removedansi-regex@5.0.16.1.0(transitive)
- Removedansi-styles@4.3.06.2.1(transitive)
- Removedboxen@8.0.0(transitive)
- Removedcamelcase@8.0.0(transitive)
- Removedchalk@5.4.1(transitive)
- Removedchardet@0.7.0(transitive)
- Removedcli-boxes@4.0.1(transitive)
- Removedcli-width@4.1.0(transitive)
- Removedcolor-convert@2.0.1(transitive)
- Removedcolor-name@1.1.4(transitive)
- Removedemoji-regex@10.4.08.0.0(transitive)
- Removedexternal-editor@3.1.0(transitive)
- Removedget-east-asian-width@1.3.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedis-fullwidth-code-point@3.0.0(transitive)
- Removedmute-stream@1.0.0(transitive)
- Removedos-tmpdir@1.0.2(transitive)
- Removedreflect-metadata@0.2.2(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsignal-exit@4.1.0(transitive)
- Removedstring-width@4.2.37.2.0(transitive)
- Removedstrip-ansi@6.0.17.1.0(transitive)
- Removedtmp@0.0.33(transitive)
- Removedtype-fest@0.21.34.36.0(transitive)
- Removedundici-types@6.20.0(transitive)
- Removedwidest-line@5.0.0(transitive)
- Removedwrap-ansi@6.2.09.0.0(transitive)
- Removedyoctocolors-cjs@2.1.2(transitive)