feat(*): refactored code and added custom handling of commands

This commit is contained in:
EvolutionX
2022-04-18 22:14:25 +05:30
parent 192e50316b
commit 0e97b7db8a
6 changed files with 2341 additions and 542 deletions

View File

@@ -1,11 +0,0 @@
{
"env": {
"browser": true,
"es2021": true,
"node": true
},
"extends": ["eslint:recommended"],
"rules": {
"no-mixed-spaces-and-tabs": "off"
}
}

View File

@@ -31,7 +31,7 @@ jobs:
- name: Run linters
uses: wearerequired/lint-action@42567b31ed576cdd5a431d77ca5bc8822430d1d0 # tag=v1
with:
eslint: true
eslint: false
prettier: true
github_token: ${{ secrets.GITHUB_TOKEN }}
auto_fix: false

2705
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -2,10 +2,11 @@
"name": "@sern-handler/cli",
"version": "0.1.0",
"description": "A CLI for @sern-handler",
"main": "src/index.js",
"exports": "./src/index.js",
"bin": {
"sern": "./src/index.js"
},
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
@@ -20,14 +21,15 @@
"sern",
"sern-handler"
],
"author": "",
"author": "EvolutionX",
"license": "MIT",
"bugs": {
"url": "https://github.com/sern-handler/cli/issues"
},
"homepage": "https://github.com/sern-handler/cli#readme",
"dependencies": {
"yargs": "^17.4.1"
"find-up": "6.3.0",
"inquirer": "8.2.2"
},
"devDependencies": {
"cz-conventional-changelog": "3.3.0",

130
src/commands/init.js Normal file
View File

@@ -0,0 +1,130 @@
import { findUp } from 'find-up';
import pkg from 'inquirer';
const { prompt } = pkg;
// TODO refactor the intents stuff and add more questions
const Intents = [
'DIRECT_MESSAGES',
'DIRECT_MESSAGE_REACTIONS',
'DIRECT_MESSAGE_TYPING',
'GUILDS',
'GUILD_BANS',
'GUILD_EMOJIS_AND_STICKERS',
'GUILD_INTEGRATIONS',
'GUILD_INVITES',
'GUILD_MEMBERS',
'GUILD_MESSAGES',
'GUILD_MESSAGE_REACTIONS',
'GUILD_MESSAGE_TYPING',
'GUILD_PRESENCES',
'GUILD_SCHEDULED_EVENTS',
'GUILD_VOICE_STATES',
'GUILD_VOICE_STATES',
'GUILD_WEBHOOKS',
].map((i, j) => ({ name: i, value: j, short: `${j}` }));
export async function init({ flags }) {
if (flags?.includes('y')) {
console.log("I see a flag there! Seems like you're lazy!\nBye!");
process.exit(0);
}
const pkg = await findUp('package.json');
if (!pkg) {
console.log('No package.json found');
return process.exit(1);
}
await whichLang();
const input = await kindPrefix();
if (input.prefix !== 'app_cmds') {
await legacy();
}
await intents();
}
const lang = {
message: 'What language you want the project to be in?',
name: 'lang',
type: 'list',
choices: [
{
name: 'JavaScript',
value: 'js',
type: 'choice',
},
{
name: 'TypeScript',
value: 'ts',
type: 'choice',
},
],
};
const intent = {
message: 'What intents do you want to keep?',
type: 'checkbox',
name: 'intents',
choices: Intents,
validate(a) {
if (a.length < 1) {
return 'You must choose at least one intent!';
}
if (a.length === 17) {
return 'You do NOT need all intents!';
}
return true;
},
default: [3, 9],
};
const legacy_prefix = {
message: 'What is the legacy prefix for your bot?',
name: 'prefix',
type: 'input',
default: '!',
validate(a) {
if (a.length < 1) {
return 'You must choose a prefix!';
}
return true;
},
};
const kind_prefix = {
message:
'What kind of prefix your bot will use? Select "Both" for both prefixes.',
name: 'prefix',
type: 'list',
choices: [
{
name: 'Prefix [Message Based]',
value: 'legacy',
},
{
name: 'Interactions [Slash Commands, Context Menu Commands]',
value: 'app_cmds',
},
{
name: 'Both',
value: 'both',
type: 'choice',
},
],
};
async function whichLang() {
const a = await prompt([lang]);
return a;
}
async function kindPrefix() {
const a = await prompt([kind_prefix]);
return a;
}
async function legacy() {
const a = await prompt([legacy_prefix]);
return a;
}
async function intents() {
const a = await prompt([intent]);
return a;
}

View File

@@ -1,10 +1,19 @@
#!/usr/bin/env node
const yargs = require('yargs');
const usage = '\nUsage: sern init';
yargs
.usage(usage)
.option('i', {
describe: 'Set up basic project without any customizations',
boolean: true,
})
.help(true).argv;
import { init } from './commands/init.js';
const regex = /(?<=--|-)\w+/gm;
const flags = process.argv.slice(2).join(' ').match(regex);
// TODO codegolf it maybe? @HighArcs @jacoobes
const args = process.argv
.slice(2)
.join(' ')
.trim()
.split(/ +/)
.filter((e) => !/(--|-)\w+/gm.test(e));
const cmdName = args[0];
const commands = new Map([['init', init]]);
const found = commands.get(cmdName);
if (found) {
await found({ args, flags });
} else console.log('Unknown Command // in future help maybe??');