mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 14:49:19 +00:00
自动更新
This commit is contained in:
parent
666dcc2540
commit
2150e94ad5
@ -13,6 +13,7 @@
|
|||||||
"express": "^4.18.0",
|
"express": "^4.18.0",
|
||||||
"ioredis": "^5.6.0",
|
"ioredis": "^5.6.0",
|
||||||
"mkdirp": "^3.0.1",
|
"mkdirp": "^3.0.1",
|
||||||
|
"simple-git": "^3.27.0",
|
||||||
"ws": "^8.18.1"
|
"ws": "^8.18.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
28
pnpm-lock.yaml
generated
28
pnpm-lock.yaml
generated
@ -26,6 +26,9 @@ importers:
|
|||||||
mkdirp:
|
mkdirp:
|
||||||
specifier: ^3.0.1
|
specifier: ^3.0.1
|
||||||
version: 3.0.1
|
version: 3.0.1
|
||||||
|
simple-git:
|
||||||
|
specifier: ^3.27.0
|
||||||
|
version: 3.27.0
|
||||||
ws:
|
ws:
|
||||||
specifier: ^8.18.1
|
specifier: ^8.18.1
|
||||||
version: 8.18.1
|
version: 8.18.1
|
||||||
@ -71,6 +74,12 @@ packages:
|
|||||||
'@jridgewell/trace-mapping@0.3.9':
|
'@jridgewell/trace-mapping@0.3.9':
|
||||||
resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==}
|
resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==}
|
||||||
|
|
||||||
|
'@kwsites/file-exists@1.1.1':
|
||||||
|
resolution: {integrity: sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw==}
|
||||||
|
|
||||||
|
'@kwsites/promise-deferred@1.1.1':
|
||||||
|
resolution: {integrity: sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==}
|
||||||
|
|
||||||
'@tsconfig/node10@1.0.11':
|
'@tsconfig/node10@1.0.11':
|
||||||
resolution: {integrity: sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==}
|
resolution: {integrity: sha512-DcRjDCujK/kCk/cUe8Xz8ZSpm8mS3mNNpta+jGCA6USEDfktlNvm1+IuZ9eTcDbNk41BHwpHHeW+N1lKCz4zOw==}
|
||||||
|
|
||||||
@ -619,6 +628,9 @@ packages:
|
|||||||
resolution: {integrity: sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==}
|
resolution: {integrity: sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==}
|
||||||
engines: {node: '>= 0.4'}
|
engines: {node: '>= 0.4'}
|
||||||
|
|
||||||
|
simple-git@3.27.0:
|
||||||
|
resolution: {integrity: sha512-ivHoFS9Yi9GY49ogc6/YAi3Fl9ROnF4VyubNylgCkA+RVqLaKWnDSzXOVzya8csELIaWaYNutsEuAhZrtOjozA==}
|
||||||
|
|
||||||
source-map-support@0.5.21:
|
source-map-support@0.5.21:
|
||||||
resolution: {integrity: sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==}
|
resolution: {integrity: sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==}
|
||||||
|
|
||||||
@ -756,6 +768,14 @@ snapshots:
|
|||||||
'@jridgewell/resolve-uri': 3.1.2
|
'@jridgewell/resolve-uri': 3.1.2
|
||||||
'@jridgewell/sourcemap-codec': 1.5.0
|
'@jridgewell/sourcemap-codec': 1.5.0
|
||||||
|
|
||||||
|
'@kwsites/file-exists@1.1.1':
|
||||||
|
dependencies:
|
||||||
|
debug: 4.4.0
|
||||||
|
transitivePeerDependencies:
|
||||||
|
- supports-color
|
||||||
|
|
||||||
|
'@kwsites/promise-deferred@1.1.1': {}
|
||||||
|
|
||||||
'@tsconfig/node10@1.0.11': {}
|
'@tsconfig/node10@1.0.11': {}
|
||||||
|
|
||||||
'@tsconfig/node12@1.0.11': {}
|
'@tsconfig/node12@1.0.11': {}
|
||||||
@ -1329,6 +1349,14 @@ snapshots:
|
|||||||
side-channel-map: 1.0.1
|
side-channel-map: 1.0.1
|
||||||
side-channel-weakmap: 1.0.2
|
side-channel-weakmap: 1.0.2
|
||||||
|
|
||||||
|
simple-git@3.27.0:
|
||||||
|
dependencies:
|
||||||
|
'@kwsites/file-exists': 1.1.1
|
||||||
|
'@kwsites/promise-deferred': 1.1.1
|
||||||
|
debug: 4.4.0
|
||||||
|
transitivePeerDependencies:
|
||||||
|
- supports-color
|
||||||
|
|
||||||
source-map-support@0.5.21:
|
source-map-support@0.5.21:
|
||||||
dependencies:
|
dependencies:
|
||||||
buffer-from: 1.1.2
|
buffer-from: 1.1.2
|
||||||
|
@ -2,13 +2,19 @@ import apps from './app';
|
|||||||
import logger from './utils/core/logger';
|
import logger from './utils/core/logger';
|
||||||
import config from './utils/core/config';
|
import config from './utils/core/config';
|
||||||
import redis from './services/redis/redis';
|
import redis from './services/redis/redis';
|
||||||
|
import autoUpdater from './utils/core/autoUpdater';
|
||||||
|
|
||||||
config.check(['PORT', 'DEBUG', 'RD_PORT', 'RD_ADD', 'WS_SECRET', 'WS_PORT']);
|
config.check(['PORT', 'DEBUG', 'RD_PORT', 'RD_ADD', 'WS_SECRET', 'WS_PORT']);
|
||||||
const PORT = config.get('PORT') || 3000;
|
const PORT = config.get('PORT') || 3000;
|
||||||
|
|
||||||
apps
|
apps
|
||||||
.createApp()
|
.createApp()
|
||||||
.then((app) => {
|
.then(async (app) => {
|
||||||
|
const isUpdated = await autoUpdater.checkForUpdates();
|
||||||
|
if (isUpdated) {
|
||||||
|
logger.warn(`检测到更新,正在自动重启..`);
|
||||||
|
process.exit(0);
|
||||||
|
}
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
logger.info(`Crystelf-core listening on ${PORT}`);
|
logger.info(`Crystelf-core listening on ${PORT}`);
|
||||||
});
|
});
|
||||||
|
58
src/utils/core/autoUpdater.ts
Normal file
58
src/utils/core/autoUpdater.ts
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import simpleGit, { SimpleGit } from 'simple-git';
|
||||||
|
import paths from './path';
|
||||||
|
import logger from './logger';
|
||||||
|
|
||||||
|
class AutoUpdater {
|
||||||
|
private git: SimpleGit;
|
||||||
|
private repoPath: string;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.git = simpleGit();
|
||||||
|
this.repoPath = paths.get('root');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查远程更新
|
||||||
|
*/
|
||||||
|
public async checkForUpdates(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
logger.info('检查仓库更新中..');
|
||||||
|
|
||||||
|
const status = await this.git.status();
|
||||||
|
if (status.ahead > 0) {
|
||||||
|
logger.info('检测到当地仓库有未提交的更改,跳过更新..');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info('正在获取远程仓库信息..');
|
||||||
|
await this.git.fetch();
|
||||||
|
|
||||||
|
const localBranch = status.current;
|
||||||
|
const diffSummary = await this.git.diffSummary([`${localBranch}..origin/${localBranch}`]);
|
||||||
|
|
||||||
|
if (diffSummary.files.length > 0) {
|
||||||
|
logger.info('检测到远程仓库有更新!');
|
||||||
|
|
||||||
|
logger.info('正在拉取更新..');
|
||||||
|
if (localBranch) {
|
||||||
|
await this.git.pull('origin', localBranch);
|
||||||
|
} else {
|
||||||
|
logger.error('当前分支名称未知,无法执行拉取操作..');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info('更新成功!');
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
logger.info('远程仓库没有新变化..');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('检查仓库更新失败: ', error);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const autoUpdater = new AutoUpdater();
|
||||||
|
export default autoUpdater;
|
Loading…
x
Reference in New Issue
Block a user