Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(commands): add leon info #178

Merged
merged 5 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 139 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@
"chalk": "4.1.2",
"clipanion": "3.1.0",
"conf": "10.1.1",
"date-and-time": "2.1.2",
"execa": "5.1.1",
"extract-zip": "2.0.1",
"get-stream": "6.0.1",
"ora": "5.4.1",
"read-pkg": "5.2.0",
"semver": "7.3.5",
"sudo-prompt": "9.2.1",
"table": "6.8.0",
"typanion": "3.7.1",
"update-notifier": "5.1.0"
},
Expand All @@ -77,6 +79,7 @@
"@swc/core": "1.2.143",
"@swc/jest": "0.2.17",
"@types/adm-zip": "0.4.34",
"@types/date-and-time": "0.13.0",
"@types/jest": "27.4.0",
"@types/mock-fs": "4.13.1",
"@types/node": "17.0.18",
Expand Down
6 changes: 4 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Builtins, Cli } from 'clipanion'

import { Leon } from './services/Leon.js'
import { packageJSON } from './packageJSON.js'
import { CreateBirthCommand } from './commands/create/birth.js'
import { CheckCommand } from './commands/check.js'
import { HelpCommand } from './commands/help.js'
import { StartCommand } from './commands/start.js'
import { Leon } from './services/Leon.js'
import { packageJSON } from './packageJSON.js'
import { InfoCommand } from './commands/info.js'

export const cli = new Cli({
binaryLabel: Leon.NAME,
Expand All @@ -17,4 +18,5 @@ cli.register(Builtins.VersionCommand)
cli.register(CreateBirthCommand)
cli.register(CheckCommand)
cli.register(HelpCommand)
cli.register(InfoCommand)
cli.register(StartCommand)
9 changes: 9 additions & 0 deletions src/commands/__test__/info.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { cli } from '../../cli.js'
import { InfoCommand } from '../info.js'

describe('leon info', () => {
it('should be instance of the command', () => {
const command = cli.process(['info'])
expect(command).toBeInstanceOf(InfoCommand)
})
})
49 changes: 49 additions & 0 deletions src/commands/info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Command, Option } from 'clipanion'
import chalk from 'chalk'

import { config } from '../services/Config.js'
import { LeonInstance } from '../services/LeonInstance.js'
import { log } from '../services/Log.js'

export class InfoCommand extends Command {
static paths = [['info']]

static usage = {
description: 'Get basic information about installed Leon instances.'
}

public name = Option.String('--name', {
description: 'Name of the Leon instance.'
})

async execute(): Promise<number> {
try {
if (this.name != null) {
console.log()
const leonInstance = await LeonInstance.get(this.name)
leonInstance.logInfo()
} else {
const instances = config.get('instances', [])
if (instances.length === 0) {
console.log(chalk.bold('No Leon instances found.'))
console.log('You can give birth to a Leon instance using:')
console.log(chalk.cyan('leon create birth'))
} else {
console.log(chalk.cyan('\nLeon instances:\n'))
for (const instance of instances) {
const leonInstance = new LeonInstance(instance)
leonInstance.logInfo()
console.log('------------------------------\n')
}
}
}
return 0
} catch (error) {
await log.error({
error,
commandPath: 'info'
})
return 1
}
}
}
19 changes: 19 additions & 0 deletions src/services/LeonInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import path from 'node:path'
import execa from 'execa'
import getStream from 'get-stream'
import ora from 'ora'
import date from 'date-and-time'
import readPackage from 'read-pkg'
import { table } from 'table'
import chalk from 'chalk'

import { config } from './Config.js'
import { LogError } from '../utils/LogError.js'
Expand Down Expand Up @@ -231,4 +235,19 @@ export class LeonInstance implements LeonInstanceOptions {
await leonInstance.build()
}
}

public logInfo(): void {
const birthDay = new Date(this.birthDate)
const birthDayString = date.format(birthDay, 'DD/MM/YYYY - HH:mm:ss')
const packageJSON = readPackage.sync({ cwd: this.path })
console.log(
table([
[chalk.bold('Name'), this.name],
[chalk.bold('Path'), this.path],
[chalk.bold('Mode'), this.mode],
[chalk.bold('Birthday'), birthDayString],
[chalk.bold('Version'), packageJSON.version]
])
)
}
}