From f6f6f607cd5adc43805213fd0d067a55076a1814 Mon Sep 17 00:00:00 2001 From: Shigma Date: Sun, 31 Jul 2022 01:17:25 +0800 Subject: [PATCH] refa: remove @koishijs/scripts to console --- packages/scripts/build/compile.ts | 13 -- packages/scripts/package.json | 53 ------- packages/scripts/src/bin.ts | 18 --- packages/scripts/src/build.ts | 121 --------------- packages/scripts/src/index.ts | 1 - packages/scripts/src/setup.ts | 142 ------------------ packages/scripts/src/utils.ts | 119 --------------- packages/scripts/template/.editorconfig | 9 -- packages/scripts/template/.gitignore | 17 --- packages/scripts/template/client/index.ts | 10 -- packages/scripts/template/client/page.vue | 3 - .../scripts/template/client/tsconfig.json | 14 -- packages/scripts/template/package.json | 18 --- packages/scripts/template/readme.md | 5 - .../scripts/template/src/index.console.ts | 14 -- .../scripts/template/src/index.default.ts | 7 - packages/scripts/template/tsconfig.json | 17 --- packages/scripts/tsconfig.json | 10 -- 18 files changed, 591 deletions(-) delete mode 100644 packages/scripts/build/compile.ts delete mode 100644 packages/scripts/package.json delete mode 100644 packages/scripts/src/bin.ts delete mode 100644 packages/scripts/src/build.ts delete mode 100644 packages/scripts/src/index.ts delete mode 100644 packages/scripts/src/setup.ts delete mode 100644 packages/scripts/src/utils.ts delete mode 100644 packages/scripts/template/.editorconfig delete mode 100644 packages/scripts/template/.gitignore delete mode 100644 packages/scripts/template/client/index.ts delete mode 100644 packages/scripts/template/client/page.vue delete mode 100644 packages/scripts/template/client/tsconfig.json delete mode 100644 packages/scripts/template/package.json delete mode 100644 packages/scripts/template/readme.md delete mode 100644 packages/scripts/template/src/index.console.ts delete mode 100644 packages/scripts/template/src/index.default.ts delete mode 100644 packages/scripts/template/tsconfig.json delete mode 100644 packages/scripts/tsconfig.json diff --git a/packages/scripts/build/compile.ts b/packages/scripts/build/compile.ts deleted file mode 100644 index 6a32bb3e88..0000000000 --- a/packages/scripts/build/compile.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { defineBuild } from '../../../build' - -export = defineBuild(async (base, options) => { - options.plugins = [{ - name: 'external library', - setup(build) { - build.onResolve({ filter: /^([@/\w-]+|.+\/utils)$/ }, (a) => ({ external: true })) - }, - }] - - options.entryPoints.push(base + '/src/bin.ts') - options.entryPoints.push(base + '/src/utils.ts') -}) diff --git a/packages/scripts/package.json b/packages/scripts/package.json deleted file mode 100644 index a6284e36bc..0000000000 --- a/packages/scripts/package.json +++ /dev/null @@ -1,53 +0,0 @@ -{ - "name": "@koishijs/scripts", - "description": "Workspace Scripts for Koishi", - "version": "3.0.0", - "main": "lib/index.js", - "typings": "lib/index.d.ts", - "engines": { - "node": ">=12.0.0" - }, - "files": [ - "lib", - "template" - ], - "bin": { - "koishi-scripts": "lib/bin.js" - }, - "author": "Shigma ", - "license": "MIT", - "repository": { - "type": "git", - "url": "git+https://github.com/koishijs/koishi.git", - "directory": "packages/scripts" - }, - "bugs": { - "url": "https://github.com/koishijs/koishi/issues" - }, - "homepage": "https://koishi.js.org", - "keywords": [ - "bot", - "chatbot", - "koishi" - ], - "peerDependencies": { - "@koishijs/client": "^4.1.1" - }, - "devDependencies": { - "@types/cross-spawn": "^6.0.2", - "@types/fs-extra": "^9.0.13", - "@types/js-yaml": "^4.0.5", - "@types/which-pm-runs": "^1.0.0" - }, - "dependencies": { - "cac": "^6.7.12", - "cross-spawn": "^7.0.3", - "fs-extra": "^10.1.0", - "globby": "^11.1.0", - "js-yaml": "^4.1.0", - "kleur": "^4.1.4", - "ora": "^5.4.1", - "prompts": "^2.4.2", - "which-pm-runs": "^1.1.0" - } -} diff --git a/packages/scripts/src/bin.ts b/packages/scripts/src/bin.ts deleted file mode 100644 index e712e766c0..0000000000 --- a/packages/scripts/src/bin.ts +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env node - -import registerBuildCommand from './build' -import registerSetupCommand from './setup' -import CAC from 'cac' - -const { version } = require('../package.json') - -const cli = CAC('koishi-scripts').help().version(version) - -registerBuildCommand(cli) -registerSetupCommand(cli) - -cli.parse() - -if (!cli.matchedCommand) { - cli.outputHelp() -} diff --git a/packages/scripts/src/build.ts b/packages/scripts/src/build.ts deleted file mode 100644 index bb5f71bbda..0000000000 --- a/packages/scripts/src/build.ts +++ /dev/null @@ -1,121 +0,0 @@ -import { CAC } from 'cac' -import { mkdir, readdir, readFile, writeFile } from 'fs-extra' -import { buildExtension } from '@koishijs/client/lib' -import { cwd, getPackages, PackageJson, spawnAsync, TsConfig } from './utils' -import { extname } from 'path' -import yaml from 'js-yaml' -import ora from 'ora' - -interface Node { - path?: string - meta?: PackageJson - prev?: string[] - next?: Set -} - -function initGraph(packages: Record) { - const nodes: Record = {} - for (const path in packages) { - const meta = packages[path] - if (!meta.main) return - nodes[meta.name] = { path, meta, prev: [], next: new Set() } - } - - for (const name in nodes) { - const { meta } = nodes[name] - const deps = { - ...meta.dependencies, - ...meta.devDependencies, - ...meta.peerDependencies, - } - for (const dep in deps) { - if (!nodes[dep]) continue - nodes[name].prev.push(dep) - nodes[dep].next.add(name) - } - delete nodes[name].meta - } - - return nodes -} - -async function buildGraph(nodes: Record) { - function check(name: string) { - const node = nodes[name] - if (node.next.size) return true - delete nodes[name] - config.references.unshift({ path: '.' + node.path }) - node.prev.forEach(dep => { - nodes[dep].next.delete(name) - }) - } - - let names: string[] - const config: TsConfig = { files: [], references: [] } - do { - names = Object.keys(nodes) - } while (names.length && !names.every(check)) - - if (names.length) { - console.log(nodes) - throw new Error('circular dependency detected') - } - - if (!config.references.length) return - await writeFile(cwd + '/tsconfig.temp.json', JSON.stringify(config, null, 2)) - - const code = await spawnAsync(['tsc', '-b', 'tsconfig.temp.json']) - if (code) process.exit(code) -} - -async function buildYamlFile(source: string, target: string, file: string) { - const content = await readFile(source + '/' + file, 'utf8') - const ext = extname(file) - const name = file.slice(0, -ext.length) - switch (ext) { - case '.yaml': - case '.yml': - await writeFile(target + '/' + name + '.json', JSON.stringify(yaml.load(content))) - break - default: - await writeFile(target + '/' + file, content) - } -} - -async function buildYamlPackage(path: string) { - const source = cwd + path + '/src/locales' - const target = cwd + path + '/lib/locales' - const files = await readdir(source) - await mkdir(target, { recursive: true }) - await Promise.all(files.map((file) => { - return buildYamlFile(source, target, file) - })) -} - -export default function (cli: CAC) { - cli.command('build [...name]', 'build packages') - .action(async (names: string[], options) => { - const spinner = ora() - - spinner.start('loading packages') - const packages = await getPackages(names) - spinner.succeed() - - spinner.start('building typescript') - const nodes = initGraph(packages) - await buildGraph(nodes) - spinner.succeed() - - spinner.start('building locales') - await Promise.all(Object.keys(packages).map((path) => { - return buildYamlPackage(path).catch(() => {}) - })) - spinner.succeed() - - spinner.start('building client') - for (const path in packages) { - await buildExtension(cwd + path) - } - spinner.succeed() - }) -} diff --git a/packages/scripts/src/index.ts b/packages/scripts/src/index.ts deleted file mode 100644 index 9c56149efa..0000000000 --- a/packages/scripts/src/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './utils' diff --git a/packages/scripts/src/setup.ts b/packages/scripts/src/setup.ts deleted file mode 100644 index e18af330c5..0000000000 --- a/packages/scripts/src/setup.ts +++ /dev/null @@ -1,142 +0,0 @@ -import { CAC } from 'cac' -import { copyFile, mkdir, readFile, readJson, writeFile } from 'fs-extra' -import { resolve } from 'path' -import { config, cwd, meta, PackageJson } from './utils' -import { blue, red } from 'kleur' -import which from 'which-pm-runs' -import spawn from 'cross-spawn' -import prompts from 'prompts' - -class Initiator { - name: string - desc: string - fullname: string - target: string - source = resolve(__dirname, '../template') - - constructor(private options: Options) {} - - async start(name: string) { - await this.init(name) - const agent = which()?.name || 'npm' - const args: string[] = agent === 'yarn' ? [] : ['install'] - spawn.sync(agent, args, { stdio: 'inherit' }) - } - - async init(name: string) { - name ||= await this.getName() - if (!/^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(name)) { - console.log(red('error'), 'plugin name contains invalid character') - process.exit(1) - } - if (name.includes('koishi-plugin-')) { - this.fullname = name - this.name = name.replace('koishi-plugin-', '') - console.log(blue('info'), 'prefix "koishi-plugin-" can be omitted') - } else { - this.name = name - this.fullname = name.replace(/^(.+\/)?/, '$1koishi-plugin-') - } - this.desc = await this.getDesc() - this.target = resolve(cwd, 'plugins', this.name) - await this.write() - } - - async getName() { - const { name } = await prompts({ - type: 'text', - name: 'name', - message: 'plugin name:', - }) - return name.trim() as string - } - - async getDesc() { - const { desc } = await prompts({ - type: 'text', - name: 'desc', - message: 'description:', - }) - return desc as string - } - - async write() { - await mkdir(this.target, { recursive: true }) - await Promise.all([ - this.writeManifest(), - this.writeTsConfig(), - this.writeIndex(), - this.writeReadme(), - this.writeClient(), - ]) - await this.initGit() - } - - async writeManifest() { - const source: PackageJson = await readJson(this.source + '/package.json', 'utf8') - if (this.options.console) { - source.peerDependencies['@koishijs/console'] = meta.dependencies['@koishijs/console'] - } - source.peerDependencies['koishi'] = meta.dependencies['koishi'] - await writeFile(this.target + '/package.json', JSON.stringify({ - name: this.fullname, - description: this.desc, - ...source, - }, null, 2)) - } - - async writeTsConfig() { - await copyFile(this.source + '/tsconfig.json', this.target + '/tsconfig.json') - } - - async writeIndex() { - await mkdir(this.target + '/src') - const filename = `/src/index.${this.options.console ? 'console' : 'default'}.ts` - const source = await readFile(this.source + filename, 'utf8') - await writeFile(this.target + '/src/index.ts', source - .replace(/\{\{name\}\}/g, this.name.replace(/^@\w+\//, ''))) - } - - async writeReadme() { - const source = await readFile(this.source + '/readme.md', 'utf8') - await writeFile(this.target + '/readme.md', source - .replace(/\{\{name\}\}/g, this.fullname) - .replace(/\{\{desc\}\}/g, this.desc)) - } - - async writeClient() { - if (!this.options.console) return - await mkdir(this.target + '/client') - await Promise.all([ - copyFile(this.source + '/client/index.ts', this.target + '/client/index.ts'), - copyFile(this.source + '/client/page.vue', this.target + '/client/page.vue'), - copyFile(this.source + '/client/tsconfig.json', this.target + '/client/tsconfig.json'), - ]) - } - - async initGit() { - if (config.mode === 'monorepo') return - await Promise.all([ - copyFile(this.source + '/.editorconfig', this.target + '/.editorconfig'), - copyFile(this.source + '/.gitignore', this.target + '/.gitignore'), - ]) - spawn.sync('git', ['init'], { cwd: this.target, stdio: 'inherit' }) - spawn.sync('git', ['add', '.'], { cwd: this.target, stdio: 'inherit' }) - spawn.sync('git', ['commit', '-m', 'initial commit'], { cwd: this.target, stdio: 'inherit' }) - } -} - -interface Options { - console?: boolean -} - -export default function (cli: CAC) { - cli.command('setup [name]', 'initialize a new plugin') - .alias('create') - .alias('init') - .alias('new') - .option('-c, --console', 'with console extension') - .action(async (name: string, options) => { - new Initiator(options).start(name) - }) -} diff --git a/packages/scripts/src/utils.ts b/packages/scripts/src/utils.ts deleted file mode 100644 index 0bb05f5e30..0000000000 --- a/packages/scripts/src/utils.ts +++ /dev/null @@ -1,119 +0,0 @@ -import spawn from 'cross-spawn' -import globby from 'globby' -import ts from 'typescript' -import ora from 'ora' -import prompts from 'prompts' -import { SpawnOptions } from 'child_process' - -export const cwd = process.cwd() -export const meta: PackageJson = require(cwd + '/package.json') - -export interface Config { - mode?: 'monorepo' | 'separate' | 'submodule' - concurrency?: number -} - -export const config: Config = { - mode: 'monorepo', - concurrency: 10, - ...meta['koishi-scripts'], -} - -export function requireSafe(id: string) { - try { - return require(id) - } catch {} -} - -export async function confirm(message: string) { - const { value } = await prompts({ - name: 'value', - type: 'confirm', - message, - }) - return value -} - -export function exit(message: string) { - const spinner = ora() - spinner.info(message) - return process.exit(0) -} - -interface FallbackOptions { - workspaces?: Record -} - -async function getWorkspaces() { - const folders = await globby(meta.workspaces, { - cwd, - deep: 0, - onlyDirectories: true, - expandDirectories: false, - }) - folders.unshift('') - - return Object.fromEntries(folders.map((path) => { - path = '/' + path - try { - return [path, require(`${cwd}${path}/package.json`)] as [string, PackageJson] - } catch {} - }).filter(Boolean)) -} - -export async function getPackages(args: readonly string[], options: FallbackOptions = {}) { - const workspaces = options.workspaces || await getWorkspaces() - if (!args.length) return workspaces - - const privates: string[] = [] - const result = Object.fromEntries(args.map((name) => { - const targets = Object.keys(workspaces).filter((folder) => { - const [last] = folder.split('/').reverse() - return name === last - }) - if (!targets.length) { - throw new Error(`cannot find workspace "${name}"`) - } else if (targets.length > 1) { - throw new Error(`ambiguous workspace "${name}": ${targets.join(', ')}`) - } - const path = targets[0] - const meta = workspaces[path] - if (meta.private) privates.push(path) - return [path, meta] as const - })) - - return result -} - -export type DependencyType = 'dependencies' | 'devDependencies' | 'peerDependencies' | 'optionalDependencies' - -export interface PackageJson extends Partial>> { - $dirty?: boolean - name: string - main?: string - module?: string - description?: string - private?: boolean - version?: string - workspaces: string[] -} - -interface Reference { - path: string -} - -export interface TsConfig { - extends?: string - files?: string[] - references?: Reference[] - compilerOptions?: ts.CompilerOptions -} - -export function spawnAsync(args: string[], options: SpawnOptions = {}) { - const child = spawn(args[0], args.slice(1), { cwd, stdio: 'inherit', ...options }) - return new Promise((resolve) => { - child.stderr?.pipe(process.stderr) - child.stdout?.pipe(process.stdout) - child.on('close', resolve) - }) -} diff --git a/packages/scripts/template/.editorconfig b/packages/scripts/template/.editorconfig deleted file mode 100644 index 8f02e58f3b..0000000000 --- a/packages/scripts/template/.editorconfig +++ /dev/null @@ -1,9 +0,0 @@ -root = true - -[*] -insert_final_newline = true -indent_style = space -indent_size = 2 -end_of_line = lf -charset = utf-8 -trim_trailing_whitespace = true diff --git a/packages/scripts/template/.gitignore b/packages/scripts/template/.gitignore deleted file mode 100644 index de8b44dfd2..0000000000 --- a/packages/scripts/template/.gitignore +++ /dev/null @@ -1,17 +0,0 @@ -lib -dist - -node_modules -npm-debug.log -yarn-debug.log -yarn-error.log -tsconfig.tsbuildinfo - -.eslintcache -.DS_Store -.idea -.vscode -*.suo -*.ntvs* -*.njsproj -*.sln diff --git a/packages/scripts/template/client/index.ts b/packages/scripts/template/client/index.ts deleted file mode 100644 index 54b77f3027..0000000000 --- a/packages/scripts/template/client/index.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Context } from '@koishijs/client' -import Page from './page.vue' - -export default (ctx: Context) => { - ctx.page({ - name: '扩展页面', - path: '/custom-page', - component: Page, - }) -} diff --git a/packages/scripts/template/client/page.vue b/packages/scripts/template/client/page.vue deleted file mode 100644 index fd936e729e..0000000000 --- a/packages/scripts/template/client/page.vue +++ /dev/null @@ -1,3 +0,0 @@ - diff --git a/packages/scripts/template/client/tsconfig.json b/packages/scripts/template/client/tsconfig.json deleted file mode 100644 index 079f39e8db..0000000000 --- a/packages/scripts/template/client/tsconfig.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "compilerOptions": { - "rootDir": ".", - "module": "esnext", - "moduleResolution": "node", - "jsx": "preserve", - "types": [ - "@koishijs/client/global" - ] - }, - "include": [ - "." - ] -} \ No newline at end of file diff --git a/packages/scripts/template/package.json b/packages/scripts/template/package.json deleted file mode 100644 index 79d8e8b532..0000000000 --- a/packages/scripts/template/package.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "private": true, - "version": "1.0.0", - "main": "lib/index.js", - "typings": "lib/index.d.ts", - "files": [ - "lib", - "dist" - ], - "license": "MIT", - "scripts": {}, - "keywords": [ - "chatbot", - "koishi", - "plugin" - ], - "peerDependencies": {} -} \ No newline at end of file diff --git a/packages/scripts/template/readme.md b/packages/scripts/template/readme.md deleted file mode 100644 index af2a5abb79..0000000000 --- a/packages/scripts/template/readme.md +++ /dev/null @@ -1,5 +0,0 @@ -# {{name}} - -[![npm](https://img.shields.io/npm/v/{{name}}?style=flat-square)](https://www.npmjs.com/package/{{name}}) - -{{desc}} diff --git a/packages/scripts/template/src/index.console.ts b/packages/scripts/template/src/index.console.ts deleted file mode 100644 index bca6165019..0000000000 --- a/packages/scripts/template/src/index.console.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { Context } from 'koishi' -import { resolve } from 'path' -import {} from '@koishijs/plugin-console' - -export const name = '{{name}}' - -export function apply(ctx: Context) { - ctx.using(['console'], (ctx) => { - ctx.console.addEntry({ - dev: resolve(__dirname, '../client/index.ts'), - prod: resolve(__dirname, '../dist'), - }) - }) -} diff --git a/packages/scripts/template/src/index.default.ts b/packages/scripts/template/src/index.default.ts deleted file mode 100644 index f3c887bb51..0000000000 --- a/packages/scripts/template/src/index.default.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Context } from 'koishi' - -export const name = '{{name}}' - -export function apply(ctx: Context) { - // write your plugin here -} diff --git a/packages/scripts/template/tsconfig.json b/packages/scripts/template/tsconfig.json deleted file mode 100644 index 8d1fad2e06..0000000000 --- a/packages/scripts/template/tsconfig.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "compilerOptions": { - "rootDir": "src", - "outDir": "lib", - "target": "es2020", - "module": "commonjs", - "declaration": true, - "composite": true, - "incremental": true, - "skipLibCheck": true, - "esModuleInterop": true, - "moduleResolution": "node" - }, - "include": [ - "src" - ] -} \ No newline at end of file diff --git a/packages/scripts/tsconfig.json b/packages/scripts/tsconfig.json deleted file mode 100644 index 389708cea6..0000000000 --- a/packages/scripts/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": "../../tsconfig.base", - "compilerOptions": { - "rootDir": "src", - "outDir": "lib" - }, - "include": [ - "src", - ], -} \ No newline at end of file