Skip to content

Commit

Permalink
refactor: update command flags to use specific types and improve work…
Browse files Browse the repository at this point in the history
…ing directory handling
  • Loading branch information
msudgh committed Jan 2, 2025
1 parent 616ad99 commit 8f40f39
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/commands/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class Init extends FactoryCommand {
public async run() {
try {
const { args, flags } = await this.parse(Init)
return action(args, this.flagsInterceptor(flags))
return action(args, this.flagsInterceptor<InitFlags>(flags))
} catch (error) {
this.handleError(error)
} finally {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Args, Flags, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import installService from '../../services/install'
import { InstallFlags } from '../../types/commands'

const { action } = installService

Expand Down Expand Up @@ -42,7 +43,7 @@ export default class Install extends FactoryCommandWithVaults {
public async run(): Promise<void> {
try {
const { args, flags } = await this.parse(Install)
return action(args, this.flagsInterceptor(flags))
return action(args, this.flagsInterceptor<InstallFlags>(flags))
} catch (error) {
this.handleError(error)
throw error
Expand Down
3 changes: 2 additions & 1 deletion src/commands/plugins/prune.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import pruneService from '../../services/prune'
import { PruneFlags } from '../../types/commands'

const { action } = pruneService

Expand All @@ -27,7 +28,7 @@ export default class Prune extends FactoryCommandWithVaults {
public async run() {
try {
const { args, flags } = await this.parse(Prune)
await action(args, this.flagsInterceptor(flags))
await action(args, this.flagsInterceptor<PruneFlags>(flags))
} catch (error) {
this.handleError(error)
} finally {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Args, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import uninstallService from '../../services/uninstall'
import { UninstallFlags } from '../../types/commands'

const { action } = uninstallService

Expand Down Expand Up @@ -34,7 +35,7 @@ export default class Uninstall extends FactoryCommandWithVaults {
public async run() {
try {
const { args, flags } = await this.parse(Uninstall)
await action(args, this.flagsInterceptor(flags))
await action(args, this.flagsInterceptor<UninstallFlags>(flags))
} catch (error) {
this.handleError(error)
} finally {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/reports/stats.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Flags, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import statsService from '../../services/stats'
import { StatsFlags } from '../../types/commands'

const { action } = statsService

Expand Down Expand Up @@ -33,7 +34,7 @@ export default class Stats extends FactoryCommandWithVaults {
public async run(): Promise<void> {
try {
const { args, flags } = await this.parse(Stats)
return await action(args, this.flagsInterceptor(flags))
return await action(args, this.flagsInterceptor<StatsFlags>(flags))
} catch (error) {
this.handleError(error)
throw error
Expand Down
13 changes: 7 additions & 6 deletions src/commands/vaults/run.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Args, Flags, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import runService from '../../services/run'
import { RunFlags } from '../../types/commands'

const { action } = runService

Expand All @@ -13,7 +14,7 @@ export default class Run extends FactoryCommandWithVaults {
'<%= config.bin %> <%= command.id %> --path=/path/to/vaults/**/.obsidian --output=json --unescape=false',
'<%= config.bin %> <%= command.id %> --output=json --async=false',
'<%= config.bin %> <%= command.id %> --output=json --silent=true',
'<%= config.bin %> <%= command.id %> --output=json --runFromVaultDirectoryAsWorkDir=false',
'<%= config.bin %> <%= command.id %> --output=json --cwd=/path/to/vaults',
]
static override readonly flags = {
output: Flags.string({
Expand All @@ -38,10 +39,10 @@ export default class Run extends FactoryCommandWithVaults {
description: 'Silent on results of the custom command on vault(s).',
default: false,
}),
runFromVaultDirectoryAsWorkDir: Flags.boolean({
char: 'r',
description: 'Run the command from the vault directory as working dir.',
default: true,
cwd: Flags.string({
char: 'w',
description:
'[default: vault path] Set working directory for custom command.',
}),
...this.commonFlagsWithPath,
}
Expand All @@ -65,7 +66,7 @@ export default class Run extends FactoryCommandWithVaults {
public async run(): Promise<void> {
try {
const { args, flags } = await this.parse(Run)
await action(args, this.flagsInterceptor(flags))
await action(args, this.flagsInterceptor<RunFlags>(flags))
} catch (error) {
this.handleError(error)
} finally {
Expand Down
6 changes: 3 additions & 3 deletions src/providers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ const commandInterpolation = (vault: Vault, command: string): string => {

const asyncExecCustomCommand = async (
vault: Vault,
command = '',
runFromVaultDirectoryAsWorkDir = true,
command: string,
cwd: string,
): Promise<string> =>
new Promise((resolve, reject) => {
exec(
commandInterpolation(vault, command),
{ cwd: runFromVaultDirectoryAsWorkDir ? vault.path : __dirname },
{ cwd },
(error, stdout, stderr) => {
if (error) {
reject(error)
Expand Down
40 changes: 36 additions & 4 deletions src/services/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai'
import { tmpdir } from 'os'
import proxyquire from 'proxyquire'
import sinon from 'sinon'
import { RunCommandIterator } from '../types/commands'
Expand Down Expand Up @@ -26,7 +27,6 @@ describe('Command: run', () => {
flags: {
...getTestCommonWithVaultPathFlags(config.path, vault.path),
output: 'json',
runFromVaultDirectoryAsWorkDir: false,
},
args: {
command: '',
Expand All @@ -45,7 +45,6 @@ describe('Command: run', () => {
config,
flags: {
...getTestCommonWithVaultPathFlags(config.path, vault.path),
runFromVaultDirectoryAsWorkDir: false,
},
args: { command: "echo 'Path: {0} {1}'" },
})
Expand All @@ -64,7 +63,6 @@ describe('Command: run', () => {
config,
flags: {
...getTestCommonWithVaultPathFlags(config.path, vault.path),
runFromVaultDirectoryAsWorkDir: false,
},
args: { command: "echo 'Path: {0} {1} {10000}'" },
})
Expand Down Expand Up @@ -94,7 +92,6 @@ describe('Command: run', () => {
config,
flags: {
...getTestCommonWithVaultPathFlags(config.path, vault.path),
runFromVaultDirectoryAsWorkDir: false,
},
args: { command: 'invalid-command' },
})
Expand All @@ -106,4 +103,39 @@ describe('Command: run', () => {

destroyVault(vault.path)
})

it('should not run command from vault directory', async () => {
const { vault, config } = setupVault()
const result = await runCommandVaultIterator({
vault,
config,
flags: {
...getTestCommonWithVaultPathFlags(config.path, vault.path),
cwd: tmpdir(),
},
args: { command: 'echo $PWD' },
})

expect(result.success).to.be.true
expect(result.stdout?.toString().trim()).to.match(new RegExp(tmpdir()))

destroyVault(vault.path)
})

it('should run command from vault directory', async () => {
const { vault, config } = setupVault()
const result = await runCommandVaultIterator({
vault,
config,
flags: {
...getTestCommonWithVaultPathFlags(config.path, vault.path),
},
args: { command: 'echo $PWD' },
})

expect(result.success).to.be.true
expect(result.stdout?.toString().trim()).to.match(new RegExp(vault.path))

destroyVault(vault.path)
})
})
12 changes: 5 additions & 7 deletions src/services/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const runCommandVaultIterator: RunCommandIterator = async (item) => {

internalCustomLogger.debug(`Execute command`)

const vaultPathHash = btoa(vault.path)
const vaultPathHash = btoa(`${vault.name}:${vault.path}`)
taskExecutedOnVaults[vaultPathHash] = {
success: null,
duration: '',
Expand All @@ -46,11 +46,8 @@ const runCommandVaultIterator: RunCommandIterator = async (item) => {

try {
const startDate = new Date()
const result = await asyncExecCustomCommand(
vault,
command,
flags?.runFromVaultDirectoryAsWorkDir,
)
const cwd = flags.cwd ? flags.cwd : vault.path
const result = await asyncExecCustomCommand(vault, command, cwd)
const endDate = new Date()
const durationLessThanSecond = endDate.getTime() - startDate.getTime()
const durationMoreThanSecond = intervalToDuration({
Expand Down Expand Up @@ -134,7 +131,8 @@ const action = async (
result.sortedTaskExecutedOnVaults = Object.entries(taskExecutedOnVaults)
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
.reduce<CommandsExecutedOnVaults>((acc, [key, value]) => {
acc[key] = value
const [vaultName] = atob(key).split(':')
acc[vaultName] = value
return acc
}, {})

Expand Down
2 changes: 1 addition & 1 deletion src/types/commands.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface StatsFlags {
export type StatsArgs = Record<string, unknown>

export interface RunFlags {
runFromVaultDirectoryAsWorkDir: boolean
cwd?: string
output?: string
unescape?: boolean
async?: boolean
Expand Down

0 comments on commit 8f40f39

Please sign in to comment.