-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): support koishi create [plugin-name]
- Loading branch information
Showing
16 changed files
with
259 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
] | ||
}) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
Oops, something went wrong.