@aomex/commander
Advanced tools
Comparing version 0.0.35 to 1.0.0
# @aomex/commander | ||
## 0.0.35 | ||
## 1.0.0 | ||
### Patch Changes | ||
### Minor Changes | ||
- Updated dependencies [[`90ca7d5`](https://github.com/aomex/aomex/commit/90ca7d5fc736b523c4fbf7949f64653428dc413c)]: | ||
- @aomex/core@0.0.28 | ||
- @aomex/console@0.0.33 | ||
- [`11103a2`](https://github.com/aomex/aomex/commit/11103a2aff4e081754c56b0ff18fa5130ca252e8) Thanks [@geekact](https://github.com/geekact)! - 重新制作 | ||
## 0.0.34 | ||
### Patch Changes | ||
- [`8becf8e`](https://github.com/aomex/aomex/commit/8becf8ee5ef86a5909783d0654e536db7be9bf5b) Thanks [@geekact](https://github.com/geekact)! - refactor(core): 工具移动到新的包@aomex/internal-tools | ||
- [`0776719`](https://github.com/aomex/aomex/commit/077671963401f1dafb5b03722899452d45df13fc) Thanks [@geekact](https://github.com/geekact)! - refactor: 文件搜索转移到新的包@aomex/internal-file-import | ||
- Updated dependencies [[`00bafbb`](https://github.com/aomex/aomex/commit/00bafbbac2d32205b63a6bf561fb0a69c38a54bb), [`8becf8e`](https://github.com/aomex/aomex/commit/8becf8ee5ef86a5909783d0654e536db7be9bf5b), [`0776719`](https://github.com/aomex/aomex/commit/077671963401f1dafb5b03722899452d45df13fc), [`f996bf7`](https://github.com/aomex/aomex/commit/f996bf7e529e7751a5e858c579feed33f5f02d65)]: | ||
- @aomex/core@0.0.27 | ||
- @aomex/internal-tools@0.0.27 | ||
- @aomex/console@0.0.32 | ||
- @aomex/internal-file-import@0.0.27 | ||
## 0.0.33 | ||
### Patch Changes | ||
- Updated dependencies [[`fbcea3d`](https://github.com/aomex/aomex/commit/fbcea3d68ff033e6861130c645c8e5ad7336193f)]: | ||
- @aomex/core@0.0.26 | ||
- @aomex/console@0.0.31 | ||
## 0.0.32 | ||
### Patch Changes | ||
- [`7237c7b`](https://github.com/aomex/aomex/commit/7237c7b827eb77671dd445457562cd763e200454) Thanks [@geekact](https://github.com/geekact)! - feat(commander): 指令返回中间件类型提示 | ||
- Updated dependencies [[`0e6ed2c`](https://github.com/aomex/aomex/commit/0e6ed2c611100dcfaafb6fb41357624ad9f5c67a)]: | ||
- @aomex/core@0.0.25 | ||
- @aomex/console@0.0.30 | ||
## 0.0.31 | ||
### Patch Changes | ||
- Updated dependencies [[`2ac62fd`](https://github.com/aomex/aomex/commit/2ac62fd28166a1d9dd60b3c6d5a6508a6f9ee82b), [`4258410`](https://github.com/aomex/aomex/commit/42584107ad9f7e34492ae1053fef83aa2d9d747a), [`4177cba`](https://github.com/aomex/aomex/commit/4177cba7877e38120842bd8d287eaed54e4926ca)]: | ||
- @aomex/core@0.0.24 | ||
- @aomex/console@0.0.29 | ||
## 0.0.30 | ||
### Patch Changes | ||
- Updated dependencies [[`7b09277`](https://github.com/aomex/aomex/commit/7b09277136910966f500c8132303c7ddee84340c), [`9c78999`](https://github.com/aomex/aomex/commit/9c78999ebcb2962f30344acfbf6de0733d6fdd41), [`f4b012d`](https://github.com/aomex/aomex/commit/f4b012d98cddb2918479ea05df6c266dd914e53a), [`95fd705`](https://github.com/aomex/aomex/commit/95fd705d2ac24f4a3c14446ccb9de6eb6817d0cd)]: | ||
- @aomex/core@0.0.23 | ||
- @aomex/console@0.0.28 | ||
## 0.0.29 | ||
### Patch Changes | ||
- Updated dependencies [[`6f7d706`](https://github.com/aomex/aomex/commit/6f7d7066c23711abdd149eb1c9a293ab8c4284a4), [`e7bf93c`](https://github.com/aomex/aomex/commit/e7bf93cee6896c61d0bf3eb0921151dc6c1bc107), [`818e840`](https://github.com/aomex/aomex/commit/818e840d36c7456a863fc071968b246c123c17f5)]: | ||
- @aomex/core@0.0.22 | ||
- @aomex/console@0.0.27 | ||
- Updated dependencies [[`11103a2`](https://github.com/aomex/aomex/commit/11103a2aff4e081754c56b0ff18fa5130ca252e8)]: | ||
- @aomex/console@1.0.0 | ||
- @aomex/core@1.0.0 | ||
- @aomex/internal-file-import@1.0.0 | ||
- @aomex/internal-tools@1.0.0 |
@@ -1,3 +0,3 @@ | ||
import { PathToFileOptions } from '@aomex/internal-file-import'; | ||
import { ConsoleMiddlewareToken, ConsoleContext, ConsoleChain } from '@aomex/console'; | ||
import { GlobPathOptions } from '@aomex/internal-file-import'; | ||
import { ConsoleMiddlewareToken, ConsoleContext, ConsoleDocument, ConsoleMiddlewareChain, ConsoleApp, ConsoleMiddleware } from '@aomex/console'; | ||
import { Middleware } from '@aomex/core'; | ||
@@ -9,11 +9,5 @@ import { Union2Intersection } from '@aomex/internal-tools'; | ||
/** | ||
* 关闭当前指令。默认值:`false` | ||
* ```typescript | ||
* commander.create('schedule:x', { | ||
* disable: process.env.NODE_ENV !== 'production' | ||
* }) | ||
* ``` | ||
* 禁用当前路由。默认 `false` | ||
*/ | ||
disable?: boolean; | ||
showInHelp?: boolean; | ||
docs?: Builder.Docs; | ||
@@ -26,12 +20,8 @@ mount?: T; | ||
type Context<Props extends object, T extends ConsoleMiddlewareToken[] | []> = Props & Union2Intersection<CollectArrayType<T[number]>> & ConsoleContext; | ||
interface Docs { | ||
summary?: string; | ||
description?: string; | ||
} | ||
type Docs = Omit<ConsoleDocument.CommandItem, 'parameters'>; | ||
} | ||
declare class Builder<Props extends object = object, T extends ConsoleMiddlewareToken[] | [] = []> { | ||
readonly chain: ConsoleChain; | ||
readonly docs: Builder.Docs; | ||
readonly commands: string[]; | ||
readonly showInHelp: boolean; | ||
readonly docs: Builder.Docs; | ||
protected readonly middlewareList: Middleware[]; | ||
constructor(prefix: string, commands: string[], options: BuilderOptions<Props, T>); | ||
@@ -43,19 +33,16 @@ match(command: string): boolean; | ||
prefix?: string; | ||
mount?: ConsoleChain<Props>; | ||
mount?: ConsoleMiddlewareChain<Props>; | ||
} | ||
declare class Commander<Props extends object = object> { | ||
protected readonly options: CommanderOptions<Props>; | ||
protected readonly chain: ConsoleChain; | ||
protected readonly opts: CommanderOptions<Props>; | ||
protected readonly middlewareChain: ConsoleMiddlewareChain; | ||
protected readonly prefix: string; | ||
protected readonly builders: Builder[]; | ||
constructor(options?: CommanderOptions<Props>); | ||
create<T extends ConsoleMiddlewareToken<object>[] | []>(command: string | string[], options: BuilderOptions<Props, T>): Builder.Interface<Props, T>; | ||
protected toMiddleware(): Middleware; | ||
constructor(opts?: CommanderOptions<Props>); | ||
create<T extends ConsoleMiddlewareToken<object>[] | []>(commandName: string | string[], options: BuilderOptions<Props, T>): Builder.Interface<Props, T>; | ||
protected toMiddleware(app: ConsoleApp): ConsoleMiddleware; | ||
} | ||
interface CommandsOptions { | ||
path: PathToFileOptions; | ||
} | ||
declare const commanders: (options: string | CommandsOptions | Commander[]) => ConsoleChain; | ||
declare const commanders: (options: GlobPathOptions | Commander[]) => ConsoleMiddleware; | ||
export { Builder, BuilderOptions, Commander, CommanderOptions, CommandsOptions, commanders }; | ||
export { Builder, type BuilderOptions, Commander, type CommanderOptions, commanders }; |
// src/commanders.ts | ||
import { compose as compose2, middleware as middleware4, chain as chain2 } from "@aomex/core"; | ||
import { compose as compose2, middleware as middleware3 } from "@aomex/core"; | ||
import { | ||
fileToModules as fileToModules2, | ||
pathToFiles as pathToFiles2 | ||
pathToFiles, | ||
getFileValues | ||
} from "@aomex/internal-file-import"; | ||
// src/commander.ts | ||
import { Chain as Chain2, chain, compose, middleware as middleware2 } from "@aomex/core"; | ||
import { | ||
mdchain, | ||
compose, | ||
middleware as middleware2, | ||
flattenMiddlewareToken as flattenMiddlewareToken2 | ||
} from "@aomex/core"; | ||
import { toArray } from "@aomex/internal-tools"; | ||
// src/builder.ts | ||
import { Chain, middleware } from "@aomex/core"; | ||
import { | ||
ConsoleChain | ||
} from "@aomex/console"; | ||
import { middleware, flattenMiddlewareToken } from "@aomex/core"; | ||
var Builder = class { | ||
chain; | ||
docs; | ||
commands; | ||
showInHelp; | ||
docs; | ||
middlewareList; | ||
constructor(prefix, commands, options) { | ||
const middlewareList = Chain.flatten(options.mount); | ||
middlewareList.push( | ||
middleware.console((ctx, _) => { | ||
return options.action(ctx); | ||
}) | ||
); | ||
this.showInHelp = options.showInHelp ?? true; | ||
this.docs = options.docs || {}; | ||
this.chain = new ConsoleChain(middlewareList); | ||
this.docs.show ??= true; | ||
this.commands = commands.map((item) => prefix + item); | ||
this.middlewareList = flattenMiddlewareToken(options.mount).concat( | ||
middleware.console((ctx, _) => options.action(ctx)) | ||
); | ||
} | ||
@@ -39,136 +37,106 @@ match(command) { | ||
// src/commander.ts | ||
import { toArray } from "@aomex/internal-tools"; | ||
import "@aomex/console"; | ||
var Commander = class { | ||
constructor(options = {}) { | ||
this.options = options; | ||
this.prefix = options.prefix || ""; | ||
this.chain = options.mount || chain.console; | ||
constructor(opts = {}) { | ||
this.opts = opts; | ||
this.prefix = opts.prefix || ""; | ||
this.middlewareChain = opts.mount || mdchain.console; | ||
} | ||
chain; | ||
middlewareChain; | ||
prefix; | ||
builders = []; | ||
create(command, options) { | ||
const builder = new Builder(this.prefix, toArray(command), options); | ||
create(commandName, options) { | ||
const builder = new Builder(this.prefix, toArray(commandName), options); | ||
options.disable !== true && this.builders.push(builder); | ||
return void 0; | ||
} | ||
toMiddleware() { | ||
const groupChain = /* @__PURE__ */ new Map(); | ||
return middleware2.console((ctx, next) => { | ||
const { | ||
app, | ||
request: { command } | ||
} = ctx; | ||
for (let i = 0; i < this.builders.length; ++i) { | ||
const builder = this.builders[i]; | ||
if (builder.match(command)) { | ||
ctx.response.commandMatched = true; | ||
if (!groupChain.has(app)) { | ||
groupChain.set(app, Chain2.split(this.chain, app.chainPoints)); | ||
toMiddleware(app) { | ||
const builders = [...this.builders]; | ||
const buildersLength = builders.length; | ||
const middlewareList = flattenMiddlewareToken2( | ||
this.middlewareChain["split"](app["point"]) | ||
); | ||
const commandFn = builders.map((builder) => { | ||
return compose(middlewareList.concat(builder["middlewareList"])); | ||
}); | ||
return middleware2.console({ | ||
fn: (ctx, next) => { | ||
const { input } = ctx; | ||
const { command } = input; | ||
for (let i = 0; i < buildersLength; ++i) { | ||
if (builders[i].match(command)) { | ||
ctx.commandMatched = true; | ||
return commandFn[i](ctx, next); | ||
} | ||
return compose([groupChain.get(app), builder.chain])(ctx, next); | ||
} | ||
} | ||
return next(); | ||
}); | ||
} | ||
}; | ||
// src/commander-to-help.ts | ||
import { ConsoleMiddleware, scriptName } from "@aomex/console"; | ||
import { Chain as Chain3, middleware as middleware3 } from "@aomex/core"; | ||
import { | ||
fileToModules, | ||
pathToFiles | ||
} from "@aomex/internal-file-import"; | ||
import { chalk } from "@aomex/internal-tools"; | ||
var commanderToHelp = (options) => middleware3.help(async (ctx, next) => { | ||
const builders = await getBuilders(options); | ||
const { command } = ctx.request; | ||
return ctx.cli.config({ | ||
all(yargs) { | ||
for (const builder of builders) { | ||
if (!builder.showInHelp) | ||
continue; | ||
const commands = builder.commands.slice(); | ||
commands[0] = chalk.yellow(commands[0]); | ||
yargs.command(commands, builder.docs.summary || ""); | ||
} | ||
}, | ||
detail(yargs) { | ||
for (const builder of builders) { | ||
if (!builder.showInHelp || !builder.match(command)) | ||
continue; | ||
const { commands, docs } = builder; | ||
const description = docs.description || docs.summary; | ||
yargs.usage( | ||
`${scriptName} ${commands.join("|")} [options]${description ? "\n\n" + chalk.bold(description) : ""}` | ||
); | ||
for (const middleware5 of Chain3.flatten(builder.chain)) { | ||
if (middleware5 instanceof ConsoleMiddleware) { | ||
middleware5.toHelp(yargs); | ||
return next(); | ||
}, | ||
help: { | ||
async onDocument(doc, { children, collectCommand }) { | ||
for (const builder of builders) { | ||
for (const commandName of builder.commands) { | ||
doc[commandName] = { ...builder.docs }; | ||
collectCommand( | ||
commandName, | ||
middlewareList.concat(builder["middlewareList"]) | ||
); | ||
} | ||
} | ||
const totalMiddlewareList = [...middlewareList]; | ||
for (const builder of builders) { | ||
totalMiddlewareList.push(...builder["middlewareList"]); | ||
} | ||
await children(totalMiddlewareList); | ||
}, | ||
async postDocument(_, { children }) { | ||
const totalMiddlewareList = [...middlewareList]; | ||
for (const builder of builders) { | ||
totalMiddlewareList.push(...builder["middlewareList"]); | ||
} | ||
await children(totalMiddlewareList); | ||
} | ||
break; | ||
} | ||
}, | ||
next, | ||
detailCommand: () => { | ||
return builders.some( | ||
(builder) => builder.showInHelp && builder.match(command) | ||
); | ||
} | ||
}); | ||
}); | ||
var getBuilders = async (options) => { | ||
const builders = []; | ||
const commanders2 = []; | ||
if (options.commanders) { | ||
commanders2.push(...options.commanders); | ||
} else if (options.paths) { | ||
const files = await pathToFiles(options.paths); | ||
const modules = await fileToModules( | ||
files, | ||
(item) => !!item && item instanceof Commander | ||
); | ||
commanders2.push(...modules); | ||
}); | ||
} | ||
for (const commander of Object.values(commanders2)) { | ||
builders.push(...commander["builders"]); | ||
} | ||
return builders; | ||
}; | ||
// src/commanders.ts | ||
import "@aomex/console"; | ||
var commanders = (options) => { | ||
let composeFn = null; | ||
let promise = null; | ||
if (Array.isArray(options)) { | ||
composeFn = compose2( | ||
options.map((commander) => commander["toMiddleware"]()) | ||
); | ||
let composeFn; | ||
let commanders2; | ||
let promise = Promise.resolve(); | ||
if (isCommanders(options)) { | ||
commanders2 = options; | ||
} else { | ||
options = typeof options === "string" ? { path: options } : options; | ||
promise = (async () => { | ||
const files = await pathToFiles2(options.path); | ||
const commanders2 = await fileToModules2( | ||
promise = pathToFiles(options).then(async (files) => { | ||
commanders2 = await getFileValues( | ||
files, | ||
(item) => !!item && item instanceof Commander | ||
); | ||
composeFn = compose2( | ||
commanders2.map((commander) => commander["toMiddleware"]()) | ||
}); | ||
} | ||
return middleware3.console({ | ||
fn: async (ctx, next) => { | ||
if (!commanders2) | ||
await promise; | ||
composeFn ||= compose2( | ||
commanders2.map((commander) => commander["toMiddleware"](ctx.app)) | ||
); | ||
})(); | ||
} | ||
const instance = middleware4.console(async (ctx, next) => { | ||
if (!composeFn) | ||
await promise; | ||
return composeFn(ctx, next); | ||
return composeFn(ctx, next); | ||
}, | ||
help: { | ||
async onDocument(_, { app, children }) { | ||
await promise; | ||
await children(commanders2.map((commander) => commander["toMiddleware"](app))); | ||
}, | ||
async postDocument(_, { app, children }) { | ||
await children(commanders2.map((commander) => commander["toMiddleware"](app))); | ||
} | ||
} | ||
}); | ||
return chain2.console.mount( | ||
commanderToHelp( | ||
Array.isArray(options) ? { commanders: options } : { paths: options.path } | ||
) | ||
).mount(instance); | ||
}; | ||
var isCommanders = (options) => { | ||
return Array.isArray(options) && options.some((item) => item instanceof Commander); | ||
}; | ||
export { | ||
@@ -175,0 +143,0 @@ Builder, |
{ | ||
"name": "@aomex/commander", | ||
"version": "0.0.35", | ||
"description": "", | ||
"version": "1.0.0", | ||
"description": "创建aomex控制台指令", | ||
"type": "module", | ||
@@ -29,16 +29,14 @@ "types": "dist/index.d.ts", | ||
"peerDependencies": { | ||
"@aomex/console": "^0.0.33", | ||
"@aomex/core": "^0.0.28" | ||
"@aomex/console": "^1.0.0", | ||
"@aomex/core": "^1.0.0" | ||
}, | ||
"dependencies": { | ||
"@aomex/internal-tools": "^0.0.27", | ||
"@aomex/internal-file-import": "^0.0.27" | ||
"@aomex/internal-tools": "^1.0.0", | ||
"@aomex/internal-file-import": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@aomex/console": "^0.0.33", | ||
"@aomex/core": "^0.0.28" | ||
"@aomex/console": "^1.0.0", | ||
"@aomex/core": "^1.0.0" | ||
}, | ||
"scripts": { | ||
"test": "vitest" | ||
} | ||
"scripts": {} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
18442
182
1
+ Added@aomex/console@1.7.0(transitive)
+ Added@aomex/core@1.7.0(transitive)
+ Added@aomex/internal-file-import@1.7.0(transitive)
+ Added@aomex/internal-tools@1.7.0(transitive)
+ Added@esbuild/aix-ppc64@0.23.1(transitive)
+ Added@esbuild/android-arm@0.23.1(transitive)
+ Added@esbuild/android-arm64@0.23.1(transitive)
+ Added@esbuild/android-x64@0.23.1(transitive)
+ Added@esbuild/darwin-arm64@0.23.1(transitive)
+ Added@esbuild/darwin-x64@0.23.1(transitive)
+ Added@esbuild/freebsd-arm64@0.23.1(transitive)
+ Added@esbuild/freebsd-x64@0.23.1(transitive)
+ Added@esbuild/linux-arm@0.23.1(transitive)
+ Added@esbuild/linux-arm64@0.23.1(transitive)
+ Added@esbuild/linux-ia32@0.23.1(transitive)
+ Added@esbuild/linux-loong64@0.23.1(transitive)
+ Added@esbuild/linux-mips64el@0.23.1(transitive)
+ Added@esbuild/linux-ppc64@0.23.1(transitive)
+ Added@esbuild/linux-riscv64@0.23.1(transitive)
+ Added@esbuild/linux-s390x@0.23.1(transitive)
+ Added@esbuild/linux-x64@0.23.1(transitive)
+ Added@esbuild/netbsd-x64@0.23.1(transitive)
+ Added@esbuild/openbsd-arm64@0.23.1(transitive)
+ Added@esbuild/openbsd-x64@0.23.1(transitive)
+ Added@esbuild/sunos-x64@0.23.1(transitive)
+ Added@esbuild/win32-arm64@0.23.1(transitive)
+ Added@esbuild/win32-ia32@0.23.1(transitive)
+ Added@esbuild/win32-x64@0.23.1(transitive)
+ Addedesbuild@0.23.1(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedget-tsconfig@4.8.1(transitive)
+ Addedresolve-pkg-maps@1.0.0(transitive)
+ Addedtsx@4.19.2(transitive)
- Removed@aomex/console@0.0.33(transitive)
- Removed@aomex/core@0.0.28(transitive)
- Removed@aomex/internal-file-import@0.0.27(transitive)
- Removed@aomex/internal-tools@0.0.27(transitive)
- Removed@cspotcode/source-map-support@0.8.1(transitive)
- Removed@jridgewell/resolve-uri@3.1.2(transitive)
- Removed@jridgewell/sourcemap-codec@1.5.0(transitive)
- Removed@jridgewell/trace-mapping@0.3.9(transitive)
- Removed@tsconfig/node10@1.0.11(transitive)
- Removed@tsconfig/node12@1.0.11(transitive)
- Removed@tsconfig/node14@1.0.3(transitive)
- Removed@tsconfig/node16@1.0.4(transitive)
- Removed@types/yargs@17.0.33(transitive)
- Removed@types/yargs-parser@21.0.3(transitive)
- Removedacorn@8.14.0(transitive)
- Removedacorn-walk@8.3.4(transitive)
- Removedarg@4.1.3(transitive)
- Removedchalk@5.3.0(transitive)
- Removedcreate-require@1.1.1(transitive)
- Removeddiff@4.0.2(transitive)
- Removedip-regex@5.0.0(transitive)
- Removedmake-error@1.3.6(transitive)
- Removedts-node@10.9.2(transitive)
- Removedv8-compile-cache-lib@3.0.1(transitive)
- Removedyn@3.1.1(transitive)
Updated@aomex/internal-tools@^1.0.0