import {Router} from "express"; import config from "config"; import * as child_process from "child_process"; import ApplicationComponent from "../ApplicationComponent"; import {ForbiddenHttpError} from "../HttpError"; import Logger from "../Logger"; export default class AutoUpdateComponent extends ApplicationComponent { public async checkSecuritySettings(): Promise { this.checkSecurityConfigField('gitlab_webhook_token'); } public async init(router: Router): Promise { router.post('/update/push.json', (req, res) => { const token = req.header('X-Gitlab-Token'); if (!token || token !== config.get('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: any) { Logger.info('Update params:', params); try { Logger.info('Starting auto update...'); // Fetch await this.runCommand(`git pull`); // Install new dependencies await this.runCommand(`yarn install --production=false`); // Process assets await this.runCommand(`yarn dist`); // Stop app await this.app!.stop(); Logger.info('Success!'); } catch (e) { Logger.error(e, 'An error occurred while running the auto update.'); } } private async runCommand(command: string): Promise { Logger.info(`> ${command}`); Logger.info(child_process.execSync(command).toString()); } }