Skip to content

Commit

Permalink
add proper types to shell execution
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Nov 27, 2023
1 parent 006ba54 commit f5ab213
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
9 changes: 4 additions & 5 deletions src/events/buttonUsed.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Component } from "@interfaces/components";
import { Event } from "@interfaces/events";
import { Client, ButtonInteraction } from "@client";
import { info } from "@helpers/logger";
Expand All @@ -8,11 +7,11 @@ export default {
async execute(client: Client, interaction: ButtonInteraction) {
client.storeAction("button", interaction);

if (client.verbose) console.log(`${info}Button used`);
let button: Component;
if (client.verbose) console.log(`${info}Button used!`);

if (interaction.customId.startsWith("pollVote__")) button = client.buttons.get("pollVote");
else button = client.buttons.get(interaction.customId);
const button = interaction.customId.startsWith("pollVote__")
? client.buttons.get("pollVote")
: client.buttons.get(interaction.customId);

if (button) return button.execute(client, interaction);
},
Expand Down
38 changes: 21 additions & 17 deletions src/helpers/exec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
// spawn a child process and execute shell command
// borrowed from https://github.com/mout/mout/ build script
// borrowed from Miller Medeiros on https://gist.github.com/millermedeiros/4724047
// released under MIT License
// version: 0.1.0 (2021/08/13)

import { dev } from "@json/tokens.json";
import { spawn, SpawnOptions } from "child_process";

/**
* spawn a child process and execute shell command
* borrowed from https://github.com/mout/mout/ build script
* borrowed from Miller Medeiros on https://gist.github.com/millermedeiros/4724047
* released under MIT License
* version: 0.1.0 (2021/08/13)
*/

/**
* execute a single shell command where "cmd" is a string
* @author Juknum
* @author Miller Medeiros
* @param cmd what command to run
* @param cb what to run afterwards (grabs error too)
* @param cb callback to run afterwards (grabs error too)
* @param options extra command line options for child_process
*/
export const execSync = (cmd: string, cb: Function, options = undefined) => {
export const execSync = (cmd: string, cb: Function, options: SpawnOptions = {}) => {
// this would be way easier on a shell/bash script :P
const child_process = require("child_process");
const parts = cmd.split(/\s+/g);

// don't spam console logs in production with command outputs
let opt = { stdio: dev ? "inherit" : "ignore" };

if (options !== undefined) opt = Object.assign({}, opt, options);
const opt: SpawnOptions = {
// ordered as [stdin, stdout, stderr]
stdio: dev ? "inherit" : ["ignore", "ignore", "inherit"],
...options,
};

const p = child_process.spawn(parts[0], parts.slice(1), opt);
const p = spawn(parts[0], parts.slice(1), opt);
p.on("exit", (code: number) => {
let err = null;
if (code) {
Expand All @@ -38,7 +42,7 @@ export const execSync = (cmd: string, cb: Function, options = undefined) => {

// execute multiple commands in series
// this could be replaced by any flow control lib
export const seriesSync = (cmds: string[], cb: Function, options = undefined) => {
export const seriesSync = (cmds: string[], cb: Function, options: SpawnOptions = {}) => {
const execNext = () => {
execSync(
cmds.shift(),
Expand All @@ -56,7 +60,7 @@ export const seriesSync = (cmds: string[], cb: Function, options = undefined) =>
execNext();
};

export const exec = async (cmd: string, options: any = undefined) => {
export const exec = async (cmd: string, options: SpawnOptions = {}) => {
return new Promise((res, rej) => {
execSync(
cmd,
Expand All @@ -69,7 +73,7 @@ export const exec = async (cmd: string, options: any = undefined) => {
});
};

export const series = async (cmds: string[], options = undefined) => {
export const series = async (cmds: string[], options: SpawnOptions = {}) => {
return new Promise((res, rej) => {
seriesSync(
cmds,
Expand Down
7 changes: 0 additions & 7 deletions src/helpers/images/tile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ export async function tile(origin: ImageSource, options: TileOptions = {}): Prom
ctx.restore();
};

/**
* Follows this pattern:
* x x x x x x . x . . x . . . .
* x x x -> x . x -> x x x -> . x . OR x x x
* x x x x x x . x . . x . . . .
*/

if (options.random == "rotation") {
// grid to get all possible rotation states matched with each other
// specific configuration originally by Pomi108
Expand Down

0 comments on commit f5ab213

Please sign in to comment.