youzanyun-devtool-worker
Version:
189 lines (188 loc) • 7.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs_extra_1 = tslib_1.__importDefault(require("fs-extra"));
const path_1 = tslib_1.__importDefault(require("path"));
const reverse_1 = tslib_1.__importDefault(require("lodash/reverse"));
const uniqWith_1 = tslib_1.__importDefault(require("lodash/uniqWith"));
const forEach_1 = tslib_1.__importDefault(require("lodash/forEach"));
const spring4js_nodejs_1 = require("spring4js-nodejs");
let ProjectDao = class ProjectDao {
constructor() {
this.dataFilePath = '';
}
async start() {
const dataDir = this.configService.getDataDir();
const dataFilePath = path_1.default.join(dataDir, 'project.json');
await fs_extra_1.default.ensureFile(dataFilePath);
this.dataFilePath = dataFilePath;
const curDataBuffer = await fs_extra_1.default.readFile(dataFilePath);
const curDataStr = curDataBuffer.toString("utf8");
const oldDataFilePath = path_1.default.join(dataDir, 'app.json');
const isExist = await fs_extra_1.default.pathExists(oldDataFilePath);
if (!curDataStr && isExist) {
const oldInfoBuffer = await fs_extra_1.default.readFile(oldDataFilePath);
const oldInfoStr = oldInfoBuffer.toString("utf8") || '{}';
const oldInfo = JSON.parse(oldInfoStr);
if (!oldInfo || !oldInfo.list || oldInfo.list.length === 0) {
return;
}
let oldProjects = (0, uniqWith_1.default)(oldInfo.list, (arrVal, othVal) => {
return arrVal.proName === othVal.proName;
});
for (let i = 0; i < oldProjects.length; i++) {
const item = oldProjects[i];
const isDesignPlatform = await fs_extra_1.default.pathExists(path_1.default.resolve(item.proPath, 'ecloud-tpl-version.json'));
const isNodeJs = await fs_extra_1.default.pathExists(path_1.default.resolve(item.proPath, 'package.json'));
const isJava = await fs_extra_1.default.pathExists(path_1.default.resolve(item.proPath, 'pom.xml'));
const isPhp = await fs_extra_1.default.pathExists(path_1.default.resolve(item.proPath, 'composer.json'));
item.isDesignPlatform = isDesignPlatform;
item.isNodeJs = isNodeJs;
item.isJava = isJava;
item.isPhp = isPhp;
}
;
await fs_extra_1.default.remove(oldDataFilePath);
await fs_extra_1.default.writeFile(dataFilePath, JSON.stringify({
maxId: oldInfo.maxId,
list: oldProjects
}));
}
}
async getData() {
const isServerMode = this.configService.isServerMode();
if (isServerMode) {
let container = global.container;
this.requestService = await container.getServiceInstance('requestService');
let res = await this.requestService.get('https://diy.youzanyun.com/api/apps/list?pageIndex=0&pageSize=100', {});
const projectList = [];
(0, forEach_1.default)((res.data || {}).items, item => {
if (item.appType == 2)
return;
projectList.push({
"proAlias": item.projectName,
"proPath": `/Users/root/yzy/${item.projectName}`,
"proName": item.projectName,
"isDesignPlatform": false,
"isNodeJs": false,
"isJava": false,
"isPhp": false,
"id": item.id
});
});
return {
"maxId": 8,
"list": projectList,
};
}
else {
try {
const data = await fs_extra_1.default.readJson(this.dataFilePath);
return data;
}
catch (err) {
return {
"maxId": 0,
"list": [],
};
}
}
}
async getAllData() {
const data = await this.getData();
const result = (0, reverse_1.default)(data.list || []);
return result;
}
async getDataById(id) {
const allData = await this.getAllData();
return allData.find((item) => +item.id === +id) || {};
}
async getDataWithCondition(params) {
const { pageNo, pageSize } = params, otherParams = tslib_1.__rest(params, ["pageNo", "pageSize"]);
const allData = await this.getAllData();
let result = allData.slice(0);
if (otherParams) {
const conditions = Object.keys(otherParams);
const filterRecords = (data, key, value) => {
return data.filter((item) => {
if (typeof value === 'object' && value instanceof Array) {
return value.indexOf(item[key]) > -1;
}
else {
return item[key] === value;
}
});
};
for (let i = 0; i < conditions.length; i++) {
const key = conditions[i];
const value = params[key];
result = filterRecords(result, key, value);
}
}
if (pageNo && pageSize) {
const startIndex = (pageNo - 1) * pageSize;
const endIndex = pageNo * pageSize;
const content = result.slice(startIndex, endIndex);
result = {
content,
pageNo,
pageSize,
total: result.length
};
}
return result;
}
async save(item) {
const data = await this.getData();
const maxId = (data.maxId || 0) + 1;
const allData = data.list || [];
item.id = maxId;
allData.push(item);
const result = {
maxId,
list: allData
};
await fs_extra_1.default.writeFile(this.dataFilePath, JSON.stringify(result));
return item;
}
async update(item) {
const data = await this.getData();
const allData = data.list || [];
const index = allData.findIndex((d) => d.id === item.id);
if (index > -1) {
allData[index] = item;
}
const result = {
maxId: data.maxId,
list: allData
};
await fs_extra_1.default.writeFile(this.dataFilePath, JSON.stringify(result));
return item;
}
async delete(params) {
const data = await this.getData();
const allData = data.list || [];
const type = typeof params;
const list = allData.filter((item) => {
if (type === 'number' || type === 'string') {
return item.id !== Number(params);
}
else if (type === 'object' && params instanceof Array) {
return params.indexOf(item.id) === -1;
}
else {
return true;
}
});
await fs_extra_1.default.writeFile(this.dataFilePath, JSON.stringify({ maxId: data.maxId, list }));
return 'ok';
}
};
tslib_1.__decorate([
(0, spring4js_nodejs_1.Resource)()
], ProjectDao.prototype, "configService", void 0);
ProjectDao = tslib_1.__decorate([
(0, spring4js_nodejs_1.Service)()
], ProjectDao);
exports.default = ProjectDao;
;