koishi-plugin-rzgtboeyndxsklmq-commons 1.0.1 → 1.0.3

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/lib/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- import { Context, Schema } from 'koishi';
2
- export * from './utils/BeanHelper';
3
- export * from './utils/File';
1
+ import { Context, Schema } from "koishi";
2
+ export * from "./utils/BeanHelper";
3
+ export * from "./utils/File";
4
+ export * from "./utils/Test";
5
+ export * from "./utils/I18n";
4
6
  export declare const name = "rzgtboeyndxsklmq-commons";
5
7
  export interface Config {
6
8
  }
package/lib/index.js CHANGED
@@ -34,8 +34,11 @@ __export(src_exports, {
34
34
  BeanType: () => BeanType,
35
35
  Config: () => Config,
36
36
  apply: () => apply,
37
+ loadLocales: () => loadLocales,
37
38
  name: () => name,
38
- readDirFiles: () => readDirFiles
39
+ readDirFiles: () => readDirFiles,
40
+ test: () => test,
41
+ test2: () => test2
39
42
  });
40
43
  module.exports = __toCommonJS(src_exports);
41
44
  var import_koishi = require("koishi");
@@ -198,24 +201,99 @@ var import_promises = __toESM(require("node:fs/promises"));
198
201
  var import_node_path = __toESM(require("node:path"));
199
202
  async function readDirFiles(dirPath, needDir = false) {
200
203
  const files = await import_promises.default.readdir(dirPath, {
204
+ recursive: true,
201
205
  withFileTypes: true
202
206
  });
203
207
  const paths = [];
204
- files.sort((a, b) => (a.isFile() ? 1 : 0) - (b.isFile() ? 1 : 0));
205
- for (const file of files) {
208
+ files.forEach((file) => {
206
209
  if (file.isDirectory()) {
207
- if (needDir) {
208
- paths.push(import_node_path.default.join(import_node_path.default.resolve(dirPath, file.name), import_node_path.default.sep));
210
+ if (!needDir) {
211
+ return;
209
212
  }
210
- paths.push(...await readDirFiles(import_node_path.default.join(dirPath, file.name)));
213
+ paths.push(import_node_path.default.join(file.parentPath, file.name, import_node_path.default.sep));
211
214
  } else if (file.isFile()) {
212
- paths.push(import_node_path.default.resolve(dirPath, file.name));
215
+ paths.push(import_node_path.default.join(file.parentPath, file.name));
213
216
  }
214
- }
217
+ });
215
218
  return paths;
216
219
  }
217
220
  __name(readDirFiles, "readDirFiles");
218
221
 
222
+ // src/utils/Test.ts
223
+ async function test(name2, count, serial, run) {
224
+ const timeout = setInterval(() => {
225
+ process.stdout.write(".");
226
+ }, 100);
227
+ let timeConsumings = [];
228
+ timeConsumings.length = count;
229
+ const testStartTime = Date.now();
230
+ if (serial) {
231
+ for (let i = 0; i < count; i++) {
232
+ const time = Date.now();
233
+ await run();
234
+ timeConsumings[i] = Date.now() - time;
235
+ }
236
+ } else {
237
+ timeConsumings = [...timeConsumings];
238
+ await Promise.all(
239
+ timeConsumings.map((_, i) => {
240
+ return new Promise((resolve) => {
241
+ setImmediate(async () => {
242
+ const time = Date.now();
243
+ await run();
244
+ timeConsumings[i] = Date.now() - time;
245
+ resolve(0);
246
+ });
247
+ });
248
+ })
249
+ );
250
+ }
251
+ const testTime = Date.now() - testStartTime;
252
+ clearInterval(timeout);
253
+ if (count < 2) {
254
+ console.log(`
255
+ run: ${name2}; total: ${timeConsumings[0]};`);
256
+ return;
257
+ }
258
+ let total = 0;
259
+ let average = 0;
260
+ let highest = 0;
261
+ let lowest = null;
262
+ for (let i = 1; i < timeConsumings.length; i++) {
263
+ const t = timeConsumings[i];
264
+ if (lowest == null || t < lowest) {
265
+ lowest = t;
266
+ }
267
+ highest = Math.max(t, highest);
268
+ total += t;
269
+ }
270
+ average = total / (count - 1);
271
+ console.log(
272
+ `
273
+ run: ${name2}; serial: ${serial}; count: ${count}; testTime: ${testTime}; total: ${total}; first: ${timeConsumings[0]}; average: ${average}; highest: ${highest}; lowest: ${lowest};`
274
+ );
275
+ }
276
+ __name(test, "test");
277
+ async function test2(name2, count, run) {
278
+ await test(name2, count, true, run);
279
+ await test(name2, count, false, run);
280
+ }
281
+ __name(test2, "test2");
282
+
283
+ // src/utils/I18n.ts
284
+ var import_node_path2 = __toESM(require("node:path"));
285
+ async function loadLocales(ctx, dirPath) {
286
+ const files = await readDirFiles(dirPath);
287
+ for (const file of files) {
288
+ if (!file.endsWith(".yml")) {
289
+ continue;
290
+ }
291
+ const l = import_node_path2.default.basename(file);
292
+ ctx.i18n.define(l, require(file));
293
+ }
294
+ }
295
+ __name(loadLocales, "loadLocales");
296
+
219
297
  // src/index.ts
220
298
  var name = "rzgtboeyndxsklmq-commons";
221
299
  var Config = import_koishi.Schema.object({});
@@ -228,6 +306,9 @@ __name(apply, "apply");
228
306
  BeanType,
229
307
  Config,
230
308
  apply,
309
+ loadLocales,
231
310
  name,
232
- readDirFiles
311
+ readDirFiles,
312
+ test,
313
+ test2
233
314
  });
@@ -0,0 +1,2 @@
1
+ import { Context } from "koishi";
2
+ export declare function loadLocales(ctx: Context, dirPath: string): Promise<void>;
@@ -0,0 +1,2 @@
1
+ export declare function test(name: string, count: number, serial: boolean, run: () => any): Promise<void>;
2
+ export declare function test2(name: string, count: number, run: () => any): Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-rzgtboeyndxsklmq-commons",
3
3
  "description": "",
4
- "version": "1.0.1",
4
+ "version": "1.0.3",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "scripts": {