@infly/libs 2.0.43 → 2.0.44
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/README.md +92 -67
- package/adapters/vue2/build/webpack5/inline-runtime-plugin.js +35 -0
- package/adapters/vue2/build/webpack5/remove-legacy-assets-plugin.js +84 -0
- package/adapters/vue2/build/webpack5/webpack.base.js +437 -0
- package/adapters/vue2/module/Permission.js +204 -0
- package/adapters/vue2/postinstall.js +46 -0
- package/adapters/vue2/project-preview.js +148 -0
- package/adapters/vue2/script-config.js +26 -0
- package/adapters/vue2/store/index.js +1 -0
- package/adapters/vue2/store/modules/user.js +268 -0
- package/bin/cli.js +71 -71
- package/build/build-dist/index.js +863 -863
- package/build/build-dist/script-management.js +5 -5
- package/build/docker/compose-command.js +74 -74
- package/build/docker/compose-release.js +91 -91
- package/build/webpack5/inline-runtime-plugin.js +1 -35
- package/build/webpack5/remove-legacy-assets-plugin.js +1 -84
- package/build/webpack5/webpack.base.js +1 -435
- package/compat/init.js +9 -0
- package/index.js +8 -20
- package/module/Permission.js +1 -204
- package/module/REST.js +31 -19
- package/module/TokenService.js +9 -0
- package/module/Uts.js +460 -7252
- package/module/cjs/request-url-rules.cjs +6 -0
- package/module/cjs/rest-error-rules.cjs +33 -0
- package/package.json +121 -92
- package/script/build/deps.js +1 -1
- package/script/build/webhook.js +3 -2
- package/script/git-automation/git-clone.js +3 -4
- package/script/git-automation/index.js +2 -2
- package/script/index.js +1 -26
- package/script/webhook/webhook.js +7 -2
- package/store/index.js +1 -0
- package/store/modules/user.js +1 -268
- package/tools/auto-export.js +1 -2
- package/tools/file-export.js +6 -4
- package/tools/project-command.js +194 -194
- package/tools/project-preview.js +1 -148
- package/tools/select-option.js +60 -60
- package/.prettierrc +0 -5
- package/build/build-dist/script-management.test.js +0 -16
- package/build/docker/compose-command.test.js +0 -148
- package/build/docker/compose-release.test.js +0 -101
- package/build/docker/docker-build-push.test.js +0 -124
- package/build/webpack5/webpack.base.test.js +0 -59
- package/lib/index.js +0 -6
- package/lib/server/connect.js +0 -3011
- package/lib/server/serve-static.js +0 -4848
- package/script/pts/cloud-scenes.mjs +0 -65
- package/script/pts/cloud.js +0 -151
- package/script/pts/generate-cloud-params.mjs +0 -210
- package/script/pts/generate-cloud-params.test.mjs +0 -67
- package/tools/project-command.test.js +0 -267
- package/tools/select-option.test.js +0 -52
- package/webpack.config.js +0 -38
package/tools/auto-export.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 自动导出目录下所有文件
|
|
3
3
|
* @param {Function} requireContext - require.context 返回的函数
|
|
4
|
-
* @param {RegExp} [filePattern=/\.vue$/] - 文件匹配正则
|
|
5
4
|
* @returns {Object} 导出对象,key 为文件名(不含扩展名),value 为模块内容
|
|
6
5
|
*
|
|
7
6
|
* @example
|
|
@@ -15,7 +14,7 @@
|
|
|
15
14
|
* // 或者直接一行
|
|
16
15
|
* Object.assign(module.exports, autoExportFiles(require.context('./', false, /\.vue$/)));
|
|
17
16
|
*/
|
|
18
|
-
function autoExportFiles(requireContext
|
|
17
|
+
function autoExportFiles(requireContext) {
|
|
19
18
|
if (!requireContext || typeof requireContext !== 'function') {
|
|
20
19
|
console.error('autoExportFiles: requireContext 必须是 require.context 返回的函数');
|
|
21
20
|
return {};
|
package/tools/file-export.js
CHANGED
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
* @param {Object} [config.MessageBox={}] - Element UI MessageBox 组件实例
|
|
12
12
|
* @param {Object} [config.Loading={}] - Element UI Loading 组件实例
|
|
13
13
|
*/
|
|
14
|
+
const isFn = (v) => typeof v === "function";
|
|
15
|
+
|
|
14
16
|
async function exportFile(config) {
|
|
15
17
|
// 1. 参数解构与默认值,并进行基本校验
|
|
16
18
|
const {
|
|
@@ -36,14 +38,14 @@ async function exportFile(config) {
|
|
|
36
38
|
|
|
37
39
|
// 辅助函数:显示消息
|
|
38
40
|
const showMessage = (type, msg) => {
|
|
39
|
-
if (Message &&
|
|
41
|
+
if (Message && isFn(Message[type])) {
|
|
40
42
|
Message[type](msg);
|
|
41
43
|
}
|
|
42
44
|
};
|
|
43
45
|
|
|
44
46
|
// 辅助函数:显示加载
|
|
45
47
|
const showLoading = () => {
|
|
46
|
-
if (Loading &&
|
|
48
|
+
if (Loading && isFn(Loading.service)) {
|
|
47
49
|
loadingInstance = Loading.service({
|
|
48
50
|
text: "文件正在下载中,请等候...",
|
|
49
51
|
spinner: "el-icon-loading",
|
|
@@ -54,7 +56,7 @@ async function exportFile(config) {
|
|
|
54
56
|
|
|
55
57
|
// 辅助函数:隐藏加载
|
|
56
58
|
const hideLoading = () => {
|
|
57
|
-
if (loadingInstance &&
|
|
59
|
+
if (loadingInstance && isFn(loadingInstance.close)) {
|
|
58
60
|
loadingInstance.close();
|
|
59
61
|
}
|
|
60
62
|
};
|
|
@@ -78,7 +80,7 @@ async function exportFile(config) {
|
|
|
78
80
|
|
|
79
81
|
try {
|
|
80
82
|
// 2. 确认框
|
|
81
|
-
if (MessageBox &&
|
|
83
|
+
if (MessageBox && isFn(MessageBox.confirm) && !hideConfirm) {
|
|
82
84
|
await MessageBox.confirm(confirmConfig?.message, "提示", {
|
|
83
85
|
confirmButtonText: "确定",
|
|
84
86
|
cancelButtonText: "取消",
|
package/tools/project-command.js
CHANGED
|
@@ -1,194 +1,194 @@
|
|
|
1
|
-
const { execFileSync } = require("node:child_process");
|
|
2
|
-
const fs = require("node:fs");
|
|
3
|
-
const path = require("node:path");
|
|
4
|
-
|
|
5
|
-
const {
|
|
6
|
-
EXIT_SELECTION,
|
|
7
|
-
selectOption: defaultSelectOption,
|
|
8
|
-
} = require("./select-option");
|
|
9
|
-
|
|
10
|
-
function loadConfig(cwd = process.cwd()) {
|
|
11
|
-
const packagePath = path.resolve(cwd, "package.json");
|
|
12
|
-
if (!fs.existsSync(packagePath)) {
|
|
13
|
-
throw new Error(`Project package not found: ${packagePath}.`);
|
|
14
|
-
}
|
|
15
|
-
delete require.cache[require.resolve(packagePath)];
|
|
16
|
-
const inflyConfig = require(packagePath).infly;
|
|
17
|
-
if (!inflyConfig?.dev && !inflyConfig?.build) {
|
|
18
|
-
throw new Error(`Project config is missing in ${packagePath}. Add infly.dev or infly.build.`);
|
|
19
|
-
}
|
|
20
|
-
return {
|
|
21
|
-
config: {
|
|
22
|
-
targets: inflyConfig.targets,
|
|
23
|
-
dev: inflyConfig.dev,
|
|
24
|
-
build: inflyConfig.build,
|
|
25
|
-
},
|
|
26
|
-
configDir: path.dirname(packagePath),
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function normalizeTargets(targets) {
|
|
31
|
-
if (Array.isArray(targets)) {
|
|
32
|
-
return Object.fromEntries(targets.map((target) => [target, {}]));
|
|
33
|
-
}
|
|
34
|
-
if (targets && typeof targets === "object") return targets;
|
|
35
|
-
throw new Error("Project targets must be a non-empty array or object.");
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function normalizeEnvironments(environments) {
|
|
39
|
-
if (Array.isArray(environments)) {
|
|
40
|
-
return Object.fromEntries(environments.map((entry) => {
|
|
41
|
-
const separatorIndex = entry.indexOf(":");
|
|
42
|
-
const environment = separatorIndex < 0 ? entry : entry.slice(0, separatorIndex);
|
|
43
|
-
const mode = separatorIndex < 0 ? entry : entry.slice(separatorIndex + 1);
|
|
44
|
-
if (!environment || !mode) {
|
|
45
|
-
throw new Error(`Invalid build environment mapping: ${entry}`);
|
|
46
|
-
}
|
|
47
|
-
return [environment, { mode, args: ["--mode", mode] }];
|
|
48
|
-
}));
|
|
49
|
-
}
|
|
50
|
-
if (environments && typeof environments === "object") return environments;
|
|
51
|
-
throw new Error("Build environments must be an array or object.");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function resolveBuildPlatforms(targets, buildConfig, targetName) {
|
|
55
|
-
const normalizedTargets = normalizeTargets(targets);
|
|
56
|
-
if (normalizedTargets[targetName]) return [targetName];
|
|
57
|
-
|
|
58
|
-
const preset = buildConfig.presets?.[targetName];
|
|
59
|
-
if (!preset) throw new Error(`Unknown build target: ${targetName}`);
|
|
60
|
-
|
|
61
|
-
return preset.targets.map((name) => {
|
|
62
|
-
if (!normalizedTargets[name]) {
|
|
63
|
-
throw new Error(`Unknown build target in preset ${targetName}: ${name}`);
|
|
64
|
-
}
|
|
65
|
-
return name;
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function commandParts(command) {
|
|
70
|
-
const parts = typeof command === "string"
|
|
71
|
-
? command.trim().split(/\s+/)
|
|
72
|
-
: command;
|
|
73
|
-
if (!Array.isArray(parts) || parts.length === 0 || !parts[0]) {
|
|
74
|
-
throw new Error("Command configuration must be a non-empty string or array.");
|
|
75
|
-
}
|
|
76
|
-
return { command: parts[0], args: parts.slice(1) };
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function defaultRunCommand(command, args, options) {
|
|
80
|
-
execFileSync(command, args, {
|
|
81
|
-
cwd: options.cwd,
|
|
82
|
-
env: options.env,
|
|
83
|
-
stdio: "inherit",
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function printDryRun(command, args, env) {
|
|
88
|
-
const platform = env.VUE_APP_PLATFORM ? `VUE_APP_PLATFORM=${env.VUE_APP_PLATFORM} ` : "";
|
|
89
|
-
console.log(`[dry-run] ${platform}${command} ${args.join(" ")}`.trim());
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function choose(selectOption, message, entries) {
|
|
93
|
-
return selectOption(
|
|
94
|
-
message,
|
|
95
|
-
entries.map(([value, item]) => ({ value, label: item.label || value })),
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
async function runProjectCommand(options = {}, dependencies = {}) {
|
|
100
|
-
if (typeof options.config === "string") {
|
|
101
|
-
throw new Error("--config is not supported for project commands; use package.json infly config.");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const configObject = options.configObject
|
|
105
|
-
|| (options.config && typeof options.config === "object" ? options.config : undefined);
|
|
106
|
-
const loaded = configObject
|
|
107
|
-
? { config: configObject, configDir: options.configDir }
|
|
108
|
-
: loadConfig(options.cwd || process.cwd());
|
|
109
|
-
const config = loaded.config;
|
|
110
|
-
const configDir = options.configDir || loaded.configDir || process.cwd();
|
|
111
|
-
if (!config) throw new Error("Project command config is required.");
|
|
112
|
-
const targets = normalizeTargets(config.targets);
|
|
113
|
-
|
|
114
|
-
const selectOption = dependencies.selectOption || defaultSelectOption;
|
|
115
|
-
const runCommand = dependencies.runCommand || defaultRunCommand;
|
|
116
|
-
const log = dependencies.log || console.log;
|
|
117
|
-
const exitIfSelected = (selection) => {
|
|
118
|
-
if (selection !== EXIT_SELECTION) return false;
|
|
119
|
-
log("已退出,未执行任何操作。");
|
|
120
|
-
return true;
|
|
121
|
-
};
|
|
122
|
-
const action = options.action || await selectOption("请选择操作", [
|
|
123
|
-
{ value: "dev", label: "启动开发环境" },
|
|
124
|
-
{ value: "build", label: "构建项目" },
|
|
125
|
-
]);
|
|
126
|
-
if (exitIfSelected(action)) return { cancelled: true };
|
|
127
|
-
|
|
128
|
-
if (action === "dev") {
|
|
129
|
-
const devConfig = config.dev;
|
|
130
|
-
if (!devConfig) throw new Error("Dev configuration is missing.");
|
|
131
|
-
const targetName = options.target || await choose(
|
|
132
|
-
selectOption,
|
|
133
|
-
"请选择目标平台",
|
|
134
|
-
Object.entries(targets),
|
|
135
|
-
);
|
|
136
|
-
if (exitIfSelected(targetName)) return { cancelled: true };
|
|
137
|
-
const target = targets[targetName];
|
|
138
|
-
if (!target) throw new Error(`Unknown dev target: ${targetName}`);
|
|
139
|
-
const parts = commandParts(devConfig.command);
|
|
140
|
-
const env = { ...process.env, VUE_APP_PLATFORM: targetName };
|
|
141
|
-
if (options.dryRun) printDryRun(parts.command, parts.args, env);
|
|
142
|
-
else runCommand(parts.command, parts.args, { cwd: configDir, env });
|
|
143
|
-
return { action, target: targetName };
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
if (action !== "build") throw new Error(`Unknown project action: ${action}`);
|
|
147
|
-
const buildConfig = config.build;
|
|
148
|
-
if (!buildConfig) throw new Error("Build configuration is missing.");
|
|
149
|
-
const environments = normalizeEnvironments(buildConfig.environments);
|
|
150
|
-
const environment = options.env || await choose(
|
|
151
|
-
selectOption,
|
|
152
|
-
"请选择构建环境",
|
|
153
|
-
Object.entries(environments),
|
|
154
|
-
);
|
|
155
|
-
if (exitIfSelected(environment)) return { cancelled: true };
|
|
156
|
-
const environmentConfig = environments[environment];
|
|
157
|
-
if (!environmentConfig) throw new Error(`Unknown build environment: ${environment}`);
|
|
158
|
-
const targetEntries = [
|
|
159
|
-
...Object.entries(targets),
|
|
160
|
-
...Object.entries(buildConfig.presets || {}),
|
|
161
|
-
];
|
|
162
|
-
const targetName = options.target || await choose(
|
|
163
|
-
selectOption,
|
|
164
|
-
"请选择目标平台",
|
|
165
|
-
targetEntries,
|
|
166
|
-
);
|
|
167
|
-
if (exitIfSelected(targetName)) return { cancelled: true };
|
|
168
|
-
const platforms = resolveBuildPlatforms(targets, buildConfig, targetName);
|
|
169
|
-
|
|
170
|
-
for (const platform of platforms) {
|
|
171
|
-
const env = { ...process.env, VUE_APP_PLATFORM: platform };
|
|
172
|
-
const lifecycleArgs = environmentConfig.mode ? ["--mode", environmentConfig.mode] : [];
|
|
173
|
-
const steps = [
|
|
174
|
-
{ command: buildConfig.before, extraArgs: lifecycleArgs },
|
|
175
|
-
{ command: buildConfig.command, extraArgs: environmentConfig.args || [] },
|
|
176
|
-
{ command: buildConfig.after, extraArgs: lifecycleArgs },
|
|
177
|
-
];
|
|
178
|
-
|
|
179
|
-
for (const step of steps) {
|
|
180
|
-
const parts = commandParts(step.command);
|
|
181
|
-
const args = [...parts.args, ...step.extraArgs];
|
|
182
|
-
if (options.dryRun) printDryRun(parts.command, args, env);
|
|
183
|
-
else runCommand(parts.command, args, { cwd: configDir, env });
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
return { action, environment, target: targetName, platforms };
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
module.exports = {
|
|
191
|
-
loadConfig,
|
|
192
|
-
resolveBuildPlatforms,
|
|
193
|
-
runProjectCommand,
|
|
194
|
-
};
|
|
1
|
+
const { execFileSync } = require("node:child_process");
|
|
2
|
+
const fs = require("node:fs");
|
|
3
|
+
const path = require("node:path");
|
|
4
|
+
|
|
5
|
+
const {
|
|
6
|
+
EXIT_SELECTION,
|
|
7
|
+
selectOption: defaultSelectOption,
|
|
8
|
+
} = require("./select-option");
|
|
9
|
+
|
|
10
|
+
function loadConfig(cwd = process.cwd()) {
|
|
11
|
+
const packagePath = path.resolve(cwd, "package.json");
|
|
12
|
+
if (!fs.existsSync(packagePath)) {
|
|
13
|
+
throw new Error(`Project package not found: ${packagePath}.`);
|
|
14
|
+
}
|
|
15
|
+
delete require.cache[require.resolve(packagePath)];
|
|
16
|
+
const inflyConfig = require(packagePath).infly;
|
|
17
|
+
if (!inflyConfig?.dev && !inflyConfig?.build) {
|
|
18
|
+
throw new Error(`Project config is missing in ${packagePath}. Add infly.dev or infly.build.`);
|
|
19
|
+
}
|
|
20
|
+
return {
|
|
21
|
+
config: {
|
|
22
|
+
targets: inflyConfig.targets,
|
|
23
|
+
dev: inflyConfig.dev,
|
|
24
|
+
build: inflyConfig.build,
|
|
25
|
+
},
|
|
26
|
+
configDir: path.dirname(packagePath),
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function normalizeTargets(targets) {
|
|
31
|
+
if (Array.isArray(targets)) {
|
|
32
|
+
return Object.fromEntries(targets.map((target) => [target, {}]));
|
|
33
|
+
}
|
|
34
|
+
if (targets && typeof targets === "object") return targets;
|
|
35
|
+
throw new Error("Project targets must be a non-empty array or object.");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function normalizeEnvironments(environments) {
|
|
39
|
+
if (Array.isArray(environments)) {
|
|
40
|
+
return Object.fromEntries(environments.map((entry) => {
|
|
41
|
+
const separatorIndex = entry.indexOf(":");
|
|
42
|
+
const environment = separatorIndex < 0 ? entry : entry.slice(0, separatorIndex);
|
|
43
|
+
const mode = separatorIndex < 0 ? entry : entry.slice(separatorIndex + 1);
|
|
44
|
+
if (!environment || !mode) {
|
|
45
|
+
throw new Error(`Invalid build environment mapping: ${entry}`);
|
|
46
|
+
}
|
|
47
|
+
return [environment, { mode, args: ["--mode", mode] }];
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
if (environments && typeof environments === "object") return environments;
|
|
51
|
+
throw new Error("Build environments must be an array or object.");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function resolveBuildPlatforms(targets, buildConfig, targetName) {
|
|
55
|
+
const normalizedTargets = normalizeTargets(targets);
|
|
56
|
+
if (normalizedTargets[targetName]) return [targetName];
|
|
57
|
+
|
|
58
|
+
const preset = buildConfig.presets?.[targetName];
|
|
59
|
+
if (!preset) throw new Error(`Unknown build target: ${targetName}`);
|
|
60
|
+
|
|
61
|
+
return preset.targets.map((name) => {
|
|
62
|
+
if (!normalizedTargets[name]) {
|
|
63
|
+
throw new Error(`Unknown build target in preset ${targetName}: ${name}`);
|
|
64
|
+
}
|
|
65
|
+
return name;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function commandParts(command) {
|
|
70
|
+
const parts = typeof command === "string"
|
|
71
|
+
? command.trim().split(/\s+/)
|
|
72
|
+
: command;
|
|
73
|
+
if (!Array.isArray(parts) || parts.length === 0 || !parts[0]) {
|
|
74
|
+
throw new Error("Command configuration must be a non-empty string or array.");
|
|
75
|
+
}
|
|
76
|
+
return { command: parts[0], args: parts.slice(1) };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function defaultRunCommand(command, args, options) {
|
|
80
|
+
execFileSync(command, args, {
|
|
81
|
+
cwd: options.cwd,
|
|
82
|
+
env: options.env,
|
|
83
|
+
stdio: "inherit",
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function printDryRun(command, args, env) {
|
|
88
|
+
const platform = env.VUE_APP_PLATFORM ? `VUE_APP_PLATFORM=${env.VUE_APP_PLATFORM} ` : "";
|
|
89
|
+
console.log(`[dry-run] ${platform}${command} ${args.join(" ")}`.trim());
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function choose(selectOption, message, entries) {
|
|
93
|
+
return selectOption(
|
|
94
|
+
message,
|
|
95
|
+
entries.map(([value, item]) => ({ value, label: item.label || value })),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function runProjectCommand(options = {}, dependencies = {}) {
|
|
100
|
+
if (typeof options.config === "string") {
|
|
101
|
+
throw new Error("--config is not supported for project commands; use package.json infly config.");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const configObject = options.configObject
|
|
105
|
+
|| (options.config && typeof options.config === "object" ? options.config : undefined);
|
|
106
|
+
const loaded = configObject
|
|
107
|
+
? { config: configObject, configDir: options.configDir }
|
|
108
|
+
: loadConfig(options.cwd || process.cwd());
|
|
109
|
+
const config = loaded.config;
|
|
110
|
+
const configDir = options.configDir || loaded.configDir || process.cwd();
|
|
111
|
+
if (!config) throw new Error("Project command config is required.");
|
|
112
|
+
const targets = normalizeTargets(config.targets);
|
|
113
|
+
|
|
114
|
+
const selectOption = dependencies.selectOption || defaultSelectOption;
|
|
115
|
+
const runCommand = dependencies.runCommand || defaultRunCommand;
|
|
116
|
+
const log = dependencies.log || console.log;
|
|
117
|
+
const exitIfSelected = (selection) => {
|
|
118
|
+
if (selection !== EXIT_SELECTION) return false;
|
|
119
|
+
log("已退出,未执行任何操作。");
|
|
120
|
+
return true;
|
|
121
|
+
};
|
|
122
|
+
const action = options.action || await selectOption("请选择操作", [
|
|
123
|
+
{ value: "dev", label: "启动开发环境" },
|
|
124
|
+
{ value: "build", label: "构建项目" },
|
|
125
|
+
]);
|
|
126
|
+
if (exitIfSelected(action)) return { cancelled: true };
|
|
127
|
+
|
|
128
|
+
if (action === "dev") {
|
|
129
|
+
const devConfig = config.dev;
|
|
130
|
+
if (!devConfig) throw new Error("Dev configuration is missing.");
|
|
131
|
+
const targetName = options.target || await choose(
|
|
132
|
+
selectOption,
|
|
133
|
+
"请选择目标平台",
|
|
134
|
+
Object.entries(targets),
|
|
135
|
+
);
|
|
136
|
+
if (exitIfSelected(targetName)) return { cancelled: true };
|
|
137
|
+
const target = targets[targetName];
|
|
138
|
+
if (!target) throw new Error(`Unknown dev target: ${targetName}`);
|
|
139
|
+
const parts = commandParts(devConfig.command);
|
|
140
|
+
const env = { ...process.env, VUE_APP_PLATFORM: targetName };
|
|
141
|
+
if (options.dryRun) printDryRun(parts.command, parts.args, env);
|
|
142
|
+
else runCommand(parts.command, parts.args, { cwd: configDir, env });
|
|
143
|
+
return { action, target: targetName };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (action !== "build") throw new Error(`Unknown project action: ${action}`);
|
|
147
|
+
const buildConfig = config.build;
|
|
148
|
+
if (!buildConfig) throw new Error("Build configuration is missing.");
|
|
149
|
+
const environments = normalizeEnvironments(buildConfig.environments);
|
|
150
|
+
const environment = options.env || await choose(
|
|
151
|
+
selectOption,
|
|
152
|
+
"请选择构建环境",
|
|
153
|
+
Object.entries(environments),
|
|
154
|
+
);
|
|
155
|
+
if (exitIfSelected(environment)) return { cancelled: true };
|
|
156
|
+
const environmentConfig = environments[environment];
|
|
157
|
+
if (!environmentConfig) throw new Error(`Unknown build environment: ${environment}`);
|
|
158
|
+
const targetEntries = [
|
|
159
|
+
...Object.entries(targets),
|
|
160
|
+
...Object.entries(buildConfig.presets || {}),
|
|
161
|
+
];
|
|
162
|
+
const targetName = options.target || await choose(
|
|
163
|
+
selectOption,
|
|
164
|
+
"请选择目标平台",
|
|
165
|
+
targetEntries,
|
|
166
|
+
);
|
|
167
|
+
if (exitIfSelected(targetName)) return { cancelled: true };
|
|
168
|
+
const platforms = resolveBuildPlatforms(targets, buildConfig, targetName);
|
|
169
|
+
|
|
170
|
+
for (const platform of platforms) {
|
|
171
|
+
const env = { ...process.env, VUE_APP_PLATFORM: platform };
|
|
172
|
+
const lifecycleArgs = environmentConfig.mode ? ["--mode", environmentConfig.mode] : [];
|
|
173
|
+
const steps = [
|
|
174
|
+
{ command: buildConfig.before, extraArgs: lifecycleArgs },
|
|
175
|
+
{ command: buildConfig.command, extraArgs: environmentConfig.args || [] },
|
|
176
|
+
{ command: buildConfig.after, extraArgs: lifecycleArgs },
|
|
177
|
+
];
|
|
178
|
+
|
|
179
|
+
for (const step of steps) {
|
|
180
|
+
const parts = commandParts(step.command);
|
|
181
|
+
const args = [...parts.args, ...step.extraArgs];
|
|
182
|
+
if (options.dryRun) printDryRun(parts.command, args, env);
|
|
183
|
+
else runCommand(parts.command, args, { cwd: configDir, env });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { action, environment, target: targetName, platforms };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
module.exports = {
|
|
191
|
+
loadConfig,
|
|
192
|
+
resolveBuildPlatforms,
|
|
193
|
+
runProjectCommand,
|
|
194
|
+
};
|
package/tools/project-preview.js
CHANGED
|
@@ -1,148 +1 @@
|
|
|
1
|
-
|
|
2
|
-
const path = require("path");
|
|
3
|
-
const net = require("net");
|
|
4
|
-
const { execSync } = require("child_process");
|
|
5
|
-
|
|
6
|
-
const { scriptWrite } = require("./file-process.js");
|
|
7
|
-
const { previewList } = require("../script");
|
|
8
|
-
|
|
9
|
-
const projectRoot = process.cwd().replace(/\\node_modules.*$/g, "");
|
|
10
|
-
const projectPkgPath = path.resolve(projectRoot, "package.json");
|
|
11
|
-
const projectVueConfigPath = path.resolve(projectRoot, "vue.config.js");
|
|
12
|
-
const projectConfig = fs.existsSync(projectVueConfigPath) ? require(projectVueConfigPath) : {};
|
|
13
|
-
const projectPackage = fs.existsSync(projectPkgPath) ? require(projectPkgPath) : {};
|
|
14
|
-
const rawArgv = process.argv.slice(2);
|
|
15
|
-
const args = rawArgv.join(" ");
|
|
16
|
-
|
|
17
|
-
function getBuildContext() {
|
|
18
|
-
try {
|
|
19
|
-
return JSON.parse(process.env.INFLY_BUILD_CONTEXT || "{}");
|
|
20
|
-
} catch {
|
|
21
|
-
return {};
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const buildContext = getBuildContext();
|
|
26
|
-
|
|
27
|
-
global.logColor = global.logColor || {
|
|
28
|
-
success: "\n\x1b[32m%s\x1b[0m",
|
|
29
|
-
error: "\n\x1b[31m%s\x1b[0m",
|
|
30
|
-
warning: "\n\x1b[33m%s\x1b[0m",
|
|
31
|
-
link: "\x1b[34m%s\x1b[0m",
|
|
32
|
-
info: "\n\x1b[36m%s\x1b[0m"
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
const DEFAULT_CONFIG = {
|
|
36
|
-
port: 9001,
|
|
37
|
-
publicPath: "/",
|
|
38
|
-
outputDir: "dist"
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
function scriptInit() {
|
|
42
|
-
if (fs.existsSync(projectVueConfigPath)) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
scriptWrite(projectPkgPath, previewList);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function isPortAvailable(port) {
|
|
49
|
-
return new Promise((resolve) => {
|
|
50
|
-
const server = net.createServer();
|
|
51
|
-
|
|
52
|
-
server.listen(port, () => {
|
|
53
|
-
server.once("close", () => {
|
|
54
|
-
resolve(true);
|
|
55
|
-
});
|
|
56
|
-
server.close();
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
server.on("error", () => {
|
|
60
|
-
resolve(false);
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
async function findAvailablePort(startPort, maxTries = 10) {
|
|
66
|
-
for (let i = 0; i < maxTries; i++) {
|
|
67
|
-
const port = startPort + i;
|
|
68
|
-
const available = await isPortAvailable(port);
|
|
69
|
-
|
|
70
|
-
if (available) {
|
|
71
|
-
if (i > 0) {
|
|
72
|
-
console.log(global.logColor.warning, `⚠️ 端口 ${startPort} 被占用,使用端口 ${port}`);
|
|
73
|
-
}
|
|
74
|
-
return port;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
throw new Error(`无法找到可用端口,已尝试从 ${startPort} 到 ${startPort + maxTries - 1}`);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
async function init(skipBuild = false) {
|
|
82
|
-
try {
|
|
83
|
-
const { connect } = require("../lib/server/connect");
|
|
84
|
-
const { serveStatic } = require("../lib/server/serve-static");
|
|
85
|
-
const app = connect();
|
|
86
|
-
const noBuild = skipBuild || rawArgv.includes("--no-build");
|
|
87
|
-
const { devServer } = projectConfig || {};
|
|
88
|
-
const { port: devServerPort } = devServer || {};
|
|
89
|
-
projectConfig.port = devServerPort || DEFAULT_CONFIG.port;
|
|
90
|
-
const { publicPath, outputDir, port } = {
|
|
91
|
-
...DEFAULT_CONFIG,
|
|
92
|
-
...projectConfig
|
|
93
|
-
};
|
|
94
|
-
const baseListenPort = port - 1000;
|
|
95
|
-
|
|
96
|
-
if (!noBuild) {
|
|
97
|
-
buildProject();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const listenPort = await findAvailablePort(baseListenPort);
|
|
101
|
-
|
|
102
|
-
app.use(
|
|
103
|
-
publicPath,
|
|
104
|
-
serveStatic(outputDir, {
|
|
105
|
-
index: ["index.html", "/"]
|
|
106
|
-
})
|
|
107
|
-
);
|
|
108
|
-
|
|
109
|
-
const server = app.listen(listenPort, function () {
|
|
110
|
-
const previewUrl = `http://localhost:${listenPort}${publicPath}`;
|
|
111
|
-
|
|
112
|
-
if (buildContext.batchPreview === true || process.env.INFLY_BATCH_PREVIEW === "1") {
|
|
113
|
-
console.log(global.logColor.link, `${projectPackage.name || path.basename(projectRoot)}: ${previewUrl}`);
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
console.log(global.logColor.success, `\x1b[42m\x1b[30m DONE \x1b[0m\x1b[32m 项目构建预览,CTRL + C结束:`);
|
|
118
|
-
console.log(global.logColor.link, previewUrl);
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
server.on("error", (error) => {
|
|
122
|
-
if (error.code === "EADDRINUSE") {
|
|
123
|
-
console.log(global.logColor.error, `❌ 端口 ${listenPort} 仍然被占用`);
|
|
124
|
-
} else {
|
|
125
|
-
console.log(global.logColor.error, `❌ 服务器启动失败:${error.message}`);
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
return server;
|
|
130
|
-
} catch (error) {
|
|
131
|
-
console.log(global.logColor.error, `❌ 预览项目失败:${error.message}`);
|
|
132
|
-
throw error;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function buildProject() {
|
|
137
|
-
try {
|
|
138
|
-
execSync(`vue-cli-service build ${args}`, { stdio: "inherit" });
|
|
139
|
-
} catch (err) {
|
|
140
|
-
console.error(global.logColor.error, "❌ 构建失败:", err.message);
|
|
141
|
-
process.exit(1);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
module.exports = {
|
|
146
|
-
init,
|
|
147
|
-
scriptInit
|
|
148
|
-
};
|
|
1
|
+
module.exports = require("../adapters/vue2/project-preview.js");
|