@umijs/core 4.0.56 → 4.0.57

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.
@@ -183,11 +183,20 @@ var Config = class {
183
183
  configKeys.delete(key);
184
184
  if (!opts.config[key])
185
185
  continue;
186
- const schema = opts.schemas[key](import_joi.default);
187
- (0, import_assert.default)(import_joi.default.isSchema(schema), `schema for config ${key} is not valid.`);
188
- const { error } = schema.validate(opts.config[key]);
189
- if (error)
190
- errors.set(key, error);
186
+ const schema = opts.schemas[key]({ ...import_joi.default, zod: import_utils.zod });
187
+ if (import_joi.default.isSchema(schema)) {
188
+ const { error } = schema.validate(opts.config[key]);
189
+ if (error)
190
+ errors.set(key, error);
191
+ } else {
192
+ (0, import_assert.default)(
193
+ (0, import_utils.isZodSchema)(schema),
194
+ `schema for config ${key} is not valid, neither joi nor zod.`
195
+ );
196
+ const { error } = schema.safeParse(opts.config[key]);
197
+ if (error)
198
+ errors.set(key, error);
199
+ }
191
200
  }
192
201
  (0, import_assert.default)(
193
202
  errors.size === 0,
@@ -77,7 +77,7 @@ function visitFiles(opts) {
77
77
  }
78
78
  function createRoutePath(routeId) {
79
79
  let path = routeId.replace(/^\$$/, "*").replace(/(\/|\.)\$$/, "/*").replace(/\$/g, ":").replace(/\./g, "/");
80
- path = /\b\/?index\/index$/.test(path) ? path.replace(/\/?index$/, "") : path;
80
+ path = /(^|\/)index\/index$/.test(path) ? path.replace(/\/index$/, "") : path;
81
81
  path = /\b\/?(?<!:)index$/.test(path) ? path.replace(/\/?index$/, "") : path;
82
82
  path = /\b\/?README$/.test(path) ? path.replace(/\/?README$/, "") : path;
83
83
  return path;
@@ -5,14 +5,16 @@ import { Generator } from './generator';
5
5
  import { IOpts as IHookOpts } from './hook';
6
6
  import { Plugin } from './plugin';
7
7
  import { Service } from './service';
8
+ import type { IMetry } from './telemetry';
8
9
  declare type Logger = typeof logger;
9
10
  declare const resolveConfigModes: readonly ["strict", "loose"];
10
- export declare type ResolveConfigMode = typeof resolveConfigModes[number];
11
+ export declare type ResolveConfigMode = (typeof resolveConfigModes)[number];
11
12
  declare type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
12
13
  export declare class PluginAPI {
13
14
  service: Service;
14
15
  plugin: Plugin;
15
16
  logger: Logger;
17
+ telemetry: IMetry;
16
18
  constructor(opts: {
17
19
  service: Service;
18
20
  plugin: Plugin;
@@ -45,6 +45,7 @@ var PluginAPI = class {
45
45
  constructor(opts) {
46
46
  this.service = opts.service;
47
47
  this.plugin = opts.plugin;
48
+ this.telemetry = opts.service.telemetry.prefixWith(this.plugin.key);
48
49
  const loggerKeys = [
49
50
  "wait",
50
51
  "error",
@@ -6,6 +6,7 @@ import { Command } from './command';
6
6
  import { Generator } from './generator';
7
7
  import { Hook } from './hook';
8
8
  import { Plugin } from './plugin';
9
+ import { Telemetry } from './telemetry';
9
10
  interface IOpts {
10
11
  cwd: string;
11
12
  env: Env;
@@ -67,6 +68,7 @@ export declare class Service {
67
68
  [key: string]: any;
68
69
  };
69
70
  pkgPath: string;
71
+ telemetry: Telemetry;
70
72
  constructor(opts: IOpts);
71
73
  applyPlugins<T>(opts: {
72
74
  key: string;
@@ -134,6 +136,7 @@ export interface IServicePluginAPI {
134
136
  }>;
135
137
  modifyDefaultConfig: IModify<typeof Service.prototype.config, null>;
136
138
  modifyPaths: IModify<typeof Service.prototype.paths, null>;
139
+ modifyTelemetryStorage: IModify<typeof Service.prototype.telemetry, null>;
137
140
  ApplyPluginsType: typeof ApplyPluginsType;
138
141
  ConfigChangeType: typeof ConfigChangeType;
139
142
  EnableBy: typeof EnableBy;
@@ -44,6 +44,7 @@ var import_env = require("./env");
44
44
  var import_path2 = require("./path");
45
45
  var import_plugin = require("./plugin");
46
46
  var import_pluginAPI = require("./pluginAPI");
47
+ var import_telemetry = require("./telemetry");
47
48
  var Service = class {
48
49
  constructor(opts) {
49
50
  this.appData = {};
@@ -67,6 +68,7 @@ var Service = class {
67
68
  this.configManager = null;
68
69
  this.pkg = {};
69
70
  this.pkgPath = "";
71
+ this.telemetry = new import_telemetry.Telemetry();
70
72
  this.cwd = opts.cwd;
71
73
  this.env = opts.env;
72
74
  this.opts = opts;
@@ -279,6 +281,11 @@ var Service = class {
279
281
  key: "modifyPaths",
280
282
  initialValue: this.paths
281
283
  });
284
+ const storage = await this.applyPlugins({
285
+ key: "modifyTelemetryStorage",
286
+ initialValue: import_telemetry.noopStorage
287
+ });
288
+ this.telemetry.useStorage(storage);
282
289
  this.stage = import_types.ServiceStage.collectAppData;
283
290
  this.appData = await this.applyPlugins({
284
291
  key: "modifyAppData",
@@ -29,7 +29,8 @@ var servicePlugin_default = (api) => {
29
29
  "modifyAppData",
30
30
  "modifyConfig",
31
31
  "modifyDefaultConfig",
32
- "modifyPaths"
32
+ "modifyPaths",
33
+ "modifyTelemetryStorage"
33
34
  ].forEach((name) => {
34
35
  api.registerMethod({ name });
35
36
  });
@@ -0,0 +1,32 @@
1
+ declare type MetreEvent = {
2
+ name: string;
3
+ timestamp?: number;
4
+ payload: any;
5
+ };
6
+ interface IStorage {
7
+ save(e: MetreEvent): Promise<void>;
8
+ }
9
+ export interface IMetry {
10
+ record(e: MetreEvent): void;
11
+ recordAsync(e: MetreEvent): Promise<boolean>;
12
+ flush(): Promise<void>;
13
+ }
14
+ export declare class Telemetry implements IMetry {
15
+ private queuedEvents;
16
+ private storage;
17
+ constructor();
18
+ prefixWith(prefix: string): IMetry;
19
+ useStorage(s: IStorage): void;
20
+ record(e: MetreEvent): void;
21
+ recordAsync(e: MetreEvent): Promise<boolean>;
22
+ flush(): Promise<void>;
23
+ private scheduleFlush;
24
+ private afterFlush;
25
+ private unFinishedEvents;
26
+ private addTimeStamp;
27
+ }
28
+ declare class NoopStorage implements IStorage {
29
+ save(_e: MetreEvent): Promise<void>;
30
+ }
31
+ export declare const noopStorage: NoopStorage;
32
+ export {};
@@ -0,0 +1,127 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/service/telemetry.ts
20
+ var telemetry_exports = {};
21
+ __export(telemetry_exports, {
22
+ Telemetry: () => Telemetry,
23
+ noopStorage: () => noopStorage
24
+ });
25
+ module.exports = __toCommonJS(telemetry_exports);
26
+ var Telemetry = class {
27
+ constructor() {
28
+ this.queuedEvents = [];
29
+ this.storage = new NoopStorage();
30
+ this.afterFlush = () => {
31
+ const un = this.unFinishedEvents();
32
+ if (un.length) {
33
+ this.scheduleFlush();
34
+ } else {
35
+ this.queuedEvents = [];
36
+ }
37
+ };
38
+ }
39
+ prefixWith(prefix) {
40
+ const upStream = this;
41
+ return {
42
+ record(e) {
43
+ const { name, ...rest } = e;
44
+ upStream.record({
45
+ name: `${prefix}:${name}`,
46
+ ...rest
47
+ });
48
+ },
49
+ async flush() {
50
+ return upStream.flush();
51
+ },
52
+ recordAsync(e) {
53
+ const { name, ...rest } = e;
54
+ return upStream.recordAsync({
55
+ name: `${prefix}:${name}`,
56
+ ...rest
57
+ });
58
+ }
59
+ };
60
+ }
61
+ useStorage(s) {
62
+ this.storage = s;
63
+ }
64
+ record(e) {
65
+ this.storage.save(this.addTimeStamp(e)).catch(() => {
66
+ this.queuedEvents.push({ event: e, tried: 1, status: "queued" });
67
+ this.scheduleFlush();
68
+ });
69
+ }
70
+ async recordAsync(e) {
71
+ try {
72
+ await this.storage.save(this.addTimeStamp(e));
73
+ return true;
74
+ } catch (e2) {
75
+ return false;
76
+ }
77
+ }
78
+ async flush() {
79
+ const events = this.unFinishedEvents().map((e) => {
80
+ if (e.status === "queued") {
81
+ e.status = "sending";
82
+ e.tried++;
83
+ e.promise = this.storage.save(e.event).then(
84
+ () => {
85
+ e.status = "sent";
86
+ },
87
+ () => {
88
+ e.status = "queued";
89
+ }
90
+ );
91
+ }
92
+ return e;
93
+ });
94
+ this.queuedEvents = events;
95
+ await Promise.all(events.map(({ promise }) => promise));
96
+ }
97
+ scheduleFlush() {
98
+ setTimeout(() => {
99
+ this.flush().then(this.afterFlush, this.afterFlush);
100
+ }, 5e3);
101
+ }
102
+ unFinishedEvents() {
103
+ return this.queuedEvents.filter((e) => {
104
+ if (e.status === "sent") {
105
+ return false;
106
+ }
107
+ return e.tried < 3;
108
+ });
109
+ }
110
+ addTimeStamp(e) {
111
+ if (!e.timestamp) {
112
+ e.timestamp = Date.now();
113
+ }
114
+ return e;
115
+ }
116
+ };
117
+ var NoopStorage = class {
118
+ async save(_e) {
119
+ return;
120
+ }
121
+ };
122
+ var noopStorage = new NoopStorage();
123
+ // Annotate the CommonJS export names for ESM import in node:
124
+ 0 && (module.exports = {
125
+ Telemetry,
126
+ noopStorage
127
+ });
package/dist/types.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import joi from '@umijs/utils/compiled/@hapi/joi';
2
+ import zod, { z } from '@umijs/utils/compiled/zod';
2
3
  export declare enum Env {
3
4
  development = "development",
4
5
  production = "production",
@@ -11,7 +12,9 @@ export declare enum PluginType {
11
12
  export interface IPluginConfig {
12
13
  default?: any;
13
14
  schema?: {
14
- (joi: joi.Root): joi.Schema;
15
+ (joi: joi.Root & {
16
+ zod: typeof z;
17
+ }): joi.Schema | zod.Schema;
15
18
  };
16
19
  onChange?: string | Function;
17
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/core",
3
- "version": "4.0.56",
3
+ "version": "4.0.57",
4
4
  "homepage": "https://github.com/umijs/umi/tree/master/packages/core#readme",
5
5
  "bugs": "https://github.com/umijs/umi/issues",
6
6
  "repository": {
@@ -21,8 +21,8 @@
21
21
  "test": "umi-scripts jest-turbo"
22
22
  },
23
23
  "dependencies": {
24
- "@umijs/bundler-utils": "4.0.56",
25
- "@umijs/utils": "4.0.56"
24
+ "@umijs/bundler-utils": "4.0.57",
25
+ "@umijs/utils": "4.0.57"
26
26
  },
27
27
  "devDependencies": {
28
28
  "dotenv": "16.0.0",