@umijs/core 4.0.0-beta.1 → 4.0.0-beta.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.
@@ -30,7 +30,7 @@ export declare class Config {
30
30
  data: ReturnType<typeof Config.diffConfigs>;
31
31
  event: string;
32
32
  path: string;
33
- }) => void;
33
+ }) => Promise<void>;
34
34
  }): () => Promise<void>;
35
35
  static getMainConfigFile(opts: {
36
36
  cwd: string;
@@ -66,10 +66,14 @@ class Config {
66
66
  updated,
67
67
  onChangeTypes: opts.onChangeTypes,
68
68
  });
69
- opts.onChange({
69
+ opts
70
+ .onChange({
70
71
  data,
71
72
  event,
72
73
  path,
74
+ })
75
+ .catch((e) => {
76
+ throw new Error(e);
73
77
  });
74
78
  }, constants_1.WATCH_DEBOUNCE_STEP));
75
79
  return () => watcher.close();
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './config/config';
2
2
  export * from './route/route';
3
+ export { GeneratorType, IGeneratorOpts } from './service/generator';
3
4
  export * from './service/pluginAPI';
4
5
  export * from './service/service';
5
6
  export { Env, IAdd, IEvent, IModify, IRoute } from './types';
package/dist/index.js CHANGED
@@ -10,9 +10,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.Env = void 0;
13
+ exports.Env = exports.GeneratorType = void 0;
14
14
  __exportStar(require("./config/config"), exports);
15
15
  __exportStar(require("./route/route"), exports);
16
+ var generator_1 = require("./service/generator");
17
+ Object.defineProperty(exports, "GeneratorType", { enumerable: true, get: function () { return generator_1.GeneratorType; } });
16
18
  __exportStar(require("./service/pluginAPI"), exports);
17
19
  __exportStar(require("./service/service"), exports);
18
20
  var types_1 = require("./types");
@@ -0,0 +1,4 @@
1
+ import { PluginAPI } from './pluginAPI';
2
+ import { IServicePluginAPI } from './service';
3
+ declare const _default: (api: PluginAPI & IServicePluginAPI) => void;
4
+ export default _default;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const utils_1 = require("@umijs/utils");
13
+ const generator_1 = require("./generator");
14
+ exports.default = (api) => {
15
+ api.registerCommand({
16
+ name: 'generate',
17
+ alias: 'g',
18
+ details: `
19
+ umi generate
20
+ `,
21
+ description: 'generate code snippets quickly',
22
+ fn({ args }) {
23
+ var _a;
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const [type] = args._;
26
+ const runGenerator = (generator) => {
27
+ generator === null || generator === void 0 ? void 0 : generator.fn({
28
+ api,
29
+ args,
30
+ generateFile: utils_1.generateFile,
31
+ installDeps: utils_1.installDeps,
32
+ updatePackageJSON: utils_1.updatePackageJSON,
33
+ });
34
+ };
35
+ if (type) {
36
+ const generator = api.service.generators[type];
37
+ if (!generator) {
38
+ throw new Error(`Generator ${type} not found.`);
39
+ }
40
+ if (generator.type === generator_1.GeneratorType.enable) {
41
+ const enable = yield ((_a = generator.checkEnable) === null || _a === void 0 ? void 0 : _a.call(generator, {
42
+ api,
43
+ args,
44
+ }));
45
+ if (!enable) {
46
+ throw new Error(`Generator ${type} is unable.The corresponding function has been turned on or is not available.`);
47
+ }
48
+ }
49
+ runGenerator(generator);
50
+ }
51
+ else {
52
+ const getEnableGenerators = (generators) => __awaiter(this, void 0, void 0, function* () {
53
+ const questions = [];
54
+ Object.keys(generators).forEach((key) => __awaiter(this, void 0, void 0, function* () {
55
+ var _b, _c;
56
+ if (generators[key].type === generator_1.GeneratorType.generate) {
57
+ questions.push({
58
+ title: `${generators[key].name} -- ${generators[key].description}` ||
59
+ '',
60
+ value: generators[key].key,
61
+ });
62
+ }
63
+ else {
64
+ const enable = yield ((_c = (_b = generators[key]) === null || _b === void 0 ? void 0 : _b.checkEnable) === null || _c === void 0 ? void 0 : _c.call(_b, {
65
+ api,
66
+ args,
67
+ }));
68
+ if (enable) {
69
+ questions.push({
70
+ title: `${generators[key].name} -- ${generators[key].description}` ||
71
+ '',
72
+ value: generators[key].key,
73
+ });
74
+ }
75
+ }
76
+ }));
77
+ return questions;
78
+ });
79
+ const questions = yield getEnableGenerators(api.service.generators);
80
+ const { gType } = yield (0, utils_1.prompts)({
81
+ type: 'select',
82
+ name: 'gType',
83
+ message: 'Pick generator type',
84
+ choices: questions,
85
+ });
86
+ runGenerator(api.service.generators[gType]);
87
+ }
88
+ });
89
+ },
90
+ });
91
+ };
@@ -0,0 +1,60 @@
1
+ import { prompts } from '@umijs/utils';
2
+ import { Plugin } from './plugin';
3
+ import { PluginAPI } from './pluginAPI';
4
+ import { IServicePluginAPI } from './service';
5
+ export declare enum GeneratorType {
6
+ generate = "generate",
7
+ enable = "enable"
8
+ }
9
+ export interface IGeneratorOpts {
10
+ key: string;
11
+ name?: string;
12
+ description?: string;
13
+ type?: GeneratorType;
14
+ checkEnable?: {
15
+ (opts: {
16
+ args: any;
17
+ api: PluginAPI & IServicePluginAPI;
18
+ }): boolean;
19
+ };
20
+ fn: {
21
+ (opts: {
22
+ args: any;
23
+ api: PluginAPI & IServicePluginAPI;
24
+ generateFile: {
25
+ (opts: {
26
+ path: string;
27
+ target: string;
28
+ data?: any;
29
+ questions?: prompts.PromptObject[];
30
+ }): void;
31
+ };
32
+ updatePackageJSON: {
33
+ (opts: {
34
+ opts: object;
35
+ cwd?: string;
36
+ }): void;
37
+ };
38
+ installDeps: {
39
+ (opts: {
40
+ opts: {
41
+ devDependencies?: string[];
42
+ dependencies?: string[];
43
+ };
44
+ cwd?: string;
45
+ }): void;
46
+ };
47
+ }): void;
48
+ };
49
+ plugin: Plugin;
50
+ }
51
+ export declare class Generator {
52
+ key: IGeneratorOpts['key'];
53
+ name?: IGeneratorOpts['name'];
54
+ description?: IGeneratorOpts['description'];
55
+ type?: IGeneratorOpts['type'];
56
+ checkEnable?: IGeneratorOpts['checkEnable'];
57
+ fn: IGeneratorOpts['fn'];
58
+ plugin: IGeneratorOpts['plugin'];
59
+ constructor(opts: IGeneratorOpts);
60
+ }
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Generator = exports.GeneratorType = void 0;
4
+ var GeneratorType;
5
+ (function (GeneratorType) {
6
+ GeneratorType["generate"] = "generate";
7
+ GeneratorType["enable"] = "enable";
8
+ })(GeneratorType = exports.GeneratorType || (exports.GeneratorType = {}));
9
+ class Generator {
10
+ constructor(opts) {
11
+ this.key = opts.key;
12
+ this.name = opts.name;
13
+ this.type = opts.type;
14
+ this.description = opts.description;
15
+ this.checkEnable = opts.checkEnable;
16
+ this.fn = opts.fn;
17
+ this.plugin = opts.plugin;
18
+ }
19
+ }
20
+ exports.Generator = Generator;
@@ -1,5 +1,6 @@
1
1
  import { EnableBy, IPluginConfig } from '../types';
2
2
  import { IOpts as ICommandOpts } from './command';
3
+ import { IGeneratorOpts } from './generator';
3
4
  import { IOpts as IHookOpts } from './hook';
4
5
  import { Plugin } from './plugin';
5
6
  import { Service } from './service';
@@ -18,6 +19,7 @@ export declare class PluginAPI {
18
19
  registerCommand(opts: Omit<ICommandOpts, 'plugin'> & {
19
20
  alias?: string | string[];
20
21
  }): void;
22
+ registerGenerator(opts: Omit<IGeneratorOpts, 'plugin'>): void;
21
23
  register(opts: Omit<IHookOpts, 'plugin'>): void;
22
24
  registerMethod(opts: {
23
25
  name: string;
@@ -8,6 +8,7 @@ const utils_1 = require("@umijs/utils");
8
8
  const assert_1 = __importDefault(require("assert"));
9
9
  const types_1 = require("../types");
10
10
  const command_1 = require("./command");
11
+ const generator_1 = require("./generator");
11
12
  const hook_1 = require("./hook");
12
13
  const plugin_1 = require("./plugin");
13
14
  const utils_2 = require("./utils");
@@ -38,6 +39,12 @@ class PluginAPI {
38
39
  });
39
40
  }
40
41
  }
42
+ registerGenerator(opts) {
43
+ var _a;
44
+ const { key } = opts;
45
+ (0, assert_1.default)(!this.service.generators[key], `api.registerGenerator() failed, the generator ${key} is exists from ${(_a = this.service.generators[key]) === null || _a === void 0 ? void 0 : _a.plugin.id}.`);
46
+ this.service.generators[key] = new generator_1.Generator(Object.assign(Object.assign({}, opts), { plugin: this.plugin }));
47
+ }
41
48
  register(opts) {
42
49
  var _a, _b;
43
50
  (_a = this.service.hooks)[_b = opts.key] || (_a[_b] = []);
@@ -2,6 +2,7 @@ import { yParser } from '@umijs/utils';
2
2
  import { Config } from '../config/config';
3
3
  import { ApplyPluginsType, ConfigChangeType, EnableBy, Env, IEvent, IModify, ServiceStage } from '../types';
4
4
  import { Command } from './command';
5
+ import { Generator } from './generator';
5
6
  import { Hook } from './hook';
6
7
  import { Plugin } from './plugin';
7
8
  interface IOpts {
@@ -17,6 +18,7 @@ export declare class Service {
17
18
  appData: Record<string, any>;
18
19
  args: yParser.Arguments;
19
20
  commands: Record<string, Command>;
21
+ generators: Record<string, Generator>;
20
22
  config: Record<string, any>;
21
23
  configSchemas: Record<string, any>;
22
24
  configDefaults: Record<string, any>;
@@ -49,7 +51,7 @@ export declare class Service {
49
51
  type?: ApplyPluginsType;
50
52
  initialValue?: any;
51
53
  args?: any;
52
- }): Promise<T>;
54
+ }): Promise<typeof opts.initialValue | T>;
53
55
  run(opts: {
54
56
  name: string;
55
57
  args?: any;
@@ -72,6 +74,7 @@ export interface IServicePluginAPI {
72
74
  args: typeof Service.prototype.args;
73
75
  config: typeof Service.prototype.config;
74
76
  cwd: typeof Service.prototype.cwd;
77
+ generators: typeof Service.prototype.generators;
75
78
  pkg: typeof Service.prototype.pkg;
76
79
  name: typeof Service.prototype.name;
77
80
  paths: Required<typeof Service.prototype.paths>;
@@ -1,23 +1,4 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
22
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
23
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -36,7 +17,6 @@ const utils_1 = require("@umijs/utils");
36
17
  const assert_1 = __importDefault(require("assert"));
37
18
  const fs_1 = require("fs");
38
19
  const path_1 = require("path");
39
- const process = __importStar(require("process"));
40
20
  const tapable_1 = require("../../compiled/tapable");
41
21
  const config_1 = require("../config/config");
42
22
  const constants_1 = require("../constants");
@@ -51,6 +31,7 @@ class Service {
51
31
  this.appData = {};
52
32
  this.args = { _: [], $0: '' };
53
33
  this.commands = {};
34
+ this.generators = {};
54
35
  this.config = {};
55
36
  this.configSchemas = {};
56
37
  this.configDefaults = {};
@@ -180,7 +161,7 @@ class Service {
180
161
  const { plugins, presets } = plugin_1.Plugin.getPluginsAndPresets({
181
162
  cwd: this.cwd,
182
163
  pkg,
183
- plugins: this.opts.plugins || [],
164
+ plugins: [require.resolve('./generatePlugin')].concat(this.opts.plugins || []),
184
165
  presets: [require.resolve('./servicePlugin')].concat(this.opts.presets || []),
185
166
  userConfig: this.userConfig,
186
167
  prefix: this.opts.frameworkName || constants_1.DEFAULT_FRAMEWORK_NAME,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/core",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -25,8 +25,8 @@
25
25
  "access": "public"
26
26
  },
27
27
  "dependencies": {
28
- "@umijs/bundler-utils": "4.0.0-beta.1",
29
- "@umijs/utils": "4.0.0-beta.1"
28
+ "@umijs/bundler-utils": "4.0.0-beta.2",
29
+ "@umijs/utils": "4.0.0-beta.2"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@hapi/joi": "17.1.1",