@book000/create-ts 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +1 -0
- package/dist/index.mjs +543 -0
- package/package.json +50 -0
- package/templates/gitignore/Node.gitignore +147 -0
- package/templates/nodejs/base/package.json +54 -0
- package/templates/nodejs/base/pnpm-lock.yaml +5639 -0
- package/templates/nodejs/base/pnpm-workspace.yaml +5 -0
- package/templates/nodejs/base/src/main.ts +13 -0
- package/templates/nodejs/base/template.json +8 -0
- package/templates/nodejs/base/test/smoke.test.ts +32 -0
- package/templates/nodejs/base/tsconfig.test.json +7 -0
- package/templates/nodejs/common/.depcheckrc.json +3 -0
- package/templates/nodejs/common/.devcontainer/devcontainer.json +9 -0
- package/templates/nodejs/common/.fixpackrc +4 -0
- package/templates/nodejs/common/.prettierrc.yml +7 -0
- package/templates/nodejs/common/Dockerfile +31 -0
- package/templates/nodejs/common/entrypoint.sh +4 -0
- package/templates/nodejs/common/eslint.config.mjs +1 -0
- package/templates/nodejs/common/package.json +29 -0
- package/templates/nodejs/common/pnpm-workspace.yaml +5 -0
- package/templates/nodejs/common/renovate.json +4 -0
- package/templates/nodejs/common/tsconfig.json +22 -0
- package/templates/nodejs/config-batch/package.json +59 -0
- package/templates/nodejs/config-batch/pnpm-lock.yaml +6128 -0
- package/templates/nodejs/config-batch/pnpm-workspace.yaml +5 -0
- package/templates/nodejs/config-batch/src/config.ts +7 -0
- package/templates/nodejs/config-batch/src/main.ts +38 -0
- package/templates/nodejs/config-batch/template.json +11 -0
- package/templates/nodejs/config-batch/test/config.test.ts +76 -0
- package/templates/nodejs/config-batch/tsconfig.test.json +7 -0
- package/templates/nodejs/discord-bot/package.json +60 -0
- package/templates/nodejs/discord-bot/pnpm-lock.yaml +6315 -0
- package/templates/nodejs/discord-bot/pnpm-workspace.yaml +5 -0
- package/templates/nodejs/discord-bot/src/config.ts +8 -0
- package/templates/nodejs/discord-bot/src/discord.ts +35 -0
- package/templates/nodejs/discord-bot/src/main.ts +53 -0
- package/templates/nodejs/discord-bot/template.json +11 -0
- package/templates/nodejs/discord-bot/test/discord.test.ts +68 -0
- package/templates/nodejs/discord-bot/tsconfig.test.json +7 -0
- package/templates/nodejs/fastify/package.json +59 -0
- package/templates/nodejs/fastify/pnpm-lock.yaml +6077 -0
- package/templates/nodejs/fastify/pnpm-workspace.yaml +5 -0
- package/templates/nodejs/fastify/src/main.ts +34 -0
- package/templates/nodejs/fastify/template.json +9 -0
- package/templates/nodejs/fastify/test/app.test.ts +49 -0
- package/templates/nodejs/fastify/tsconfig.test.json +7 -0
- package/templates/workflows/add-reviewer.yml +15 -0
- package/templates/workflows/docker.yml +92 -0
- package/templates/workflows/nodejs-ci-pnpm.yml +79 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Client } from 'discord.js'
|
|
2
|
+
import { ConfigFramework } from '@book000/node-utils'
|
|
3
|
+
import { ConfigInterface } from './config'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Discord Bot のメインクラス
|
|
7
|
+
*/
|
|
8
|
+
export class Discord {
|
|
9
|
+
private readonly client: Client
|
|
10
|
+
private readonly config: ConfigFramework<ConfigInterface>
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param client - Discord クライアント
|
|
14
|
+
* @param config - 設定フレームワーク
|
|
15
|
+
*/
|
|
16
|
+
constructor(client: Client, config: ConfigFramework<ConfigInterface>) {
|
|
17
|
+
this.client = client
|
|
18
|
+
this.config = config
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 設定フレームワークを返す
|
|
23
|
+
*/
|
|
24
|
+
protected getConfig(): ConfigFramework<ConfigInterface> {
|
|
25
|
+
return this.config
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Bot の準備完了時に呼ばれる
|
|
30
|
+
*/
|
|
31
|
+
public onReady(): void {
|
|
32
|
+
console.log(`Logged in as ${this.client.user?.tag ?? 'unknown'}`)
|
|
33
|
+
// TODO: this.getConfig().get('key') で設定値を取得して処理を実装する
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Client, GatewayIntentBits } from 'discord.js'
|
|
2
|
+
import { ConfigFramework } from '@book000/node-utils'
|
|
3
|
+
import { ConfigInterface } from './config'
|
|
4
|
+
import { Discord } from './discord'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 設定フレームワーク実装
|
|
8
|
+
*/
|
|
9
|
+
class Configuration extends ConfigFramework<ConfigInterface> {
|
|
10
|
+
/**
|
|
11
|
+
* バリデーションルールを返す
|
|
12
|
+
*/
|
|
13
|
+
protected validates(): Record<string, (config: ConfigInterface) => boolean> {
|
|
14
|
+
return {
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
16
|
+
'token is required': (config) => config.token !== undefined,
|
|
17
|
+
'token is string': (config) => typeof config.token === 'string',
|
|
18
|
+
// TODO: バリデーションルールを追加する
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* エントリポイント
|
|
25
|
+
*/
|
|
26
|
+
async function main(): Promise<void> {
|
|
27
|
+
const config = new Configuration('./data/config.json')
|
|
28
|
+
config.load()
|
|
29
|
+
if (!config.validate()) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`Configuration validation failed: ${config.getValidateFailures().join(', ')}`
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const client = new Client({
|
|
36
|
+
intents: [GatewayIntentBits.Guilds],
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
const discord = new Discord(client, config)
|
|
40
|
+
|
|
41
|
+
client.once('ready', () => {
|
|
42
|
+
console.log(`Logged in as ${client.user?.tag ?? 'unknown'}`)
|
|
43
|
+
discord.onReady()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
await client.login(config.get('token'))
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
main().catch((error: unknown) => {
|
|
50
|
+
console.error(error)
|
|
51
|
+
// eslint-disable-next-line unicorn/no-process-exit
|
|
52
|
+
process.exit(1)
|
|
53
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"configSchema": true,
|
|
3
|
+
"dependencies": ["discord.js", "@book000/node-utils"],
|
|
4
|
+
"devDependencies": ["typescript-json-schema"],
|
|
5
|
+
"scripts": {
|
|
6
|
+
"generate-schema": "typescript-json-schema --required src/config.ts ConfigInterface -o schema/Configuration.json"
|
|
7
|
+
},
|
|
8
|
+
"depcheckIgnore": ["typescript-json-schema"],
|
|
9
|
+
"src": ["src/main.ts", "src/config.ts", "src/discord.ts"],
|
|
10
|
+
"testSrc": ["test/discord.test.ts", "tsconfig.test.json"]
|
|
11
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* discord-bot バリアントのテスト
|
|
3
|
+
*
|
|
4
|
+
* discord.js の Client が正しく初期化できること、および Discord クラスの
|
|
5
|
+
* 基本動作を確認する。実際のトークンでのログインは行わない。
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { Client, GatewayIntentBits } from 'discord.js'
|
|
9
|
+
import { Discord } from '../src/discord'
|
|
10
|
+
import type { ConfigFramework } from '@book000/node-utils'
|
|
11
|
+
import type { ConfigInterface } from '../src/config'
|
|
12
|
+
|
|
13
|
+
/** テスト用 ConfigFramework モック */
|
|
14
|
+
const createMockConfig = (): ConfigFramework<ConfigInterface> =>
|
|
15
|
+
({
|
|
16
|
+
get: jest.fn(),
|
|
17
|
+
load: jest.fn(),
|
|
18
|
+
validate: jest.fn().mockReturnValue(true),
|
|
19
|
+
getValidateFailures: jest.fn().mockReturnValue([]),
|
|
20
|
+
}) as unknown as ConfigFramework<ConfigInterface>
|
|
21
|
+
|
|
22
|
+
describe('Discord クラス', () => {
|
|
23
|
+
let client: Client
|
|
24
|
+
|
|
25
|
+
beforeEach(() => {
|
|
26
|
+
client = new Client({ intents: [GatewayIntentBits.Guilds] })
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
client.destroy()
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
it('Discord インスタンスを作成できる', () => {
|
|
34
|
+
const mockConfig = createMockConfig()
|
|
35
|
+
const discord = new Discord(client, mockConfig)
|
|
36
|
+
expect(discord).toBeDefined()
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
it('onReady() を呼び出せる', () => {
|
|
40
|
+
const mockConfig = createMockConfig()
|
|
41
|
+
const discord = new Discord(client, mockConfig)
|
|
42
|
+
const consoleSpy = jest.spyOn(console, 'log').mockImplementation()
|
|
43
|
+
|
|
44
|
+
expect(() => discord.onReady()).not.toThrow()
|
|
45
|
+
|
|
46
|
+
consoleSpy.mockRestore()
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
describe('discord.js Client', () => {
|
|
51
|
+
it('Client インスタンスを作成できる', () => {
|
|
52
|
+
const testClient = new Client({ intents: [GatewayIntentBits.Guilds] })
|
|
53
|
+
expect(testClient).toBeDefined()
|
|
54
|
+
testClient.destroy()
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('複数の Intent を設定できる', () => {
|
|
58
|
+
const testClient = new Client({
|
|
59
|
+
intents: [
|
|
60
|
+
GatewayIntentBits.Guilds,
|
|
61
|
+
GatewayIntentBits.GuildMessages,
|
|
62
|
+
GatewayIntentBits.MessageContent,
|
|
63
|
+
],
|
|
64
|
+
})
|
|
65
|
+
expect(testClient).toBeDefined()
|
|
66
|
+
testClient.destroy()
|
|
67
|
+
})
|
|
68
|
+
})
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@book000/template",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "book000",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"preinstall": "npx only-allow pnpm",
|
|
9
|
+
"start": "tsx ./src/main.ts",
|
|
10
|
+
"dev": "tsx watch ./src/main.ts",
|
|
11
|
+
"test": "jest --runInBand --passWithNoTests --detectOpenHandles --forceExit",
|
|
12
|
+
"lint": "run-z lint:prettier,lint:eslint,lint:tsc",
|
|
13
|
+
"lint:prettier": "prettier --check src/",
|
|
14
|
+
"lint:eslint": "eslint src/ -c eslint.config.mjs",
|
|
15
|
+
"lint:tsc": "tsc --noEmit",
|
|
16
|
+
"fix": "run-z fix:prettier fix:eslint",
|
|
17
|
+
"fix:eslint": "eslint src/ -c eslint.config.mjs --fix",
|
|
18
|
+
"fix:prettier": "prettier --write src/"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@fastify/cors": "*",
|
|
22
|
+
"fastify": "*"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@book000/eslint-config": "*",
|
|
26
|
+
"@types/jest": "*",
|
|
27
|
+
"@types/node": "*",
|
|
28
|
+
"eslint": "*",
|
|
29
|
+
"fastify-raw-body": "*",
|
|
30
|
+
"jest": "*",
|
|
31
|
+
"prettier": "*",
|
|
32
|
+
"run-z": "*",
|
|
33
|
+
"ts-jest": "*",
|
|
34
|
+
"tsx": "*",
|
|
35
|
+
"typescript": "*"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=24"
|
|
39
|
+
},
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/book000/template.git"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/book000/template/issues"
|
|
46
|
+
},
|
|
47
|
+
"jest": {
|
|
48
|
+
"preset": "ts-jest",
|
|
49
|
+
"testEnvironment": "node",
|
|
50
|
+
"transform": {
|
|
51
|
+
"^.+\\.tsx?$": [
|
|
52
|
+
"ts-jest",
|
|
53
|
+
{
|
|
54
|
+
"tsconfig": "tsconfig.test.json"
|
|
55
|
+
}
|
|
56
|
+
]
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|