Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
feat(jitp): improve console output
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jul 18, 2019
1 parent 623b78b commit ea3126a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
23 changes: 15 additions & 8 deletions scripts/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@ 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,
region: endpointAddress.split('.')[2],
})

connection.on('connect', async () => {
console.log(chalk.green(chalk.inverse(' connected ')))
console.timeEnd(chalk.green(chalk.inverse(' connected ')))
clearInterval(connectingNote)
})

connection.on('close', () => {
Expand All @@ -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)
})
15 changes: 12 additions & 3 deletions scripts/generate-device-certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
4 changes: 2 additions & 2 deletions scripts/jitp/generateDeviceCertificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -78,5 +78,5 @@ export const generateDeviceCertificate = async (args: {
'utf-8',
)

log(`Done: ${deviceId}`)
return { deviceId }
}
12 changes: 10 additions & 2 deletions scripts/jitp/registerCA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down Expand Up @@ -148,7 +148,7 @@ export const registerCA = async (args: {
})
.promise()

await iot
const res = await iot
.registerCACertificate({
caCertificate,
verificationCertificate,
Expand Down Expand Up @@ -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,
}
}
5 changes: 3 additions & 2 deletions scripts/print-react-app-configuration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { stackOutputToCRAEnvironment } from './cloudformation/stackOutputToCRAEnvironment'
import chalk from 'chalk'

/**
* Prints the stack outputs as create-react-app environment variables
Expand All @@ -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)
})
13 changes: 10 additions & 3 deletions scripts/register-ca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})

0 comments on commit ea3126a

Please sign in to comment.