@qingtian/qtcli 1.0.0

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.
Files changed (45) hide show
  1. package/README.md +193 -0
  2. package/dist/bin/cli.js +11 -0
  3. package/dist/package.json +209 -0
  4. package/dist/qt.conf.js +101 -0
  5. package/dist/src/App.js +10 -0
  6. package/dist/src/commands/build.js +74 -0
  7. package/dist/src/commands/dev.js +68 -0
  8. package/dist/src/commands/index.js +83 -0
  9. package/dist/src/commands/init.js +128 -0
  10. package/dist/src/cores/context.js +123 -0
  11. package/dist/src/index.js +29 -0
  12. package/dist/src/processEnv/index.js +23 -0
  13. package/dist/src/processEnv/injector.js +39 -0
  14. package/dist/src/processEnv/loader.js +201 -0
  15. package/dist/src/processEnv/manager.js +122 -0
  16. package/dist/src/processEnv/types.js +2 -0
  17. package/dist/src/proxys/build/index.js +225 -0
  18. package/dist/src/proxys/dev/index.js +284 -0
  19. package/dist/src/proxys/init/index.js +90 -0
  20. package/dist/src/servers/server.js +46 -0
  21. package/dist/src/types/baseInterface.js +46 -0
  22. package/dist/src/types/contextModel.js +21 -0
  23. package/dist/src/utils/childProcess.js +134 -0
  24. package/dist/src/utils/env.js +56 -0
  25. package/dist/src/utils/fileUtils.js +121 -0
  26. package/dist/src/utils/format.js +23 -0
  27. package/dist/src/utils/formatOutput.js +242 -0
  28. package/dist/src/utils/logger.js +70 -0
  29. package/dist/src/utils/printer.js +148 -0
  30. package/dist/src/utils/sleep.js +11 -0
  31. package/dist/src/utils/url.js +42 -0
  32. package/dist/src/webpack/npm/es.js +127 -0
  33. package/dist/src/webpack/npm/index.js +100 -0
  34. package/dist/src/webpack/npm/lib.js +128 -0
  35. package/dist/src/webpack/npm/webpack.base.js +221 -0
  36. package/dist/src/webpack/npm/webpack.prod.js +76 -0
  37. package/dist/src/webpack/plugins/DonePlugin.js +14 -0
  38. package/dist/src/webpack/plugins/FileListPlugin.js +28 -0
  39. package/dist/src/webpack/plugins/InjectScriptPlugin.js +33 -0
  40. package/dist/src/webpack/web/index.js +130 -0
  41. package/dist/src/webpack/web/webpack.analy.js +10 -0
  42. package/dist/src/webpack/web/webpack.base.js +419 -0
  43. package/dist/src/webpack/web/webpack.dev.js +38 -0
  44. package/dist/src/webpack/web/webpack.prod.js +82 -0
  45. package/package.json +209 -0
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __generator = (this && this.__generator) || function (thisArg, body) {
35
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
+ function verb(n) { return function (v) { return step([n, v]); }; }
38
+ function step(op) {
39
+ if (f) throw new TypeError("Generator is already executing.");
40
+ while (_) try {
41
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
+ if (y = 0, t) op = [op[0] & 2, t.value];
43
+ switch (op[0]) {
44
+ case 0: case 1: t = op; break;
45
+ case 4: _.label++; return { value: op[1], done: false };
46
+ case 5: _.label++; y = op[1]; op = [0]; continue;
47
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
+ default:
49
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
+ if (t[2]) _.ops.pop();
54
+ _.trys.pop(); continue;
55
+ }
56
+ op = body.call(thisArg, _);
57
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
+ }
60
+ };
61
+ var __importDefault = (this && this.__importDefault) || function (mod) {
62
+ return (mod && mod.__esModule) ? mod : { "default": mod };
63
+ };
64
+ Object.defineProperty(exports, "__esModule", { value: true });
65
+ var commander_1 = require("commander");
66
+ var pkg = __importStar(require("../../package.json"));
67
+ var build_1 = __importDefault(require("./build"));
68
+ var dev_1 = __importDefault(require("./dev"));
69
+ var init_1 = __importDefault(require("./init"));
70
+ exports.default = (function () { return __awaiter(void 0, void 0, void 0, function () {
71
+ return __generator(this, function (_a) {
72
+ commander_1.program
73
+ .name(pkg.name)
74
+ .usage("\u5DE5\u7A0B\u5E95\u5EA7\u4ECB\u7ECD \n\n \u5F53\u524D\u7248\u672C\uFF08".concat(pkg.version, "\uFF09\n\n \u547D\u4EE4\u7528\u6CD5\uFF08qtcli [command] [--options]\uFF09\n\n \u5F53\u524D\u76EE\u5F55\uFF08").concat(process.cwd(), "\uFF09\n "))
75
+ .version(pkg.version);
76
+ commander_1.program.option('-D, --debug', '启用调试模式');
77
+ commander_1.program.addCommand((0, init_1.default)());
78
+ commander_1.program.addCommand((0, dev_1.default)());
79
+ commander_1.program.addCommand((0, build_1.default)());
80
+ commander_1.program.parse(process.argv);
81
+ return [2 /*return*/];
82
+ });
83
+ }); });
@@ -0,0 +1,128 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
4
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
5
+ return new (P || (P = Promise))(function (resolve, reject) {
6
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
7
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
8
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
9
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
10
+ });
11
+ };
12
+ var __generator = (this && this.__generator) || function (thisArg, body) {
13
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
14
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
15
+ function verb(n) { return function (v) { return step([n, v]); }; }
16
+ function step(op) {
17
+ if (f) throw new TypeError("Generator is already executing.");
18
+ while (_) try {
19
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
20
+ if (y = 0, t) op = [op[0] & 2, t.value];
21
+ switch (op[0]) {
22
+ case 0: case 1: t = op; break;
23
+ case 4: _.label++; return { value: op[1], done: false };
24
+ case 5: _.label++; y = op[1]; op = [0]; continue;
25
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
26
+ default:
27
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
28
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
29
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
30
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
31
+ if (t[2]) _.ops.pop();
32
+ _.trys.pop(); continue;
33
+ }
34
+ op = body.call(thisArg, _);
35
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
36
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
37
+ }
38
+ };
39
+ var __importDefault = (this && this.__importDefault) || function (mod) {
40
+ return (mod && mod.__esModule) ? mod : { "default": mod };
41
+ };
42
+ Object.defineProperty(exports, "__esModule", { value: true });
43
+ var commander_1 = __importDefault(require("commander"));
44
+ var inquirer_1 = __importDefault(require("inquirer"));
45
+ var context_1 = require("../cores/context");
46
+ var contextModel_1 = require("../types/contextModel");
47
+ var logger_1 = require("../utils/logger");
48
+ exports.default = (function () {
49
+ var heat = new commander_1.default.Command('init');
50
+ heat
51
+ .description('初始化工程目录结构')
52
+ .option('-n, --name <type>', '项目名字')
53
+ .option('-t, --template <type>', '模板名字')
54
+ .action(function (options, cmd) { return __awaiter(void 0, void 0, void 0, function () {
55
+ var promptList, c, defaultTarName, promptList, c, cm;
56
+ return __generator(this, function (_a) {
57
+ switch (_a.label) {
58
+ case 0:
59
+ if (!!options.template) return [3 /*break*/, 2];
60
+ promptList = [
61
+ {
62
+ type: 'list',
63
+ message: '请选择模板',
64
+ name: 'template',
65
+ default: '1',
66
+ choices: [
67
+ {
68
+ key: '1',
69
+ name: 'react 「一个标准的React工程模板」',
70
+ value: 'react',
71
+ },
72
+ {
73
+ key: '2',
74
+ name: 'Commponent 「一个标准的创建组件的工程模板」',
75
+ value: 'Commponent',
76
+ },
77
+ {
78
+ key: '3',
79
+ name: 'Library 「一个标准的创建库工程模板」',
80
+ value: 'Library',
81
+ },
82
+ ],
83
+ filter: function (val) {
84
+ return val.toLowerCase();
85
+ },
86
+ },
87
+ ];
88
+ (0, logger_1.logStart)();
89
+ return [4 /*yield*/, inquirer_1.default.prompt(promptList).then(function (a) {
90
+ return a.template;
91
+ })];
92
+ case 1:
93
+ c = _a.sent();
94
+ options.template = c;
95
+ _a.label = 2;
96
+ case 2:
97
+ if (!!options.name) return [3 /*break*/, 4];
98
+ defaultTarName = "".concat(options.template, "_").concat(Math.floor(Math.random() * 100000000));
99
+ promptList = [
100
+ {
101
+ type: 'input',
102
+ message: '请输入项目的名称',
103
+ name: 'name',
104
+ default: defaultTarName,
105
+ filter: function (val) {
106
+ return val.toLowerCase();
107
+ },
108
+ },
109
+ ];
110
+ return [4 /*yield*/, inquirer_1.default.prompt(promptList).then(function (a) {
111
+ return a.name;
112
+ })];
113
+ case 3:
114
+ c = _a.sent();
115
+ options.name = c;
116
+ _a.label = 4;
117
+ case 4:
118
+ cm = new contextModel_1.ContextModel();
119
+ cm.cmd = cmd.name();
120
+ cm.proxy = cmd.name();
121
+ cm.argsCmd = options;
122
+ new context_1.Context(cm).run();
123
+ return [2 /*return*/];
124
+ }
125
+ });
126
+ }); });
127
+ return heat;
128
+ });
@@ -0,0 +1,123 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ exports.Context = void 0;
58
+ var colors_1 = __importDefault(require("colors"));
59
+ var events_1 = require("events");
60
+ var fs_1 = __importDefault(require("fs"));
61
+ var path_1 = __importDefault(require("path"));
62
+ var formatOutput_1 = require("../utils/formatOutput");
63
+ var printer_1 = require("../utils/printer");
64
+ var Context = /** @class */ (function (_super) {
65
+ __extends(Context, _super);
66
+ function Context(opts) {
67
+ var _this = _super.call(this) || this;
68
+ _this.opts = opts;
69
+ _this.opts.rootDir = process.cwd();
70
+ _this.opts.argsProcess = process.argv;
71
+ _this.opts.config = fs_1.default.existsSync(path_1.default.resolve(__dirname, './solution.cnf.js'))
72
+ ? require(path_1.default.resolve(__dirname, './solution.cnf.js'))
73
+ : null;
74
+ return _this;
75
+ }
76
+ Context.prototype.exec = function () {
77
+ return __awaiter(this, void 0, void 0, function () {
78
+ var ptime, proxy;
79
+ return __generator(this, function (_a) {
80
+ ptime = new Date().getTime();
81
+ console.log((0, formatOutput_1.formatNode)({
82
+ type: 'process3',
83
+ title: '执行命令',
84
+ content: "qtcli ".concat(process.argv.slice(2).join(' ')),
85
+ }));
86
+ proxy = require(path_1.default.resolve(__dirname, "../proxys/".concat(this.opts.proxy))).default;
87
+ if (!proxy) {
88
+ printer_1.printer.error("\u547D\u4EE4 \u300C".concat(this.opts.cmd, "\u300D\u4E0D\u5B58\u5728"));
89
+ process.exit(1);
90
+ }
91
+ proxy(this.opts).then(function () {
92
+ console.log((0, formatOutput_1.formatNode)({
93
+ type: 'complete',
94
+ title: "\u6267\u884C\u300Cqtcli ".concat(process.argv.slice(2).join(' '), "\u300D\u6210\u529F ").concat(colors_1.default.green("".concat(((new Date().getTime() - ptime) / 1000).toFixed(2), "s"))),
95
+ status: 'success',
96
+ }));
97
+ }, function () {
98
+ console.log((0, formatOutput_1.formatNode)({
99
+ type: 'complete',
100
+ title: "\u6267\u884C\u300Cqtcli ".concat(process.argv.slice(2).join(' '), "\u300D\u5931\u8D25 ").concat(colors_1.default.red("".concat(((new Date().getTime() - ptime) / 1000).toFixed(2), "s"))),
101
+ status: 'error',
102
+ }));
103
+ process.exit(1);
104
+ });
105
+ return [2 /*return*/];
106
+ });
107
+ });
108
+ };
109
+ Context.prototype.run = function () {
110
+ return __awaiter(this, void 0, void 0, function () {
111
+ return __generator(this, function (_a) {
112
+ switch (_a.label) {
113
+ case 0: return [4 /*yield*/, this.exec()];
114
+ case 1:
115
+ _a.sent();
116
+ return [2 /*return*/];
117
+ }
118
+ });
119
+ });
120
+ };
121
+ return Context;
122
+ }(events_1.EventEmitter));
123
+ exports.Context = Context;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var jsx_runtime_1 = require("react/jsx-runtime");
7
+ var react_1 = __importDefault(require("react"));
8
+ var client_1 = require("react-dom/client");
9
+ var App_1 = __importDefault(require("./App"));
10
+ require("./CompanyType1.scss");
11
+ // 创建根节点
12
+ var container = document.getElementById('root');
13
+ var root = (0, client_1.createRoot)(container);
14
+ // 渲染函数
15
+ var render = function () {
16
+ root.render((0, jsx_runtime_1.jsx)(react_1.default.StrictMode, { children: (0, jsx_runtime_1.jsx)(App_1.default, {}) }));
17
+ };
18
+ // 初始渲染
19
+ render();
20
+ // 热更新处理
21
+ if (module.hot) {
22
+ module.hot.accept('./CompanyType1.scss', function () {
23
+ console.log('Styles updated');
24
+ });
25
+ module.hot.accept('./App', function () {
26
+ console.log('App updated');
27
+ requestAnimationFrame(render);
28
+ });
29
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.processEnv = void 0;
18
+ var manager_1 = require("./manager");
19
+ // 创建环境变量管理器实例
20
+ var manager = manager_1.ProcessEnvManager.getInstance();
21
+ exports.processEnv = manager;
22
+ // 导出类型
23
+ __exportStar(require("./types"), exports);
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ProcessEnvInjector = void 0;
4
+ /* eslint-disable @typescript-eslint/member-ordering */
5
+ var manager_1 = require("./manager");
6
+ var ProcessEnvInjector = /** @class */ (function () {
7
+ function ProcessEnvInjector() {
8
+ this.manager = manager_1.ProcessEnvManager.getInstance();
9
+ }
10
+ ProcessEnvInjector.getInstance = function () {
11
+ if (!ProcessEnvInjector.instance) {
12
+ ProcessEnvInjector.instance = new ProcessEnvInjector();
13
+ }
14
+ return ProcessEnvInjector.instance;
15
+ };
16
+ /**
17
+ * 获取 DefinePlugin 配置
18
+ */
19
+ ProcessEnvInjector.prototype.getDefinePluginConfig = function () {
20
+ try {
21
+ var clientEnv = this.manager.getClientEnvironment();
22
+ var config = {
23
+ 'process.env': JSON.stringify(clientEnv.raw),
24
+ };
25
+ return config;
26
+ }
27
+ catch (error) {
28
+ var defaultConfig = {
29
+ 'process.env': JSON.stringify({
30
+ NODE_ENV: process.env.NODE_ENV || 'development',
31
+ PUBLIC_URL: process.env.PUBLIC_URL || '',
32
+ }),
33
+ };
34
+ return defaultConfig;
35
+ }
36
+ };
37
+ return ProcessEnvInjector;
38
+ }());
39
+ exports.ProcessEnvInjector = ProcessEnvInjector;
@@ -0,0 +1,201 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.ProcessEnvLoader = void 0;
18
+ var dotenv_1 = __importDefault(require("dotenv"));
19
+ var dotenv_expand_1 = __importDefault(require("dotenv-expand"));
20
+ var fs_1 = __importDefault(require("fs"));
21
+ var path_1 = __importDefault(require("path"));
22
+ var ProcessEnvLoader = /** @class */ (function () {
23
+ function ProcessEnvLoader() {
24
+ this.envSources = [];
25
+ this.mergedEnv = {};
26
+ this.initialized = false;
27
+ this.qtConfig = null;
28
+ // 初始化时添加系统环境变量作为第一个配置源
29
+ this.envSources.push(__assign({}, process.env));
30
+ }
31
+ ProcessEnvLoader.getInstance = function () {
32
+ if (!ProcessEnvLoader.instance) {
33
+ ProcessEnvLoader.instance = new ProcessEnvLoader();
34
+ }
35
+ return ProcessEnvLoader.instance;
36
+ };
37
+ /**
38
+ * 加载环境变量文件
39
+ */
40
+ ProcessEnvLoader.prototype.loadEnvFiles = function (env, rootDir) {
41
+ var _this = this;
42
+ var _a, _b, _c;
43
+ try {
44
+ // 加载 qtcli.config.js
45
+ var qtConfigPath = path_1.default.join(rootDir, 'qt.conf.js');
46
+ if (fs_1.default.existsSync(qtConfigPath)) {
47
+ this.qtConfig = require(qtConfigPath);
48
+ // 将 qtConfig 中的环境变量添加到 process.env
49
+ if ((_c = (_b = (_a = this.qtConfig) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.env) === null || _c === void 0 ? void 0 : _c[env]) {
50
+ Object.assign(process.env, this.qtConfig.config.env[env]);
51
+ }
52
+ }
53
+ // 根据环境确定要加载的文件
54
+ var envFiles = [];
55
+ if (env === 'development') {
56
+ envFiles = ['.env.development.local', '.env.local', '.env.development', '.env'];
57
+ }
58
+ else if (env === 'production') {
59
+ envFiles = ['.env.production.local', '.env.local', '.env.production', '.env'];
60
+ }
61
+ else if (env === 'test') {
62
+ envFiles = ['.env.test.local', '.env.test', '.env'];
63
+ }
64
+ // 确保 NODE_ENV 被正确设置
65
+ process.env.NODE_ENV = env;
66
+ envFiles.forEach(function (file) {
67
+ var filePath = path_1.default.resolve(rootDir, file);
68
+ if (fs_1.default.existsSync(filePath)) {
69
+ console.log("\n\u52A0\u8F7D\u6587\u4EF6: ".concat(file));
70
+ var envConfig = dotenv_1.default.config({
71
+ path: filePath,
72
+ });
73
+ if (envConfig.error) {
74
+ console.warn("\u8B66\u544A: \u52A0\u8F7D ".concat(file, " \u5931\u8D25:"), envConfig.error);
75
+ return;
76
+ }
77
+ // 使用 dotenv-expand 支持变量展开
78
+ (0, dotenv_expand_1.default)(envConfig);
79
+ // 将新的环境变量添加到配置源中
80
+ _this.envSources.push(__assign({}, process.env));
81
+ }
82
+ else {
83
+ console.log("\u6587\u4EF6\u4E0D\u5B58\u5728: ".concat(file));
84
+ }
85
+ });
86
+ // 确保基本环境变量存在
87
+ if (!process.env.NODE_ENV) {
88
+ process.env.NODE_ENV = env;
89
+ }
90
+ if (!process.env.PUBLIC_URL) {
91
+ process.env.PUBLIC_URL = '';
92
+ }
93
+ this.initialized = true;
94
+ }
95
+ catch (error) {
96
+ console.error('加载环境变量文件时出错:', error);
97
+ this.initialized = false;
98
+ }
99
+ };
100
+ /**
101
+ * 合并环境变量
102
+ */
103
+ ProcessEnvLoader.prototype.mergeEnvs = function () {
104
+ var _a, _b;
105
+ try {
106
+ // 按照优先级顺序合并配置
107
+ this.mergedEnv = this.envSources.reduce(function (acc, source) {
108
+ return __assign(__assign({}, acc), source);
109
+ }, {});
110
+ // 合并 qtcli.config.js 中的环境变量
111
+ if ((_b = (_a = this.qtConfig) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.env) {
112
+ var envConfig = this.qtConfig.config.env[process.env.NODE_ENV || 'development'];
113
+ if (envConfig) {
114
+ // 将环境变量添加到 process.env
115
+ Object.entries(envConfig).forEach(function (_a) {
116
+ var key = _a[0], value = _a[1];
117
+ process.env[key] = String(value);
118
+ });
119
+ // 将环境变量添加到 mergedEnv
120
+ this.mergedEnv = __assign(__assign({}, this.mergedEnv), envConfig);
121
+ }
122
+ }
123
+ }
124
+ catch (error) {
125
+ this.mergedEnv = {};
126
+ }
127
+ };
128
+ /**
129
+ * 获取合并后的环境变量
130
+ */
131
+ ProcessEnvLoader.prototype.getMergedEnv = function () {
132
+ if (!this.initialized) {
133
+ return {};
134
+ }
135
+ return this.mergedEnv;
136
+ };
137
+ /**
138
+ * 获取 qtcli.config.js 配置
139
+ */
140
+ ProcessEnvLoader.prototype.getQtConfig = function () {
141
+ return this.qtConfig;
142
+ };
143
+ /**
144
+ * 获取客户端环境变量
145
+ */
146
+ ProcessEnvLoader.prototype.getClientEnvironment = function (publicUrl) {
147
+ var _this = this;
148
+ if (!this.initialized) {
149
+ console.warn('环境变量未初始化');
150
+ var defaultEnv = {
151
+ raw: {
152
+ NODE_ENV: process.env.NODE_ENV || 'development',
153
+ PUBLIC_URL: publicUrl,
154
+ },
155
+ stringified: {
156
+ 'process.env': {
157
+ NODE_ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
158
+ PUBLIC_URL: JSON.stringify(publicUrl),
159
+ },
160
+ },
161
+ };
162
+ return defaultEnv;
163
+ }
164
+ var raw = {
165
+ NODE_ENV: this.mergedEnv.NODE_ENV || 'development',
166
+ PUBLIC_URL: publicUrl,
167
+ WDS_SOCKET_HOST: this.mergedEnv.WDS_SOCKET_HOST,
168
+ WDS_SOCKET_PATH: this.mergedEnv.WDS_SOCKET_PATH,
169
+ WDS_SOCKET_PORT: this.mergedEnv.WDS_SOCKET_PORT,
170
+ FAST_REFRESH: this.mergedEnv.FAST_REFRESH !== 'false' ? 'true' : 'false',
171
+ };
172
+ // 添加 REACT_APP_ 前缀的环境变量
173
+ Object.keys(this.mergedEnv)
174
+ .filter(function (key) { return /^REACT_APP_/i.test(key); })
175
+ .forEach(function (key) {
176
+ raw[key] = _this.mergedEnv[key];
177
+ });
178
+ // 字符串化所有值,用于 webpack DefinePlugin
179
+ var stringified = {
180
+ 'process.env': Object.keys(raw).reduce(function (env, key) {
181
+ env[key] = JSON.stringify(raw[key]);
182
+ return env;
183
+ },
184
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
185
+ {}),
186
+ };
187
+ var clientEnv = { raw: raw, stringified: stringified };
188
+ return clientEnv;
189
+ };
190
+ /**
191
+ * 重置环境变量
192
+ */
193
+ ProcessEnvLoader.prototype.reset = function () {
194
+ this.envSources = [__assign({}, process.env)];
195
+ this.mergedEnv = {};
196
+ this.initialized = false;
197
+ this.qtConfig = null;
198
+ };
199
+ return ProcessEnvLoader;
200
+ }());
201
+ exports.ProcessEnvLoader = ProcessEnvLoader;