Skip to content

Commit 55906be

Browse files
committed
feat: refactored and improve the new edit changes
1 parent 33b7e6b commit 55906be

3 files changed

Lines changed: 111 additions & 105 deletions

File tree

src/commands/edit.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ For more information, visit: https://codifycli.com/docs/commands/edit
2121

2222
await EditOrchestrator.run(rootCommand, this.reporter);
2323

24+
process.exit(0);
2425
}
2526
}

src/orchestrators/edit.ts

Lines changed: 2 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,12 @@
11
import open from 'open';
2-
import * as fs from 'node:fs/promises';
3-
import { tmpdir } from 'node:os';
4-
import os from 'node:os';
5-
import path from 'node:path';
62

73
import { DashboardApiClient } from '../api/dashboard/index.js';
84
import { config } from '../config.js';
95
import { LoginHelper } from '../connect/login-helper.js';
106
import { Reporter } from '../ui/reporters/reporter.js';
11-
import { OsUtils } from '../utils/os-utils.js';
12-
import { spawn } from '../utils/spawn.js';
7+
import { getDesktopAppPath, getDesktopDownloadUrl, installDesktopApp } from '../utils/desktop-installer.js';
138
import { ConnectOrchestrator } from './connect.js';
149

15-
const DESKTOP_APP_PATHS = {
16-
darwin: '/Applications/Codify.app',
17-
linux: '/usr/bin/codify',
18-
};
19-
20-
const DOWNLOAD_URLS: Record<string, Record<string, string>> = {
21-
darwin: {
22-
arm64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_aarch64.dmg',
23-
x64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_x64.dmg',
24-
},
25-
linux_deb: {
26-
arm64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_arm64.deb',
27-
x64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_amd64.deb',
28-
},
29-
linux_rpm: {
30-
aarch64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_aarch64.rpm',
31-
x64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_x86_64.rpm',
32-
},
33-
};
34-
35-
async function getDesktopAppPath(): Promise<string | null> {
36-
const appPath = OsUtils.isMacOS()
37-
? DESKTOP_APP_PATHS.darwin
38-
: OsUtils.isLinux()
39-
? DESKTOP_APP_PATHS.linux
40-
: null;
41-
42-
if (!appPath) return null;
43-
44-
try {
45-
await fs.access(appPath);
46-
return appPath;
47-
} catch {
48-
return null;
49-
}
50-
}
51-
52-
function getDownloadUrl(): { url: string; platform: 'darwin' | 'linux_deb' | 'linux_rpm' } | null {
53-
const arch = os.arch();
54-
55-
if (OsUtils.isMacOS()) {
56-
return {
57-
url: DOWNLOAD_URLS.darwin[arch] ?? DOWNLOAD_URLS.darwin['x64'],
58-
platform: 'darwin',
59-
};
60-
}
61-
62-
if (OsUtils.isLinux()) {
63-
const platform = OsUtils.isDebianBased() ? 'linux_deb' : 'linux_rpm';
64-
return {
65-
url: DOWNLOAD_URLS[platform][arch] ?? DOWNLOAD_URLS[platform]['x64'],
66-
platform,
67-
};
68-
}
69-
70-
return null;
71-
}
72-
73-
async function installDesktopApp(reporter: Reporter, url: string, platform: 'darwin' | 'linux_deb' | 'linux_rpm'): Promise<void> {
74-
const ext = url.split('.').pop()!;
75-
const tmpFile = path.join(tmpdir(), `codify-desktop.${ext}`);
76-
77-
console.log('Downloading Codify desktop app...');
78-
await spawn(`curl -L -o ${tmpFile} ${url}`);
79-
80-
if (platform === 'darwin') {
81-
const mountPoint = path.join(tmpdir(), 'codify-dmg-mount');
82-
try {
83-
console.log('Installing Codify desktop app...');
84-
await spawn(`hdiutil attach ${tmpFile} -mountpoint ${mountPoint} -nobrowse -quiet`);
85-
await spawn(`cp -R ${mountPoint}/Codify.app /Applications/Codify.app`);
86-
} finally {
87-
await spawn(`hdiutil detach ${mountPoint} -quiet`).catch(() => {});
88-
await fs.unlink(tmpFile).catch(() => {});
89-
}
90-
} else {
91-
const password = await reporter.promptSudo('codify-installer', {
92-
command: platform === 'linux_deb' ? `dpkg -i ${tmpFile}` : `rpm -i ${tmpFile}`,
93-
options: { requiresRoot: true },
94-
});
95-
96-
if (password == null) {
97-
console.log('Installation cancelled.');
98-
return;
99-
}
100-
101-
try {
102-
console.log('Installing Codify desktop app...');
103-
const cmd = platform === 'linux_deb' ? `dpkg -i ${tmpFile}` : `rpm -i ${tmpFile}`;
104-
await spawn(cmd, { requiresRoot: true }, undefined, password);
105-
} finally {
106-
await fs.unlink(tmpFile).catch(() => {});
107-
}
108-
}
109-
110-
console.log('Codify desktop app installed successfully.');
111-
}
112-
11310
export class EditOrchestrator {
11411

11512
static async run(rootCommand: string, reporter: Reporter) {
@@ -120,7 +17,7 @@ export class EditOrchestrator {
12017
return;
12118
}
12219

123-
const download = getDownloadUrl();
20+
const download = getDesktopDownloadUrl();
12421
if (download) {
12522
const shouldInstall = await reporter.promptConfirmation(
12623
'Codify desktop app is not installed. Would you like to download and install it?'

src/utils/desktop-installer.ts

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import * as fs from 'node:fs/promises';
2+
import { tmpdir } from 'node:os';
3+
import os from 'node:os';
4+
import path from 'node:path';
5+
6+
import { Reporter } from '../ui/reporters/reporter.js';
7+
import { OsUtils } from './os-utils.js';
8+
import { spawn } from './spawn.js';
9+
10+
const DESKTOP_APP_PATHS = {
11+
darwin: '/Applications/Codify.app',
12+
linux: '/usr/bin/codify',
13+
};
14+
15+
const DOWNLOAD_URLS: Record<string, Record<string, string>> = {
16+
darwin: {
17+
arm64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_aarch64.dmg',
18+
x64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_x64.dmg',
19+
},
20+
linux_deb: {
21+
arm64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_arm64.deb',
22+
x64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_amd64.deb',
23+
},
24+
linux_rpm: {
25+
aarch64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_aarch64.rpm',
26+
x64: 'https://releases-desktop.codifycli.com/channels/stable/Codify_x86_64.rpm',
27+
},
28+
};
29+
30+
export type DesktopPlatform = 'darwin' | 'linux_deb' | 'linux_rpm';
31+
32+
export async function getDesktopAppPath(): Promise<string | null> {
33+
const appPath = OsUtils.isMacOS()
34+
? DESKTOP_APP_PATHS.darwin
35+
: OsUtils.isLinux()
36+
? DESKTOP_APP_PATHS.linux
37+
: null;
38+
39+
if (!appPath) return null;
40+
41+
try {
42+
await fs.access(appPath);
43+
return appPath;
44+
} catch {
45+
return null;
46+
}
47+
}
48+
49+
export function getDesktopDownloadUrl(): { url: string; platform: DesktopPlatform } | null {
50+
const arch = os.arch();
51+
52+
if (OsUtils.isMacOS()) {
53+
return {
54+
url: DOWNLOAD_URLS.darwin[arch] ?? DOWNLOAD_URLS.darwin['x64'],
55+
platform: 'darwin',
56+
};
57+
}
58+
59+
if (OsUtils.isLinux()) {
60+
const platform = OsUtils.isDebianBased() ? 'linux_deb' : 'linux_rpm';
61+
return {
62+
url: DOWNLOAD_URLS[platform][arch] ?? DOWNLOAD_URLS[platform]['x64'],
63+
platform,
64+
};
65+
}
66+
67+
return null;
68+
}
69+
70+
export async function installDesktopApp(reporter: Reporter, url: string, platform: DesktopPlatform): Promise<void> {
71+
const ext = url.split('.').pop()!;
72+
const tmpFile = path.join(tmpdir(), `codify-desktop.${ext}`);
73+
74+
console.log('Downloading Codify desktop app...');
75+
await spawn(`curl -L -o ${tmpFile} ${url}`);
76+
77+
if (platform === 'darwin') {
78+
const mountPoint = path.join(tmpdir(), 'codify-dmg-mount');
79+
try {
80+
console.log('Installing Codify desktop app...');
81+
await spawn(`hdiutil attach ${tmpFile} -mountpoint ${mountPoint} -nobrowse -quiet`);
82+
await spawn(`cp -R ${mountPoint}/Codify.app /Applications/Codify.app`);
83+
} finally {
84+
await spawn(`hdiutil detach ${mountPoint} -quiet`).catch(() => {});
85+
await fs.unlink(tmpFile).catch(() => {});
86+
}
87+
} else {
88+
const password = await reporter.promptSudo('codify-installer', {
89+
command: platform === 'linux_deb' ? `dpkg -i ${tmpFile}` : `rpm -i ${tmpFile}`,
90+
options: { requiresRoot: true },
91+
});
92+
93+
if (password == null) {
94+
console.log('Installation cancelled.');
95+
return;
96+
}
97+
98+
try {
99+
console.log('Installing Codify desktop app...');
100+
const cmd = platform === 'linux_deb' ? `dpkg -i ${tmpFile}` : `rpm -i ${tmpFile}`;
101+
await spawn(cmd, { requiresRoot: true }, undefined, password);
102+
} finally {
103+
await fs.unlink(tmpFile).catch(() => {});
104+
}
105+
}
106+
107+
console.log('Codify desktop app installed successfully.');
108+
}

0 commit comments

Comments
 (0)