Skip to content

Commit

Permalink
Merge pull request #57 from m1-dev/master
Browse files Browse the repository at this point in the history
feat: create-commandkit typescript template
  • Loading branch information
notunderctrl authored Feb 2, 2024
2 parents a0df3cc + 646b641 commit d226d2f
Show file tree
Hide file tree
Showing 18 changed files with 264 additions and 21 deletions.
19 changes: 9 additions & 10 deletions packages/create-commandkit/src/functions/installDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ interface InstallDepsProps {
stdio: IOType;
}

export function installDeps({
manager,
dir,
lang,
stdio = 'pipe',
}: InstallDepsProps) {
execSync(`${manager} add ${dependencies[lang].join(' ')}`, {
cwd: dir,
stdio,
});
export function installDeps({ manager, dir, lang, stdio = 'pipe' }: InstallDepsProps) {
const depsCommand = `${manager} add ${dependencies[lang].dependencies.join(' ')}`;
const devDepsCommand = `${manager} add ${dependencies.ts.devDependencies.join(' ')}`;

execSync(depsCommand, { cwd: dir, stdio });

if (lang == 'ts') {
execSync(devDepsCommand, { cwd: dir, stdio });
}
}
12 changes: 10 additions & 2 deletions packages/create-commandkit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
console.clear();

import type { ModuleType, PackageManager } from './types';
import type { Language, ModuleType, PackageManager } from './types';

import { intro, text, select, password, confirm, outro } from '@clack/prompts';
import { commandkit, hints, outroMsg } from './utils';
Expand Down Expand Up @@ -46,6 +46,14 @@ const manager = (await select({
],
})) as PackageManager;

const lang = (await select({
message: 'Select the language to use:',
options: [
{ label: 'JavaScript', value: 'js' },
{ label: 'TypeScript', value: 'ts' },
],
})) as Language;

const type = (await select({
message: 'Select a module type:',
options: [
Expand Down Expand Up @@ -75,7 +83,7 @@ const installNow = await confirm({
outro(colors.cyan('Setup complete.'));

await setup({ manager, dir, token, type });
await copyTemplates({ type, dir, lang: 'js' });
await copyTemplates({ type, dir, lang });

if (installNow) {
await installDeps({ manager, dir, lang: 'js', stdio: 'inherit' });
Expand Down
2 changes: 1 addition & 1 deletion packages/create-commandkit/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type Language = 'js';
export type Language = 'js' | 'ts';
export type PackageManager = 'npm' | 'pnpm' | 'yarn';
export type ModuleType = 'esm' | 'cjs';
28 changes: 20 additions & 8 deletions packages/create-commandkit/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

export const templates = {
js: {
esm: `${__dirname}/../templates/JavaScript/esm`,
cjs: `${__dirname}/../templates/JavaScript/cjs`,
esm: path.join(__dirname, '..', 'templates', 'JavaScript', 'esm'),
cjs: path.join(__dirname, '..', 'templates', 'JavaScript', 'cjs'),
},
ts: {
esm: path.join(__dirname, '..', 'templates', 'TypeScript', 'esm'),
cjs: path.join(__dirname, '..', 'templates', 'TypeScript', 'cjs'),
},
};

export const textColors = {
Expand All @@ -22,16 +26,24 @@ export const textColors = {
ts: ['#2480c5', '#2480c5'],
};

const baseDependencies = ['commandkit', 'discord.js', 'dotenv'];

export const dependencies = {
js: ['commandkit', 'discord.js', 'dotenv'],
js: {
dependencies: baseDependencies,
},
ts: {
dependencies: baseDependencies,
devDependencies: ['@types/node', 'typescript'],
},
};

export const commands = {
init: {
npm: 'npm init -y',
yarn: 'yarn init -y; yarn config set nodeLinker node-modules',
pnpm: 'pnpm init',
},
init: {
npm: 'npm init -y',
yarn: 'yarn init -y; yarn config set nodeLinker node-modules; yarn set version stable',
pnpm: 'pnpm init',
},
};

export const hints = {
Expand Down
33 changes: 33 additions & 0 deletions packages/create-commandkit/templates/TypeScript/cjs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# dependencies
node_modules

# build output
build
out
dist

# commandkit
.commandkit

# env
**/*.env*
!**/*.env.example*

# logging
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# yarn v2+
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# other
**/*.DS_Store
10 changes: 10 additions & 0 deletions packages/create-commandkit/templates/TypeScript/cjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Welcome to CommandKit

> This project was generated by [create-commandkit](https://npmjs.com/package/create-commandkit).
Thanks for choosing CommandKit to build your Discord bot!

## Useful links

- [Documentation](https://commandkit.js.org)
- [Discord](https://ctrl.lol/discord)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { defineConfig } = require('commandkit');

module.exports = defineConfig({
src: 'src',
main: 'index.js',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { CommandData, SlashCommandProps, CommandOptions } from 'commandkit';

export const data: CommandData = {
name: 'ping',
description: 'Replies with Pong',
};

export const run = ({ interaction }: SlashCommandProps) => {
interaction.reply('Pong!');
};

export const options: CommandOptions = {
// https://commandkit.js.org/typedef/CommandOptions
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Client } from 'discord.js';

export default (client: Client<true>) => {
console.log(`${client.user.tag} is online!`);
};
22 changes: 22 additions & 0 deletions packages/create-commandkit/templates/TypeScript/cjs/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import 'dotenv/config';

import { Client, IntentsBitField } from 'discord.js';
import { CommandKit } from 'commandkit';
import { join } from 'node:path';

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});

new CommandKit({
client,
eventsPath: join(__dirname, 'events'),
commandsPath: join(__dirname, 'commands'),
});

client.login(process.env.TOKEN);
20 changes: 20 additions & 0 deletions packages/create-commandkit/templates/TypeScript/cjs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": ["ES2022"],
"target": "ES2022",
"moduleResolution": "Bundler",
"module": "ES2022",
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"noUncheckedIndexedAccess": true,
"removeComments": true,
"allowJs": true,
"strict": true,
"noEmit": true,
"declaration": false
},
"include": ["src"],
"exclude": ["dist", "node_modules", ".commandkit"]
}
33 changes: 33 additions & 0 deletions packages/create-commandkit/templates/TypeScript/esm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# dependencies
node_modules

# build output
build
out
dist

# commandkit
.commandkit

# env
**/*.env*
!**/*.env.example*

# logging
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# yarn v2+
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# other
**/*.DS_Store
10 changes: 10 additions & 0 deletions packages/create-commandkit/templates/TypeScript/esm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Welcome to CommandKit

> This project was generated by [create-commandkit](https://npmjs.com/package/create-commandkit).
Thanks for choosing CommandKit to build your Discord bot!

## Useful links

- [Documentation](https://commandkit.js.org)
- [Discord](https://ctrl.lol/discord)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from 'commandkit';

export default defineConfig({
src: 'src',
main: 'index.js',
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type { CommandData, SlashCommandProps, CommandOptions } from 'commandkit';

export const data: CommandData = {
name: 'ping',
description: 'Replies with Pong',
};

export const run = ({ interaction }: SlashCommandProps) => {
interaction.reply('Pong!');
};

export const options: CommandOptions = {
// https://commandkit.js.org/typedef/CommandOptions
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Client } from 'discord.js';

export default (client: Client<true>) => {
console.log(`${client.user.tag} is online!`);
};
26 changes: 26 additions & 0 deletions packages/create-commandkit/templates/TypeScript/esm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dotenv/config';

import { Client, IntentsBitField } from 'discord.js';
import { CommandKit } from 'commandkit';

import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));

const client = new Client({
intents: [
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.GuildMembers,
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.MessageContent,
],
});

new CommandKit({
client,
eventsPath: join(__dirname, 'events'),
commandsPath: join(__dirname, 'commands'),
});

client.login(process.env.TOKEN);
20 changes: 20 additions & 0 deletions packages/create-commandkit/templates/TypeScript/esm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"lib": ["ES2022"],
"target": "ES2022",
"moduleResolution": "Bundler",
"module": "ES2022",
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"noUncheckedIndexedAccess": true,
"removeComments": true,
"allowJs": true,
"strict": true,
"noEmit": true,
"declaration": false
},
"include": ["src"],
"exclude": ["dist", "node_modules", ".commandkit"]
}

0 comments on commit d226d2f

Please sign in to comment.