-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·61 lines (57 loc) · 2.14 KB
/
cli.js
File metadata and controls
executable file
·61 lines (57 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
import fs from 'fs/promises'
import packageJson from '../package.json' with { type: 'json' }
import { chat } from './chat.js'
import { serve } from './serve.js'
const updateCheck = checkForUpdates()
const arg = process.argv[2]
if (arg === 'chat') {
await updateCheck // wait for update check to finish before chat
chat()
} else if (arg === '--help' || arg === '-H' || arg === '-h') {
console.log('Usage:')
console.log(' hyperparam [path] start hyperparam webapp. "path" is a directory or a URL.')
console.log(' defaults to the current directory.')
console.log(' hyperparam chat start chat client')
console.log(' ')
console.log(' hyperparam -h, --help, give this help list')
console.log(' hyperparam -v, --version print program version')
} else if (arg === '--version' || arg === '-V' || arg === '-v') {
console.log(packageJson.version)
} else if (!arg) {
serve(process.cwd(), undefined) // current directory
} else if (/^https?:\/\//.exec(arg)) {
serve(undefined, arg) // url
} else {
// resolve file or directory
fs.stat(arg).then(async stat => {
const path = await fs.realpath(arg)
if (stat.isDirectory()) {
serve(path, undefined)
} else if (stat.isFile()) {
const parent = path.split('/').slice(0, -1).join('/')
const key = path.split('/').pop()
serve(parent, key)
}
}).catch(() => {
console.error(`Error: file ${process.argv[2]} does not exist`)
process.exit(1)
})
}
/**
* Check for updates and notify user if a newer version is available.
* Runs in the background.
* @returns {Promise<void>}
*/
function checkForUpdates() {
const currentVersion = packageJson.version
return fetch('https://registry.npmjs.org/hyperparam/latest')
.then(response => response.json())
.then(data => {
const latestVersion = data.version
if (latestVersion && latestVersion !== currentVersion) {
console.log(`\x1b[33mA newer version of hyperparam is available: ${latestVersion} (current: ${currentVersion})\x1b[0m`)
console.log('\x1b[33mRun \'npm install -g hyperparam\' to update\x1b[0m')
}
})
}