-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.ts
199 lines (179 loc) · 5.92 KB
/
install.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { Args, Flags, flush, handle } from '@oclif/core'
import { eachSeries } from 'async'
import {
installPluginFromGithub,
isPluginInstalled,
Vault,
} from 'obsidian-utils'
import { InstallArgs, InstallFlags } from '../../commands'
import FactoryCommand, { FactoryFlags } from '../../providers/command'
import { Config, safeLoadConfig, writeConfig } from '../../providers/config'
import {
findPluginInRegistry,
handleExceedRateLimitError,
} from '../../providers/github'
import { modifyCommunityPlugins } from '../../providers/plugins'
import { vaultsSelector } from '../../providers/vaults'
import { VAULTS_PATH_FLAG_DESCRIPTION } from '../../utils/constants'
import { PluginNotFoundInRegistryError } from '../../utils/errors'
import { logger } from '../../utils/logger'
/**
* Install command installs specified plugins in vaults.
*/
export default class Install extends FactoryCommand {
static readonly aliases = ['pi', 'plugins install']
static override readonly description = `Install plugin(s) in specified vaults.`
static override readonly examples = [
'<%= config.bin %> <%= command.id %> --path=/path/to/vaults',
'<%= config.bin %> <%= command.id %> --path=/path/to/vaults/*/.obsidian',
'<%= config.bin %> <%= command.id %> --path=/path/to/vaults/**/.obsidian',
'<%= config.bin %> <%= command.id %> id',
]
static override readonly flags = {
path: Flags.string({
char: 'p',
description: VAULTS_PATH_FLAG_DESCRIPTION,
default: '',
}),
enable: Flags.boolean({
char: 'e',
description: 'Enable all chosen plugins',
default: true,
}),
...this.commonFlags,
}
static override readonly args = {
pluginId: Args.string({
description: 'Specific Plugin ID to install',
required: false,
}),
}
/**
* Executes the command.
* Parses the arguments and flags, and calls the action method.
* Handles errors and ensures flushing of logs.
*/
public async run() {
try {
const { args, flags } = await this.parse(Install)
await this.action(args, this.flagsInterceptor(flags))
} catch (error) {
this.handleError(error)
} finally {
flush()
}
}
/**
* Main action method for the command.
* Loads vaults, selects vaults, and install specified plugins.
* @param {InstallArgs} args - The arguments passed to the command.
* @param {FactoryFlags<InstallFlags>} flags - The flags passed to the command.
* @returns {Promise<void>}
*/
private async action(
args: InstallArgs,
flags: FactoryFlags<InstallFlags>,
): Promise<void> {
const { path, enable } = flags
const {
success: loadConfigSuccess,
data: config,
error: loadConfigError,
} = await safeLoadConfig(flags.config)
if (!loadConfigSuccess) {
logger.error('Failed to load config', { error: loadConfigError })
process.exit(1)
}
const vaults = await this.loadVaults(path)
const selectedVaults = await vaultsSelector(vaults)
// Check if pluginId is provided and install only that plugin
const { pluginId } = args
if (pluginId) {
await this.installPluginInVaults(selectedVaults, config, flags, pluginId)
} else {
await this.installPluginsInVaults(selectedVaults, config, flags, enable)
}
}
private async installPluginsInVaults(
vaults: Vault[],
config: Config,
flags: FactoryFlags<InstallFlags>,
specific = false,
) {
const installVaultIterator = async (vault: Vault) => {
logger.debug(`Install plugins for vault`, { vault })
const installedPlugins = []
const failedPlugins = []
for (const stagePlugin of config.plugins) {
const childLogger = logger.child({ plugin: stagePlugin, vault })
const pluginInRegistry = await findPluginInRegistry(stagePlugin.id)
if (!pluginInRegistry) {
throw new PluginNotFoundInRegistryError(stagePlugin.id)
}
if (await isPluginInstalled(pluginInRegistry.id, vault.path)) {
childLogger.info(`Plugin already installed`)
continue
}
stagePlugin.version = stagePlugin.version ?? 'latest'
try {
await installPluginFromGithub(
pluginInRegistry.repo,
stagePlugin.version,
vault.path,
)
installedPlugins.push({
repo: pluginInRegistry.repo,
version: stagePlugin.version,
})
if (flags.enable) {
// Enable the plugin
await modifyCommunityPlugins(stagePlugin, vault.path, 'enable')
}
if (specific) {
// Add the plugin to the config
const newPlugins = new Set([...config.plugins])
const updatedConfig = { ...config, plugins: [...newPlugins] }
await writeConfig(updatedConfig, flags.config)
}
childLogger.info(`Installed plugin`)
} catch (error) {
failedPlugins.push({
repo: pluginInRegistry.repo,
version: stagePlugin.version,
})
handleExceedRateLimitError(error)
childLogger.error(`Failed to install plugin`, { error })
}
}
if (installedPlugins.length) {
logger.info(`Installed ${installedPlugins.length} plugins`, {
vault,
})
}
return { installedPlugins, failedPlugins }
}
eachSeries(vaults, installVaultIterator, (error) => {
if (error) {
logger.debug('Error installing plugins', { error })
handle(error)
}
})
}
private async installPluginInVaults(
vaults: Vault[],
config: Config,
flags: FactoryFlags<InstallFlags>,
pluginId: string,
) {
const pluginInRegistry = await findPluginInRegistry(pluginId)
if (!pluginInRegistry) {
throw new PluginNotFoundInRegistryError(pluginId)
}
await this.installPluginsInVaults(
vaults,
{ ...config, plugins: [{ id: pluginId }] },
flags,
true,
)
}
}