Skip to content

Commit

Permalink
refactor: improve platform-specific handling of tmp config path
Browse files Browse the repository at this point in the history
  • Loading branch information
msudgh committed Aug 25, 2024
1 parent 031aa30 commit 9069525
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: test
name: Unit Tests
on:
push:
branches:
Expand Down
2 changes: 0 additions & 2 deletions src/commands/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import FactoryCommand, {
FactoryFlags,
} from '../../providers/command'
import { createDefaultConfig, safeLoadConfig } from '../../providers/config'
import { logger } from '../../utils/logger'

/**
* Init command configure an ovm.json config file in user's home dir.
Expand Down Expand Up @@ -60,7 +59,6 @@ export default class Init extends FactoryCommand {
if (typedError && typedError.message === 'Config file not found') {
try {
await createDefaultConfig(flags.config)
logger.info('Config file created', { path: flags.config })
} catch (error) {
this.handleError(error)
}
Expand Down
16 changes: 9 additions & 7 deletions src/providers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ export const safeLoadConfig = (

export const writeConfig = (
config: Config,
configPath: string,
path: string,
): Promise<void | Error> => {
logger.debug('Writing config', { configPath })
logger.debug('Writing config', { path })
return new Promise((resolve, reject) => {
try {
const content = JSON.stringify(config, null, 2)
writeFileSync(configPath, content)
logger.debug('Config written', { configPath })

writeFileSync(path, content)
logger.debug('Config written', { path })
resolve()
} catch (error) {
reject(error as Error)
Expand All @@ -85,14 +86,15 @@ export const writeConfig = (
}

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

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

logger.info('Config file created', { path })

resolve(defaultConfig)
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ export const runCommand = async (
}

export const getTmpConfigFilePath = () => {
if (platform() === 'win32') {
return path.win32.join(tmpdir(), OVM_CONFIG_FILENAME)
}

return path.join(tmpdir(), OVM_CONFIG_FILENAME)
}

Expand Down

0 comments on commit 9069525

Please sign in to comment.