Commit b99d8788 authored by h3remi7's avatar h3remi7

add tar install

parent 00440da2
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
<button type="button" class="btn btn-secondary">正版代购</button> <button type="button" class="btn btn-secondary">正版代购</button>
</div> </div>
<div *ngIf="appsService.getDownloadInfo(routingService.app)"> <div *ngIf="appsService.getDownloadInfo(routingService.app)">
<div *ngIf="appsService.getDownloadInfo(routingService.app).status === 'install'">正在安装...</div>
<div *ngIf="appsService.getDownloadInfo(routingService.app).status === 'wait'">等待安装...</div>
<progress *ngIf="appsService.getDownloadInfo(routingService.app).status === 'active'" <progress *ngIf="appsService.getDownloadInfo(routingService.app).status === 'active'"
class="progress progress-striped progress-animated" class="progress progress-striped progress-animated"
value="{{appsService.getDownloadInfo(routingService.app).progress}}" max="100"> value="{{appsService.getDownloadInfo(routingService.app).progress}}" max="100">
...@@ -67,6 +69,8 @@ ...@@ -67,6 +69,8 @@
<td *ngIf="!checkInstall(mod.id)"> <td *ngIf="!checkInstall(mod.id)">
<button *ngIf="!appsService.getDownloadInfo(mod.id)" (click)="install(mod.id)" type="button" class="btn btn-primary btn-sm">安装</button> <button *ngIf="!appsService.getDownloadInfo(mod.id)" (click)="install(mod.id)" type="button" class="btn btn-primary btn-sm">安装</button>
<progress *ngIf="appsService.getDownloadInfo(mod.id) && appsService.getDownloadInfo(mod.id).status === 'active'" class="progress progress-striped progress-animated" value="{{appsService.getDownloadInfo(mod.id).progress}}" max="100"></progress> <progress *ngIf="appsService.getDownloadInfo(mod.id) && appsService.getDownloadInfo(mod.id).status === 'active'" class="progress progress-striped progress-animated" value="{{appsService.getDownloadInfo(mod.id).progress}}" max="100"></progress>
<div *ngIf="appsService.getDownloadInfo(mod.id) && appsService.getDownloadInfo(mod.id).status === 'wait'">等待安装...</div>
<div *ngIf="appsService.getDownloadInfo(mod.id) && appsService.getDownloadInfo(mod.id).status === 'install'">正在安装...</div>
</td> </td>
</tr> </tr>
</tbody> </tbody>
...@@ -77,7 +81,9 @@ ...@@ -77,7 +81,9 @@
<pre class="debug"> <pre class="debug">
{{appsService.downloadsInfo | json}} {{appsService.downloadsInfo | json}}
{{appsService.installConfig | json}} {{appsService.installConfig | json}}
{{appsService.tarQueue | json}}
</pre> </pre>
<button (click)="appsService.doTar()" >DOTAR</button>
<!-- Modal --> <!-- Modal -->
<div class="modal fade" id="install-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" <div class="modal fade" id="install-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
......
...@@ -4,6 +4,7 @@ import { RoutingService } from './routing.service' ...@@ -4,6 +4,7 @@ import { RoutingService } from './routing.service'
import {App} from "./app"; import {App} from "./app";
declare var process; declare var process;
declare var $;
@Component({ @Component({
selector: 'app-detail', selector: 'app-detail',
...@@ -124,8 +125,7 @@ export class AppDetailComponent { ...@@ -124,8 +125,7 @@ export class AppDetailComponent {
} }
} }
//TODO tar file $("#install-modal").modal("hide");
} }
......
...@@ -4,4 +4,5 @@ ...@@ -4,4 +4,5 @@
export class AppLocal { export class AppLocal {
path: string; path: string;
version: string; version: string;
files: string[];
} }
...@@ -31,11 +31,16 @@ export class AppsService { ...@@ -31,11 +31,16 @@ export class AppsService {
} }
os = window['System']._nodeRequire('os');
fs = window['System']._nodeRequire('fs'); fs = window['System']._nodeRequire('fs');
path = window['System']._nodeRequire('path'); path = window['System']._nodeRequire('path');
mkdirp = window['System']._nodeRequire('mkdirp'); mkdirp = window['System']._nodeRequire('mkdirp');
electron = window['System']._nodeRequire('electron'); electron = window['System']._nodeRequire('electron');
Aria2 = window['System']._nodeRequire('aria2'); Aria2 = window['System']._nodeRequire('aria2');
spawn = window['System']._nodeRequire('child_process');
execFile = window['System']._nodeRequire('child_process').execFile;
//localStorage = window['localStorage'];
data: App[]; data: App[];
...@@ -70,6 +75,12 @@ export class AppsService { ...@@ -70,6 +75,12 @@ export class AppsService {
} else { } else {
this.downloadsInfo[index].status = "wait"; this.downloadsInfo[index].status = "wait";
let tarObj = {
id: this.downloadsInfo[index].id,
xzFile: res.files[0].path,
installDir: this.installConfig.installDir
};
this.tarPush(tarObj);
} }
} else { } else {
console.log("cannot found download info!"); console.log("cannot found download info!");
...@@ -115,7 +126,23 @@ export class AppsService { ...@@ -115,7 +126,23 @@ export class AppsService {
getApps(callback) { getApps(callback) {
this.http.get('./apps.json') this.http.get('./apps.json')
.map(response => { .map(response => {
return response.json() let apps = response.json();
let localAppData = JSON.parse(localStorage.getItem("localAppData"));
console.log("app:",apps);
console.log("store:",localAppData);
apps = apps.map((app)=>{
if(localAppData) {
localAppData.map((v)=>{
if(v.id == app.id) {
app.local = v.local;
}
});
}
return app;
});
return apps;
}) })
.subscribe((data) => { .subscribe((data) => {
this.data = data; this.data = data;
...@@ -140,8 +167,6 @@ export class AppsService { ...@@ -140,8 +167,6 @@ export class AppsService {
download(id, uri) { download(id, uri) {
//console.log(id); //console.log(id);
//console.log(uri); //console.log(uri);
let tmp_gid;
let i = this.downloadsInfo.findIndex((v)=>{return v.id == id});
//console.log(i); //console.log(i);
if(this.downloadsInfo.findIndex((v)=>{return v.id == id}) !== -1) { if(this.downloadsInfo.findIndex((v)=>{return v.id == id}) !== -1) {
console.log("this app downloading") console.log("this app downloading")
...@@ -182,7 +207,7 @@ export class AppsService { ...@@ -182,7 +207,7 @@ export class AppsService {
} }
let tmp = { let tmp = {
installDir: __dirname, installDir: this.path.join(this.electron.remote.app.getPath('appData'), 'mycard'),
shortcut: { shortcut: {
desktop: false, desktop: false,
application: false application: false
...@@ -190,11 +215,132 @@ export class AppsService { ...@@ -190,11 +215,132 @@ export class AppsService {
mods: mods mods: mods
}; };
//console.log(tmp); //console.log(tmp);
this.installConfig = tmp; this.installConfig = tmp;
return tmp; return tmp;
} }
// tar
tarQueue = [];
isExtracting = false;
tarPush(tarObj) {
this.tarQueue.push(tarObj);
if(this.tarQueue.length > 0 && !this.isExtracting) {
this.doTar();
}
}
doTar() {
let tarPath;
switch (process.platform) {
case 'win32':
tarPath = this.path.join(process.execPath, '..', '..', 'tar.exe');
break;
case 'darwin':
tarPath = 'bsdtar'; // for debug
break;
default:
throw 'unsupported platform';
}
let opt = {
};
let tarObj;
if(this.tarQueue.length > 0) {
tarObj = this.tarQueue[0];
} else {
console.log("Empty Queue!");
return;
}
this.isExtracting = true;
console.log("Start tar " + tarObj.id);
let downLoadsInfoIndex = this.downloadsInfo.findIndex((v)=>{return v.id == tarObj.id});
if(downLoadsInfoIndex !== -1) {
this.downloadsInfo[downLoadsInfoIndex].status = "install";
} else {
console.log("cannot found download info!");
}
let xzFile = tarObj.xzFile;
let installDir = this.path.join(tarObj.installDir, tarObj.id);
if (!this.fs.existsSync(installDir)) {
console.log('app dir not exists');
this.mkdirp(installDir, (err)=> {
if (err) {
console.error(err)
} else {
console.log('create app dir');
}
});
}
let tar = this.execFile(tarPath, ['xvf', xzFile, '-C', installDir], opt, (err, stdout, stderr)=>{
if(err) {
throw err;
}
let re = /^x\s(.*)/;
let logArr = stderr.toString().trim().split(this.os.EOL);
logArr = logArr.map((v)=>{
if(v.match(re)) {
return v.match(re)[1];
} else {
console.log("no match");
return v;
}
});
let appLocal = {
id: tarObj.id,
local: {
path: installDir,
version: "0.1",
files: logArr
}
};
let localAppData = JSON.parse(localStorage.getItem("localAppData"));
if(!localAppData || !Array.isArray(localAppData)) {
localAppData = [];
}
let index = localAppData.findIndex((v)=>{
return v.id == tarObj.id;
});
if(index === -1) {
localAppData.push(appLocal);
} else {
localAppData[index] = appLocal;
}
localStorage.setItem("localAppData", JSON.stringify(localAppData));
let tmp = this.tarQueue.shift();
this.isExtracting = false;
this.downloadsInfo[downLoadsInfoIndex].status = "complete";
this.data = this.data.map((app)=>{
if(app.id == tarObj.id) {
app.local = appLocal.local;
}
return app;
});
console.log(tmp);
console.log("this app complete!");
console.log(localAppData);
this.doTar();
});
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment