@infly/libs 2.0.42 → 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.
Files changed (56) hide show
  1. package/README.md +92 -67
  2. package/adapters/vue2/build/webpack5/inline-runtime-plugin.js +35 -0
  3. package/adapters/vue2/build/webpack5/remove-legacy-assets-plugin.js +84 -0
  4. package/adapters/vue2/build/webpack5/webpack.base.js +437 -0
  5. package/adapters/vue2/module/Permission.js +204 -0
  6. package/adapters/vue2/postinstall.js +46 -0
  7. package/adapters/vue2/project-preview.js +148 -0
  8. package/adapters/vue2/script-config.js +26 -0
  9. package/adapters/vue2/store/index.js +1 -0
  10. package/adapters/vue2/store/modules/user.js +268 -0
  11. package/bin/cli.js +71 -71
  12. package/build/build-dist/index.js +863 -863
  13. package/build/build-dist/script-management.js +5 -5
  14. package/build/docker/compose-command.js +74 -74
  15. package/build/docker/compose-release.js +91 -91
  16. package/build/webpack5/inline-runtime-plugin.js +1 -35
  17. package/build/webpack5/remove-legacy-assets-plugin.js +1 -84
  18. package/build/webpack5/webpack.base.js +1 -435
  19. package/compat/init.js +9 -0
  20. package/index.js +8 -20
  21. package/module/Permission.js +1 -204
  22. package/module/REST.js +31 -19
  23. package/module/TokenService.js +9 -0
  24. package/module/Uts.js +460 -7252
  25. package/module/cjs/request-url-rules.cjs +6 -0
  26. package/module/cjs/rest-error-rules.cjs +33 -0
  27. package/package.json +121 -92
  28. package/script/build/deps.js +1 -1
  29. package/script/build/webhook.js +3 -2
  30. package/script/git-automation/git-clone.js +3 -4
  31. package/script/git-automation/index.js +2 -2
  32. package/script/index.js +1 -26
  33. package/script/webhook/webhook.js +7 -2
  34. package/store/index.js +1 -0
  35. package/store/modules/user.js +1 -268
  36. package/tools/auto-export.js +1 -2
  37. package/tools/file-export.js +6 -4
  38. package/tools/project-command.js +194 -194
  39. package/tools/project-preview.js +1 -148
  40. package/tools/select-option.js +60 -60
  41. package/.prettierrc +0 -5
  42. package/build/build-dist/script-management.test.js +0 -16
  43. package/build/docker/compose-command.test.js +0 -148
  44. package/build/docker/compose-release.test.js +0 -101
  45. package/build/docker/docker-build-push.test.js +0 -124
  46. package/build/webpack5/webpack.base.test.js +0 -59
  47. package/lib/index.js +0 -6
  48. package/lib/server/connect.js +0 -3011
  49. package/lib/server/serve-static.js +0 -4848
  50. package/script/pts/cloud-scenes.mjs +0 -65
  51. package/script/pts/cloud.js +0 -151
  52. package/script/pts/generate-cloud-params.mjs +0 -210
  53. package/script/pts/generate-cloud-params.test.mjs +0 -67
  54. package/tools/project-command.test.js +0 -267
  55. package/tools/select-option.test.js +0 -52
  56. package/webpack.config.js +0 -38
@@ -0,0 +1,148 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+ const net = require("net");
4
+ const { execSync } = require("child_process");
5
+
6
+ const { scriptWrite } = require("../../tools/file-process.js");
7
+ const { previewList } = require("./script-config.js");
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("connect");
84
+ const serveStatic = require("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
+ };
@@ -0,0 +1,26 @@
1
+ module.exports = {
2
+ previewList: [
3
+ {
4
+ key: "preview",
5
+ value: "infly-libs --preview"
6
+ },
7
+ {
8
+ key: "preview:no-build",
9
+ value: "infly-libs --preview --no-build"
10
+ }
11
+ ],
12
+ buildList: [
13
+ {
14
+ key: "build:prod",
15
+ value: "infly-libs beforeBuild && vue-cli-service build && infly-libs afterBuild"
16
+ },
17
+ {
18
+ key: "build:stage",
19
+ value: "infly-libs beforeBuild && vue-cli-service build --mode staging && infly-libs afterBuild"
20
+ },
21
+ {
22
+ key: "build:test",
23
+ value: "infly-libs beforeBuild && infly-libs afterBuild"
24
+ }
25
+ ]
26
+ };
@@ -0,0 +1 @@
1
+ export { default as user } from "./modules/user";
@@ -0,0 +1,268 @@
1
+ import Uts from "../../../../module/Uts.js";
2
+ import TokenService from "../../../../module/TokenService.js";
3
+
4
+ const CONFIG = {
5
+ router: {},
6
+ settings: {},
7
+ login: () => Promise.resolve(),
8
+ getInfo: () => Promise.resolve(),
9
+ redirectRoute: {}
10
+ };
11
+
12
+ function init(config = {}) {
13
+ const { settings } = config || {};
14
+ if (Uts.notEmptyObject(settings)) {
15
+ TokenService.init(settings);
16
+ }
17
+
18
+ if (Uts.notEmptyObject(config)) {
19
+ Uts.mergeObjectWithReference(CONFIG, config);
20
+ }
21
+ }
22
+
23
+ const getDefaultState = () => {
24
+ return {
25
+ token: TokenService.getToken(),
26
+ name: "",
27
+ avatar: "",
28
+ user: {
29
+ id: "",
30
+ email: "",
31
+ header: "",
32
+ level_id: "",
33
+ level_name: "",
34
+ is_active_str: "",
35
+ mobile: "",
36
+ nickname: "",
37
+ role: "",
38
+ username: "",
39
+ company: "",
40
+ affiliation: {
41
+ province: "",
42
+ city: "",
43
+ district: ""
44
+ },
45
+ dimensionality_choices: [],
46
+ merchant_set: [],
47
+ merchantinfo: {},
48
+ map_key: ""
49
+ },
50
+ chatToken: TokenService.getChatToken() // 商家端聊天管理token
51
+ };
52
+ };
53
+
54
+ const userModule = {
55
+ namespaced: true,
56
+ state: () => getDefaultState(),
57
+ mutations: {
58
+ RESET_STATE: (state) => {
59
+ Uts.mergeObjectWithReference(state, getDefaultState());
60
+ },
61
+ SET_USER: (state, user) => (state.user = user),
62
+ SET_TOKEN: (state, token) => {
63
+ state.token = token;
64
+ TokenService.setToken(token);
65
+ },
66
+ SET_NAME: (state, name) => {
67
+ state.name = name;
68
+ },
69
+ SET_AVATAR: (state, avatar) => {
70
+ state.avatar = avatar;
71
+ },
72
+ SET_MAPKEY: (state, mapKey) => {
73
+ Uts.storage.set("mapKey", mapKey);
74
+ },
75
+ //邮惠权益商家端聊天管理的登录凭证
76
+ SET_CHAT_TOKEN: (state, token) => {
77
+ state.chatToken = token;
78
+ TokenService.setChatToken(token);
79
+ },
80
+ SET_PERMISSION_CODES: (state, codes) => {
81
+ state.permission_codes = codes || [];
82
+ Uts.storage.set(CONFIG.settings.permissionsCodeKey, codes);
83
+ },
84
+ UPDATE_ROUTES: (state, menus_permission_codes) => {
85
+ const constantRoutes = CONFIG?.router?.options?.routes || [];
86
+ updateRoutesPermission(constantRoutes, menus_permission_codes);
87
+ CONFIG?.router?.resetRouter?.(constantRoutes);
88
+ }
89
+ },
90
+ actions: {
91
+ login({ commit }, userInfo) {
92
+ const { mobile, sms_code } = userInfo;
93
+ return new Promise((resolve, reject) => {
94
+ CONFIG?.login?.({
95
+ mobile: mobile,
96
+ sms_code: sms_code
97
+ })
98
+ .then((response) => {
99
+ commit("SET_TOKEN", response.token);
100
+ resolve();
101
+ })
102
+ .catch((error) => {
103
+ reject(error);
104
+ });
105
+ });
106
+ },
107
+
108
+ getInfo({ commit, state }) {
109
+ return new Promise((resolve, reject) => {
110
+ CONFIG.getInfo(state.token)
111
+ .then((response) => {
112
+ if (!response) {
113
+ reject("验证失败,请重新登录。");
114
+ }
115
+
116
+ const {
117
+ name,
118
+ header,
119
+ map_key,
120
+ merchantinfo,
121
+ permission_codes = [],
122
+ menus,
123
+ menus_permission_codes = Uts.pluck(menus, "path") || []
124
+ } = response;
125
+ const { service_token } = merchantinfo || {};
126
+ const fullePermissionCodes = [...permission_codes, ...menus_permission_codes];
127
+
128
+ commit("SET_USER", response);
129
+ commit("SET_NAME", name);
130
+ commit("SET_AVATAR", header);
131
+ commit("SET_MAPKEY", map_key);
132
+
133
+ if (service_token) {
134
+ commit("SET_CHAT_TOKEN", service_token);
135
+ }
136
+
137
+ if (
138
+ CONFIG.settings.permissionsCodeKey &&
139
+ JSON.stringify(fullePermissionCodes) !==
140
+ JSON.stringify(Uts.storage.get(CONFIG.settings.permissionsCodeKey))
141
+ ) {
142
+ commit("SET_PERMISSION_CODES", fullePermissionCodes);
143
+ }
144
+ commit("UPDATE_ROUTES", menus_permission_codes);
145
+ resolve({ ...response, PMRedirectPath: CONFIG.redirectRoute.redirect });
146
+ })
147
+ .catch((error) => {
148
+ reject(error);
149
+ });
150
+ });
151
+ },
152
+
153
+ logout({ commit, state }, payload) {
154
+ return new Promise((resolve, reject) => {
155
+ TokenService.removeAllTokens();
156
+ commit("RESET_STATE");
157
+ commit("SET_MAPKEY", "");
158
+ commit("SET_CHAT_TOKEN", "");
159
+ commit("SET_PERMISSION_CODES", "");
160
+
161
+ if (payload?.redirectLogin) {
162
+ CONFIG?.router?.push?.("/login");
163
+ }
164
+
165
+ if (TokenService.getUrlToken()) {
166
+ TokenService.removeUrlToken();
167
+ }
168
+ resolve();
169
+ });
170
+ },
171
+
172
+ resetToken({ commit }) {
173
+ return new Promise((resolve) => {
174
+ commit("SET_TOKEN", "");
175
+ commit("SET_MAPKEY", "");
176
+ commit("RESET_STATE");
177
+ resolve();
178
+ });
179
+ },
180
+
181
+ refreshToken({ commit }, { token }) {
182
+ return new Promise((resolve, reject) => {
183
+ commit("SET_TOKEN", token);
184
+ resolve();
185
+ });
186
+ }
187
+ }
188
+ };
189
+
190
+ // 递归更新路由权限
191
+ // 递归应用路由权限(设置 hidden)
192
+ const applyRoutesPermission = (routes = []) => {
193
+ routes.forEach((route) => {
194
+ if (route.meta?.permission && !route.hidden) {
195
+ route.hidden = !Uts.getPM(route.meta.permission);
196
+ }
197
+ if (route.children?.length) {
198
+ applyRoutesPermission(route.children);
199
+ }
200
+ });
201
+ };
202
+
203
+ // 深度优先查找第一个可访问的叶子路由完整路径
204
+ const getFirstAccessiblePath = (routes = [], parentPath = "") => {
205
+ for (const route of routes) {
206
+ if (route.hidden || !route.path) continue;
207
+
208
+ // 绝对路径直接用,相对路径拼接父路径
209
+ const fullPath = route.path.startsWith("/")
210
+ ? route.path
211
+ : parentPath
212
+ ? `${parentPath}/${route.path}`
213
+ : route.path;
214
+
215
+ if (route.children?.length) {
216
+ const childPath = getFirstAccessiblePath(route.children, fullPath);
217
+ if (childPath) return childPath;
218
+ } else {
219
+ return fullPath;
220
+ }
221
+ }
222
+ return "";
223
+ };
224
+
225
+ // 更新路由权限并设置重定向
226
+ const findAccessibleRouteByPath = (routes = [], targetPath, parentPath = "") => {
227
+ if (!targetPath) return null;
228
+
229
+ for (const route of routes) {
230
+ if (route.hidden || !route.path) continue;
231
+
232
+ const fullPath = route.path.startsWith("/")
233
+ ? route.path
234
+ : parentPath
235
+ ? `${parentPath}/${route.path}`
236
+ : route.path;
237
+
238
+ if (fullPath === targetPath) {
239
+ return route;
240
+ }
241
+
242
+ if (route.children?.length) {
243
+ const matched = findAccessibleRouteByPath(route.children, targetPath, fullPath);
244
+ if (matched) return matched;
245
+ }
246
+ }
247
+
248
+ return null;
249
+ };
250
+
251
+ const updateRoutesPermission = (routes = []) => {
252
+ applyRoutesPermission(routes);
253
+
254
+ const redirectRoute = routes.find((r) => r.path === "");
255
+ if (redirectRoute) {
256
+ const availableRoutes = routes.filter((r) => r.path !== "");
257
+ const initRoutePath = CONFIG.settings?.projectConfig?.initRoutePath;
258
+ const initRoute = findAccessibleRouteByPath(availableRoutes, initRoutePath);
259
+ const firstPath = getFirstAccessiblePath(availableRoutes);
260
+ redirectRoute.redirect = initRoute ? initRoutePath : firstPath || "*";
261
+ CONFIG.redirectRoute = redirectRoute;
262
+ }
263
+ };
264
+
265
+ export default {
266
+ ...userModule,
267
+ init
268
+ };
package/bin/cli.js CHANGED
@@ -5,73 +5,73 @@ function loadBuildDist() {
5
5
  }
6
6
 
7
7
  function loadPreview() {
8
- return require("../tools/project-preview");
8
+ return require("../adapters/vue2/project-preview");
9
9
  }
10
10
 
11
- function parseArgs(argv) {
12
- const options = { env: "prod", local: false, dryRun: false };
13
- const valueOptions = {
14
- "--env": "env",
15
- "--cicd-url": "cicdUrl",
16
- "--project-dir": "projectDir",
17
- "--file": "file",
11
+ function parseArgs(argv) {
12
+ const options = { env: "prod", local: false, dryRun: false };
13
+ const valueOptions = {
14
+ "--env": "env",
15
+ "--cicd-url": "cicdUrl",
16
+ "--project-dir": "projectDir",
17
+ "--file": "file",
18
18
  "--bump": "bump",
19
19
  "--config": "config",
20
- "--target": "target",
21
- };
22
-
23
- let i = 0;
24
- while (i < argv.length) {
25
- const arg = argv[i];
26
- const equalsIndex = arg.indexOf("=");
27
- if (equalsIndex > 0) {
28
- const flag = arg.slice(0, equalsIndex);
29
- const key = valueOptions[flag];
30
- if (key) {
31
- const value = arg.slice(equalsIndex + 1);
32
- if (!value) throw new Error(`${flag} requires a value.`);
33
- options[key] = value;
34
- i++;
35
- continue;
36
- }
37
- }
38
-
39
- if (arg === "--local") {
40
- options.local = true;
41
- i++;
42
- continue;
43
- }
44
- if (arg === "--dry-run") {
45
- options.dryRun = true;
46
- i++;
47
- continue;
48
- }
49
- if (arg === "--push") {
50
- options.push = true;
51
- i++;
52
- continue;
53
- }
54
-
55
- const key = valueOptions[arg];
56
- if (key) {
57
- const value = argv[i + 1];
58
- if (!value || value.startsWith("--")) throw new Error(`${arg} requires a value.`);
59
- options[key] = value;
60
- i += 2;
61
- continue;
62
- }
63
-
64
- i++;
65
- }
66
- return options;
20
+ "--target": "target",
21
+ };
22
+
23
+ let i = 0;
24
+ while (i < argv.length) {
25
+ const arg = argv[i];
26
+ const equalsIndex = arg.indexOf("=");
27
+ if (equalsIndex > 0) {
28
+ const flag = arg.slice(0, equalsIndex);
29
+ const key = valueOptions[flag];
30
+ if (key) {
31
+ const value = arg.slice(equalsIndex + 1);
32
+ if (!value) throw new Error(`${flag} requires a value.`);
33
+ options[key] = value;
34
+ i++;
35
+ continue;
36
+ }
37
+ }
38
+
39
+ if (arg === "--local") {
40
+ options.local = true;
41
+ i++;
42
+ continue;
43
+ }
44
+ if (arg === "--dry-run") {
45
+ options.dryRun = true;
46
+ i++;
47
+ continue;
48
+ }
49
+ if (arg === "--push") {
50
+ options.push = true;
51
+ i++;
52
+ continue;
53
+ }
54
+
55
+ const key = valueOptions[arg];
56
+ if (key) {
57
+ const value = argv[i + 1];
58
+ if (!value || value.startsWith("--")) throw new Error(`${arg} requires a value.`);
59
+ options[key] = value;
60
+ i += 2;
61
+ continue;
62
+ }
63
+
64
+ i++;
65
+ }
66
+ return options;
67
+ }
68
+ function parseProjectArgs(argv) {
69
+ const options = parseArgs(argv);
70
+ const hasExplicitEnv = argv.some((arg) => arg === "--env" || arg.startsWith("--env="));
71
+ delete options.local;
72
+ if (!hasExplicitEnv) delete options.env;
73
+ return options;
67
74
  }
68
- function parseProjectArgs(argv) {
69
- const options = parseArgs(argv);
70
- const hasExplicitEnv = argv.some((arg) => arg === "--env" || arg.startsWith("--env="));
71
- delete options.local;
72
- if (!hasExplicitEnv) delete options.env;
73
- return options;
74
- }
75
75
  async function main(argv = process.argv.slice(2)) {
76
76
  const command = argv[0];
77
77
  const commandMap = {
@@ -89,15 +89,15 @@ async function main(argv = process.argv.slice(2)) {
89
89
  "build:docker": () => {
90
90
  const opts = parseArgs(argv.slice(1));
91
91
  return require("../build/docker/docker-build-push").dockerBuildPush(opts);
92
- },
93
- "docker:compose": () => {
94
- const opts = parseArgs(argv.slice(1));
95
- return require("../build/docker/compose-command").runComposeCommand(opts);
96
- },
97
- project: () => {
98
- const action = argv[1] && !argv[1].startsWith("--") ? argv[1] : undefined;
99
- const opts = parseProjectArgs(argv.slice(action ? 2 : 1));
100
- return require("../tools/project-command").runProjectCommand({ ...opts, action });
92
+ },
93
+ "docker:compose": () => {
94
+ const opts = parseArgs(argv.slice(1));
95
+ return require("../build/docker/compose-command").runComposeCommand(opts);
96
+ },
97
+ project: () => {
98
+ const action = argv[1] && !argv[1].startsWith("--") ? argv[1] : undefined;
99
+ const opts = parseProjectArgs(argv.slice(action ? 2 : 1));
100
+ return require("../tools/project-command").runProjectCommand({ ...opts, action });
101
101
  },
102
102
  };
103
103