From ea3126add4c4a4d0fd841d1a87a516a21cb0c3ea Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Thu, 18 Jul 2019 11:13:29 +0200 Subject: [PATCH] feat(jitp): improve console output --- scripts/connect.ts | 23 +++++++++++++++-------- scripts/generate-device-certificate.ts | 15 ++++++++++++--- scripts/jitp/generateDeviceCertificate.ts | 4 ++-- scripts/jitp/registerCA.ts | 12 ++++++++++-- scripts/print-react-app-configuration.ts | 5 +++-- scripts/register-ca.ts | 13 ++++++++++--- 6 files changed, 52 insertions(+), 20 deletions(-) diff --git a/scripts/connect.ts b/scripts/connect.ts index 548a5624..95107b1b 100644 --- a/scripts/connect.ts +++ b/scripts/connect.ts @@ -26,13 +26,19 @@ const main = async (args: { deviceId: string }) => { const certsDir = path.resolve(process.cwd(), 'certificates') const deviceFiles = deviceFileLocations(certsDir, args.deviceId) + console.time(chalk.green(chalk.inverse(' connected '))) + + const note = chalk.magenta( + `Still connecting ... First connect takes around 30 seconds`, + ) + console.time(note) + const connectingNote = setInterval(() => { + console.timeLog(note) + }, 5000) + const connection = new device({ - privateKey: path.resolve(process.cwd(), 'certificates', deviceFiles.key), - clientCert: path.resolve( - process.cwd(), - 'certificates', - deviceFiles.certWithCA, - ), + privateKey: deviceFiles.key, + clientCert: deviceFiles.certWithCA, caCert: path.resolve(process.cwd(), 'data', 'AmazonRootCA1.pem'), clientId: args.deviceId.trim(), host: endpointAddress, @@ -40,7 +46,8 @@ const main = async (args: { deviceId: string }) => { }) connection.on('connect', async () => { - console.log(chalk.green(chalk.inverse(' connected '))) + console.timeEnd(chalk.green(chalk.inverse(' connected '))) + clearInterval(connectingNote) }) connection.on('close', () => { @@ -53,6 +60,6 @@ const main = async (args: { deviceId: string }) => { } main({ deviceId: process.argv[process.argv.length - 1] }).catch(error => { - console.error(error) + console.error(chalk.red(error)) process.exit(1) }) diff --git a/scripts/generate-device-certificate.ts b/scripts/generate-device-certificate.ts index 6e719949..f298100b 100644 --- a/scripts/generate-device-certificate.ts +++ b/scripts/generate-device-certificate.ts @@ -13,7 +13,16 @@ generateDeviceCertificate({ debug: (...message: any[]) => { console.log(...message.map(m => chalk.cyan(m))) }, -}).catch(error => { - console.error(error) - process.exit(1) }) + .then(({ deviceId }) => { + console.log( + chalk.green( + `Certificate for device ${chalk.yellow(deviceId)} generated.`, + ), + ) + console.log(chalk.green('You can now connect to the broker.')) + }) + .catch(error => { + console.error(chalk.red(error)) + process.exit(1) + }) diff --git a/scripts/jitp/generateDeviceCertificate.ts b/scripts/jitp/generateDeviceCertificate.ts index cb280d41..b6fa2ad1 100644 --- a/scripts/jitp/generateDeviceCertificate.ts +++ b/scripts/jitp/generateDeviceCertificate.ts @@ -13,7 +13,7 @@ export const generateDeviceCertificate = async (args: { certsDir: string log: (...message: any[]) => void debug: (...message: any[]) => void -}) => { +}): Promise<{ deviceId: string }> => { const { certsDir, log, debug } = args try { @@ -78,5 +78,5 @@ export const generateDeviceCertificate = async (args: { 'utf-8', ) - log(`Done: ${deviceId}`) + return { deviceId } } diff --git a/scripts/jitp/registerCA.ts b/scripts/jitp/registerCA.ts index 64df7a42..c8315ec2 100644 --- a/scripts/jitp/registerCA.ts +++ b/scripts/jitp/registerCA.ts @@ -14,7 +14,7 @@ export const registerCA = async (args: { region?: string log: (...message: any[]) => void debug: (...message: any[]) => void -}) => { +}): Promise<{ certificateId: string }> => { const { certsDir, stackId, region, log, debug } = args const iot = new Iot({ region }) const cf = new CloudFormation({ region }) @@ -148,7 +148,7 @@ export const registerCA = async (args: { }) .promise() - await iot + const res = await iot .registerCACertificate({ caCertificate, verificationCertificate, @@ -190,7 +190,15 @@ export const registerCA = async (args: { }) .promise() + if (!res || !res.certificateId) { + throw new Error('Failed to register CA!') + } + log( `Registered CA and enabled auto-registration to group ${stackOutput.thingGroupName}`, ) + + return { + certificateId: res.certificateId, + } } diff --git a/scripts/print-react-app-configuration.ts b/scripts/print-react-app-configuration.ts index 2802e76b..bb2ade8f 100644 --- a/scripts/print-react-app-configuration.ts +++ b/scripts/print-react-app-configuration.ts @@ -1,4 +1,5 @@ import { stackOutputToCRAEnvironment } from './cloudformation/stackOutputToCRAEnvironment' +import chalk from 'chalk' /** * Prints the stack outputs as create-react-app environment variables @@ -10,7 +11,7 @@ stackOutputToCRAEnvironment({ .then(env => { process.stdout.write(env) }) - .catch(err => { - console.error(err) + .catch(error => { + console.error(chalk.red(error)) process.exit(1) }) diff --git a/scripts/register-ca.ts b/scripts/register-ca.ts index 070ecc4b..9520373d 100644 --- a/scripts/register-ca.ts +++ b/scripts/register-ca.ts @@ -15,7 +15,14 @@ registerCA({ debug: (...message: any[]) => { console.log(...message.map(m => chalk.cyan(m))) }, -}).catch(error => { - console.error(error) - process.exit(1) }) + .then(({ certificateId }) => { + console.log( + chalk.green(`CA certificate ${chalk.yellow(certificateId)} registered.`), + ) + console.log(chalk.green('You can now generate device certificates.')) + }) + .catch(error => { + console.error(chalk.red(error)) + process.exit(1) + })