@initx-plugin/core 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -40,8 +40,13 @@ declare abstract class InitxHandler {
40
40
  abstract matchers: Matchers;
41
41
  abstract handle(options: InitxOptions, ...others: string[]): MaybePromise<void>;
42
42
  run(options: InitxOptions, ...others: string[]): HandlerInfo[];
43
+ private matchBaseMatchers;
44
+ private matchArrayBaseMatchers;
45
+ private matchTypeMatchers;
46
+ private isBaseMatchers;
47
+ private isArrayBaseMatchers;
43
48
  private isObject;
44
49
  private isPassed;
45
50
  }
46
51
 
47
- export { InitxHandler, type InitxOptions };
52
+ export { type HandlerInfo, InitxHandler, type InitxOptions };
package/dist/index.d.ts CHANGED
@@ -40,8 +40,13 @@ declare abstract class InitxHandler {
40
40
  abstract matchers: Matchers;
41
41
  abstract handle(options: InitxOptions, ...others: string[]): MaybePromise<void>;
42
42
  run(options: InitxOptions, ...others: string[]): HandlerInfo[];
43
+ private matchBaseMatchers;
44
+ private matchArrayBaseMatchers;
45
+ private matchTypeMatchers;
46
+ private isBaseMatchers;
47
+ private isArrayBaseMatchers;
43
48
  private isObject;
44
49
  private isPassed;
45
50
  }
46
51
 
47
- export { InitxHandler, type InitxOptions };
52
+ export { type HandlerInfo, InitxHandler, type InitxOptions };
package/dist/index.mjs CHANGED
@@ -1,30 +1,32 @@
1
1
  class InitxHandler {
2
2
  run(options, ...others) {
3
- const handlers = [];
4
- if (!Array.isArray(this.matchers) && this.matchers.matching && this.matchers.description && this.isPassed(this.matchers.matching, options.key)) {
5
- handlers.push({
6
- handler: () => this.handle(options, ...others),
7
- description: this.matchers.description
8
- });
9
- return handlers;
3
+ if (this.isBaseMatchers(this.matchers)) {
4
+ return this.matchBaseMatchers(this.matchers, options, ...others);
5
+ }
6
+ if (this.isArrayBaseMatchers(this.matchers)) {
7
+ return this.matchArrayBaseMatchers(this.matchers, options, ...others);
10
8
  }
11
9
  if (this.isObject(this.matchers)) {
12
- const keys = Object.keys(this.matchers);
13
- for (let i = 0; i < keys.length; i++) {
14
- const matcher = this.matchers[keys[i]];
15
- const isPassed = this.isPassed(matcher.matching, options.key);
16
- if (isPassed) {
17
- handlers.push({
18
- handler: () => this.handle(options, keys[i], ...others),
19
- description: matcher.description
20
- });
21
- break;
22
- }
23
- }
24
- return handlers;
10
+ return this.matchTypeMatchers(this.matchers, options, ...others);
11
+ }
12
+ return [];
13
+ }
14
+ // BaseMatchers
15
+ matchBaseMatchers(matchers, options, ...others) {
16
+ if (!this.isPassed(matchers.matching, options.key)) {
17
+ return [];
25
18
  }
26
- for (let i = 0; i < this.matchers.length; i++) {
27
- const matcher = this.matchers[i];
19
+ return [
20
+ {
21
+ handler: () => this.handle(options, ...others),
22
+ description: matchers.description
23
+ }
24
+ ];
25
+ }
26
+ matchArrayBaseMatchers(matchers, options, ...others) {
27
+ const handlers = [];
28
+ for (let i = 0; i < matchers.length; i++) {
29
+ const matcher = matchers[i];
28
30
  const isPassed = this.isPassed(matcher.matching, options.key);
29
31
  if (isPassed) {
30
32
  handlers.push({
@@ -35,6 +37,29 @@ class InitxHandler {
35
37
  }
36
38
  return handlers;
37
39
  }
40
+ matchTypeMatchers(matchers, options, ...others) {
41
+ const handlers = [];
42
+ const keys = Object.keys(matchers);
43
+ for (let i = 0; i < keys.length; i++) {
44
+ const matcher = matchers[keys[i]];
45
+ const isPassed = this.isPassed(matcher.matching, options.key);
46
+ if (isPassed) {
47
+ handlers.push({
48
+ handler: () => this.handle(options, keys[i], ...others),
49
+ description: matcher.description
50
+ });
51
+ break;
52
+ }
53
+ }
54
+ return handlers;
55
+ }
56
+ isBaseMatchers(matchers) {
57
+ const keys = Object.keys(matchers);
58
+ return this.isObject(matchers) && keys.length === 2 && keys.every((key) => key === "matching" || key === "description");
59
+ }
60
+ isArrayBaseMatchers(matchers) {
61
+ return Array.isArray(matchers) && matchers.every(this.isBaseMatchers.bind(this));
62
+ }
38
63
  isObject(value) {
39
64
  return typeof value === "object" && value !== null && !Array.isArray(value);
40
65
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@initx-plugin/core",
3
3
  "type": "module",
4
- "version": "0.0.8",
4
+ "version": "0.0.10",
5
5
  "description": "core module for initx plugins",
6
6
  "author": "imba97",
7
7
  "license": "MIT",
@@ -16,7 +16,7 @@
16
16
  "keywords": [
17
17
  "initx"
18
18
  ],
19
- "main": "dist/index.cjs",
19
+ "main": "dist/index.mjs",
20
20
  "module": "dist/index.mjs",
21
21
  "types": "dist/index.d.ts",
22
22
  "files": [
package/dist/index.cjs DELETED
@@ -1,54 +0,0 @@
1
- 'use strict';
2
-
3
- class InitxHandler {
4
- run(options, ...others) {
5
- const handlers = [];
6
- if (!Array.isArray(this.matchers) && this.matchers.matching && this.matchers.description && this.isPassed(this.matchers.matching, options.key)) {
7
- handlers.push({
8
- handler: () => this.handle(options, ...others),
9
- description: this.matchers.description
10
- });
11
- return handlers;
12
- }
13
- if (this.isObject(this.matchers)) {
14
- const keys = Object.keys(this.matchers);
15
- for (let i = 0; i < keys.length; i++) {
16
- const matcher = this.matchers[keys[i]];
17
- const isPassed = this.isPassed(matcher.matching, options.key);
18
- if (isPassed) {
19
- handlers.push({
20
- handler: () => this.handle(options, keys[i], ...others),
21
- description: matcher.description
22
- });
23
- break;
24
- }
25
- }
26
- return handlers;
27
- }
28
- for (let i = 0; i < this.matchers.length; i++) {
29
- const matcher = this.matchers[i];
30
- const isPassed = this.isPassed(matcher.matching, options.key);
31
- if (isPassed) {
32
- handlers.push({
33
- handler: () => this.handle(options, ...others),
34
- description: matcher.description
35
- });
36
- }
37
- }
38
- return handlers;
39
- }
40
- isObject(value) {
41
- return typeof value === "object" && value !== null && !Array.isArray(value);
42
- }
43
- isPassed(matchers, key) {
44
- const tests = Array.isArray(matchers) ? matchers : [matchers];
45
- return tests.some((test) => {
46
- if (typeof test === "string") {
47
- return test === key;
48
- }
49
- return test.test(key);
50
- });
51
- }
52
- }
53
-
54
- exports.InitxHandler = InitxHandler;
package/dist/index.d.cts DELETED
@@ -1,47 +0,0 @@
1
- type MaybeArray<T> = T | T[];
2
- type MaybePromise<T> = T | Promise<T>;
3
- interface BaseMatchers {
4
- matching: MaybeArray<string | RegExp>;
5
- /**
6
- * Description of the handler
7
- *
8
- * If multiple handlers are matched, this description will be displayed
9
- */
10
- description: string;
11
- }
12
- type TypeMatchers = Record<string, BaseMatchers>;
13
- type Matchers = MaybeArray<BaseMatchers> | TypeMatchers;
14
- interface HandlerInfo {
15
- handler: () => MaybePromise<void>;
16
- description: string;
17
- }
18
- interface InitxOptions {
19
- /**
20
- * Matching string
21
- *
22
- * The key that was used to match the handler
23
- */
24
- key: string;
25
- /**
26
- * CLI options
27
- *
28
- * cac package parsed options
29
- */
30
- cliOptions: Record<string, any>;
31
- /**
32
- * Options list
33
- *
34
- * cli options list, like
35
- * @example ['--global']
36
- */
37
- optionsList: string[];
38
- }
39
- declare abstract class InitxHandler {
40
- abstract matchers: Matchers;
41
- abstract handle(options: InitxOptions, ...others: string[]): MaybePromise<void>;
42
- run(options: InitxOptions, ...others: string[]): HandlerInfo[];
43
- private isObject;
44
- private isPassed;
45
- }
46
-
47
- export { InitxHandler, type InitxOptions };