开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
forked from robergroup/chiner
关闭
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
develop
分支 (2)
标签 (4)
develop
master
v3.1.0
v3.0.2
v3.0.1
v3.0.0
develop
分支 (2)
标签 (4)
develop
master
v3.1.0
v3.0.2
v3.0.1
v3.0.0
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
develop
分支 (2)
标签 (4)
develop
master
v3.1.0
v3.0.2
v3.0.1
v3.0.0
chiner
/
src
/
lib
/
json.js
chiner
/
src
/
lib
/
json.js
json.js 14.63 KB
一键复制 编辑 原始数据 按行查看 历史
jiaoshou1234 提交于 2021年08月08日 16:40 +08:00 . fix: fix bugs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
// 桌面版
import fs from 'fs';
import path from 'path';
import moment from 'moment';
import * as _ from 'lodash/object';
import { projectSuffix } from '../../profile';
import { defaultJVM } from './datasource_util';
const { execFile } = require('child_process');
const { ipcRenderer, shell } = require('electron');
const { app, dialog } = require('@electron/remote');
const user_config = 'user_config.json';
const project_config = 'project_config.json';
const base_path = 'userData';
const basePath = app.getPath(base_path);
// 此处分为两个目录 一个为用户的配置信息user_config 一个为通用的项目配置信息project_config
const userConfigPath = basePath + path.sep + user_config;
const projectConfigPath = basePath + path.sep + project_config;
// 执行jar包时输出的json文件路径
const execJarOut = basePath + path.sep;
// 删除整个目录 包括目下的所有文件
const deleteDirectoryFile = (dirPath) => {
if (fs.existsSync(dirPath)) {
fs.readdirSync(dirPath).forEach((file) => {
const curPath = `${dirPath}${path.sep}${file}`;
if (fs.lstatSync(curPath).isDirectory()) {
deleteDirectoryFile(curPath);
} else {
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(dirPath);
}
};
// 判断目录是否存在 如果不存在则创建
export const ensureDirectoryExistence = (dirPath) => {
if (!fs.existsSync(dirPath)) {
const parentDir = path.dirname(dirPath);
ensureDirectoryExistence(parentDir);
fs.mkdirSync(dirPath);
}
};
// 文件名和路径拼接
export const dirSplicing = (dir, fileName) => {
return path.join(dir, fileName);
};
// 获取文件所在的目录
export const dirname = (file) => {
return path.dirname(file);
};
// 判断文件是否已经存在
export const fileExists = (filePath) => {
return fs.existsSync(filePath);
};
// 删除文件
export const deleteFile = (filePath) => {
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
};
export const saveNormalFile = (file, dataBuffer) => {
// 通用的文件保方法
return new Promise((res, rej) => {
fs.writeFile(file, dataBuffer, (err) => {
if(err){
rej(err);
}else{
res(dataBuffer);
}
});
});
};
export const readNormalFile = (filePath) => {
// 通用的文件读取方法
return new Promise((res, rej) => {
fs.readFile(filePath, (err, data) => {
if (err) {
rej(err);
} else {
res(data);
}
});
})
};
export const saveJsonPromise = (filePath, data) => {
return new Promise((res, rej) => {
const tempData = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
const tempFilePath = filePath.endsWith('.json') ? filePath : `${filePath}.json`;
if (!tempData) {
rej(new Error('error'));
} else {
saveNormalFile(tempFilePath, tempData).then((data) => {
res(data);
}).catch((err) => {
rej(err);
});
}
});
};
export const readJsonPromise = (filePath) => {
return new Promise((res, rej) => {
const tempFilePath = filePath.endsWith('.json') ? filePath : `${filePath}.json`;
readNormalFile(tempFilePath).then((data) => {
res(JSON.parse(data.toString().replace(/^\uFEFF/, '')));
}).catch((err) => {
rej(err);
});
});
};
export const saveJsonPromiseAs = (data, refactor) => {
const tempData = typeof data === 'string' ? data : JSON.stringify(data, null, 2);
const extensions = [];
if (process.platform !== 'win32') {
// mac 下无法识别.XXX.json为后缀的文件
extensions.push('json');
} else {
extensions.push(`${projectSuffix}.json`);
}
return new Promise((res, rej) => {
saveFile(tempData, [{ name: projectSuffix, extensions: extensions}], (path) => {
if (!path.endsWith('.json')) {
return `${path}.${projectSuffix}.json`;
}else if (!path.endsWith(`.${projectSuffix}.json`)) {
return path.replace(/\.json$/g, `.${projectSuffix}.json`);
}
return path;
}, {}, refactor).then(({filePath}) => res(filePath)).catch(err => rej(err));
});
};
export const getUserConfig = () => {
return new Promise((res, rej) => {
// 获取用户的配置信息
const defaultUserConfigData = {
projectHistories: [], // 历史项目记录
lang: 'zh', // 当前应用使用的语言
};
const defaultProjectConfigData = {
commonFields: [], // 通用字段信息 多个项目可通用
};
/*
* %APPDATA% Windows 中
* $XDG_CONFIG_HOME or ~/.config Linux 中
* ~/Library/Application Support macOS 中
* */
const getData = (path, defaultData) => {
return new Promise((r, j) => {
readJsonPromise(path).then((data) => {
r({
...defaultData,
...data,
});
}).catch(() => {
// 如果用户信息报错 则需要使用默认数据进行覆盖
saveJsonPromise(path, defaultData).then(() => {
r(defaultData);
}).catch((err) => {
rej(err);
})
});
})
};
Promise.all([getData(userConfigPath, defaultUserConfigData),
getData(projectConfigPath, defaultProjectConfigData)]).then(result => {
setTimeout(() => {
res(result);
}, 1000);
}).catch((err) => {
rej(err);
});
});
};
export const saveUserConfig = (data = []) => {
return new Promise((res, rej) => {
Promise.all([saveJsonPromise(userConfigPath, data[0] || {}),
saveJsonPromise(projectConfigPath, data[1] || {})]).then((result) => {
setTimeout(() => {
res(result);
}, 100);
}).catch((err) => {
rej(err);
});
});
};
export const saveFile = (data, filters, fileValidate, options, refactor) => {
// 将调用系统的目录弹出框
return new Promise((res, rej) => {
dialog.showSaveDialog({
filters: filters || [],
...options,
}).then(({filePath}) => {
if (filePath) {
let tempFile = filePath;
if (fileValidate) {
// 需要重组文件名
tempFile = fileValidate(filePath);
}
saveNormalFile(tempFile, refactor ? refactor(data, tempFile) : data).then((data) => {
res({
data,
filePath: tempFile,
});
}).catch((err) => {
rej(err);
})
}
}).catch((err) => {
rej(err);
})
});
};
export const openFile = (filters) => {
return new Promise((res, rej) => {
dialog.showOpenDialog({
filters: filters || []
}).then(({filePaths}) => {
if (filePaths.length > 0) {
readNormalFile(filePaths[0]).then((data) => {
res(data);
}).catch((err) => {
rej(err);
});
}
}).catch((err) => {
rej(err);
})
});
};
export const getJavaHome = () => {
return process.env.JAVA_HOME || process.env.JER_HOME || '';
};
export const openFileOrDirPath = (filters, properties) => {
return new Promise((res, rej) => {
dialog.showOpenDialog({
filters: filters || [],
properties: properties || ['openFile'], // 默认是打开文件
}).then(({filePaths}) => {
if (filePaths.length > 0) {
res(filePaths[0])
}
}).catch((err) => {
rej(err);
})
});
};
export const openProjectFilePath = (errorFileMessage, suffix) => {
const tempSuffix = suffix || projectSuffix;
const extensions = [];
if (process.platform !== 'win32') {
// mac 下无法识别XXX.json为后缀的文件
extensions.push('json');
} else {
extensions.push(`${tempSuffix}.json`);
}
return new Promise((res, rej) => {
openFileOrDirPath([{ name: tempSuffix, extensions: extensions}]).then((file) => {
if (extensions.every(e => !file.endsWith(`.${e}`))) {
// 项目名不符合规范 抛出异常
rej(new Error(errorFileMessage || 'Invalid project file'));
} else {
res(file);
}
}).catch((err) => {
rej(err);
})
});
};
const getProject = (project, type) => {
const tempArray = project.split(path.sep);
if (type === 'name') {
return tempArray[tempArray.length - 1].split('.pdman.json')[0];
}
return tempArray.splice(0, tempArray.length - 1).join(path.sep);
};
export const getAllVersionProject = (project, data) => {
// 获取当前项目的所有版本数据
const proName = data?.name || getProject(project, 'name');
const proPath = getProject(project, 'path');
const proVersionPath = `${proPath}${path.sep}.${proName}.version${path.sep}`;
return new Promise((res, rej) => {
fs.readdir(proVersionPath, (err, files) => {
if (!err) {
// 读取所有的文件信息 此处需要过滤其他无效的文件(目前先过滤非json文件,以.开头的文件)
Promise.all(files
.filter(f => f.endsWith('.json') && !f.startsWith('.'))
.map(f => readJsonPromise(proVersionPath + f))).
then((results) => {
// 返回的项目信息需要以时间顺序进行排序 最近的放在最前面
const format = 'YYYY/M/D HH:mm:ss';
res(results
.sort((a, b) => moment(a?.date, format).millisecond() - moment(b?.date, format).millisecond()))
}).catch(err => {
rej(err);
});
} else {
res([]);
}
})
});
};
export const removeAllVersionProject = (project) => {
// 获取当前项目的所有版本数据
const proName = getProject(project, 'name');
const proPath = getProject(project, 'path');
const proVersionPath = `${proPath}${path.sep}.${proName}.version${path.sep}`;
deleteDirectoryFile(proVersionPath);
};
export const saveVersionProject = (project, versionData, versionInfo) => {
const proName = getProject(project, 'name');
const proPath = getProject(project, 'path');
const proVersionPath = `${proPath}${path.sep}.${proName}.version${path.sep}`;
// 判断版本文件目录是否存在
ensureDirectoryExistence(proVersionPath);
const filePath = `${proVersionPath}${proName}-${versionInfo.version}.pdman.json`;
return saveJsonPromise(filePath, {...versionData, ...versionInfo});
};
export const removeVersionProject = (project, versionInfo) => {
const proName = getProject(project, 'name');
const proPath = getProject(project, 'path');
const proVersionPath = `${proPath}${path.sep}.${proName}.version${path.sep}`;
const filePath = `${proVersionPath}${proName}-${versionInfo.version}.pdman.json`;
deleteFile(filePath);
};
export const getPathStep = () => {
return path.sep;
};
export const execFileCmd = (cmd, params, cb) => {
execFile(cmd, params,
{
maxBuffer: 100 * 1024 * 1024, // 100M
},
(error, stdout, stderr) => {
cb && cb(error, stdout, stderr);
});
};
export const connectDB = (dataSource, config, params = {}, cmd, cb) => {
// 创建临时文件
const outFile = `${execJarOut}${moment().unix()}.json`;
console.log(outFile);
const getParam = (params) => {
const paramArray = [];
Object.keys(params).forEach((p) => {
if (p !== 'customer_driver') {
const param = params[p] || '';
const value = param.includes('') ? `"${param}"` : param;
paramArray.push(`${p}=${value}`);
} if (p === 'driver') {
paramArray.push(`driver_class_name=${params[p]}`);
}
});
return paramArray.concat(`out=${outFile}`);
};
const javaHome = config?.javaHome || _.get(dataSource, 'profile.javaHome', '');
const jvm = ('jvm' in (config || {})) ? config.jvm : defaultJVM;
const jar = ipcRenderer.sendSync('jarPath');
const tempValue = javaHome ? `${javaHome}${path.sep}bin${path.sep}java` : 'java';
const customerDriver = _.get(params, 'customer_driver', '');
const commend = [
'-Dfile.encoding=utf-8',
...jvm.split(''),
'-jar', jar, cmd,
...getParam(params),
];
if (customerDriver) {
commend.unshift(`-Xbootclasspath/a:${customerDriver}`);
}
execFile(tempValue, commend,
{
maxBuffer: 100 * 1024 * 1024, // 100M
},
(error, stdout, stderr) => {
if (error) {
cb && cb({
status : "FAILED",
body : (stdout || stderr || error.message),
});
} else {
readJsonPromise(outFile).then((d) => {
cb && cb(d);
}).catch(err => {
cb && cb({
status : "FAILED",
body : err.message,
});
}).finally(() => {
// 删除该文件
deleteFile(outFile);
// 删除临时文件夹
if (params.imgDir) {
deleteDirectoryFile(params.imgDir);
}
});
}
//const result = (stdout || stderr || error.message);
//cb && cb(error ? result : null);
});
};
export const copyFile = (defaultPath, filters) => {
return new Promise((res, rej) => {
dialog.showSaveDialog({
filters: filters || []
}).then(({filePath}) => {
if (filePath) {
fs.copyFile(defaultPath, filePath, (err) => {
if (!err) {
res(filePath);
} else {
rej(err);
}
});
}
}).catch((err) => {
rej(err);
});
});
};
export const saveTempImages = (images) => {
// 创建临时目录
const userConfigPath = basePath + path.sep + 'temp_img';
ensureDirectoryExistence(userConfigPath);
return new Promise((res, rej) => {
Promise.all(images.map(i => {
const filePath = userConfigPath + path.sep + (i.group ? `${i.group}-${i.fileName}` : i.fileName) + '.png';
return saveNormalFile(filePath, i.data);
})).then(() => {
res(userConfigPath);
}).catch((err) => {
rej(err);
})
});
};
const getDefaultWordTemplate = () => {
return ipcRenderer.sendSync('docx');
}
export const saveAsWordTemplate = () => {
return copyFile(getDefaultWordTemplate(), [{name: 'chiner-docx-tpl', extensions: ['docx']}]);
};
export const selectWordFile = (dataSource) => {
const template = _.get(dataSource, 'profile.generatorDoc.docTemplate');
const name = _.get(dataSource, 'name');
let defaultPath = template || getDefaultWordTemplate();
return new Promise((res, rej) => {
openFileOrDirPath([], ['openDirectory']).then((dir) => {
res([`${dir}${path.sep}${name}-${moment().format('YYYYMDHHmmss')}.docx`, defaultPath]);
});
})
};
export const writeLog = (err) => {
const logPath = `${basePath}${path.sep}${moment().format('YYYY-M-D-HH-mm-ss')}-error-log.txt`;
return new Promise((res) => {
saveNormalFile(logPath,err.stack)
.then(() => res(logPath))
});
};
export const getLogPath = () => {
return path.join(app.getPath('home'), '/logs/chiner');
}
export const showItemInFolder = () => {
shell.openPath(getLogPath());
}
export const showErrorLogFolder = (file) => {
shell.openPath(file);
}
export const basename = (fileName, extension) => {
return path.basename(fileName, extension);
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

元数建模,一款丰富数据库生态,独立于具体数据库之外的,数据库关系模型设计平台。
暂无标签
MulanPubL-2.0
使用 MulanPubL-2.0 开源许可协议
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/sync-github/chiner.git
git@gitee.com:sync-github/chiner.git
sync-github
chiner
chiner
develop
点此查找更多帮助

搜索帮助

评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /