Skip to content

Commit a74b70a

Browse files
CLI: add upgrade command
1 parent b578945 commit a74b70a

File tree

3 files changed

+117
-4
lines changed

3 files changed

+117
-4
lines changed

deno.lock

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "incognito",
3-
"version": "1.1.7",
3+
"version": "1.1.6",
44
"private": true,
55
"type": "module",
66
"dependencies": {

server/cli.ts

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { parseArgs } from "jsr:@std/cli";
77
import { startServer as HonoServer } from './standalone/standalone.ts';
88
import { startServer as FastifyServer } from './full/server.ts';
99
import { fromFileUrl } from 'jsr:@std/path';
10+
import ora, { Ora } from 'npm:ora';
1011

1112
import packageJSON from "../package.json" with { type: 'json' };
1213
const { version } = packageJSON;
@@ -22,7 +23,9 @@ interface StartArgs extends CLIArgs {
2223
seo?: boolean;
2324
};
2425

25-
interface UpgradeArgs extends CLIArgs {};
26+
interface UpgradeArgs extends CLIArgs {
27+
check: boolean;
28+
};
2629

2730
interface DescriptionType {
2831
name: string;
@@ -73,8 +76,59 @@ if (args._.length !== 0 && args._[0] !== "upgrade") {
7376
}
7477

7578
if (args._[0] === "upgrade") {
76-
const upgradeCLI = new CLI<UpgradeArgs>({ booleans: [], strings: [] });
77-
await upgradeCLI.start(() => {});
79+
const upgradeCLI = new CLI<UpgradeArgs>({ booleans: ["check"], strings: [] });
80+
const uArgs = upgradeCLI.getArgs();
81+
82+
const checkForVersion = async (checkFlag: boolean) => {
83+
const res = await fetch("https://raw.githubusercontent.com/titaniumnetwork-dev/Incognito/refs/heads/main/package.json");
84+
const resp = await res.json();
85+
if (resp.version !== version) {
86+
checkFlag ? console.log(chalk.greenBright.bold(`A new version is available! Re-run this command without --check to upgrade!`)): '';
87+
return resp.version;
88+
}
89+
else {
90+
console.log(chalk.redBright.bold('A new version is not available at this moment, check back later'));
91+
Deno.exit();
92+
}
93+
}
94+
95+
if (uArgs.help) {
96+
upgradeCLI.help([
97+
{ name: 'check', description: 'Check for a new version' }
98+
]);
99+
}
100+
101+
if (uArgs.check) {
102+
await checkForVersion(true);
103+
Deno.exit();
104+
}
105+
106+
const download = async (version: string, spinner: Ora) => {
107+
const os = Deno.build.os;
108+
const arch = Deno.build.arch;
109+
if (os === "aix" || os === "netbsd" || os === "android" || os === "freebsd" || os === "solaris" || os === "illumos") {
110+
spinner.fail('Your OS is NOT supported! Exiting...');
111+
Deno.exit();
112+
}
113+
if (os === "darwin") {
114+
const res = await fetch(`https://github.com/titaniumnetwork-dev/incognito/releases/download/v${version}/incognito-MacOS${arch === "aarch64" ? '-arm': ''}`);
115+
const resp = await res.bytes();
116+
await Deno.writeFile(`incognito-MacOS${arch === "aarch64" ? '-arm': ''}`, resp, { mode: 0o775 });
117+
}
118+
else {
119+
const res = await fetch(`https://github.com/titaniumnetwork-dev/incognito/releases/download/v${version}/incognito-${os}${arch === "aarch64" ? '-arm': ''}`);
120+
const resp = await res.bytes();
121+
await Deno.writeFile(`incognito-${os}${arch === "aarch64" ? '-arm': ''}`, resp, { mode: 0o775 });
122+
}
123+
};
124+
125+
await upgradeCLI.start(async () => {
126+
const version = await checkForVersion(false);
127+
const spinner = ora('Downloading...').start();
128+
await download(version, spinner);
129+
spinner.succeed('Upgraded!');
130+
});
131+
78132
Deno.exit();
79133
}
80134

0 commit comments

Comments
 (0)