Skip to content

Commit

Permalink
feat(cli): support koishi create [plugin-name]
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Feb 19, 2022
1 parent 7e65450 commit 8231e34
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 142 deletions.
13 changes: 12 additions & 1 deletion packages/cli/build/compile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { defineBuild } from '../../../build'

export = defineBuild(async (base, options) => {
options.entryPoints.push(base + '/src/worker.ts')
options.plugins = [{
name: 'external library',
setup(build) {
build.onResolve({ filter: /^([@/\w-]+|.+\/utils)$/ }, (a) => ({ external: true }))
},
}]

options.entryPoints = [
base + '/src/bin.ts',
base + '/src/utils.ts',
base + '/src/worker/index.ts',
]
})
9 changes: 0 additions & 9 deletions packages/cli/index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions packages/cli/index.js

This file was deleted.

14 changes: 7 additions & 7 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
"name": "@koishijs/cli",
"description": "CLI for Koishi",
"version": "4.2.2",
"main": "index.js",
"typings": "index.d.ts",
"main": "lib/utils.js",
"typings": "lib/utils.d.ts",
"engines": {
"node": ">=12.0.0"
},
"files": [
"lib",
"index.js",
"index.d.ts"
"lib"
],
"bin": {
"koishi": "lib/index.js"
"koishi": "lib/bin.js"
},
"author": "Shigma <shigma10826@gmail.com>",
"license": "MIT",
Expand All @@ -33,6 +31,9 @@
"chatbot",
"koishi"
],
"peerDependencies": {
"koishi": "^4.2.2"
},
"devDependencies": {
"@types/js-yaml": "^4.0.5",
"@types/prompts": "^2.0.14",
Expand All @@ -44,7 +45,6 @@
"dotenv": "^16.0.0",
"js-yaml": "^4.1.0",
"kleur": "^4.1.4",
"koishi": "^4.2.2",
"prompts": "^2.4.2",
"throttle-debounce": "^3.0.1"
}
Expand Down
55 changes: 0 additions & 55 deletions packages/cli/src/addons/index.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/cli/src/index.ts → packages/cli/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

import registerCreateCommand from './create'
import registerStartCommand from './start'
import CAC from 'cac'

Expand All @@ -8,6 +9,7 @@ declare const KOISHI_VERSION: string
const cli = CAC('koishi').help().version(KOISHI_VERSION)

registerStartCommand(cli)
registerCreateCommand(cli)

cli.parse()

Expand Down
108 changes: 108 additions & 0 deletions packages/cli/src/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { CAC } from 'cac'
import { promises as fsp } from 'fs'
import { resolve } from 'path'
import { getAgent } from './utils'
import spawn from 'cross-spawn'
import kleur from 'kleur'
import prompts from 'prompts'

class Runner {
meta: any
root: string
name: string
fullname: string

async init(name: string) {
this.name = name || await this.getName()
this.fullname = name.includes('/')
? name.replace('/', '/koishi-plugin-')
: 'koishi-plugin-' + name
this.root = resolve(process.cwd(), 'plugins', name)
await this.write()
}

async start(name: string) {
const [agent] = await Promise.all([
getAgent(),
this.init(name),
])
const args: string[] = agent === 'yarn' ? [] : ['install']
spawn.sync(agent, args, { stdio: 'inherit' })
}

async getName() {
const { name } = await prompts({
type: 'text',
name: 'name',
message: 'plugin name:',
})
return name.trim() as string
}

async write() {
await fsp.mkdir(this.root + '/src', { recursive: true })
await Promise.all([
this.writeManifest(),
this.writeTsConfig(),
this.writeIndex(),
])
}

async writeManifest() {
await fsp.writeFile(this.root + '/package.json', JSON.stringify({
name: this.fullname,
version: '1.0.0',
main: 'lib/index.js',
typings: 'lib/index.d.ts',
files: ['lib'],
license: 'MIT',
scripts: {
build: 'tsc -b',
},
keywords: [
'chatbot',
'koishi',
'plugin',
],
peerDependencies: {
koishi: this.meta.dependencies.koishi,
},
}, null, 2))
}

async writeTsConfig() {
await fsp.writeFile(this.root + '/tsconfig.json', JSON.stringify({
extends: '../../tsconfig.base',
compilerOptions: {
rootDir: 'src',
outDir: 'lib',
},
include: ['src'],
}, null, 2))
}

async writeIndex() {
await fsp.writeFile(this.root + '/src/index.ts', [
`import { Context } from 'koishi'`,
'',
`export const name = '${this.name.replace(/^@\w+\//, '')}'`,
'',
`export function apply(ctx: Context) {`,
` // write your plugin here`,
`}`,
'',
].join('\n'))
}
}

export default function (cli: CAC) {
cli.command('create [name]', 'create a plugin')
.action(async (name: string, options) => {
const meta = require(process.cwd() + '/package.json')
if (!meta.workspaces) {
console.log(kleur.red('error') + ' koishi create is only supported in workspaces.')
} else {
new Runner().start(name)
}
})
}
39 changes: 39 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import spawn from 'cross-spawn'
import { existsSync } from 'fs'
import { resolve } from 'path'

export type { Loader, Watcher } from './worker'

function supports(command: string, args: string[] = []) {
return new Promise<boolean>((resolve) => {
const child = spawn(command, args, { stdio: 'ignore' })
child.on('exit', (code) => {
resolve(!code)
})
child.on('error', () => {
resolve(false)
})
})
}

export type Agent = 'yarn' | 'npm' | 'pnpm'

let agentTask: Promise<Agent>

async function $getAgent(cwd: string) {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { npm_execpath } = process.env
const isYarn = npm_execpath.includes('yarn')
if (isYarn) return 'yarn'

if (existsSync(resolve(cwd, 'yarn.lock'))) return 'yarn'
if (existsSync(resolve(cwd, 'pnpm-lock.yaml'))) return 'pnpm'
if (existsSync(resolve(cwd, 'package-lock.json'))) return 'npm'

const hasPnpm = await supports('pnpm', ['--version'])
return hasPnpm ? 'pnpm' : 'npm'
}

export function getAgent(cwd = process.cwd()) {
return agentTask ||= $getAgent(cwd)
}
28 changes: 0 additions & 28 deletions packages/cli/src/worker.ts

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 8231e34

Please sign in to comment.