@kokkoro/cli
Advanced tools
| import ora from 'ora'; | ||
| import prompts from 'prompts'; | ||
| import { join } from 'node:path'; | ||
| import { exit } from 'node:process'; | ||
| import { existsSync } from 'node:fs'; | ||
| import { cp } from 'node:fs/promises'; | ||
| import { promisify } from 'node:util'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { exec } from 'node:child_process'; | ||
| import { colors, config_path, plugins_path, TIP_ERROR, TIP_INFO } from './index.js'; | ||
| const promiseExec = promisify(exec); | ||
| const questions = [ | ||
| { | ||
| type: 'select', | ||
| name: 'style', | ||
| message: 'Which plugin style would you like to use', | ||
| choices: [ | ||
| { title: 'Javascript', value: 'javascript' }, | ||
| { title: 'Typescript (Hook)', value: 'hook' }, | ||
| { title: 'Typescript (Decorator)', value: 'decorator' }, | ||
| ], | ||
| }, | ||
| ]; | ||
| const onCancel = () => { | ||
| console.log(`${TIP_INFO} plugin module creation has been aborted.\n`); | ||
| exit(0); | ||
| }; | ||
| export default function (program) { | ||
| program | ||
| .command('plugin <name>') | ||
| .description('generate a kokkoro plugin project') | ||
| .action(async (name) => { | ||
| if (!existsSync(config_path)) { | ||
| console.error(`${TIP_ERROR} config file is not exists. If you want to create the file, use ${colors.cyan('kokkoro init')}.\n`); | ||
| exit(1); | ||
| } | ||
| try { | ||
| const response = await prompts(questions, { onCancel }); | ||
| const { style } = response; | ||
| const module_path = join(plugins_path, name); | ||
| const module_main = style === 'javascript' ? 'index.js' : 'lib/index.js'; | ||
| if (existsSync(module_path)) { | ||
| console.warn(`${TIP_ERROR} plugin directory already exists.\n`); | ||
| exit(1); | ||
| } | ||
| const url = join(import.meta.url, `../../template/${style}`); | ||
| const path = fileURLToPath(url); | ||
| await cp(path, module_path, { | ||
| recursive: true, | ||
| }); | ||
| const command = `cd ./plugins/${name} && npm init -y && npm pkg set type="module" && npm pkg set main="${module_main}"`; | ||
| const spinner = ora(`Initialize ${name} package.json`).start(); | ||
| try { | ||
| await promiseExec(command); | ||
| spinner.succeed(); | ||
| } | ||
| catch (error) { | ||
| spinner.fail(); | ||
| } | ||
| console.log(`${TIP_INFO} plugin module create successful.\n`); | ||
| } | ||
| catch (error) { | ||
| const message = error instanceof Error ? error.message : JSON.stringify(error); | ||
| console.warn(`\n${TIP_ERROR} ${message}.`); | ||
| exit(1); | ||
| } | ||
| }); | ||
| } |
+2
-2
@@ -9,3 +9,3 @@ #!/usr/bin/env node | ||
| import start from './start.js'; | ||
| import create from './create.js'; | ||
| import plugin from './plugin.js'; | ||
| export const plugins_path = join(cwd(), `plugins`); | ||
@@ -29,3 +29,3 @@ export const config_path = join(cwd(), 'kokkoro.json'); | ||
| start(program); | ||
| create(program); | ||
| plugin(program); | ||
| program.name('kokkoro').version(version, '-v, --version').parse(); | ||
@@ -32,0 +32,0 @@ /** |
+2
-2
| { | ||
| "name": "@kokkoro/cli", | ||
| "version": "2.0.0", | ||
| "version": "2.0.1", | ||
| "description": "Cli tool for kokkoro.", | ||
@@ -33,3 +33,3 @@ "engines": { | ||
| "devDependencies": { | ||
| "@kokkoro/core": "^3.0.4", | ||
| "@kokkoro/core": "^3.0.8", | ||
| "@types/prompts": "^2.4.9" | ||
@@ -36,0 +36,0 @@ }, |
@@ -1,2 +0,2 @@ | ||
| import { Command, CommandEvent, Event, Plugin } from '@kokkoro/core'; | ||
| import { Command, CommandContext, Context, Event, Plugin } from '@kokkoro/core'; | ||
@@ -9,4 +9,4 @@ @Plugin({ | ||
| @Event('session.ready') | ||
| onReady() { | ||
| console.log('Bot online.'); | ||
| onReady(ctx: Context<'session.ready'>) { | ||
| ctx.logger.mark('Bot online.'); | ||
| } | ||
@@ -20,5 +20,5 @@ | ||
| @Command('/复读 <message>') | ||
| replayMessage(event: CommandEvent) { | ||
| return event.query.message; | ||
| replayMessage(ctx: CommandContext<{ message: string }>) { | ||
| return ctx.query.message; | ||
| } | ||
| } |
@@ -9,8 +9,11 @@ import { Metadata, useCommand, useEvent } from '@kokkoro/core'; | ||
| export default function Example(): void { | ||
| useEvent(() => { | ||
| console.log('Bot online.'); | ||
| }, ['session.ready']); | ||
| useEvent( | ||
| ctx => { | ||
| ctx.logger.mark('Bot online.'); | ||
| }, | ||
| ['session.ready'], | ||
| ); | ||
| useCommand('/测试', () => 'hello world'); | ||
| useCommand('/复读 <message>', event => event.query.message); | ||
| useCommand<{ message: string }>('/复读 <message>', ctx => ctx.query.message); | ||
| } |
@@ -8,10 +8,15 @@ import { useCommand, useEvent } from '@kokkoro/core'; | ||
| name: 'example', | ||
| description: '示例插件', | ||
| description: '插件示例', | ||
| }; | ||
| export default function Example() { | ||
| useEvent(() => console.log('Bot online.'), ['session.ready']); | ||
| useEvent( | ||
| ctx => { | ||
| ctx.logger.mark('Bot online.'); | ||
| }, | ||
| ['session.ready'], | ||
| ); | ||
| useCommand('/测试', () => 'hello world'); | ||
| useCommand('/复读 <message>', event => event.query.message); | ||
| useCommand('/复读 <message>', ctx => ctx.query.message); | ||
| } |
| import ora from 'ora'; | ||
| import prompts from 'prompts'; | ||
| import { join } from 'node:path'; | ||
| import { exit } from 'node:process'; | ||
| import { existsSync } from 'node:fs'; | ||
| import { cp } from 'node:fs/promises'; | ||
| import { promisify } from 'node:util'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { exec } from 'node:child_process'; | ||
| import { colors, config_path, plugins_path, TIP_ERROR, TIP_INFO } from './index.js'; | ||
| const promiseExec = promisify(exec); | ||
| const questions = [ | ||
| { | ||
| type: 'select', | ||
| name: 'style', | ||
| message: 'Which plugin style would you like to use', | ||
| choices: [ | ||
| { title: 'Javascript', value: 'javascript' }, | ||
| { title: 'Typescript (Hook)', value: 'hook' }, | ||
| { title: 'Typescript (Decorator)', value: 'decorator' }, | ||
| ], | ||
| }, | ||
| ]; | ||
| const onCancel = () => { | ||
| console.log(`${TIP_INFO} plugin module creation has been aborted.\n`); | ||
| exit(0); | ||
| }; | ||
| export default function (program) { | ||
| program | ||
| .command('create <name>') | ||
| .description('create a kokkoro plugin project') | ||
| .action(async (name) => { | ||
| if (!existsSync(config_path)) { | ||
| console.error(`${TIP_ERROR} config file is not exists. If you want to create the file, use ${colors.cyan('kokkoro init')}.\n`); | ||
| exit(1); | ||
| } | ||
| try { | ||
| const response = await prompts(questions, { onCancel }); | ||
| const { style } = response; | ||
| const module_path = join(plugins_path, name); | ||
| if (existsSync(module_path)) { | ||
| console.warn(`${TIP_ERROR} plugin directory already exists.\n`); | ||
| exit(1); | ||
| } | ||
| const url = join(import.meta.url, `../../template/${style}`); | ||
| const path = fileURLToPath(url); | ||
| await cp(path, module_path, { | ||
| recursive: true, | ||
| }); | ||
| const command = `cd ./plugins/${name} && npm init -y && npm pkg set type="module"`; | ||
| const spinner = ora(`Initialize ${name} package.json`).start(); | ||
| try { | ||
| await promiseExec(command); | ||
| spinner.succeed(); | ||
| } | ||
| catch (error) { | ||
| spinner.fail(); | ||
| } | ||
| console.log(`${TIP_INFO} plugin module create successful.\n`); | ||
| } | ||
| catch (error) { | ||
| const message = error instanceof Error ? error.message : JSON.stringify(error); | ||
| console.warn(`\n${TIP_ERROR} ${message}.`); | ||
| exit(1); | ||
| } | ||
| }); | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
37989
0.68%528
1.73%0
-100%