cloudservices/src/commands/pull.ts

93 lines
5.3 KiB
TypeScript
Raw Normal View History

2019-10-30 08:53:36 -04:00
import { Message } from 'eris';
import { Client } from '..';
import { Command } from '../class';
export default class Pull extends Command {
constructor(client: Client) {
super(client);
this.name = 'pull';
this.description = 'Fetches the latest commit from Gitlab';
this.aliases = ['update'];
this.enabled = true;
2019-12-30 12:03:17 -05:00
this.permissions = { roles: ['525441307037007902'] };
2019-10-30 08:53:36 -04:00
}
public async run(message: Message) {
try {
2019-11-30 18:55:49 -05:00
if (this.client.updating) return message.channel.createMessage(`${this.client.stores.emojis.error} ***Update in progress***`);
2019-11-19 08:32:33 -05:00
this.client.updating = true;
2019-12-16 19:04:39 -05:00
const updateMessage = await message.channel.createMessage(`${this.client.stores.emojis.loading} ***Fetching latest commit...***\n\`\`\`sh\n$ git pull\n\`\`\``);
2019-10-30 08:53:36 -04:00
let pull: string;
2019-10-30 13:19:44 -04:00
2019-10-30 08:53:36 -04:00
try {
2020-03-17 01:39:18 -04:00
pull = await this.client.util.exec('git pull', { cwd: '/opt/CloudServices' });
2019-10-30 08:53:36 -04:00
} catch (error) {
2019-11-01 19:15:39 -04:00
const updatedMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.error} ***Could not fetch latest commit***`)
.replace(/```$/, `${error.message}\n\`\`\``);
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-01 19:15:39 -04:00
return updateMessage.edit(updatedMessage);
}
if (pull.includes('Already up to date')) {
const updatedMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.success} ***No updates available***`)
.replace(/```$/, `${pull}\n\`\`\``);
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-01 19:15:39 -04:00
return updateMessage.edit(updatedMessage);
2019-10-30 08:53:36 -04:00
}
2019-12-18 18:26:42 -05:00
if (!pull.includes('origin/master') && !pull.includes(' changed, ')) {
2019-11-01 19:15:39 -04:00
const updatedMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.error} ***Unexpected git output***`)
.replace(/```$/, `${pull}\n\`\`\``);
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-01 19:15:39 -04:00
return updateMessage.edit(updatedMessage);
}
const continueMessage = updateMessage.content.replace(`${this.client.stores.emojis.loading} ***Fetching latest commit...***`, `${this.client.stores.emojis.success} ***Pulled latest commit***\n${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`)
2019-12-16 19:04:39 -05:00
.replace(/```$/, `${pull}\n$ yarn install\n\`\`\``);
2019-11-01 19:15:39 -04:00
const passedPull = await updateMessage.edit(continueMessage);
2019-10-30 13:19:44 -04:00
2019-11-01 19:15:39 -04:00
let install: string;
2019-10-30 13:19:44 -04:00
try {
2020-03-17 01:39:18 -04:00
install = await this.client.util.exec('yarn install', { cwd: '/opt/CloudServices' });
2019-10-30 13:19:44 -04:00
} catch (error) {
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-01 19:15:39 -04:00
const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.error} ***Failed to reinstall dependencies***`)
2019-10-30 13:19:44 -04:00
.replace(/```$/, `${error.message}\n\`\`\``);
return updateMessage.edit(updatedMessage);
}
2019-11-01 19:15:39 -04:00
let updatedPackages: Message;
if (install.includes('Already up-to-date')) {
const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.success} ***No dependency updates available***\n${this.client.stores.emojis.loading} ***Rebuilding files...***`)
2019-12-16 19:04:39 -05:00
.replace(/```$/, `${install}\n$ yarn run build\n\`\`\``);
2019-11-01 19:15:39 -04:00
updatedPackages = await updateMessage.edit(updatedMessage);
} else if (install.includes('success Saved lockfile.')) {
const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.success} ***Updated dependencies***\n${this.client.stores.emojis.loading} ***Rebuilding files...***`)
2019-12-16 19:04:39 -05:00
.replace(/```$/, `${install}\n$ yarn run build\n\`\`\``);
2019-11-01 19:15:39 -04:00
updatedPackages = await updateMessage.edit(updatedMessage);
} else {
const updatedMessage = passedPull.content.replace(`${this.client.stores.emojis.loading} ***Reinstalling dependencies...***`, `${this.client.stores.emojis.error} ***Unexpected yarn install output***`)
.replace(/```$/, `${pull}\n\`\`\``);
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-01 19:15:39 -04:00
return updateMessage.edit(updatedMessage);
}
2019-10-30 13:19:44 -04:00
2019-11-01 19:15:39 -04:00
let build: string;
try {
2020-03-17 01:39:18 -04:00
build = await this.client.util.exec('yarn run build', { cwd: '/opt/CloudServices' });
2019-11-01 19:15:39 -04:00
} catch (error) {
const updatedMessage = updatedPackages.content.replace(`${this.client.stores.emojis.loading} ***Rebuilding files...***`, `${this.client.stores.emojis.error} ***Failed to rebuild files***`)
.replace(/```$/, `${error.message}\n\`\`\``);
2019-11-30 18:55:49 -05:00
this.client.buildError = true;
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-01 19:15:39 -04:00
return updateMessage.edit(updatedMessage);
}
const finalMessage = updatedPackages.content.replace(`${this.client.stores.emojis.loading} ***Rebuilding files...***`, `${this.client.stores.emojis.success} ***Files rebuilt***`)
.replace(/```$/, `${build}\n\`\`\``);
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-30 18:55:49 -05:00
this.client.buildError = false;
2019-10-30 14:35:30 -04:00
return updateMessage.edit(finalMessage);
2019-10-30 08:53:36 -04:00
} catch (error) {
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-10-30 08:53:36 -04:00
return this.client.util.handleError(error, message, this);
}
}
}