50 lines
1.5 KiB
TypeScript
50 lines
1.5 KiB
TypeScript
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<void> {
|
|
public async init(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: 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<void> {
|
|
Logger.info(`> ${command}`);
|
|
Logger.info(child_process.execSync(command).toString());
|
|
}
|
|
} |