11import { createWriteStream , existsSync , rmSync } from 'fs' ;
2- import { resolve , delimiter , join } from 'path' ;
2+ import { delimiter , join } from 'path' ;
33import { HttpService } from '@nestjs/axios' ;
44import { Presets , SingleBar } from 'cli-progress' ;
55import decompress from 'decompress' ;
66import { exit } from 'node:process' ;
77import { InitOptions } from '../types/init-options.interface' ;
88import { Injectable } from '@nestjs/common' ;
99import { firstValueFrom } from 'rxjs' ;
10+ import { FileManagerService } from '@/file-manager/file-manager.service' ;
1011
1112@Injectable ( )
1213export class InitCliUsecases {
1314 CORTEX_RELEASES_URL = 'https://api.github.com/repos/janhq/cortex/releases' ;
1415 CUDA_DOWNLOAD_URL =
1516 'https://catalog.jan.ai/dist/cuda-dependencies/<version>/<platform>/cuda.tar.gz' ;
1617
17- constructor ( private readonly httpService : HttpService ) { }
18+ constructor (
19+ private readonly httpService : HttpService ,
20+ private readonly fileManagerService : FileManagerService ,
21+ ) { }
1822
1923 installEngine = async (
2024 engineFileName : string ,
@@ -53,7 +57,8 @@ export class InitCliUsecases {
5357 }
5458
5559 console . log ( `Downloading engine file ${ engineFileName } ` ) ;
56- const engineDir = resolve ( this . rootDir ( ) , 'cortex-cpp' ) ;
60+ const dataFolderPath = await this . fileManagerService . getDataFolderPath ( ) ;
61+ const engineDir = join ( dataFolderPath , 'cortex-cpp' ) ;
5762 if ( existsSync ( engineDir ) ) rmSync ( engineDir , { recursive : true } ) ;
5863
5964 const download = await firstValueFrom (
@@ -66,7 +71,7 @@ export class InitCliUsecases {
6671 process . exit ( 1 ) ;
6772 }
6873
69- const destination = resolve ( this . rootDir ( ) , toDownloadAsset . name ) ;
74+ const destination = join ( dataFolderPath , toDownloadAsset . name ) ;
7075
7176 await new Promise ( ( resolve , reject ) => {
7277 const writer = createWriteStream ( destination ) ;
@@ -95,10 +100,7 @@ export class InitCliUsecases {
95100 } ) ;
96101
97102 try {
98- await decompress (
99- resolve ( this . rootDir ( ) , destination ) ,
100- resolve ( this . rootDir ( ) ) ,
101- ) ;
103+ await decompress ( destination , join ( dataFolderPath ) ) ;
102104 } catch ( e ) {
103105 console . error ( 'Error decompressing file' , e ) ;
104106 exit ( 1 ) ;
@@ -124,8 +126,6 @@ export class InitCliUsecases {
124126 return `${ engineName } .tar.gz` ;
125127 } ;
126128
127- rootDir = ( ) => resolve ( __dirname , `../../../../` ) ;
128-
129129 cudaVersion = async ( ) => {
130130 let filesCuda12 : string [ ] ;
131131 let filesCuda11 : string [ ] ;
@@ -178,11 +178,12 @@ export class InitCliUsecases {
178178 installCudaToolkitDependency = async ( options : InitOptions ) => {
179179 const platform = process . platform === 'win32' ? 'windows' : 'linux' ;
180180
181+ const dataFolderPath = await this . fileManagerService . getDataFolderPath ( ) ;
181182 const url = this . CUDA_DOWNLOAD_URL . replace (
182183 '<version>' ,
183184 options . cudaVersion === '11' ? '11.7' : '12.0' ,
184185 ) . replace ( '<platform>' , platform ) ;
185- const destination = resolve ( this . rootDir ( ) , 'cuda-toolkit.tar.gz' ) ;
186+ const destination = join ( dataFolderPath , 'cuda-toolkit.tar.gz' ) ;
186187
187188 const download = await firstValueFrom (
188189 this . httpService . get ( url , {
@@ -222,10 +223,7 @@ export class InitCliUsecases {
222223 } ) ;
223224
224225 try {
225- await decompress (
226- resolve ( this . rootDir ( ) , destination ) ,
227- resolve ( this . rootDir ( ) , 'cortex-cpp' ) ,
228- ) ;
226+ await decompress ( destination , join ( dataFolderPath , 'cortex-cpp' ) ) ;
229227 } catch ( e ) {
230228 console . log ( e ) ;
231229 exit ( 1 ) ;
0 commit comments