alemonjs 2.0.0 → 2.0.2

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/bin/start.js CHANGED
@@ -1,7 +1,30 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { readFileSync } from 'fs'
4
- import { join } from 'path'
3
+ import path from 'path';
4
+ import fs from 'fs'
5
+ import { createRequire } from 'module';
6
+ const require = createRequire(import.meta.url);
7
+
8
+ const createExports = (packageJson) => {
9
+ if (packageJson?.exports) {
10
+ if (typeof packageJson.exports === 'string') {
11
+ return packageJson.exports;
12
+ } else if (typeof packageJson.exports === 'object') {
13
+ return packageJson.exports['.'] || packageJson.exports['./index.js'];
14
+ }
15
+ }
16
+ }
17
+
18
+ const getInputExportPath = (input) => {
19
+ const packageJsonPath = path.join(input ?? process.cwd(), 'package.json');
20
+ if (fs.existsSync(packageJsonPath)) {
21
+ const packageJson = require(packageJsonPath);
22
+ const main = packageJson?.main || createExports(packageJson);
23
+ if (main) {
24
+ return main;
25
+ }
26
+ }
27
+ }
5
28
 
6
29
  /**
7
30
  *
@@ -9,9 +32,7 @@ import { join } from 'path'
9
32
  */
10
33
  export const start = () => {
11
34
  // 读取配置文件
12
- const dir = join(process.cwd(), 'package.json')
13
- const start = readFileSync(dir, 'utf-8')
14
- const { main } = JSON.parse(start) ?? {}
35
+ const main = getInputExportPath();
15
36
  import('../lib/index.js').then(res => {
16
37
  res.start(main)
17
38
  })
package/lib/app/load.js CHANGED
@@ -1,20 +1,10 @@
1
1
  import { dirname, join } from 'path';
2
- import { existsSync, readFileSync } from 'fs';
2
+ import { existsSync } from 'fs';
3
3
  import { unChildren } from './hook-use-api.js';
4
4
  import { showErrorModule, getRecursiveDirFiles, createEventName } from './utils.js';
5
+ import { createRequire } from 'module';
5
6
 
6
- // import { createRequire } from 'module'
7
- // const createChildrenKey = () => {
8
- // // 随机一个key
9
- // const KEY = `${Date.now()}:${Math.random()}`
10
- // // 如果存在。则重新生成
11
- // // 把子应用挂起来
12
- // // if (alemonjsCore.storeChildren[KEY]) {
13
- // // return createChildrenKey()
14
- // // }
15
- // //
16
- // }
17
- // const require = createRequire(import.meta.url)
7
+ const require = createRequire(import.meta.url);
18
8
  const mwReg = /^mw(\.|\..*\.)(js|ts|jsx|tsx)$/;
19
9
  /**
20
10
  * 加载文件
@@ -68,7 +58,7 @@ const loadChildren = async (mainPath, node) => {
68
58
  return;
69
59
  }
70
60
  const mainDir = dirname(mainPath);
71
- const show = e => {
61
+ const show = (e) => {
72
62
  showErrorModule(e);
73
63
  // 卸载索引
74
64
  unChildren(mainDir);
@@ -149,17 +139,8 @@ const loadChildrenFile = async (node) => {
149
139
  logger.error('The module name is not correct');
150
140
  return;
151
141
  }
152
- const dir = join(process.cwd(), 'node_modules', node);
153
- const pkgPath = join(dir, 'package.json');
154
- if (!existsSync(pkgPath)) {
155
- logger.error('The package.json does not exist', pkgPath);
156
- return;
157
- }
158
142
  try {
159
- // 存在 package
160
- const packageJson = JSON.parse(readFileSync(pkgPath, 'utf-8'));
161
- // main
162
- const mainPath = join(dir, packageJson.main);
143
+ const mainPath = require.resolve(node);
163
144
  // 不存在 main
164
145
  if (!existsSync(mainPath)) {
165
146
  logger.error('The main file does not exist', mainPath);
@@ -54,5 +54,6 @@ declare const showErrorModule: (e: Error) => void;
54
54
  * @deprecated
55
55
  */
56
56
  declare const ErrorModule: (e: Error) => void;
57
+ declare const getInputExportPath: (input?: string) => any;
57
58
 
58
- export { ErrorModule, createEventName, createHash, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey };
59
+ export { ErrorModule, createEventName, createHash, getInputExportPath, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey };
package/lib/app/utils.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import crypto from 'crypto';
2
- import { existsSync, readdirSync } from 'fs';
3
- import { join } from 'path';
2
+ import fs, { existsSync, readdirSync } from 'fs';
3
+ import path, { join } from 'path';
4
+ import { createRequire } from 'module';
4
5
 
6
+ const require = createRequire(import.meta.url);
5
7
  /**
6
8
  * 将字符串转为定长字符串
7
9
  * @param str 输入字符串
@@ -113,5 +115,25 @@ const showErrorModule = (e) => {
113
115
  * @deprecated
114
116
  */
115
117
  const ErrorModule = showErrorModule;
118
+ const createExports = (packageJson) => {
119
+ if (packageJson?.exports) {
120
+ if (typeof packageJson.exports === 'string') {
121
+ return packageJson.exports;
122
+ }
123
+ else if (typeof packageJson.exports === 'object') {
124
+ return packageJson.exports['.'] || packageJson.exports['./index.js'];
125
+ }
126
+ }
127
+ };
128
+ const getInputExportPath = (input) => {
129
+ const packageJsonPath = path.join(input ?? process.cwd(), 'package.json');
130
+ if (fs.existsSync(packageJsonPath)) {
131
+ const packageJson = require(packageJsonPath);
132
+ const main = packageJson?.main || createExports(packageJson);
133
+ if (main) {
134
+ return main;
135
+ }
136
+ }
137
+ };
116
138
 
117
- export { ErrorModule, createEventName, createHash, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey };
139
+ export { ErrorModule, createEventName, createHash, getInputExportPath, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey };
package/lib/index.d.ts CHANGED
@@ -27,18 +27,19 @@ export { Image, ImageFile, ImageURL, Mention, Text } from './app/hook-message-fo
27
27
  export { createSelects, unChildren, unMount, useMention, useSend } from './app/hook-use-api.js';
28
28
  export { eventState, onState, unEventState, unState, useState } from './app/hook-use-state.js';
29
29
  export { useObserver, useSubscribe } from './app/hook-use-subscribe.js';
30
- export { ErrorModule, createEventName, createHash, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey } from './app/utils.js';
30
+ export { ErrorModule, createEventName, createHash, getInputExportPath, getRecursiveDirFiles, showErrorModule, stringToNumber, useUserHashKey } from './app/utils.js';
31
31
  export { ConfigCore, getConfig, getConfigValue } from './config.js';
32
32
 
33
33
  /**
34
34
  * 运行指定 main
35
- * @param input
35
+ * @param input 入口地址
36
36
  * @returns
37
37
  */
38
38
  declare const run: (input: string) => void;
39
39
  /**
40
40
  * 启动
41
- * @param input
41
+ * @param input (可选)main入口地址,默认选择 package.json 中的 main
42
+ * @param pm (可选)平台名称,默认@alemonjs/gui。
42
43
  */
43
44
  declare const start: (input?: string, pm?: string) => Promise<void>;
44
45
 
package/lib/index.js CHANGED
@@ -5,7 +5,7 @@ import { existsSync } from 'fs';
5
5
  import { getConfig } from './config.js';
6
6
  export { ConfigCore, getConfigValue } from './config.js';
7
7
  import { loadChildren, loadChildrenFile } from './app/load.js';
8
- import { showErrorModule } from './app/utils.js';
8
+ import { showErrorModule, getInputExportPath } from './app/utils.js';
9
9
  export { ErrorModule, createEventName, createHash, getRecursiveDirFiles, stringToNumber, useUserHashKey } from './app/utils.js';
10
10
  export { createSendDataFormat, sendToChannel, sendToUser } from './app/api.js';
11
11
  export { defineBot, definePlatform } from './app/event-bot.js';
@@ -20,18 +20,16 @@ export { useObserver, useSubscribe } from './app/hook-use-subscribe.js';
20
20
 
21
21
  /**
22
22
  * 运行指定 main
23
- * @param input
23
+ * @param input 入口地址
24
24
  * @returns
25
25
  */
26
26
  const run = (input) => {
27
- if (!input) {
28
- // 抛出错误
29
- throw new Error('The input is not correct');
30
- }
27
+ if (!input || input == '')
28
+ return;
29
+ let mainPath = join(process.cwd(), input);
31
30
  // 路径
32
- const mainPath = join(process.cwd(), input);
33
- if (!existsSync(mainPath)) {
34
- logger.error(`The file ${mainPath} does not exist`);
31
+ if (!existsSync(input)) {
32
+ logger.error('未找到主要入口文件', mainPath);
35
33
  return;
36
34
  }
37
35
  // 指定运行的,name识别为 'main:apps:xxx'
@@ -39,7 +37,8 @@ const run = (input) => {
39
37
  };
40
38
  /**
41
39
  * 启动
42
- * @param input
40
+ * @param input (可选)main入口地址,默认选择 package.json 中的 main
41
+ * @param pm (可选)平台名称,默认@alemonjs/gui。
43
42
  */
44
43
  const start = async (input, pm) => {
45
44
  const cfg = getConfig();
@@ -83,14 +82,12 @@ const start = async (input, pm) => {
83
82
  }
84
83
  // 运行本地模块
85
84
  try {
86
- const dir = input ?? cfg.argv?.main ?? cfg.value?.main;
87
- if (dir) {
88
- run(dir);
89
- }
85
+ const dir = input ?? cfg.argv?.main ?? cfg.value?.main ?? getInputExportPath();
86
+ run(dir);
90
87
  }
91
88
  catch (e) {
92
89
  logger.error(e);
93
90
  }
94
91
  };
95
92
 
96
- export { getConfig, run, showErrorModule, start };
93
+ export { getConfig, getInputExportPath, run, showErrorModule, start };
package/lib/jsx.js CHANGED
@@ -8,10 +8,10 @@ import './app/event-utlis.js';
8
8
  import { Text as Text$1, ImageURL as ImageURL$1, ImageFile as ImageFile$1, Image as Image$1, Mention as Mention$1 } from './app/hook-message-format.js';
9
9
  import { useSend as useSend$1 } from './app/hook-use-api.js';
10
10
  import './app/hook-use-state.js';
11
- import 'crypto';
11
+ import './app/utils.js';
12
+ import './global.js';
12
13
  import 'fs';
13
14
  import 'path';
14
- import './global.js';
15
15
  import 'yaml';
16
16
 
17
17
  /**
@@ -99,9 +99,11 @@ function JSX(...arg) {
99
99
  }
100
100
  else if (dataType === 'Mention') {
101
101
  // <@!123456> 文本的显示会被平台显示。不用自己定义显示的文本,此处不使用 children
102
- data.push(Mention$1(props.value, props?.belong ? {
103
- belong: props?.belong
104
- } : null));
102
+ data.push(Mention$1(props.value, props?.belong
103
+ ? {
104
+ belong: props?.belong
105
+ }
106
+ : null));
105
107
  }
106
108
  }
107
109
  if (data.length === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alemonjs",
3
- "version": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "bot script",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",