Skip to content

Commit a13ebd9

Browse files
committed
Extract update check utilities and refactor check for updates command
- for 'minimum wait time' to work in `apps/editor/src/commands/check-for-updates-command.ts`, we need to run the check independently in the command and only pass info to the frontend - move 'fetch update info' to `apps/editor/src/utils` - place is_newer in a separate folder-util, with necessary tests (keep them simple) └── apps └── editor └── src ├── commands │ ├── check-for-updates-command.ts │ └── install-specific-version-command.ts ├── constants │ └── values.ts ├── extension.ts ├── i18n │ └── translations │ └── commands │ └── check-for-updates-command.ts ├── utils │ ├── build-user-content.ts │ ├── changes-integration │ │ ├── diff-processor │ │ │ ├── diff-processor.spec.ts │ │ │ ├── diff-processor.ts │ │ │ ├── index.ts │ │ │ ├── test-cases │ │ │ │ ├── deletion-only │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── empty-file │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── empty-file-2 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── failing │ │ │ │ │ ├── diff.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── failing-2 │ │ │ │ │ ├── diff.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-1 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-2 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-3 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-4 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-5 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-6 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-7 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-8 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── generic-9 │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── insert-start-end │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── loose-gap-match │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── multiple-hunks │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── redundant-new-empty-line │ │ │ │ │ ├── diff.txt │ │ │ │ │ ├── expected.txt │ │ │ │ │ └── original.txt │ │ │ │ └── same-line-replace │ │ │ │ ├── diff.txt │ │ │ │ ├── expected.txt │ │ │ │ └── original.txt │ │ │ ├── types.ts │ │ │ └── utils │ │ │ ├── apply-blocks.ts │ │ │ ├── empty-file-handler.ts │ │ │ ├── match-blocks.ts │ │ │ ├── normalize.ts │ │ │ └── parse-blocks.ts │ │ ├── search-replace-processor │ │ │ ├── apply-search-replace-to-content │ │ │ │ ├── apply-search-replace-to-content.spec.ts │ │ │ │ ├── apply-search-replace-to-content.ts │ │ │ │ ├── index.ts │ │ │ │ └── test-cases │ │ │ │ ├── basic-conflict │ │ │ │ │ ├── expected.txt │ │ │ │ │ ├── markers.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── deletion │ │ │ │ │ ├── expected.txt │ │ │ │ │ ├── markers.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── multiple-conflicts │ │ │ │ │ ├── expected.txt │ │ │ │ │ ├── markers.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── rst-syntax │ │ │ │ │ ├── expected.txt │ │ │ │ │ ├── markers.txt │ │ │ │ │ └── original.txt │ │ │ │ ├── trailing-whitespace │ │ │ │ │ ├── expected.txt │ │ │ │ │ ├── markers.txt │ │ │ │ │ └── original.txt │ │ │ │ └── whitespace-issues │ │ │ │ ├── expected.txt │ │ │ │ ├── markers.txt │ │ │ │ └── original.txt │ │ │ ├── index.ts │ │ │ ├── parse-search-replace-segments.ts │ │ │ └── types.ts │ │ └── truncations-processor │ │ ├── index.ts │ │ ├── test-cases │ │ │ ├── basic │ │ │ │ ├── expected.txt │ │ │ │ ├── original.txt │ │ │ │ └── truncated.txt │ │ │ ├── edge-truncations │ │ │ │ ├── expected.txt │ │ │ │ ├── original.txt │ │ │ │ └── truncated.txt │ │ │ ├── multiple-truncations │ │ │ │ ├── expected.txt │ │ │ │ ├── original.txt │ │ │ │ └── truncated.txt │ │ │ └── update-after-truncation │ │ │ ├── expected.txt │ │ │ ├── original.txt │ │ │ └── truncated.txt │ │ ├── truncations-processor.spec.ts │ │ ├── truncations-processor.ts │ │ └── utils │ │ ├── find-line-sequence.ts │ │ └── is-truncation-line.ts │ ├── fetch-update-info.ts │ ├── is-newer │ │ ├── index.ts │ │ ├── is-newer.spec.ts │ │ └── is-newer.ts │ └── update-extension.ts └── views └── prompt ├── backend │ ├── message-handlers │ │ └── handle-update-extension.ts │ ├── prompt-view-provider.ts │ └── utils │ └── check-for-updates.ts ├── frontend │ ├── Home │ │ └── Home.tsx │ ├── Prompt.tsx │ └── hooks │ └── use-prompt │ └── use-prompt.ts └── types └── messages.ts
1 parent eeeb0ed commit a13ebd9

6 files changed

Lines changed: 165 additions & 114 deletions

File tree

‎apps/editor/src/commands/check-for-updates-command.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { t } from '../i18n'
33
import { PromptViewProvider } from '../views/prompt/backend/prompt-view-provider'
44
import { update_extension } from '../utils/update-extension'
55
import { MINIMUM_WAIT_TIME } from '../constants/values'
6+
import { fetch_update_info } from '../utils/fetch-update-info'
7+
import * as state_keys from '../constants/state-keys'
68

79
export const check_for_updates_command = (
810
prompt_view_provider: PromptViewProvider
@@ -20,14 +22,35 @@ export const check_for_updates_command = (
2022
},
2123
async () => {
2224
const start_time = Date.now()
23-
await prompt_view_provider.check_for_updates({ force: true })
25+
const current_version =
26+
prompt_view_provider.extension_context.extension.packageJSON.version
27+
28+
if (!current_version) return
29+
30+
const update_info = await fetch_update_info(current_version, 0)
31+
32+
prompt_view_provider.extension_context.globalState.update(
33+
state_keys.LAST_UPDATE_CHECK_TIMESTAMP_STATE_KEY,
34+
Date.now()
35+
)
36+
prompt_view_provider.extension_context.globalState.update(
37+
state_keys.CACHED_UPDATE_INFO_STATE_KEY,
38+
update_info
39+
)
40+
2441
const elapsed_time = Date.now() - start_time
2542

2643
if (elapsed_time < MINIMUM_WAIT_TIME) {
2744
await new Promise((resolve) =>
2845
setTimeout(resolve, MINIMUM_WAIT_TIME - elapsed_time)
2946
)
3047
}
48+
49+
prompt_view_provider.update_info = update_info
50+
prompt_view_provider.send_message({
51+
command: 'UPDATE_INFO',
52+
update_info: prompt_view_provider.update_info
53+
})
3154
}
3255
)
3356

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import axios from 'axios'
2+
import { UpdateInfo } from '@/views/prompt/types/messages'
3+
import { is_newer } from './is-newer'
4+
5+
export const get_version_age_days = async (
6+
version: string,
7+
tag_name?: string
8+
): Promise<number> => {
9+
const parts = version.split('.')
10+
if (parts.length >= 2) {
11+
const year = parseInt(parts[0], 10)
12+
const mmdd = parts[1]
13+
if (!isNaN(year) && mmdd.length === 4) {
14+
const month = parseInt(mmdd.substring(0, 2), 10) - 1
15+
const day = parseInt(mmdd.substring(2, 4), 10)
16+
if (!isNaN(month) && !isNaN(day)) {
17+
const version_date = Date.UTC(year, month, day)
18+
const age_days = Math.floor(
19+
(Date.now() - version_date) / (1000 * 60 * 60 * 24)
20+
)
21+
return age_days >= 0 ? age_days : 0
22+
}
23+
}
24+
}
25+
26+
if (tag_name) {
27+
try {
28+
const res = await axios.get(
29+
`https://api.github.com/repos/CodeWebChat/CWC/commits/${tag_name}`,
30+
{ validateStatus: () => true }
31+
)
32+
if (res.status === 200) {
33+
const date = res.data.commit?.committer?.date
34+
if (date) {
35+
const age_days = Math.floor(
36+
(Date.now() - new Date(date).getTime()) / (1000 * 60 * 60 * 24)
37+
)
38+
return age_days >= 0 ? age_days : 0
39+
}
40+
}
41+
} catch (e) {
42+
console.error('Failed to fetch commit date:', e)
43+
}
44+
}
45+
46+
return Infinity
47+
}
48+
49+
export const fetch_update_info = async (
50+
current_version: string,
51+
minimum_release_age: number
52+
): Promise<UpdateInfo> => {
53+
if (!current_version) return { is_available: false }
54+
55+
try {
56+
let page = 1
57+
while (page <= 10) {
58+
const res = await axios.get(
59+
`https://api.github.com/repos/CodeWebChat/CWC/tags?page=${page}`,
60+
{ validateStatus: () => true }
61+
)
62+
if (res.status !== 200) break
63+
const tags = res.data
64+
if (!Array.isArray(tags) || tags.length === 0) break
65+
66+
for (const tag of tags) {
67+
if (tag.name?.endsWith('-vscode')) {
68+
const latest_version = tag.name.replace('-vscode', '')
69+
if (is_newer(latest_version, current_version)) {
70+
if (minimum_release_age > 0) {
71+
const tag_age_days = await get_version_age_days(
72+
latest_version,
73+
tag.name
74+
)
75+
if (tag_age_days < minimum_release_age) {
76+
continue
77+
}
78+
}
79+
80+
const current_version_age_days = await get_version_age_days(
81+
current_version,
82+
`${current_version}-vscode`
83+
)
84+
85+
return {
86+
is_available: true,
87+
latest_version,
88+
latest_version_tag: tag.name,
89+
current_version_age_days
90+
}
91+
} else {
92+
return { is_available: false }
93+
}
94+
}
95+
}
96+
page++
97+
}
98+
} catch (e) {
99+
console.error('Failed to check for updates:', e)
100+
}
101+
102+
return { is_available: false }
103+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './is-newer'
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { is_newer } from './is-newer'
2+
3+
describe('is_newer', () => {
4+
it('returns true when the first version is newer', () => {
5+
expect(is_newer('1.0.1', '1.0.0')).toBe(true)
6+
expect(is_newer('2.0.0', '1.9.9')).toBe(true)
7+
expect(is_newer('1.1.0', '1.0.9')).toBe(true)
8+
})
9+
10+
it('returns false when the first version is older', () => {
11+
expect(is_newer('1.0.0', '1.0.1')).toBe(false)
12+
expect(is_newer('0.9.9', '1.0.0')).toBe(false)
13+
})
14+
15+
it('returns false when versions are identical', () => {
16+
expect(is_newer('1.0.0', '1.0.0')).toBe(false)
17+
})
18+
19+
it('handles versions of different segment lengths', () => {
20+
expect(is_newer('1.0', '1.0.0')).toBe(false)
21+
expect(is_newer('1.0.1', '1.0')).toBe(true)
22+
expect(is_newer('2', '1.9.9')).toBe(true)
23+
})
24+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const is_newer = (latest: string, current: string) => {
2+
const l = latest.split('.').map(Number)
3+
const c = current.split('.').map(Number)
4+
for (let i = 0; i < Math.max(l.length, c.length); i++) {
5+
const l_val = l[i] || 0
6+
const c_val = c[i] || 0
7+
if (l_val > c_val) return true
8+
if (l_val < c_val) return false
9+
}
10+
return false
11+
}
Lines changed: 2 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,9 @@
11
import * as vscode from 'vscode'
2-
import axios from 'axios'
32
import * as state_keys from '@/constants/state-keys'
43
import { UpdateInfo } from '@/views/prompt/types/messages'
54
import { PromptViewProvider } from '../prompt-view-provider'
6-
7-
const is_newer = (latest: string, current: string) => {
8-
const l = latest.split('.').map(Number)
9-
const c = current.split('.').map(Number)
10-
for (let i = 0; i < Math.max(l.length, c.length); i++) {
11-
const l_val = l[i] || 0
12-
const c_val = c[i] || 0
13-
if (l_val > c_val) return true
14-
if (l_val < c_val) return false
15-
}
16-
return false
17-
}
18-
19-
const get_version_age_days = async (
20-
version: string,
21-
tag_name?: string
22-
): Promise<number> => {
23-
const parts = version.split('.')
24-
if (parts.length >= 2) {
25-
const year = parseInt(parts[0], 10)
26-
const mmdd = parts[1]
27-
if (!isNaN(year) && mmdd.length === 4) {
28-
const month = parseInt(mmdd.substring(0, 2), 10) - 1
29-
const day = parseInt(mmdd.substring(2, 4), 10)
30-
if (!isNaN(month) && !isNaN(day)) {
31-
const version_date = Date.UTC(year, month, day)
32-
const age_days = Math.floor(
33-
(Date.now() - version_date) / (1000 * 60 * 60 * 24)
34-
)
35-
return age_days >= 0 ? age_days : 0
36-
}
37-
}
38-
}
39-
40-
if (tag_name) {
41-
try {
42-
const res = await axios.get(
43-
`https://api.github.com/repos/CodeWebChat/CWC/commits/${tag_name}`,
44-
{ validateStatus: () => true }
45-
)
46-
if (res.status === 200) {
47-
const date = res.data.commit?.committer?.date
48-
if (date) {
49-
const age_days = Math.floor(
50-
(Date.now() - new Date(date).getTime()) / (1000 * 60 * 60 * 24)
51-
)
52-
return age_days >= 0 ? age_days : 0
53-
}
54-
}
55-
} catch (e) {
56-
console.error('Failed to fetch commit date:', e)
57-
}
58-
}
59-
60-
return Infinity
61-
}
5+
import { fetch_update_info } from '@/utils/fetch-update-info'
6+
import { is_newer } from '@/utils/is-newer'
627

638
export const check_for_updates = async (
649
provider: PromptViewProvider,
@@ -140,59 +85,3 @@ export const check_for_updates = async (
14085
update_info: provider.update_info
14186
})
14287
}
143-
144-
const fetch_update_info = async (
145-
current_version: string,
146-
minimum_release_age: number
147-
): Promise<UpdateInfo> => {
148-
if (!current_version) return { is_available: false }
149-
150-
try {
151-
let page = 1
152-
while (page <= 10) {
153-
const res = await axios.get(
154-
`https://api.github.com/repos/CodeWebChat/CWC/tags?page=${page}`,
155-
{ validateStatus: () => true }
156-
)
157-
if (res.status !== 200) break
158-
const tags = res.data
159-
if (!Array.isArray(tags) || tags.length === 0) break
160-
161-
for (const tag of tags) {
162-
if (tag.name?.endsWith('-vscode')) {
163-
const latest_version = tag.name.replace('-vscode', '')
164-
if (is_newer(latest_version, current_version)) {
165-
if (minimum_release_age > 0) {
166-
const tag_age_days = await get_version_age_days(
167-
latest_version,
168-
tag.name
169-
)
170-
if (tag_age_days < minimum_release_age) {
171-
continue
172-
}
173-
}
174-
175-
const current_version_age_days = await get_version_age_days(
176-
current_version,
177-
`${current_version}-vscode`
178-
)
179-
180-
return {
181-
is_available: true,
182-
latest_version,
183-
latest_version_tag: tag.name,
184-
current_version_age_days
185-
}
186-
} else {
187-
return { is_available: false }
188-
}
189-
}
190-
}
191-
page++
192-
}
193-
} catch (e) {
194-
console.error('Failed to check for updates:', e)
195-
}
196-
197-
return { is_available: false }
198-
}

0 commit comments

Comments
 (0)