Skip to content

Commit

Permalink
fix: leon info should not crash with package.json not found
Browse files Browse the repository at this point in the history
  • Loading branch information
theoludwig committed Sep 10, 2022
1 parent af71292 commit e1220f0
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 43 deletions.
64 changes: 47 additions & 17 deletions src/commands/__test__/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,24 @@ import type { LeonInstanceOptions } from '../../services/LeonInstance.js'
import { LeonInstance } from '../../services/LeonInstance.js'
import { Log } from '../../services/Log.js'

const leonInstanceOptions: LeonInstanceOptions = {
name: 'random-name',
birthDate: '2022-02-20T10:11:33.315Z',
mode: 'docker',
path: '/path',
startCount: 0
}

const leonInstance = new LeonInstance(leonInstanceOptions)
const configData: ConfigData = {
instances: [leonInstance]
}
const version = '1.0.0'
const birthDayString = date.format(
new Date(leonInstance.birthDate),
'DD/MM/YYYY - HH:mm:ss'
)

await tap.test('leon info', async (t) => {
t.afterEach(() => {
fsMock.restore()
Expand All @@ -29,22 +47,6 @@ await tap.test('leon info', async (t) => {
async (t) => {
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
const leonInstanceOptions: LeonInstanceOptions = {
name: 'random-name',
birthDate: '2022-02-20T10:11:33.315Z',
mode: 'docker',
path: '/path',
startCount: 0
}
const leonInstance = new LeonInstance(leonInstanceOptions)
const configData: ConfigData = {
instances: [leonInstance]
}
const version = '1.0.0'
const birthDayString = date.format(
new Date(leonInstance.birthDate),
'DD/MM/YYYY - HH:mm:ss'
)
fsMock({
[config.path]: JSON.stringify(configData),
[leonInstance.path]: {
Expand All @@ -71,7 +73,7 @@ await tap.test('leon info', async (t) => {
)

await t.test(
'should succeeds and advise the user to create a instance',
'should succeeds and advise the user to create an instance',
async (t) => {
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
Expand All @@ -95,6 +97,34 @@ await tap.test('leon info', async (t) => {
}
)

await t.test(
'should succeeds even with `package.json` of the instance not found',
async (t) => {
sinon.stub(console, 'log').value(() => {})
const consoleLogSpy = sinon.spy(console, 'log')
fsMock({
[config.path]: JSON.stringify(configData),
[leonInstance.path]: {}
})
const command = cli.process(['info'])
const exitCode = await command.execute()
t.equal(exitCode, 0)
t.equal(consoleLogSpy.calledWith(chalk.cyan('\nLeon instances:\n')), true)
t.equal(
consoleLogSpy.calledWith(
table([
[chalk.bold('Name'), leonInstance.name],
[chalk.bold('Path'), `${leonInstance.path}`],
[chalk.bold('Mode'), leonInstance.mode],
[chalk.bold('Birth date'), birthDayString],
[chalk.bold('Version'), '0.0.0']
])
),
true
)
}
)

await t.test('should fails and show a error message', async (t) => {
sinon.stub(console, 'error').value(() => {})
const consoleErrorSpy = sinon.spy(console, 'error')
Expand Down
2 changes: 1 addition & 1 deletion src/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class CheckCommand extends Command {

async execute(): Promise<number> {
try {
const leonInstance = LeonInstance.get(this.name)
const leonInstance = await LeonInstance.get(this.name)
await leonInstance.check()
return 0
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class DeleteCommand extends Command {
async execute(): Promise<number> {
try {
const { yes = false } = this
const leonInstance = LeonInstance.get(this.name)
const leonInstance = await LeonInstance.get(this.name)
console.log(
`You are about to delete Leon instance "${leonInstance.name}".`
)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class InfoCommand extends Command {
try {
if (this.name != null) {
console.log()
const leonInstance = LeonInstance.get(this.name)
const leonInstance = await LeonInstance.get(this.name)
await leonInstance.logInfo()
} else {
const instances = config.get('instances', [])
Expand Down
2 changes: 1 addition & 1 deletion src/commands/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class RunCommand extends Command {
try {
const script = this.script.join(' ')
const command = 'npm run ' + script
const leonInstance = LeonInstance.get(this.name)
const leonInstance = await LeonInstance.get(this.name)
const isBuiltin = BUILTIN_COMMANDS_KEYS.includes(script)
if (isBuiltin) {
const runCommand = BUILTIN_COMMANDS[script]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class StartCommand extends Command {

async execute(): Promise<number> {
try {
const leonInstance = LeonInstance.get(this.name)
const leonInstance = await LeonInstance.get(this.name)
await leonInstance.start(this.port)
return 0
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class UpdateCommand extends Command {

async execute(): Promise<number> {
try {
const leonInstance = LeonInstance.get(this.name)
const leonInstance = await LeonInstance.get(this.name)
const leon = new Leon({
useDevelopGitBranch: this.useDevelopGitBranch,
birthPath: leonInstance.path,
Expand Down
2 changes: 1 addition & 1 deletion src/e2e/tests/2-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LeonInstance } from '../../services/LeonInstance.js'

export const test2Update = async (): Promise<void> => {
await tap.test('leon update', async (t) => {
const leonInstance = LeonInstance.get()
const leonInstance = await LeonInstance.get()
let oldVersion = await leonInstance.getVersion()
const leonUpdateWithSameVersion = await execa('leon', ['update'])
let newVersion = await leonInstance.getVersion()
Expand Down
2 changes: 1 addition & 1 deletion src/services/Leon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class Leon implements LeonOptions {
const sourceCodePath = await this.getSourceCode()
await this.transferSourceCodeFromTemporaryToBirthPath(sourceCodePath)
}
const leonInstance = LeonInstance.create({
const leonInstance = await LeonInstance.create({
name: this.name,
path: this.birthPath,
mode
Expand Down
27 changes: 17 additions & 10 deletions src/services/LeonInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface LeonInstanceOptions extends CreateOptions {
}

export class LeonInstance implements LeonInstanceOptions {
static readonly INVALID_VERSION = '0.0.0'

public name: string
public path: string
public mode: InstanceType
Expand Down Expand Up @@ -152,7 +154,7 @@ export class LeonInstance implements LeonInstanceOptions {
})
}

static find(name: string): LeonInstance | null {
static async find(name: string): Promise<LeonInstance | null> {
const instances = config.get('instances', [])
const instance = instances.find((instance) => {
return instance.name === name
Expand All @@ -163,7 +165,7 @@ export class LeonInstance implements LeonInstanceOptions {
return null
}

static get(name?: string): LeonInstance {
static async get(name?: string): Promise<LeonInstance> {
if (name == null) {
const instances = config.get('instances', [])
const isEmptyInstances = instances.length === 0
Expand All @@ -174,7 +176,7 @@ export class LeonInstance implements LeonInstanceOptions {
}
return new LeonInstance(instances[0])
}
const leonInstance = LeonInstance.find(name)
const leonInstance = await LeonInstance.find(name)
if (leonInstance == null) {
throw new LogError({
message: "This instance doesn't exists, please provider another name."
Expand Down Expand Up @@ -210,8 +212,8 @@ export class LeonInstance implements LeonInstanceOptions {
}
}

static create(options: CreateOptions): LeonInstance {
let leonInstance = LeonInstance.find(options.name)
static async create(options: CreateOptions): Promise<LeonInstance> {
let leonInstance = await LeonInstance.find(options.name)
if (leonInstance != null) {
throw new LogError({
message: 'This instance name already exists, please choose another name'
Expand Down Expand Up @@ -253,11 +255,16 @@ export class LeonInstance implements LeonInstanceOptions {
}

public static async getVersion(sourceCodePath: string): Promise<string> {
const packageJSON = await readPackage({
cwd: sourceCodePath,
normalize: false
})
return packageJSON.version ?? '0.0.0'
const packageJsonPath = path.join(sourceCodePath, 'package.json')
let version = LeonInstance.INVALID_VERSION
if (await isExistingPath(packageJsonPath)) {
const packageJSON = await readPackage({
cwd: sourceCodePath,
normalize: false
})
version = packageJSON.version ?? LeonInstance.INVALID_VERSION
}
return version
}

public async getVersion(): Promise<string> {
Expand Down
16 changes: 8 additions & 8 deletions src/services/__test__/LeonInstance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ await tap.test('services/LeonInstance', async (t) => {
})

await t.test('should find the instance with its name', async (t) => {
const instance = LeonInstance.find(leonInstance.name)
const instance = await LeonInstance.find(leonInstance.name)
t.not(instance, undefined)
t.equal(instance?.name, leonInstance.name)
})

await t.test('should not find the instance with wrong name', async (t) => {
const instance = LeonInstance.find('wrong name')
const instance = await LeonInstance.find('wrong name')
t.equal(instance, null)
})
})
Expand All @@ -52,8 +52,8 @@ await tap.test('services/LeonInstance', async (t) => {
fsMock({
[config.path]: ''
})
t.throws(() => {
return LeonInstance.get()
await t.rejects(async () => {
return await LeonInstance.get()
})
})

Expand All @@ -63,7 +63,7 @@ await tap.test('services/LeonInstance', async (t) => {
fsMock({
[config.path]: JSON.stringify(configData)
})
const instance = LeonInstance.get()
const instance = await LeonInstance.get()
t.equal(instance.name, leonInstance.name)
}
)
Expand All @@ -74,8 +74,8 @@ await tap.test('services/LeonInstance', async (t) => {
fsMock({
[config.path]: JSON.stringify(configData)
})
t.throws(() => {
return LeonInstance.get('wrong name')
await t.rejects(async () => {
return await LeonInstance.get('wrong name')
})
}
)
Expand All @@ -86,7 +86,7 @@ await tap.test('services/LeonInstance', async (t) => {
fsMock({
[config.path]: JSON.stringify(configData)
})
const instance = LeonInstance.get(leonInstance.name)
const instance = await LeonInstance.get(leonInstance.name)
t.equal(instance.name, leonInstance.name)
}
)
Expand Down

0 comments on commit e1220f0

Please sign in to comment.