youzanyun-devtool-worker
Version:
- web - ws - proxy
134 lines (133 loc) • 5.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
const download_1 = tslib_1.__importDefault(require("download"));
const fs_1 = tslib_1.__importDefault(require("fs"));
const spring4js_nodejs_1 = require("spring4js-nodejs");
let DatabaseService = class DatabaseService {
constructor() {
this._MySQLShellOut = new spring4js_nodejs_1.Emitter();
this.onMySQLShellOut = this._MySQLShellOut.registerEvent;
this.appName = null;
}
async start() {
}
async excuteShell(relativePath, command, arg) {
try {
let installShell = await this.getShellDir(relativePath);
const result = await this.executeService.promiseSpawm(installShell, [], {}, {
report: (data) => {
this._MySQLShellOut.fire({
command,
data
});
}
});
return result == 0;
}
catch (error) {
return false;
}
}
async downloadMysql({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/mysql_image_download", "setMySQLShellOut");
}
async downloadRedis({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/redis_image_download", "setRedisShellOut");
}
async startMysql({ workbenchId, projectId }) {
try {
const { proName } = await this.projectService.getProjectById(projectId);
const { appDetail: { appName } } = await this.applicationService.getAppDetail(proName);
const startShell = await this.getShellDir("code-tpl/mysql_docker_start");
const shell = `${startShell} -n ${appName.replace(/-(\w)/g, ($0, $1) => $1.toUpperCase())}`;
const result = await this.executeService.promiseSpawm(shell, [], {}, {
report: (data) => {
this._MySQLShellOut.fire({
command: "setMySQLShellOut",
data
});
}
});
return result == 0;
}
catch (error) {
return false;
}
}
async stopMysql({ workbenchId, projectId }) {
try {
const startShell = await this.getShellDir("code-tpl/mysql_docker_start");
const shell = `${startShell} -s`;
const result = await this.executeService.promiseSpawm(shell, [], {}, {
report: (data) => {
this._MySQLShellOut.fire({
command: "setMySQLShellOut",
data
});
}
});
return result == 0;
}
catch (error) {
return false;
}
}
async startRedis({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/redis_docker_start", "setRedisShellOut");
}
async stopRedis({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/redis_docker_start", "setRedisShellOut", '-s');
}
async syncMysql({ workbenchId, projectId, type }) {
try {
const { proName } = await this.projectService.getProjectById(projectId);
const { appDetail: { appName } } = await this.applicationService.getAppDetail(proName);
const sqlFile = `https://baymax-qa.s.qima-inc.com/database/tables/${type}?appName=app-one`;
const sqlDir = path_1.default.resolve(this.configService.getSqlDir(), `${appName}.sql`);
download_1.default(sqlFile).pipe(fs_1.default.createWriteStream(sqlDir));
const syncShell = await this.getShellDir("code-tpl/mysql_docker_sync");
const shell = `sh ${syncShell} -n ${appName.replace(/-(\w)/g, ($0, $1) => $1.toUpperCase())} -f ${sqlDir}`;
const result = await this.executeService.promiseSpawm(shell, [], {}, {
report: (data) => {
this._MySQLShellOut.fire({
command: "setMySQLShellOut",
data
});
}
});
return result == 0;
}
catch (error) {
return false;
}
}
async getShellDir(relativePath) {
let shellDir;
if (process.platform === "darwin") {
shellDir = path_1.default.resolve(this.configService.getWorkerDir(), `${relativePath}.sh`);
await this.executeService.promiseExec(`chmod +x ${shellDir}`, {});
}
else {
shellDir = path_1.default.resolve(this.configService.getWorkerDir(), `${relativePath}.bat`);
}
return shellDir;
}
};
tslib_1.__decorate([
spring4js_nodejs_1.Resource()
], DatabaseService.prototype, "configService", void 0);
tslib_1.__decorate([
spring4js_nodejs_1.Resource()
], DatabaseService.prototype, "executeService", void 0);
tslib_1.__decorate([
spring4js_nodejs_1.Resource()
], DatabaseService.prototype, "projectService", void 0);
tslib_1.__decorate([
spring4js_nodejs_1.Resource()
], DatabaseService.prototype, "applicationService", void 0);
DatabaseService = tslib_1.__decorate([
spring4js_nodejs_1.Service()
], DatabaseService);
exports.default = DatabaseService;