Skip to content

Commit

Permalink
fix: command hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Dec 15, 2023
1 parent 9418d80 commit 398a07b
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 16 deletions.
10 changes: 9 additions & 1 deletion src/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Command, Flags, Interfaces, Config } from '@oclif/core'
import { transports } from '@surgio/logger'
import ora from 'ora'
import { resolve } from 'path'
import redis from './redis'

import { CommandConfig } from './types'
import { loadConfig } from './config'
Expand Down Expand Up @@ -49,7 +50,7 @@ abstract class BaseCommand<T extends typeof Command> extends Command {
}

this.projectDir = flags.project
this.surgioConfig = await loadConfig(this.projectDir)
this.surgioConfig = loadConfig(this.projectDir)
}

protected async catch(err: Error & { exitCode?: number }): Promise<any> {
Expand All @@ -59,6 +60,13 @@ abstract class BaseCommand<T extends typeof Command> extends Command {
await errorHandler.call(this, err)
this.exit(err.exitCode || 1)
}

protected async cleanup(): Promise<void> {
await redis.destroyRedis()
if (this.ora.isSpinning) {
this.ora.succeed()
}
}
}

BaseCommand.enableJsonFlag = true
Expand Down
3 changes: 1 addition & 2 deletions src/commands/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import path from 'path'
import inquirer from 'inquirer'

import BaseCommand from '../base-command'
import redis from '../redis'
import { getConfig } from '../config'
import { getProvider } from '../provider'

Expand Down Expand Up @@ -48,7 +47,7 @@ class CheckCommand extends BaseCommand<typeof CheckCommand> {

console.log(JSON.stringify(answers.node, null, 2))

await redis.destroyRedis()
await this.cleanup()
}

private async getNodeList(providerName: string) {
Expand Down
3 changes: 1 addition & 2 deletions src/commands/clean-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import fs from 'fs-extra'

import BaseCommand from '../base-command'
import { TMP_FOLDER_NAME } from '../constant'
import redis from '../redis'
import { cleanCaches } from '../utils/cache'

class CleanCacheCommand extends BaseCommand<typeof CleanCacheCommand> {
Expand All @@ -25,7 +24,7 @@ class CleanCacheCommand extends BaseCommand<typeof CleanCacheCommand> {

ux.action.stop()

await redis.destroyRedis()
await this.cleanup()
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/commands/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// istanbul ignore file
import BaseCommand from '../base-command'
import redis from '../redis'
import { generateDoctorInfo } from '../utils/doctor'

class DoctorCommand extends BaseCommand<typeof DoctorCommand> {
Expand All @@ -16,7 +15,7 @@ class DoctorCommand extends BaseCommand<typeof DoctorCommand> {
console.log(item)
})

await redis.destroyRedis()
await this.cleanup()
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'path'

import BaseCommand from '../base-command'
import { Artifact, getEngine } from '../generator'
import redis from '../redis'
import { ArtifactConfig } from '../types'
import { setConfig } from '../config'
import { checkAndFix } from '../utils/linter'
Expand All @@ -30,7 +29,7 @@ class GenerateCommand extends BaseCommand<typeof GenerateCommand> {

await this.generate(this.flags['skip-fail'], this.flags['cache-snippet'])

await redis.destroyRedis()
await this.cleanup()
}

private async generate(
Expand Down
2 changes: 2 additions & 0 deletions src/commands/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class LintCommand extends BaseCommand<typeof LintCommand> {
} else {
console.log('✅ JS 语法检查通过')
}

await this.cleanup()
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/commands/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ class NewCommand extends BaseCommand<typeof NewCommand> {
cwd: this.projectDir,
logger: new Logger(console.log.bind(console)),
createPrompter: () => require('inquirer'),
exec: (action, body) => {
exec: async (action, body) => {
const opts = body && body.length > 0 ? { input: body } : {}
return require('execa').shell(action, opts)
await require('execa')(action, opts)
},
})

await this.cleanup()
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/commands/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { basename, join } from 'path'
import { createLogger } from '@surgio/logger'

import BaseCommand from '../base-command'
import redis from '../redis'
import { getProvider, PossibleProviderType } from '../provider'
import { formatSubscriptionUserInfo } from '../utils'

Expand Down Expand Up @@ -39,7 +38,7 @@ class SubscriptionsCommand extends BaseCommand<typeof SubscriptionsCommand> {
}
}

await redis.destroyRedis()
await this.cleanup()
}

private async listProviders(): Promise<ReadonlyArray<PossibleProviderType>> {
Expand Down
5 changes: 2 additions & 3 deletions src/commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dir from 'node-dir'
import path from 'path'

import BaseCommand from '../base-command'
import redis from '../redis'
import { setConfig } from '../config'

class UploadCommand extends BaseCommand<typeof UploadCommand> {
Expand Down Expand Up @@ -96,8 +95,8 @@ class UploadCommand extends BaseCommand<typeof UploadCommand> {
this.ora.start('开始上传到阿里云 OSS')
await upload()
await deleteUnwanted()
await redis.destroyRedis()
this.ora.succeed()

await this.cleanup()
}
}

Expand Down

0 comments on commit 398a07b

Please sign in to comment.