-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.ts
82 lines (67 loc) · 2.27 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Flags } from '@oclif/core'
import { execSync } from 'node:child_process'
import * as fs from 'node:fs'
import { AblyBaseCommand } from '../base-command.js'
export default class Config extends AblyBaseCommand {
static override description = 'Open the Ably config file in the default text editor'
static override examples = [
'<%= config.bin %> <%= command.id %> edit',
]
static override flags = {
...AblyBaseCommand.globalFlags,
editor: Flags.string({
char: 'e',
description: 'Text editor to use (defaults to $EDITOR environment variable)',
}),
}
public async run(): Promise<void> {
const { flags } = await this.parse(Config)
// Get the path to the config file
const configPath = this.configManager.getConfigPath()
// Create the config file if it doesn't exist
if (!fs.existsSync(configPath)) {
this.log(`Config file does not exist. Creating it at ${configPath}`)
this.configManager.saveConfig()
}
// Determine which editor to use
const editor = flags.editor || process.env.EDITOR || process.env.VISUAL || this.getDefaultEditor()
if (!editor) {
this.error('No text editor found. Please set one with the --editor flag or set the $EDITOR environment variable.')
return
}
this.log(`Opening config file at ${configPath} with ${editor}...`)
try {
// Open the editor
execSync(`${editor} "${configPath}"`, { stdio: 'inherit' })
this.log('Configuration file has been opened for editing.')
} catch (error) {
this.error(`Failed to open editor: ${error}`)
}
}
private getDefaultEditor(): string | undefined {
// Platform-specific default editors
if (process.platform === 'win32') {
return 'notepad'
}
if (process.platform === 'darwin') {
return 'open -e' // TextEdit on macOS
}
// Try common editors on Linux
try {
execSync('which nano', { stdio: 'ignore' })
return 'nano'
} catch {
try {
execSync('which vim', { stdio: 'ignore' })
return 'vim'
} catch {
try {
execSync('which vi', { stdio: 'ignore' })
return 'vi'
} catch {
return undefined
}
}
}
}
}