diff --git a/package.json b/package.json index 41de1be..5e5afdd 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { - "name": "create", + "name": "@sern/create-bot", "version": "1.0.0", "main": "./dist/index.js", "license": "MIT", "bin": { - "@sern/create": "index.js" + "@sern/create-bot": "index.js" }, "devDependencies": { "@types/minimist": "^1.2.2", diff --git a/src/index.ts b/src/index.ts index c2964ea..4766a4b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,8 +1,9 @@ -import { greenBright, redBright, underline } from 'colorette'; +import { greenBright, red, redBright } from 'colorette'; import prompt, { PromptObject } from 'prompts'; import minimist from 'minimist' import { writeFile } from 'fs/promises' -import { join } from 'path' +import path from 'path' +import fs from 'fs' const argv = minimist<{ template?: string }>(process.argv.slice(2)); @@ -43,8 +44,25 @@ async function runInteractive() { const result: prompt.Answers<'template'|'name'> = await prompt([ template, name, - ]); + ], + { + onCancel: () => { + throw new Error(red('✖') + ' Operation cancelled') + }, + }, + ); + const root = path.join(cwd, result.name); + + + let overwrite = false; + if (overwrite) { + emptyDir(root) + } else if (!fs.existsSync(root)) { + fs.mkdirSync(root, { recursive: true }) + } + //determine template const isTypescript = (result.template as string).includes('typescript'); + const configJson = { language : isTypescript ? 'typescript' : 'javascript', paths: { @@ -52,16 +70,15 @@ async function runInteractive() { cmds_dir: 'commands' }, }; + //console.log(greenBright('Writing sern.config.json to '+ result.name + "/sern.config.json")); - console.log(greenBright('Writing sern.config.json to '+ result.name + "/sern.config.json")); - try{ - await writeFile(join(cwd, result.name, 'sern.config.json'), JSON.stringify(configJson), 'utf8'); - } catch(E) { - console.error(redBright(E)); - process.exit(1); - }; +// try{ +// await writeFile(path.join(cwd, result.name, 'sern.config.json'), JSON.stringify(configJson), 'utf8'); +// } catch(E) { +// console.error(redBright(E)); +// process.exit(1); +// }; - } @@ -78,7 +95,17 @@ async function init() { } } - +function emptyDir(dir: string) { + if (!fs.existsSync(dir)) { + return + } + for (const file of fs.readdirSync(dir)) { + if (file === '.git') { + continue + } + fs.rmSync(path.resolve(dir, file), { recursive: true, force: true }) + } +} init() diff --git a/template-javascript-esm/placeholder b/template-javascript-esm/placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/template-javascript/placeholder b/template-javascript/placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/template-js-esm/.gitignore b/template-js-esm/.gitignore new file mode 100644 index 0000000..7af7f04 --- /dev/null +++ b/template-js-esm/.gitignore @@ -0,0 +1,2 @@ +/node_modules +.env \ No newline at end of file diff --git a/template-js-esm/README.md b/template-js-esm/README.md new file mode 100644 index 0000000..4640904 --- /dev/null +++ b/template-js-esm/README.md @@ -0,0 +1 @@ +# TODO diff --git a/template-js-esm/package.json b/template-js-esm/package.json new file mode 100644 index 0000000..7459fd8 --- /dev/null +++ b/template-js-esm/package.json @@ -0,0 +1,24 @@ +{ + "name": "js-example", + "version": "1.0.0", + "description": "", + "private": true, + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "javacsript", + "sern", + "discord.js" + ], + "license": "UNLICENSED", + "dependencies": { + "@sern/handler": "^3.0.0", + "discord.js": "latest" + }, + "devDependencies": { + "@types/node": "^17.0.25" + }, + "type": "module" +} diff --git a/template-js-esm/src/commands/ping.js b/template-js-esm/src/commands/ping.js new file mode 100644 index 0000000..8e7be84 --- /dev/null +++ b/template-js-esm/src/commands/ping.js @@ -0,0 +1,11 @@ +const { CommandType, commandModule } = require('@sern/handler'); + +exports.default = commandModule({ + type: CommandType.Both, + plugins: [], + description: 'A ping command', + //alias : [], + execute: async (ctx, args) => { + await ctx.reply('Pong 🏓'); + }, +}); diff --git a/template-js-esm/src/index.js b/template-js-esm/src/index.js new file mode 100644 index 0000000..f7eb9bf --- /dev/null +++ b/template-js-esm/src/index.js @@ -0,0 +1,35 @@ +import { Client, GatewayIntentBits } from 'discord.js'; +import { Sern, single } from '@sern/handler'; + +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMembers, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, // Make sure this is enabled for text commands! + ], +}); + +/** + * Where all of your dependencies are composed. + * '@sern/client' is usually your Discord Client. + * View documentation for pluggable dependencies + * Configure your dependency root to your liking. + * It follows the npm package iti https://itijs.org/. + * Use this function to access all of your dependencies. + * This is used for external event modules as well + */ +await makeDependencies({ + build: (root) => + root + .add({ '@sern/client': single(() => client) }) +}); + +//View docs for all options +Sern.init({ + defaultPrefix: '!', // removing defaultPrefix will shut down text commands + commands: 'src/commands', + // events: 'src/events' (optional), +}); + +client.login(); diff --git a/template-js/.gitignore b/template-js/.gitignore new file mode 100644 index 0000000..7af7f04 --- /dev/null +++ b/template-js/.gitignore @@ -0,0 +1,2 @@ +/node_modules +.env \ No newline at end of file diff --git a/template-js/README.md b/template-js/README.md new file mode 100644 index 0000000..4640904 --- /dev/null +++ b/template-js/README.md @@ -0,0 +1 @@ +# TODO diff --git a/template-js/package.json b/template-js/package.json new file mode 100644 index 0000000..7d71725 --- /dev/null +++ b/template-js/package.json @@ -0,0 +1,23 @@ +{ + "name": "js-example", + "version": "1.0.0", + "description": "", + "private": true, + "main": "src/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "javacsript", + "sern", + "discord.js" + ], + "license": "UNLICENSED", + "dependencies": { + "@sern/handler": "^3.0.0", + "discord.js": "latest" + }, + "devDependencies": { + "@types/node": "^17.0.25" + } +} diff --git a/template-js/src/commands/ping.js b/template-js/src/commands/ping.js new file mode 100644 index 0000000..8e7be84 --- /dev/null +++ b/template-js/src/commands/ping.js @@ -0,0 +1,11 @@ +const { CommandType, commandModule } = require('@sern/handler'); + +exports.default = commandModule({ + type: CommandType.Both, + plugins: [], + description: 'A ping command', + //alias : [], + execute: async (ctx, args) => { + await ctx.reply('Pong 🏓'); + }, +}); diff --git a/template-js/src/index.js b/template-js/src/index.js new file mode 100644 index 0000000..656bbd7 --- /dev/null +++ b/template-js/src/index.js @@ -0,0 +1,38 @@ +const { Client, GatewayIntentBits } = require('discord.js'); +const { Sern, single, DefaultLogging } = require('@sern/handler'); + +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMembers, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, // Make sure this is enabled for text commands! + ], +}); + +/** + * Where all of your dependencies are composed. + * '@sern/client' is usually your Discord Client. + * View documentation for pluggable dependencies + * Configure your dependency root to your liking. + * It follows the npm package iti https://itijs.org/. + * Use this function to access all of your dependencies. + * This is used for external event modules as well + */ + +async function init() { + await makeDependencies({ + build: (root) => + root.add({ '@sern/client': single(() => client) }) + }); + + //View docs for all options + Sern.init({ + defaultPrefix: '!', // removing defaultPrefix will shut down text commands + commands: 'src/commands', + // events: 'src/events' (optional), + }); +} + +init(); +client.login(); diff --git a/template-ts-esm/.gitignore b/template-ts-esm/.gitignore new file mode 100644 index 0000000..c7fb7a4 --- /dev/null +++ b/template-ts-esm/.gitignore @@ -0,0 +1,3 @@ +/node_modules +/dist +.env \ No newline at end of file diff --git a/template-ts-esm/README.md b/template-ts-esm/README.md new file mode 100644 index 0000000..4640904 --- /dev/null +++ b/template-ts-esm/README.md @@ -0,0 +1 @@ +# TODO diff --git a/template-ts-esm/package.json b/template-ts-esm/package.json new file mode 100644 index 0000000..83a3167 --- /dev/null +++ b/template-ts-esm/package.json @@ -0,0 +1,26 @@ +{ + "name": "ts-example", + "version": "1.0.0", + "description": "", + "main": "dist/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "tsc", + "start": "tsc && node ./dist/index.js" + }, + "keywords": [ + "typescript", + "sern", + "discord.js" + ], + "license": "UNLICENSED", + "dependencies": { + "@sern/handler": "^3.0.0", + "discord.js": "^14.7.1" + }, + "devDependencies": { + "@types/node": "^17.0.25", + "typescript": "5.0" + }, + "type": "module" +} diff --git a/template-ts-esm/src/commands/ping.ts b/template-ts-esm/src/commands/ping.ts new file mode 100644 index 0000000..18a7d3a --- /dev/null +++ b/template-ts-esm/src/commands/ping.ts @@ -0,0 +1,11 @@ +import { commandModule, CommandType } from '@sern/handler'; + +export default commandModule({ + type: CommandType.Both, + plugins: [], + description: 'A ping command', + //alias : [], + execute: async (ctx, args) => { + await ctx.reply('Pong 🏓'); + }, +}); diff --git a/template-ts-esm/src/index.ts b/template-ts-esm/src/index.ts new file mode 100644 index 0000000..37af360 --- /dev/null +++ b/template-ts-esm/src/index.ts @@ -0,0 +1,38 @@ +import { Client, GatewayIntentBits } from 'discord.js'; +import { + Sern, + makeDependencies, + single, +} from '@sern/handler'; + +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMembers, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, //Make sure this is enabled for text commands! + ], +}); + +/** + * Where all of your dependencies are composed. + * '@sern/client' is usually your Discord Client. + * View documentation for pluggable dependencies + * Configure your dependency root to your liking. + * It follows the npm package iti https://itijs.org/. + * Use this function to access all of your dependencies. + * This is used for external event modules as well + */ +await makeDependencies({ + build: (root) => + root.add({ '@sern/client': single(() => client) }) +}); + +//View docs for all options +Sern.init({ + defaultPrefix: '!', // removing defaultPrefix will shut down text commands + commands: 'dist/commands', + // events: 'dist/events' (optional), +}); + +client.login(); diff --git a/template-ts-esm/tsconfig.json b/template-ts-esm/tsconfig.json new file mode 100644 index 0000000..4f0157c --- /dev/null +++ b/template-ts-esm/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "resolveJsonModule": true, + "target": "ESNext", + "module": "ESNext", + "outDir": "dist", + "rootDir": "src", + "strict": true, + "esModuleInterop": true, + "noImplicitAny": true, + "strictNullChecks": true, + "importsNotUsedAsValues": "error", + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + } +} diff --git a/template-ts/.gitignore b/template-ts/.gitignore new file mode 100644 index 0000000..c7fb7a4 --- /dev/null +++ b/template-ts/.gitignore @@ -0,0 +1,3 @@ +/node_modules +/dist +.env \ No newline at end of file diff --git a/template-ts/README.md b/template-ts/README.md new file mode 100644 index 0000000..4640904 --- /dev/null +++ b/template-ts/README.md @@ -0,0 +1 @@ +# TODO diff --git a/template-ts/package.json b/template-ts/package.json new file mode 100644 index 0000000..6ac1586 --- /dev/null +++ b/template-ts/package.json @@ -0,0 +1,25 @@ +{ + "name": "ts-example", + "version": "1.0.0", + "private": true, + "description": "", + "main": "dist/index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "tsc", + "start": "tsc && node ./dist/index.js" + }, + "keywords": [ + "typescript", + "sern", + "discord.js" + ], + "dependencies": { + "@sern/handler": "^3.0.0", + "discord.js": "latest" + }, + "devDependencies": { + "@types/node": "^17.0.25", + "typescript": "^5.0" + } +} diff --git a/template-ts/src/commands/ping.ts b/template-ts/src/commands/ping.ts new file mode 100644 index 0000000..18a7d3a --- /dev/null +++ b/template-ts/src/commands/ping.ts @@ -0,0 +1,11 @@ +import { commandModule, CommandType } from '@sern/handler'; + +export default commandModule({ + type: CommandType.Both, + plugins: [], + description: 'A ping command', + //alias : [], + execute: async (ctx, args) => { + await ctx.reply('Pong 🏓'); + }, +}); diff --git a/template-ts/src/index.ts b/template-ts/src/index.ts new file mode 100644 index 0000000..6c34015 --- /dev/null +++ b/template-ts/src/index.ts @@ -0,0 +1,41 @@ +import { Client, GatewayIntentBits } from 'discord.js'; +import { + Sern, + single, + makeDependencies +} from '@sern/handler'; + +const client = new Client({ + intents: [ + GatewayIntentBits.Guilds, + GatewayIntentBits.GuildMembers, + GatewayIntentBits.GuildMessages, + GatewayIntentBits.MessageContent, //Make sure this is enabled for text commands! + ], +}); + +/** + * Where all of your dependencies are composed. + * '@sern/client' is usually your Discord Client. + * View documentation for pluggable dependencies + * Configure your dependency root to your liking. + * It follows the npm package iti https://itijs.org/. + * Use this function to access all of your dependencies. + * This is used for external event modules as well + */ +async function init() { + await makeDependencies({ + build: (root) => + root.add({ '@sern/client': single(() => client) }) + }); + + //View docs for all options + Sern.init({ + defaultPrefix: '!', // removing defaultPrefix will shut down text commands + commands: 'dist/commands', + // events: 'dist/events' (optional), + }); +} + + +client.login(); diff --git a/template-ts/tsconfig.json b/template-ts/tsconfig.json new file mode 100644 index 0000000..c87a775 --- /dev/null +++ b/template-ts/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "resolveJsonModule": true, + "target": "ESNext", + "module": "CommonJS", + "outDir": "dist", + "rootDir": "src", + "strict": true, + "esModuleInterop": true, + "noImplicitAny": true, + "strictNullChecks": true, + "importsNotUsedAsValues": "error", + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + } +} diff --git a/template-typescript-esm/placeholder b/template-typescript-esm/placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/template-typescript/placeholder b/template-typescript/placeholder deleted file mode 100644 index e69de29..0000000