import {Express, 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"; const ROUTE = '/update/push.json'; export default class AutoUpdateComponent extends ApplicationComponent { public async start(app: Express, router: Router): Promise { router.post(ROUTE, (req, res, next) => { 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.checkout_sha) .catch(Logger.error); res.json({ 'status': 'ok', }); }); } public async stop(): Promise { } private async update(checkout_sha: string) { await this.app!.stop(); try { Logger.info('Starting auto update...'); // Fetch await this.runCommand(`git fetch`); // Checkout new source await this.runCommand(`git checkout ${checkout_sha}`); // Install new dependencies await this.runCommand(`yarn install --production=false`); // Process assets await this.runCommand(`yarn dist`); Logger.info('Success!'); } catch (e) { Logger.error(e, 'An error occurred while running the auto update.'); } } private async runCommand(command: string) { Logger.info(`> ${command}`); Logger.info(child_process.execSync(command).toString()); } }