57 lines
1.8 KiB
TypeScript
57 lines
1.8 KiB
TypeScript
import * as child_process from "child_process";
|
|
import config from "config";
|
|
import {Router} from "express";
|
|
|
|
import ApplicationComponent from "../ApplicationComponent.js";
|
|
import {ForbiddenHttpError} from "../HttpError.js";
|
|
import {logger} from "../Logger.js";
|
|
|
|
export default class AutoUpdateComponent extends ApplicationComponent {
|
|
private static async runCommand(command: string): Promise<void> {
|
|
logger.info(`> ${command}`);
|
|
logger.info(child_process.execSync(command).toString());
|
|
}
|
|
|
|
public async checkSecuritySettings(): Promise<void> {
|
|
this.checkSecurityConfigField('gitlab_webhook_token');
|
|
}
|
|
|
|
public async initRoutes(router: Router): Promise<void> {
|
|
router.post('/update/push.json', (req, res) => {
|
|
const token = req.header('X-Gitlab-Token');
|
|
if (!token || token !== config.get<string>('gitlab_webhook_token'))
|
|
throw new ForbiddenHttpError('Invalid token', req.url);
|
|
|
|
this.update(req.body).catch(logger.error);
|
|
|
|
res.json({
|
|
'status': 'ok',
|
|
});
|
|
});
|
|
}
|
|
|
|
private async update(params: { [p: string]: unknown }) {
|
|
logger.info('Update params:', params);
|
|
|
|
try {
|
|
logger.info('Starting auto update...');
|
|
|
|
// Fetch
|
|
await AutoUpdateComponent.runCommand(`git pull`);
|
|
|
|
// Install new dependencies
|
|
await AutoUpdateComponent.runCommand(`yarn install --production=false`);
|
|
|
|
// Process assets
|
|
await AutoUpdateComponent.runCommand(`yarn dist`);
|
|
|
|
// Stop app
|
|
await this.getApp().stop();
|
|
|
|
logger.info('Success!');
|
|
} catch (e) {
|
|
logger.error(e, 'An error occurred while running the auto update.');
|
|
}
|
|
}
|
|
}
|