Skip to content

Commit

Permalink
Merge branch 'release/0.2.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
msudgh committed Aug 4, 2024
2 parents 72b0284 + 992469a commit b4fc3a2
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 54 deletions.
10 changes: 2 additions & 8 deletions .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,8 @@
"extends": "@istanbuljs/nyc-config-typescript",
"all": true,
"check-coverage": false,
"extensions": [
".ts"
],
"reporter": [
"text",
"html",
"lcov"
],
"extensions": [".ts"],
"reporter": ["text", "html", "lcov"],
"exclude": [
"eslint.config.cjs",
"coverage/**",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Install plugins in specified vaults.

### `ovm plugins prune`

Prune plugins from specified vaults.
Prune plugin/s from specified vaults.

- _Usage:_ `ovm help plugins prune`
- _See code:_ [src/commands/plugins/prune.ts](src/commands/plugins/prune.ts)
Expand Down
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ovm",
"description": "Obsidian Vaults Manager",
"type": "commonjs",
"version": "0.2.2",
"version": "0.2.3",
"license": "MIT",
"author": "Masoud Ghorbani",
"homepage": "https://github.com/msudgh/ovm",
Expand Down Expand Up @@ -41,8 +41,7 @@
"dirname": "ovm",
"commands": "./dist/commands",
"plugins": [
"@oclif/plugin-help",
"@oclif/plugin-not-found"
"@oclif/plugin-help"
],
"topicSeparator": " ",
"topics": {
Expand All @@ -64,7 +63,6 @@
"@inquirer/prompts": "^5.2.1",
"@oclif/core": "^4.0.12",
"@oclif/plugin-help": "^6.2.6",
"@oclif/plugin-not-found": "^3.2.12",
"async": "^3.2.5",
"glob": "^11.0.0",
"inquirer": "^10.0.4",
Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/commands/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class Init extends FactoryCommand {
flags: FactoryFlags<CommonFlags>,
): Promise<void> {
try {
const { data: config, error } = await safeLoadConfig()
const { data: config, error } = await safeLoadConfig(flags.config)

if (config) {
logger.error('File already exists!', { config: flags.config })
Expand Down
41 changes: 26 additions & 15 deletions src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Vault,
} from 'obsidian-utils'
import FactoryCommand, { FactoryFlags } from '../../providers/command'
import { Config, Plugin, safeLoadConfig, writeConfig } from '../../providers/config'
import { Config, safeLoadConfig, writeConfig } from '../../providers/config'
import {
findPluginInRegistry,
handleExceedRateLimitError,
Expand Down Expand Up @@ -95,7 +95,7 @@ export default class Install extends FactoryCommand {
success: loadConfigSuccess,
data: config,
error: loadConfigError,
} = await safeLoadConfig()
} = await safeLoadConfig(flags.config)

if (!loadConfigSuccess) {
logger.error('Failed to load config', { error: loadConfigError })
Expand All @@ -108,24 +108,24 @@ export default class Install extends FactoryCommand {
// Check if pluginId is provided and install only that plugin
const { pluginId } = args
if (pluginId) {
await this.installPluginInVaults(selectedVaults, pluginId, enable)
await this.installPluginInVaults(selectedVaults, config, flags, pluginId)
} else {
await this.installPluginsInVaults(selectedVaults, config.plugins, enable)
await this.installPluginsInVaults(selectedVaults, config, flags, enable)
}
}

private async installPluginsInVaults(
vaults: Vault[],
plugins: Plugin[],
enable: boolean,
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 plugins) {
for (const stagePlugin of config.plugins) {
const childLogger = logger.child({ stagePlugin, vault })

const pluginInRegistry = await findPluginInRegistry(stagePlugin.id)
Expand All @@ -138,7 +138,7 @@ export default class Install extends FactoryCommand {
continue
}

stagePlugin.version = stagePlugin.version ?? 'latest';
stagePlugin.version = stagePlugin.version ?? 'latest'

try {
await installPluginFromGithub(
Expand All @@ -151,15 +151,16 @@ export default class Install extends FactoryCommand {
version: stagePlugin.version,
})

if (enable) {
if (flags.enable) {
// Enable the plugin
await modifyCommunityPlugins(stagePlugin, vault.path, 'enable')
}

if (specific) {
// Add the plugin to the config
const newPlugins = new Set([...plugins])
await writeConfig({ plugins: [...newPlugins] })
const newPlugins = new Set([...config.plugins])
const updatedConfig = { ...config, plugins: [...newPlugins] }
await writeConfig(updatedConfig, flags.config)
}

childLogger.debug(`Installed plugin`)
Expand Down Expand Up @@ -190,12 +191,22 @@ export default class Install extends FactoryCommand {
})
}

private async installPluginInVaults(vaults: Vault[], id: string, enable: boolean) {
const pluginInRegistry = await findPluginInRegistry(id)
private async installPluginInVaults(
vaults: Vault[],
config: Config,
flags: FactoryFlags<InstallFlags>,
pluginId: string,
) {
const pluginInRegistry = await findPluginInRegistry(pluginId)
if (!pluginInRegistry) {
throw new PluginNotFoundInRegistryError(id)
throw new PluginNotFoundInRegistryError(pluginId)
}

await this.installPluginsInVaults(vaults, [{ id }], enable, true)
await this.installPluginsInVaults(
vaults,
{ ...config, plugins: [{ id: pluginId }] },
flags,
true,
)
}
}
4 changes: 2 additions & 2 deletions src/commands/plugins/prune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface PrunePluginVaultOpts {
*/
export default class Prune extends FactoryCommand {
static readonly aliases = ['pp', 'plugins:prune']
static override readonly description = `Prune plugins from specified vaults.`
static override readonly description = `Prune plugin/s from specified vaults.`
static override readonly examples = [
'<%= config.bin %> <%= command.id %> --path=/path/to/vaults',
'<%= config.bin %> <%= command.id %> --path=/path/to/vaults/*/.obsidian',
Expand Down Expand Up @@ -70,7 +70,7 @@ export default class Prune extends FactoryCommand {
success: loadConfigSuccess,
data: config,
error: loadConfigError,
} = await safeLoadConfig()
} = await safeLoadConfig(flags.config)

if (!loadConfigSuccess) {
logger.error('Failed to load config', { error: loadConfigError })
Expand Down
2 changes: 1 addition & 1 deletion src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class Uninstall extends FactoryCommand {
success: loadConfigSuccess,
data: config,
error: loadConfigError,
} = await safeLoadConfig()
} = await safeLoadConfig(flags.config)

if (!loadConfigSuccess) {
logger.error('Failed to load config', { error: loadConfigError })
Expand Down
2 changes: 1 addition & 1 deletion src/commands/reports/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export default class Stats extends FactoryCommand {
success: loadConfigSuccess,
data: config,
error: loadConfigError,
} = await safeLoadConfig()
} = await safeLoadConfig(flags.config)
if (!loadConfigSuccess) {
logger.error('Failed to load config', { error: loadConfigError })
process.exit(1)
Expand Down
6 changes: 3 additions & 3 deletions src/providers/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createDefaultConfig, safeLoadConfig } from './config'
import { ConfigSchema, safeLoadConfig } from './config'

import { expect } from 'chai'
import mock from 'mock-fs'
Expand All @@ -9,7 +9,7 @@ describe('Config', () => {
const userHome = '/home/user'
const configPath = `${userHome}/${OVM_CONFIG_FILENAME}`

const sampleDefaultConfig = await createDefaultConfig()
const sampleDefaultConfig = ConfigSchema.parse({})

mock({
[userHome]: {
Expand All @@ -20,8 +20,8 @@ describe('Config', () => {
const config = await safeLoadConfig(configPath)

expect(config.success).to.be.true
expect(config.data).to.deep.equal(config.data)
expect(config.error).to.be.undefined
expect(config.data).to.deep.equal(sampleDefaultConfig)
mock.restore()
})

Expand Down
13 changes: 5 additions & 8 deletions src/providers/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { readFileSync, writeFileSync } from 'fs'
import { GitHubPluginVersion } from 'obsidian-utils'
import z from 'zod'
import { DEFAULT_CONFIG_PATH } from '../constants'
import { logger } from '../utils/logger'

const PluginSchema = z.object({
Expand Down Expand Up @@ -35,9 +34,7 @@ type SafeLoadConfigResult =
} & SafeLoadConfigResultSuccess)
| SafeLoadConfigResultError

export const safeLoadConfig = (
configPath = DEFAULT_CONFIG_PATH,
): Promise<SafeLoadConfigResult> => {
export const safeLoadConfig = (configPath: string): Promise<SafeLoadConfigResult> => {
return new Promise((resolve) => {
try {
const config = readFileSync(configPath)
Expand Down Expand Up @@ -70,14 +67,14 @@ export const safeLoadConfig = (

export const writeConfig = (
config: Config,
configPath = DEFAULT_CONFIG_PATH,
configPath: string,
): Promise<void | Error> => {
logger.debug('Writing config', { configPath })
return new Promise((resolve, reject) => {
try {
const content = JSON.stringify(config, null, 2)
writeFileSync(configPath, content)
logger.debug('Config written', { configPath })
logger.debug('Config written', { configPath })
resolve()
} catch (error) {
reject(error as Error)
Expand All @@ -86,12 +83,12 @@ export const writeConfig = (
}

export const createDefaultConfig = (
configPath = DEFAULT_CONFIG_PATH,
configPath: string,
): Promise<Config | Error> => {
return new Promise((resolve, reject) => {
try {
const defaultConfig = ConfigSchema.parse({})
writeConfig(defaultConfig)
writeConfig(defaultConfig, configPath)

logger.debug('Default config created', { configPath })

Expand Down
9 changes: 2 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@
"rootDir": "src",
"strict": true,
"esModuleInterop": true,
"types": [
"node",
"mocha"
],
"types": ["node", "mocha"],
"noImplicitAny": true,
"skipLibCheck": false,
"sourceMap": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*"
]
"include": ["src/**/*"]
}

0 comments on commit b4fc3a2

Please sign in to comment.