Skip to content

Commit

Permalink
fix: merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
rpidanny committed Jul 3, 2024
2 parents 745c866 + 755032f commit a5c866f
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [1.28.0](https://github.com/rpidanny/darwin/compare/v1.27.0...v1.28.0) (2024-07-03)

### Features

* add check-update hook ([#71](https://github.com/rpidanny/darwin/issues/71)) ([8ad8e33](https://github.com/rpidanny/darwin/commit/8ad8e3356b2d615612d9407bc56f9aba47c1ff77))

## [1.27.0](https://github.com/rpidanny/darwin/compare/v1.26.1...v1.27.0) (2024-07-03)

### Features
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $ npm install -g @rpidanny/darwin
$ darwin COMMAND
running command...
$ darwin (--version)
@rpidanny/darwin/1.27.0 linux-x64 node-v20.15.0
@rpidanny/darwin/1.28.0 linux-x64 node-v20.15.0
$ darwin --help [COMMAND]
USAGE
$ darwin COMMAND
Expand Down
2 changes: 1 addition & 1 deletion docs/chat.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ EXAMPLES
$ darwin chat
```

_See code: [src/commands/chat/index.ts](https://github.com/rpidanny/darwin/blob/v1.27.0/src/commands/chat/index.ts)_
_See code: [src/commands/chat/index.ts](https://github.com/rpidanny/darwin/blob/v1.28.0/src/commands/chat/index.ts)_
4 changes: 2 additions & 2 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ EXAMPLES
$ darwin config get
```

_See code: [src/commands/config/get.ts](https://github.com/rpidanny/darwin/blob/v1.27.0/src/commands/config/get.ts)_
_See code: [src/commands/config/get.ts](https://github.com/rpidanny/darwin/blob/v1.28.0/src/commands/config/get.ts)_

## `darwin config set`

Expand All @@ -42,4 +42,4 @@ EXAMPLES
$ darwin config set
```

_See code: [src/commands/config/set.ts](https://github.com/rpidanny/darwin/blob/v1.27.0/src/commands/config/set.ts)_
_See code: [src/commands/config/set.ts](https://github.com/rpidanny/darwin/blob/v1.28.0/src/commands/config/set.ts)_
2 changes: 1 addition & 1 deletion docs/download.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ EXAMPLES
$ darwin download papers "crispr cas9" -o papers/ -c 100 --log-level debug
```

_See code: [src/commands/download/papers.ts](https://github.com/rpidanny/darwin/blob/v1.27.0/src/commands/download/papers.ts)_
_See code: [src/commands/download/papers.ts](https://github.com/rpidanny/darwin/blob/v1.28.0/src/commands/download/papers.ts)_
4 changes: 2 additions & 2 deletions docs/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ FLAG DESCRIPTIONS
Summaries are generated using LLM so make sure LLMs are configured by running `darwin config set`
```

_See code: [src/commands/search/accession.ts](https://github.com/rpidanny/darwin/blob/v1.27.0/src/commands/search/accession.ts)_
_See code: [src/commands/search/accession.ts](https://github.com/rpidanny/darwin/blob/v1.28.0/src/commands/search/accession.ts)_

## `darwin search papers KEYWORDS`

Expand Down Expand Up @@ -100,4 +100,4 @@ FLAG DESCRIPTIONS
Summaries are generated using LLM so make sure LLMs are configured by running `darwin config set`
```

_See code: [src/commands/search/papers.ts](https://github.com/rpidanny/darwin/blob/v1.27.0/src/commands/search/papers.ts)_
_See code: [src/commands/search/papers.ts](https://github.com/rpidanny/darwin/blob/v1.28.0/src/commands/search/papers.ts)_
6 changes: 3 additions & 3 deletions package-lock.json

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

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@rpidanny/darwin",
"description": "An elegant CLI wizard enhancing biotech research efficiency, with adaptable features for other domains, albeit with minor constraints.",
"version": "1.27.0",
"version": "1.28.0",
"author": "Abhishek <[email protected]>",
"bin": {
"darwin": "./bin/run.js"
Expand Down Expand Up @@ -61,6 +61,7 @@
"playwright": "^1.44.1",
"pretty-ms": "^9.0.0",
"reflect-metadata": "^0.2.2",
"semver": "^7.6.2",
"tough-cookie": "^4.1.4",
"typedi": "^0.10.0"
},
Expand Down Expand Up @@ -150,7 +151,8 @@
"hooks": {
"init": [
"./dist/hooks/init/analytics",
"./dist/hooks/init/banner"
"./dist/hooks/init/banner",
"./dist/hooks/init/check-update"
],
"postrun": "./dist/hooks/postrun/analytics"
},
Expand Down
86 changes: 86 additions & 0 deletions src/hooks/init/check-update.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { jest } from '@jest/globals'
import { Hook } from '@oclif/core'
import nock from 'nock'

import { getMockConfig } from '../../../test/fixtures/config.js'
import { getMockHooksContext } from '../../../test/fixtures/hooks.js'
import uiOutput from '../../utils/ui/output'
import checkUpdateHook from './check-update.js'

describe('Hooks - init:check-update', () => {
const latest = '2.1.0'
const current = '1.27.1'
const mockConfig = getMockConfig({ version: current })

let hookContext: Hook.Context

beforeEach(() => {
nock('https://registry.npmjs.org')
.get('/@rpidanny/darwin/latest')
.reply(200, { version: latest })

hookContext = getMockHooksContext({ config: mockConfig })

jest.spyOn(uiOutput, 'printUpdateBanner').mockImplementation(() => {})
})

afterEach(() => {
jest.resetAllMocks()
jest.clearAllMocks()
})

it('should call uiOutput.printUpdateBanner when current version is lower than latest version', async () => {
await checkUpdateHook.call(hookContext, {
id: 'some-command',
argv: [],
config: mockConfig,
context: hookContext,
})

expect(uiOutput.printUpdateBanner).toHaveBeenCalledTimes(1)
expect(uiOutput.printUpdateBanner).toHaveBeenCalledWith(latest, hookContext.log)
})

it('should not call uiOutput.printUpdateBanner when current version is the latest version', async () => {
const config = getMockConfig({ version: latest })
const context = getMockHooksContext({ config })

await checkUpdateHook.call(context, {
id: 'some-command',
argv: [],
config,
context,
})

expect(uiOutput.printUpdateBanner).not.toHaveBeenCalled()
})

it.each`
command
${'autocomplete:script'}
${'readme'}
`('should not call uiOutput.printUpdateBanner if command is $command', async ({ command }) => {
await checkUpdateHook.call(hookContext, {
id: command,
argv: [],
config: mockConfig,
context: hookContext,
})

expect(uiOutput.printUpdateBanner).not.toHaveBeenCalled()
})

it('should not throw an error if the request fails', async () => {
nock.cleanAll()
nock('https://registry.npmjs.org').get('/@rpidanny/darwin/latest').reply(500)

await expect(
checkUpdateHook.call(hookContext, {
id: 'some-command',
argv: [],
config: mockConfig,
context: hookContext,
}),
).resolves.not.toThrow()
})
})
28 changes: 28 additions & 0 deletions src/hooks/init/check-update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Hook } from '@oclif/core'
import got from 'got'
import semver from 'semver'

import ui from '../../utils/ui/output.js'

const hook: Hook<'init'> = async function (opts) {
if (opts.id && ['autocomplete:script', 'readme'].includes(opts.id)) return

try {
const { version: latestVersion } = await got(
'https://registry.npmjs.org/@rpidanny/darwin/latest',
{
responseType: 'json',
timeout: 1_000,
},
).json<{ version: string }>()

if (semver.gt(latestVersion, this.config.version)) {
ui.printUpdateBanner(latestVersion, this.log)
}
} catch (error) {
// Silence errors
return
}
}

export default hook
11 changes: 11 additions & 0 deletions src/utils/ui/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ function printBanner(version: string, log: (msg: string) => void): void {
)
}

function printUpdateBanner(version: string, log: (msg: string) => void): void {
log(
`${chalk.hex('#cf402f')(
`A new version (v${version}) of darwin is available. Please run ${chalk.bold(
'npm install -g @rpidanny/darwin@latest',
)} to update and enjoy the latest features!`,
)}`,
)
}

export default {
printBanner,
printUpdateBanner,
}
5 changes: 3 additions & 2 deletions test/fixtures/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ import path from 'path'

import { TConfig } from '../../src/config/schema.js'

export function getMockConfig(): Config {
export function getMockConfig(override?: Partial<Config>): Config {
const mockConfig = new Config({
root: process.cwd(),
ignoreManifest: true,
version: '1.2.3',
channel: 'stable',
...override,
})

mockConfig.configDir = path.join(process.cwd(), './test/data/configs')
mockConfig.dataDir = path.join(process.cwd(), './test/data')
mockConfig.version = '1.2.3'
mockConfig.version = override?.version ?? '1.2.3'

return mockConfig
}
Expand Down

0 comments on commit a5c866f

Please sign in to comment.