53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
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<void> {
|
|
public async start(app: Express, router: Router): Promise<void> {
|
|
router.post(ROUTE, (req, res, next) => {
|
|
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.checkout_sha)
|
|
.catch(Logger.error);
|
|
|
|
res.json({
|
|
'status': 'ok',
|
|
});
|
|
});
|
|
}
|
|
|
|
public async stop(): Promise<void> {
|
|
}
|
|
|
|
private async update(checkout_sha: string) {
|
|
await this.app!.stop();
|
|
|
|
try {
|
|
Logger.info('Starting auto update...');
|
|
|
|
// 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());
|
|
}
|
|
} |