From 115e3e0393c909bb4f2a064ef9b5b846bfa8612b Mon Sep 17 00:00:00 2001 From: v1rtl Date: Mon, 27 Nov 2023 18:58:41 +0200 Subject: [PATCH 1/2] fix: TTY output --- src/actions/deploy.ts | 21 +++++++++++++-------- src/actions/ens.ts | 10 +++++++--- src/constants.ts | 2 ++ src/utils/logger.ts | 6 ++++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/actions/deploy.ts b/src/actions/deploy.ts index f1f3852..edd28ea 100644 --- a/src/actions/deploy.ts +++ b/src/actions/deploy.ts @@ -1,5 +1,5 @@ import path from 'node:path' -import { PROVIDERS } from '../constants.js' +import { PROVIDERS, isTTY } from '../constants.js' import { MissingDirectoryError, NoProvidersError } from '../errors.js' import { walk, fileSize, packCAR, parseTokensFromEnv, tokensToProviderNames, findEnvVarProviderName } from '../index.js' import { exists } from '../utils/fs.js' @@ -37,12 +37,14 @@ export const deployAction = async ( if (size === 0) throw new MissingDirectoryError(dir) - logger.start(`Packing ${colors.cyan(dir === '.' ? name : dir)} (${fileSize(size, 2)})`) + const distName = dir === '.' ? name : dir + + logger.start(`Packing ${isTTY ? colors.cyan(distName) : distName} (${fileSize(size, 2)})`) const { rootCID, blob } = await packCAR(files, name, dist) const cid = rootCID.toString() - logger.info(`Root CID: ${colors.white(cid)}`) + logger.info(`Root CID: ${isTTY ? colors.white(cid) : cid}`) const apiTokens = parseTokensFromEnv() @@ -54,7 +56,7 @@ export const deployAction = async ( let total = 0 - const bar = process.stdout.isTTY + const bar = isTTY ? new AsciiBar({ total: providers.length, formatString: '#spinner #bar #message', @@ -102,11 +104,14 @@ export const deployAction = async ( } else logger.success('Deployed across all providers') + const dwebLink = `https://${cid}.ipfs.dweb.link` + const ipfsScanLink = `https://ipfs-scan.io/?cid=${cid}` + console.log( - `\nOpen in a browser:\n${colors.bold('IPFS')}: ${colors.underline( - `https://${cid}.ipfs.dweb.link`, - )}\n${colors.bold('IPFS Scan')}: ${colors.underline( - `https://ipfs-scan.io/?cid=${cid}`, + `\nOpen in a browser:\n${isTTY ? colors.bold('IPFS') : 'IPFS'}: ${colors.underline( + isTTY ? colors.underline(dwebLink) : dwebLink, + )}\n${isTTY ? colors.bold('IPFS Scan') : 'IPFS Scan'}: ${colors.underline( + isTTY ? colors.underline(ipfsScanLink) : ipfsScanLink, )}`, ) diff --git a/src/actions/ens.ts b/src/actions/ens.ts index d3ea0ff..7556684 100644 --- a/src/actions/ens.ts +++ b/src/actions/ens.ts @@ -20,6 +20,7 @@ import { ApiClient } from '@stauro/piggybank/api' import { chainIdToSafeApiUrl } from '../utils/safe.js' import * as colors from 'colorette' import { logger } from '../utils/logger.js' +import { isTTY } from '../constants.js' export const ensAction = async ( cid: string, @@ -129,8 +130,10 @@ export const ensAction = async ( chainId: chain.id, origin: 'Piggybank', }) - // eslint-disable-next-line @stylistic/max-len - logger.success(`Transaction proposed to a Safe wallet.\nOpen in a browser: ${colors.underline(`https://app.safe.global/transactions/queue?safe=${safeAddress}`)}`) + const safeLink = `https://app.safe.global/transactions/queue?safe=${safeAddress}` + logger.success(`Transaction proposed to a Safe wallet.\nOpen in a browser: ${ + isTTY ? colors.underline(safeLink) : safeLink + }`) } catch (e) { logger.error('Failed to propose a transaction', e) @@ -168,7 +171,8 @@ export const ensAction = async ( if (receipt.status === 'reverted') return logger.error('Transaction reverted') logger.success('Transaction submitted') - logger.info(`Open in a browser: ${colors.underline(`https://${domain}.limo`)}`) + const browserLink = `https://${domain}.limo` + logger.info(`Open in a browser: ${isTTY ? colors.underline(browserLink) : browserLink}`) } return process.exit() } diff --git a/src/constants.ts b/src/constants.ts index 0e006f5..56878f9 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -33,3 +33,5 @@ string, supported: 'both', }, } + +export const isTTY = process.stdout.isTTY diff --git a/src/utils/logger.ts b/src/utils/logger.ts index c583669..88ae947 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -1,5 +1,6 @@ import { bgGreen, bgRed, bgYellow, cyan, green } from 'colorette' import { SupportedMethods } from '../types.js' +import { isTTY } from '../constants.js' const responseStatus = (status: number) => { if (status < 300) return bgGreen(status) @@ -12,7 +13,7 @@ export const logger = { console.log('📦', ...args) }, info(...args: unknown[]) { - console.info('🛈 ', ...args) + console.info('🟢 ', ...args) }, error(...args: unknown[]) { console.error('🚨', ...args) @@ -24,7 +25,8 @@ export const logger = { console.log('✔', ...args) }, request(method: 'GET' | 'POST' | 'PUT', url: string, status: number) { - console.log('\n', method === 'GET' ? cyan(method) : green(method), url, responseStatus(status)) + if (isTTY) console.log('\n', method === 'GET' ? cyan(method) : green(method), url, responseStatus(status)) + else console.log('\n', method, url, status) }, } From 8f2a34c260357daf2cab16735c3d9e4a03badd37 Mon Sep 17 00:00:00 2001 From: v1rtl Date: Mon, 27 Nov 2023 19:07:44 +0200 Subject: [PATCH 2/2] pass name to gw3 --- src/actions/deploy.ts | 3 +-- src/providers/gw3.ts | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/actions/deploy.ts b/src/actions/deploy.ts index edd28ea..1e89259 100644 --- a/src/actions/deploy.ts +++ b/src/actions/deploy.ts @@ -36,8 +36,7 @@ export const deployAction = async ( const [size, files] = await walk(normalizedPath) if (size === 0) throw new MissingDirectoryError(dir) - - const distName = dir === '.' ? name : dir + const distName = ['.', 'dist'].includes(dir) ? name : dir logger.start(`Packing ${isTTY ? colors.cyan(distName) : distName} (${fileSize(size, 2)})`) diff --git a/src/providers/gw3.ts b/src/providers/gw3.ts index 759470d..beea0f8 100644 --- a/src/providers/gw3.ts +++ b/src/providers/gw3.ts @@ -23,7 +23,7 @@ const mapGw3StatusToGenericStatus = (status: GW3PinStatus): PinStatus => { const baseURL = 'https://gw3.io' const providerName = 'Gateway3' -export const uploadOnGW3: UploadFunction = async ({ token, car, cid, accessKey, first, verbose }) => { +export const uploadOnGW3: UploadFunction = async ({ token, car, cid, accessKey, first, name, verbose }) => { if (!accessKey) throw new MissingKeyError('GW3_ACCESS_KEY') if (first) { const res1 = await fetch( @@ -63,7 +63,7 @@ export const uploadOnGW3: UploadFunction = async ({ token, car, cid, accessKey, } const res = await fetch( - new URL(`/api/v0/pin/add?arg=${cid}&ts=${getTs()}`, baseURL), + new URL(`/api/v0/pin/add?arg=${cid}&ts=${getTs()}&name=${name}`, baseURL), { method: 'POST', headers: {