From a48df92516309b2f12598b36fee2458fcf13a0e1 Mon Sep 17 00:00:00 2001 From: Masoud Ghorbani Date: Fri, 9 Aug 2024 22:51:36 +0200 Subject: [PATCH 1/2] chore: resolved eslint issues --- README.md | 7 ++ eslint.config.cjs | 13 +++- src/commands.d.ts | 9 +++ src/commands/plugins/install.ts | 3 +- src/commands/plugins/uninstall.ts | 3 +- src/commands/vaults/run.ts | 103 +++++++++++++++--------------- src/providers/config.test.ts | 4 +- src/{types.ts => types.d.ts} | 2 +- 8 files changed, 85 insertions(+), 59 deletions(-) rename src/{types.ts => types.d.ts} (59%) diff --git a/README.md b/README.md index 5f80585..9b9f91a 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ Example config file for following [Commands](#commands) section is as follows: > The content used in the examples below is for illustrative purposes only. e.g. In the output sections, the vaults are stored in `~/Documents/` directory. The actual output may vary. ### `ovm config init` + Aliases: `ovm ci` Configure an ovm.json config file in user's home directory. @@ -110,6 +111,7 @@ $ cat ~/ovm.json ``` ### `ovm plugins install` + Aliases: `ovm pi` Install plugin(s) in specified vaults. @@ -137,6 +139,7 @@ info: Installed 3 plugins {"vault":{"name":"Goals","path":"~/Documents/obsidian/ ``` ### `ovm plugins prune` + Aliases: `ovm pp` Prune existing plugin(s) from vaults that are unspecified in the config file. @@ -154,6 +157,7 @@ info: Pruned 1 plugins {"vault":{"name":"Test","path":"~/Documents/obsidian/Test ``` ### `ovm plugins uninstall` + Aliases: `ovm pu` Uninstall plugin(s) from vaults. @@ -182,6 +186,7 @@ info: Uninstalled 3 plugins {"vault":{"name":"Goals","path":"~/Documents/obsidia ``` ### `ovm reports stats` + Aliases: `ovm rs` Statistics of vaults and installed plugins. @@ -210,6 +215,7 @@ $ ovm reports stats ``` ### `ovm vaults run` + Aliases: `ovm vr` / `ovm r` / `ovm run` Run a shell command on selected vaults (using Node.js child_process). @@ -261,6 +267,7 @@ info: Run operation finished! {"custom_commands_log_path":"/var/folders/_v/j4w6k A custom command can be executed on vault(s) by using reserved placeholders as string value within the shell command. The placeholders are replaced with the actual values during the execution. List of placeholders: + - `{0}`: Vault path - `{1}`: Vault name diff --git a/eslint.config.cjs b/eslint.config.cjs index ae5d729..9c79ec7 100644 --- a/eslint.config.cjs +++ b/eslint.config.cjs @@ -6,7 +6,16 @@ const tsEslint = require('typescript-eslint') const rulesConfig = [ { rules: { - 'no-unused-vars': 'warn', + 'no-unused-vars': [ + 'error', + { + 'vars': 'all', + 'args': 'after-used', + 'ignoreRestSiblings': true, + 'varsIgnorePattern': '^_', // Ignore variables that start with _ + 'argsIgnorePattern': '^_' // Ignore arguments that start with _ + } + ], 'no-undef': 'warn', 'prefer-arrow-callback': ['error', {allowNamedFunctions: false}], 'func-style': ['error', 'expression', {allowArrowFunctions: true}], @@ -14,7 +23,7 @@ const rulesConfig = [ }, ] -const ignoresConfig = [{ignores: ['dist', 'bin']}] +const ignoresConfig = [{ignores: ['dist', 'bin', 'eslint.config.cjs']}] module.exports = [ {files: ['**/*.{ts}', '**/*.test.ts', '**/*.spec.ts']}, diff --git a/src/commands.d.ts b/src/commands.d.ts index 9c73448..b6a769a 100644 --- a/src/commands.d.ts +++ b/src/commands.d.ts @@ -36,3 +36,12 @@ export interface UninstallPluginVaultOpts { vault: Vault config: Config } + +export type CommandsExecutedOnVaults = Record< + string, + { + success: null | boolean + duration: string + error: null | Error | string + } +> diff --git a/src/commands/plugins/install.ts b/src/commands/plugins/install.ts index dc4dee8..96771a7 100644 --- a/src/commands/plugins/install.ts +++ b/src/commands/plugins/install.ts @@ -161,10 +161,11 @@ export default class Install extends FactoryCommand { } } - installedPlugins.length && + if (installedPlugins.length) { logger.info(`Installed ${installedPlugins.length} plugins`, { vault, }) + } return { installedPlugins, failedPlugins } } diff --git a/src/commands/plugins/uninstall.ts b/src/commands/plugins/uninstall.ts index 9da90b3..14d36e4 100644 --- a/src/commands/plugins/uninstall.ts +++ b/src/commands/plugins/uninstall.ts @@ -112,10 +112,11 @@ export default class Uninstall extends FactoryCommand { } } - uninstalledPlugins.length && + if (uninstalledPlugins.length) { logger.info(`Uninstalled ${uninstalledPlugins.length} plugins`, { vault, }) + } return { uninstalledPlugins, failedPlugins } } diff --git a/src/commands/vaults/run.ts b/src/commands/vaults/run.ts index 8b5e857..9b43bcb 100644 --- a/src/commands/vaults/run.ts +++ b/src/commands/vaults/run.ts @@ -3,6 +3,7 @@ import { each, eachSeries, ErrorCallback } from 'async' import { exec, ExecException } from 'child_process' import { formatDuration, intervalToDuration } from 'date-fns' import { Vault } from 'obsidian-utils' +import { CommandsExecutedOnVaults } from '../../commands' import FactoryCommand, { FactoryFlags } from '../../providers/command' import { safeLoadConfig } from '../../providers/config' import { vaultsSelector } from '../../providers/vaults' @@ -128,12 +129,12 @@ export default class Run extends FactoryCommand { const vaults = await this.loadVaults(path) const selectedVaults = await vaultsSelector(vaults) - const vaultsWithCommand = selectedVaults.map((vault: any) => ({ + const vaultsWithCommand = selectedVaults.map((vault: Vault) => ({ vault, command: this.commandInterpolation(vault, command), })) - const taskExecutedOnVaults: Record = {} + const taskExecutedOnVaults: CommandsExecutedOnVaults = {} const commandVaultIterator = async (opts: { vault: Vault @@ -142,31 +143,44 @@ export default class Run extends FactoryCommand { const { vault, command } = opts logger.debug(`Execute command`, { vault, command }) - const startDate = new Date() - const { error, result } = await this.asyncExecCustomCommand( - command, - flags.runFromVaultDirectoryAsWorkDir, - vault, - ) - const endDate = new Date() - const durationLessThanSecond = endDate.getTime() - startDate.getTime() - const durationMoreThanSecond = intervalToDuration({ - start: startDate, - end: endDate, - }) + try { + const startDate = new Date() + const result = await this.asyncExecCustomCommand( + command, + flags.runFromVaultDirectoryAsWorkDir, + vault, + ) + const endDate = new Date() + const durationLessThanSecond = endDate.getTime() - startDate.getTime() + const durationMoreThanSecond = intervalToDuration({ + start: startDate, + end: endDate, + }) - const formattedDuration = - formatDuration(durationMoreThanSecond, { - format: ['hours', 'minutes', 'seconds'], - }) || `${durationLessThanSecond} ms` + const formattedDuration = + formatDuration(durationMoreThanSecond, { + format: ['hours', 'minutes', 'seconds'], + }) || `${durationLessThanSecond} ms` - taskExecutedOnVaults[vault.name] = { - success: null, - duration: formattedDuration, - error: null, - } + taskExecutedOnVaults[vault.name] = { + success: null, + duration: formattedDuration, + error: null, + } - if (error) { + if (result) { + taskExecutedOnVaults[vault.name]['success'] = true + customCommandLogger.info('Executed successfully', { + result, + vault, + command, + }) + if (!flags.silent) { + logger.info(`Run command`, { vault, command }) + console.log(result) + } + } + } catch (error) { taskExecutedOnVaults[vault.name]['error'] = JSON.stringify(error) customCommandLogger.error('Execution failed', { error: JSON.stringify(error), @@ -174,22 +188,11 @@ export default class Run extends FactoryCommand { command, }) } - - if (result) { - taskExecutedOnVaults[vault.name]['success'] = true - customCommandLogger.info('Executed successfully', { - result, - vault, - command, - }) - if (!flags.silent) { - logger.info(`Run command`, { vault, command }) - console.log(result) - } - } } - const commandVaultErrorCallback: ErrorCallback = (error: any) => { + const commandVaultErrorCallback: ErrorCallback = ( + error: Error | null | undefined, + ) => { if (error) { logger.debug('UnhandledException', { error: JSON.stringify(error), @@ -200,13 +203,10 @@ export default class Run extends FactoryCommand { } else { const sortedTaskExecutedOnVaults = Object.entries(taskExecutedOnVaults) .sort(([keyA], [keyB]) => keyA.localeCompare(keyB)) - .reduce( - (acc, [key, value]) => { - acc[key] = value - return acc - }, - {} as Record, - ) + .reduce((acc, [key, value]) => { + acc[key] = value + return acc + }, {} as CommandsExecutedOnVaults) logger.info('Run operation finished!', { custom_commands_log_path: CUSTOM_COMMAND_LOGGER_FILE, @@ -238,17 +238,16 @@ export default class Run extends FactoryCommand { command: string, runFromVaultDirectoryAsWorkDir: boolean, vault: Vault, - ): Promise< - Pick & { - result: string - } - > { + ): Promise | string> { return new Promise((resolve, reject) => { exec( command, { cwd: runFromVaultDirectoryAsWorkDir ? vault.path : __dirname }, - (error: any, stdout: any, stderr: any) => { - resolve({ result: `${stderr}\n${stdout}`, error }) + (error, stdout, stderr) => { + if (error) { + return reject(error) + } + resolve(`${stderr}\n${stdout}`) }, ) }) diff --git a/src/providers/config.test.ts b/src/providers/config.test.ts index 6bcbc38..99b4a34 100644 --- a/src/providers/config.test.ts +++ b/src/providers/config.test.ts @@ -19,8 +19,8 @@ describe('Config', () => { const config = await safeLoadConfig(configPath) - expect(config.success).to.be.true - expect(config.error).to.be.undefined + expect(config.success).to.be.true.equal(true) + expect(config.error).to.be.undefined.equal(undefined) expect(config.data).to.deep.equal(sampleDefaultConfig) mock.restore() }) diff --git a/src/types.ts b/src/types.d.ts similarity index 59% rename from src/types.ts rename to src/types.d.ts index ae75813..3887500 100644 --- a/src/types.ts +++ b/src/types.d.ts @@ -1,6 +1,6 @@ import { Vault } from 'obsidian-utils' -type CommandOnVault = (vault: Vault, ...args: string[]) => string +export type CommandOnVault = (_vault: Vault, ..._args: string[]) => string export type ReservedVariables = { [key: string]: CommandOnVault } From afe398d2ce19d24be8a42973119d73c375a68ae4 Mon Sep 17 00:00:00 2001 From: Masoud Ghorbani Date: Fri, 9 Aug 2024 22:51:57 +0200 Subject: [PATCH 2/2] 0.4.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0af424a..950c2d1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "ovm", "description": "Obsidian Vaults Manager", "type": "commonjs", - "version": "0.4.0", + "version": "0.4.1", "license": "MIT", "author": "Masoud Ghorbani", "homepage": "https://github.com/msudgh/ovm",