heartcode 0.1.37 → 0.1.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/update.js ADDED
@@ -0,0 +1,59 @@
1
+ // A newer HeartCode: asked of npm at most once a day (in the background, never waited on), shown in
2
+ // the status bar, installed with /update the way HeartCode was installed. A git checkout (npm link)
3
+ // isn't npm-installed over: it's told to git pull.
4
+ import fs from 'node:fs'
5
+ import path from 'node:path'
6
+ import { fileURLToPath } from 'node:url'
7
+ import { HOME, VERSION, readJson, writeJson } from './config.js'
8
+
9
+ const FILE = path.join(HOME, 'update-check.json')
10
+ const DAY = 24 * 60 * 60 * 1000
11
+ const PKG = fileURLToPath(new URL('..', import.meta.url))
12
+
13
+ // "0.1.38" > "0.1.37": numbers compared part by part (a pre-release tag isn't offered)
14
+ export const newer = (a, b) => {
15
+ const [x, y] = [a, b].map((v) => String(v).split('-')[0].split('.').map(Number))
16
+ for (let i = 0; i < 3; i++) if ((x[i] || 0) !== (y[i] || 0)) return (x[i] || 0) > (y[i] || 0)
17
+ return false
18
+ }
19
+
20
+ // { current, latest, newer }, or null when npm can't be reached. force: ask now, not from the day's answer.
21
+ export async function checkUpdate({ force = false } = {}) {
22
+ const was = readJson(FILE, {})
23
+ let latest = was.latest
24
+ if (force || !latest || Date.now() - (was.checked ?? 0) > DAY) {
25
+ try {
26
+ const r = await fetch('https://registry.npmjs.org/heartcode/latest', { signal: AbortSignal.timeout(8000) })
27
+ latest = (await r.json()).version
28
+ writeJson(FILE, { checked: Date.now(), latest })
29
+ } catch {
30
+ if (!latest) return null
31
+ }
32
+ }
33
+ return { current: VERSION, latest, newer: Boolean(latest) && newer(latest, VERSION) }
34
+ }
35
+
36
+ // How HeartCode was installed: 'git' (a checkout, linked), or the package manager that put it there.
37
+ export function installKind() {
38
+ if (fs.existsSync(path.join(PKG, '.git')) || !PKG.split(path.sep).includes('node_modules')) return 'git'
39
+ if (/[\\/]pnpm[\\/]/.test(PKG)) return 'pnpm'
40
+ if (/[\\/]\.bun[\\/]/.test(PKG)) return 'bun'
41
+ if (/[\\/]yarn[\\/]/.test(PKG)) return 'yarn'
42
+ return 'npm'
43
+ }
44
+
45
+ export const UPDATE_COMMANDS = { npm: 'npm install -g heartcode@latest', pnpm: 'pnpm add -g heartcode@latest', yarn: 'yarn global add heartcode@latest', bun: 'bun add -g heartcode@latest' }
46
+ export const packageDir = PKG
47
+
48
+ // The changelog's sections from the newest down to (not including) the one running, or ''.
49
+ export async function whatsNew(current, latest) {
50
+ try {
51
+ const r = await fetch('https://raw.githubusercontent.com/p32929/HeartCode/main/CHANGELOG.md', { signal: AbortSignal.timeout(8000) })
52
+ const text = await r.text()
53
+ const start = text.indexOf(`## ${latest}`)
54
+ const end = text.indexOf(`## ${current}`)
55
+ return start >= 0 ? text.slice(start, end > start ? end : undefined).trim().replace(/\n {2,}(?=\S)/g, ' ') : '' // a list item's wrapped lines joined: the box wraps it to its width
56
+ } catch {
57
+ return ''
58
+ }
59
+ }