youzanyun-devtool-worker
Version:
140 lines (139 loc) • 6.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path_1 = tslib_1.__importDefault(require("path"));
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 = [], projectId) {
try {
let installShell = await this.getShellDir(relativePath);
const result = await this.executeService.promiseSpawm(installShell, arg, {}, {
report: (data) => {
this.syncLogToFile(command, data, projectId, async (path) => {
const data = await fs_1.default.readFileSync(path).toString();
this._MySQLShellOut.fire({
command,
data,
});
});
}
});
return result == 0;
}
catch (error) {
return false;
}
}
async syncLogToFile(command, data, projectId, cb) {
const dataDir = this.configService.getDataDir();
let mpConfigFIle = path_1.default.resolve(dataDir, `${command}.${projectId}.log`);
let exists = await fs_1.default.existsSync(mpConfigFIle);
if (!exists) {
await fs_1.default.writeFileSync(mpConfigFIle, data);
}
else {
await fs_1.default.appendFileSync(mpConfigFIle, data);
}
cb && cb(mpConfigFIle);
}
async clearLog(command, projectId) {
const dataDir = this.configService.getDataDir();
let mpConfigFIle = path_1.default.resolve(dataDir, `${command}.${projectId}.log`);
let exists = await fs_1.default.existsSync(mpConfigFIle);
await fs_1.default.writeFileSync(mpConfigFIle, '', null);
}
async downloadMysql({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/mysql_image_download", "setMySQLShellOut", [], projectId);
}
async downloadRedis({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/redis_image_download", "setRedisShellOut", [], projectId);
}
async startMysql({ workbenchId, projectId }) {
const command = 'setMySQLShellOut';
await this.clearLog(command, projectId);
const { proName } = await this.projectService.getProjectById(projectId);
const { appDetail: { appName } } = await this.applicationService.getAppDetail(proName);
return await this.excuteShell("code-tpl/mysql_docker_start", command, ['-n', appName.replace(/-(\w)/g, ($0, $1) => $1.toUpperCase())], projectId);
}
async stopMysql({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/mysql_docker_start", "setMySQLShellOut", ['-s'], projectId);
}
async startRedis({ workbenchId, projectId }) {
const command = 'setRedisShellOut';
await this.clearLog(command, projectId);
return await this.excuteShell("code-tpl/redis_docker_start", command, [], projectId);
}
async stopRedis({ workbenchId, projectId }) {
return await this.excuteShell("code-tpl/redis_docker_start", "setRedisShellOut", ['-s'], projectId);
}
async syncMysql({ workbenchId, projectId, type }) {
try {
const { proName } = await this.projectService.getProjectById(projectId);
const { appDetail: { appName, appId, appType } } = await this.applicationService.getAppDetail(proName);
const sqlFile = `https://diy.youzanyun.com/api/custom/v2/devtool/getDBfile?type=${type}`;
const sqlDir = path_1.default.resolve(this.configService.getSqlDir(), `${appName}.sql`);
const fileRes = await this.requestService.get(sqlFile, {}, {
projectName: appName,
});
await fs_1.default.writeFileSync(sqlDir, fileRes);
return await this.excuteShell("code-tpl/mysql_docker_sync", "setMySQLShellOut", ['-n', appName.replace(/-(\w)/g, ($0, $1) => $1.toUpperCase()), '-f', sqlDir], projectId);
}
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;
}
async getDouckerStatus(arg) {
try {
let statusShell = await this.getShellDir("code-tpl/docker_check_status");
const result = await this.executeService.promiseSpawm(statusShell, arg, {}, {
report: (data) => {
}
});
return result === 0 ? true : false;
}
catch (error) {
return false;
}
}
};
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], DatabaseService.prototype, "configService", void 0);
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], DatabaseService.prototype, "executeService", void 0);
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], DatabaseService.prototype, "projectService", void 0);
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], DatabaseService.prototype, "applicationService", void 0);
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], DatabaseService.prototype, "userService", void 0);
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], DatabaseService.prototype, "requestService", void 0);
DatabaseService = tslib_1.__decorate([
(0, spring4js_nodejs_1.Service)()
], DatabaseService);
exports.default = DatabaseService;