1
0
Fork 0
cloudservices/src/commands/pull.ts

95 lines
5.3 KiB
TypeScript
Raw Normal View History

import { Message } from 'discord.js';
2020-05-03 19:46:51 -04:00
import axios from 'axios';
2020-06-29 02:50:26 -04:00
import { Client, Command } from '../class';
2019-10-30 08:53:36 -04:00
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;
2020-04-19 12:37:43 -04:00
this.permissions = { roles: ['662163685439045632'] };
2019-10-30 08:53:36 -04:00
}
2020-05-03 21:14:31 -04:00
public async run(message: Message, args: string[]) {
2019-10-30 08:53:36 -04:00
try {
2020-06-29 02:35:14 -04:00
if (this.client.updating) return this.error(message.channel, 'An update is already in progress.');
2019-11-19 08:32:33 -05:00
this.client.updating = true;
2020-06-29 02:35:14 -04:00
const updateMessage = await this.loading(message.channel, 'Fetching latest commit...');
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) {
2020-07-28 14:37:22 -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***\n`
2020-05-03 18:55:57 -04:00
+ `\`\`\`\n${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);
}
2020-05-03 21:14:31 -04:00
if (pull.includes('Already up to date') && args[0] !== '-f') {
2020-07-28 14:37:22 -04:00
const updatedMessage = updateMessage.content.replace(`***${this.client.stores.emojis.loading} Fetching latest commit...***`, `***${this.client.stores.emojis.success} No updates available***`);
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, ')) {
2020-07-28 14:37:22 -04:00
const updatedMessage = updateMessage.content.replace(`***${this.client.stores.emojis.loading} Fetching latest commit...***`, `***${this.client.stores.emojis.error} Unexpected git output***`);
2019-11-19 08:32:33 -05:00
this.client.updating = false;
2019-11-01 19:15:39 -04:00
return updateMessage.edit(updatedMessage);
}
2020-07-28 14:37:22 -04:00
const continueMessage = updateMessage.content.replace(`***${this.client.stores.emojis.loading} Fetching latest commit...***`, `***${this.client.stores.emojis.success} ${pull.includes('Already up to date') ? 'No updates available' : 'Fetched latest commit'}***\n`
+ `***${this.client.stores.emojis.loading} Reinstalling dependencies...***`);
2019-11-01 19:15:39 -04:00
const passedPull = await updateMessage.edit(continueMessage);
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;
2020-07-28 14:37:22 -04:00
const updatedMessage = passedPull.content.replace(`***${this.client.stores.emojis.loading} Reinstalling dependencies...***`, `***${this.client.stores.emojis.error} Failed to reinstall dependencies***\n`
2020-05-03 18:55:57 -04:00
+ `\`\`\`\n${error.message}\n\`\`\``);
2019-10-30 13:19:44 -04:00
return updateMessage.edit(updatedMessage);
}
2019-11-01 19:15:39 -04:00
let updatedPackages: Message;
if (install.includes('Already up-to-date')) {
2020-07-28 14:37:22 -04:00
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-11-01 19:15:39 -04:00
updatedPackages = await updateMessage.edit(updatedMessage);
2020-06-09 17:48:43 -04:00
} else {
2020-07-28 14:37:22 -04:00
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-11-01 19:15:39 -04:00
updatedPackages = await 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) {
2020-07-28 14:37:22 -04:00
let updatedMessage = updatedPackages.content.replace(`***${this.client.stores.emojis.loading} Rebuilding files...***`, `***${this.client.stores.emojis.error} Failed to rebuild files***\n`);
2020-05-03 19:46:51 -04:00
if (error.message.length < 1800) updatedMessage += `\`\`\`\n${error.message}\n\`\`\``;
else {
const split = this.client.util.splitString(error.message, 1975);
if (split[5]) {
try {
const { data } = await axios.post('https://snippets.cloud.libraryofcode.org/documents', split.join(''));
updatedMessage += `${this.client.stores.emojis.success} The error output can be found on https://snippets.cloud.libraryofcode.org/${data.key}`;
} catch (e) {
updatedMessage += `${this.client.stores.emojis.error} Could not upload error: ${e}`;
}
} else {
split.forEach((m) => message.channel.send(`\`\`\`bash\n${m}\n\`\`\``));
2020-05-03 19:46:51 -04:00
}
}
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);
}
2020-07-28 14:37:22 -04:00
const finalMessage = updatedPackages.content.replace(`***${this.client.stores.emojis.loading} Rebuilding files...***`, `***${this.client.stores.emojis.success} Files rebuilt***`);
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);
}
}
}