Skip to content

Commit

Permalink
Merge branch 'release/0.4.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
msudgh committed Aug 25, 2024
2 parents b9cbadb + 334a737 commit 32e0a22
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .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 Expand Up @@ -32,7 +32,6 @@ jobs:
with:
node-version: ${{ matrix.node_version }}
cache: 'pnpm'
- run: pnpm run build
- run: pnpm run test:coverage
- name: SonarCloud Scan
if: matrix.os == 'ubuntu-latest'
Expand Down
8 changes: 4 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.4.1",
"version": "0.4.2",
"license": "MIT",
"author": "Masoud Ghorbani",
"homepage": "https://github.com/msudgh/ovm",
Expand All @@ -11,8 +11,8 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "shx rm -rf dist && tsc -p tsconfig.json",
"build:watch": "shx rm -rf dist && tsc -p tsconfig.json -w",
"build": "shx rm -rf dist && tsc -p tsconfig.release.json",
"build:watch": "shx rm -rf dist && tsc -p tsconfig.release.json -w",
"build:release": "shx rm -rf dist && tsc -p tsconfig.release.json",
"lint": "eslint .",
"lint:fix": "eslint . --fix",
Expand All @@ -21,7 +21,7 @@
"postpack": "shx rm -f oclif.manifest.json",
"posttest": "pnpm run lint",
"prepack": "oclif manifest && pnpm run build:release && pnpm run test:unit",
"test:unit": "mocha",
"test:unit": "pnpm run build && mocha",
"test:coverage": "nyc npm run test:unit"
},
"keywords": [
Expand Down
22 changes: 22 additions & 0 deletions src/commands/config/init.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { expect } from 'chai'
import { existsSync } from 'fs'
import { destroyConfigMockFile, getTmpConfigFilePath, runCommand } from '../../utils/testing'

describe('Command: config init', () => {
beforeEach(async () => {
const tmpConfigFilePath = getTmpConfigFilePath()
if (tmpConfigFilePath && existsSync(tmpConfigFilePath)) {
await destroyConfigMockFile(tmpConfigFilePath.normalize('NFC'))
}
})

it('should create a config file', async () => {
const tmpConfigFilePath = getTmpConfigFilePath()
const result = await runCommand(`config init -c ${tmpConfigFilePath}`)
const normalizedOutput = result?.stdout?.trim().replace(/\\\\/g, '\\')
expect(normalizedOutput).to.equal(
`info: Config file created {"path":"${tmpConfigFilePath.replace(/\\\\/g, '\\')}"}`,
)
await destroyConfigMockFile(tmpConfigFilePath.normalize('NFC'))
})
})
5 changes: 1 addition & 4 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 @@ -48,8 +47,7 @@ export default class Init extends FactoryCommand {
const { data: config, error } = await safeLoadConfig(flags.config)

if (config) {
logger.error('File already exists!', { config: flags.config })
process.exit(1)
throw new Error('File already exists!')
}

if (error) {
Expand All @@ -61,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
5 changes: 4 additions & 1 deletion src/providers/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export default class FactoryCommand extends Command {
}

public handleError(error: unknown) {
if (error instanceof ExitPromptError) {
// Avoid handling errors by logger for CI environment
if (process.env.CI) {
throw error
} else if (error instanceof ExitPromptError) {
logger.debug('Exit prompt error:', { error })
} else if (error instanceof Error) {
logger.debug('An error occurred while installation:', { 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
1 change: 0 additions & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const getFormat = () => {
const jsonLogging = process.env.OVM_ENABLE_LOG_JSON === 'true'
const enableTimestamp = process.env.OVM_ENABLE_LOG_TIMESTAMP === 'true'
return format.combine(
format.colorize(),
...(enableTimestamp ? [format.timestamp()] : []),
jsonLogging
? (format.json(), format.prettyPrint())
Expand Down
51 changes: 51 additions & 0 deletions src/utils/testing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { exec, ExecException } from 'child_process'
import { rm } from 'fs/promises'
import { platform, tmpdir } from 'os'
import path from 'path'
import { OVM_CONFIG_FILENAME } from './constants'

type CommandResult = {
stdout: string
stderr: string
}

export const runCommand = async (
command: string,
dev = false,
): Promise<CommandResult | (ExecException | null)> => {
return new Promise((resolve, reject) => {
const detectedPlatform = platform()
const runnerExt = detectedPlatform === 'win32' ? 'cmd' : 'js'
const runnerType = dev ? 'dev' : 'run'
const runnerFilePath = `${runnerType}.${runnerExt}`
const formattedCommand =
detectedPlatform === 'win32'
? path.win32.normalize(
path.join(
__dirname,
'..',
'..',
`bin/${runnerFilePath} ${command}`,
),
)
: `./bin/${runnerFilePath} ${command}`
exec(formattedCommand, (error, stdout, stderr) => {
if (error) {
reject(error)
}
resolve({ stdout, stderr })
})
})
}

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

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

export const destroyConfigMockFile = async (path: string) => {
return await rm(path, { force: true })
}

0 comments on commit 32e0a22

Please sign in to comment.