Commit b99d8788 authored by h3remi7's avatar h3remi7

add tar install

parent 00440da2
......@@ -7,6 +7,8 @@
<button type="button" class="btn btn-secondary">正版代购</button>
</div>
<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'"
class="progress progress-striped progress-animated"
value="{{appsService.getDownloadInfo(routingService.app).progress}}" max="100">
......@@ -67,6 +69,8 @@
<td *ngIf="!checkInstall(mod.id)">
<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>
<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>
</tr>
</tbody>
......@@ -77,7 +81,9 @@
<pre class="debug">
{{appsService.downloadsInfo | json}}
{{appsService.installConfig | json}}
{{appsService.tarQueue | json}}
</pre>
<button (click)="appsService.doTar()" >DOTAR</button>
<!-- Modal -->
<div class="modal fade" id="install-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
......
......@@ -4,6 +4,7 @@ import { RoutingService } from './routing.service'
import {App} from "./app";
declare var process;
declare var $;
@Component({
selector: 'app-detail',
......@@ -124,8 +125,7 @@ export class AppDetailComponent {
}
}
//TODO tar file
$("#install-modal").modal("hide");
}
......
......@@ -4,4 +4,5 @@
export class AppLocal {
path: string;
version: string;
files: string[];
}
......@@ -31,11 +31,16 @@ export class AppsService {
}
os = window['System']._nodeRequire('os');
fs = window['System']._nodeRequire('fs');
path = window['System']._nodeRequire('path');
mkdirp = window['System']._nodeRequire('mkdirp');
electron = window['System']._nodeRequire('electron');
Aria2 = window['System']._nodeRequire('aria2');
spawn = window['System']._nodeRequire('child_process');
execFile = window['System']._nodeRequire('child_process').execFile;
//localStorage = window['localStorage'];
data: App[];
......@@ -70,6 +75,12 @@ export class AppsService {
} else {
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 {
console.log("cannot found download info!");
......@@ -115,7 +126,23 @@ export class AppsService {
getApps(callback) {
this.http.get('./apps.json')
.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) => {
this.data = data;
......@@ -140,8 +167,6 @@ export class AppsService {
download(id, uri) {
//console.log(id);
//console.log(uri);
let tmp_gid;
let i = this.downloadsInfo.findIndex((v)=>{return v.id == id});
//console.log(i);
if(this.downloadsInfo.findIndex((v)=>{return v.id == id}) !== -1) {
console.log("this app downloading")
......@@ -182,7 +207,7 @@ export class AppsService {
}
let tmp = {
installDir: __dirname,
installDir: this.path.join(this.electron.remote.app.getPath('appData'), 'mycard'),
shortcut: {
desktop: false,
application: false
......@@ -190,11 +215,132 @@ export class AppsService {
mods: mods
};
//console.log(tmp);
this.installConfig = 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