@zhipu/zp-cli 0.0.0 → 0.0.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.
@@ -0,0 +1,105 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { createJiti } from "jiti";
4
+ //#region src/utils/config.ts
5
+ const CONFIG_FILE_PREFIX = "zpc.config";
6
+ const SCHEMA_FILE_NAME = `${CONFIG_FILE_PREFIX}.schema.json`;
7
+ const CONFIG_FORMATS = [
8
+ "ts",
9
+ "mts",
10
+ "cts",
11
+ "js",
12
+ "mjs",
13
+ "cjs",
14
+ "json"
15
+ ];
16
+ function defineConfig(config) {
17
+ return config;
18
+ }
19
+ function isConfigFormat(value) {
20
+ return CONFIG_FORMATS.includes(value);
21
+ }
22
+ function configFileName(format) {
23
+ return `${CONFIG_FILE_PREFIX}.${format}`;
24
+ }
25
+ function templateFileName(format) {
26
+ return `${configFileName(format)}.template`;
27
+ }
28
+ const jiti = createJiti(import.meta.url);
29
+ function listConfigFiles(dir) {
30
+ return CONFIG_FORMATS.map((format) => path.join(dir, configFileName(format))).filter((file) => fs.existsSync(file));
31
+ }
32
+ function findConfigFile(dir) {
33
+ return listConfigFiles(dir)[0];
34
+ }
35
+ async function loadConfig(dir) {
36
+ const file = findConfigFile(dir);
37
+ if (!file) throw new Error(`[config] 在 ${dir} 找不到 ${CONFIG_FILE_PREFIX}.{${CONFIG_FORMATS.join(",")}},请先运行 zpc init`);
38
+ let loaded;
39
+ try {
40
+ loaded = await readConfigFile(file);
41
+ } catch (err) {
42
+ const message = err instanceof Error ? err.message : String(err);
43
+ throw new Error(`[config] 无法加载 ${file}: ${message}`);
44
+ }
45
+ return {
46
+ file,
47
+ config: validateConfig(loaded, file)
48
+ };
49
+ }
50
+ async function readConfigFile(file) {
51
+ if (file.endsWith(".json")) return JSON.parse(fs.readFileSync(file, "utf8"));
52
+ return await jiti.import(file, { default: true });
53
+ }
54
+ function validateConfig(config, source) {
55
+ if (!isPlainObject(config)) throw new Error(`[config] ${source} 必须导出一个对象`);
56
+ if (!isSafeName(config.projectName)) throw new Error(`[config] ${source} 缺少合法 projectName`);
57
+ const api = optionalAppList(config.api, "api", source);
58
+ const web = optionalAppList(config.web, "web", source);
59
+ if (api.length === 0 && web.length === 0) throw new Error(`[config] ${source} 中没有 api / web 项目`);
60
+ return {
61
+ projectName: config.projectName,
62
+ ...api.length ? { api } : {},
63
+ ...web.length ? { web } : {}
64
+ };
65
+ }
66
+ function optionalAppList(value, kind, source) {
67
+ if (value == null) return [];
68
+ if (!Array.isArray(value)) throw new Error(`[config] ${source} 中 ${kind} 必须是数组`);
69
+ return value.map((item, index) => parseAppConfig(item, `${source} ${kind}[${index}]`));
70
+ }
71
+ function parseAppConfig(item, label) {
72
+ if (!isPlainObject(item)) throw new Error(`[config] ${label} 必须是对象`);
73
+ if (typeof item.path !== "string" || item.path.trim() === "") throw new Error(`[config] ${label} 缺少 path`);
74
+ if (!isSafeName(item.name)) throw new Error(`[config] ${label} 缺少合法 name`);
75
+ const parsed = {
76
+ path: item.path,
77
+ name: item.name
78
+ };
79
+ if (item.proxy != null) {
80
+ if (typeof item.proxy !== "string") throw new Error(`[config] ${label} 的 proxy 必须是字符串`);
81
+ parsed.proxy = item.proxy;
82
+ }
83
+ if (item.buildEnv != null) parsed.buildEnv = parseEnvMap(item.buildEnv, `${label} buildEnv`);
84
+ if (item.env != null) parsed.env = parseEnvMap(item.env, `${label} env`);
85
+ return parsed;
86
+ }
87
+ function parseEnvMap(value, label) {
88
+ if (!isPlainObject(value)) throw new Error(`[config] ${label} 必须是对象`);
89
+ const out = {};
90
+ for (const [key, entry] of Object.entries(value)) {
91
+ if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) throw new Error(`[config] ${label} 含非法环境变量名: ${key}`);
92
+ if (typeof entry !== "string" && typeof entry !== "number" && typeof entry !== "boolean") throw new Error(`[config] ${label}.${key} 必须是 string / number / boolean`);
93
+ out[key] = entry;
94
+ }
95
+ return out;
96
+ }
97
+ function isPlainObject(value) {
98
+ return typeof value === "object" && value !== null && !Array.isArray(value);
99
+ }
100
+ function isSafeName(name) {
101
+ if (typeof name !== "string" || name === "" || name === "." || name === "..") return false;
102
+ return !name.includes("/") && !name.includes("\\") && !name.includes(String.fromCharCode(0));
103
+ }
104
+ //#endregion
105
+ export { findConfigFile as a, loadConfig as c, defineConfig as i, templateFileName as l, SCHEMA_FILE_NAME as n, isConfigFormat as o, configFileName as r, listConfigFiles as s, CONFIG_FORMATS as t };
package/dist/index.d.mts CHANGED
@@ -1,4 +1,24 @@
1
- //#region src/index.d.ts
2
- declare function fn(): string;
1
+ //#region src/utils/config.d.ts
2
+ declare const CONFIG_FORMATS: readonly ["ts", "mts", "cts", "js", "mjs", "cjs", "json"];
3
+ type ConfigFormat = (typeof CONFIG_FORMATS)[number];
4
+ type ZpcEnvMap = Record<string, string | number | boolean>;
5
+ interface ZpcAppConfig {
6
+ /** 相对项目根的源码目录 */
7
+ path: string;
8
+ /** 打包 / 部署目录名 */
9
+ name: string;
10
+ /** api 对外路径前缀;web 未写 buildEnv.VITE_API_PROXY 时也可作回退 */
11
+ proxy?: string;
12
+ /** 仅构建期注入 */
13
+ buildEnv?: ZpcEnvMap;
14
+ /** 仅运行时注入(pm2) */
15
+ env?: ZpcEnvMap;
16
+ }
17
+ interface ZpcConfig {
18
+ projectName: string;
19
+ api?: ZpcAppConfig[];
20
+ web?: ZpcAppConfig[];
21
+ }
22
+ declare function defineConfig(config: ZpcConfig): ZpcConfig;
3
23
  //#endregion
4
- export { fn };
24
+ export { type ConfigFormat, type ZpcAppConfig, type ZpcConfig, type ZpcEnvMap, defineConfig };
package/dist/index.mjs CHANGED
@@ -1,6 +1,2 @@
1
- //#region src/index.ts
2
- function fn() {
3
- return "Hello, tsdown!";
4
- }
5
- //#endregion
6
- export { fn };
1
+ import { i as defineConfig } from "./config-BZc9pg7J.mjs";
2
+ export { defineConfig };
package/package.json CHANGED
@@ -1,37 +1,92 @@
1
1
  {
2
2
  "name": "@zhipu/zp-cli",
3
- "version": "0.0.0",
3
+ "version": "0.0.2",
4
4
  "description": "内部单命令部署工具",
5
5
  "bin": {
6
6
  "zpc": "./dist/cli.mjs"
7
7
  },
8
8
  "files": [
9
- "dist"
9
+ "dist",
10
+ "src/templates"
10
11
  ],
11
12
  "type": "module",
12
13
  "exports": {
13
14
  ".": "./dist/index.mjs",
14
- "./cli": "./dist/cli.mjs",
15
15
  "./package.json": "./package.json"
16
16
  },
17
17
  "publishConfig": {
18
18
  "access": "public"
19
19
  },
20
+ "dependencies": {
21
+ "@commander-js/extra-typings": "^15.0.0",
22
+ "@vue-tui/components": "^0.3.0",
23
+ "@vue-tui/runtime": "^0.3.0",
24
+ "chalk": "^6.0.0",
25
+ "commander": "^15.0.0",
26
+ "jiti": "^2.7.0",
27
+ "strip-ansi": "^7.2.0",
28
+ "vue": "^3.5.41"
29
+ },
20
30
  "devDependencies": {
21
31
  "@types/node": "^26.1.1",
32
+ "@vitejs/plugin-vue": "^6.0.8",
33
+ "@vue-tui/testing": "^0.3.0",
22
34
  "bumpp": "^11.1.0",
23
35
  "oxfmt": "^0.64.0",
24
36
  "typescript": "^7.0.2",
37
+ "unplugin-vue": "^7.2.0",
25
38
  "vite": "npm:@voidzero-dev/vite-plus-core@0.2.6",
26
- "vite-plus": "0.2.6"
39
+ "vite-plus": "0.2.6",
40
+ "node": "runtime:25.9.0"
27
41
  },
28
42
  "devEngines": {
29
43
  "packageManager": {
30
44
  "name": "pnpm",
31
45
  "version": "11.22.0",
32
46
  "onFail": "download"
47
+ },
48
+ "runtime": {
49
+ "name": "node",
50
+ "version": "25.9.0",
51
+ "onFail": "download"
33
52
  }
34
53
  },
54
+ "inlinedDependencies": {
55
+ "@alcalzone/ansi-tokenize": "0.3.0",
56
+ "@babel/parser": "7.29.8",
57
+ "@commander-js/extra-typings": "15.0.0",
58
+ "@vue-tui/components": "0.3.0",
59
+ "@vue-tui/runtime": "0.3.0",
60
+ "@vue/compiler-core": "3.5.41",
61
+ "@vue/compiler-dom": "3.5.41",
62
+ "@vue/reactivity": "3.5.41",
63
+ "@vue/runtime-core": "3.5.41",
64
+ "@vue/runtime-dom": "3.5.41",
65
+ "@vue/shared": "3.5.41",
66
+ "ansi-escapes": "7.3.0",
67
+ "ansi-styles": "6.2.3",
68
+ "chalk": [
69
+ "5.6.2",
70
+ "6.0.0"
71
+ ],
72
+ "cli-boxes": "3.0.0",
73
+ "cli-truncate": "6.1.1",
74
+ "commander": "15.0.0",
75
+ "entities": "7.0.1",
76
+ "environment": "1.1.0",
77
+ "estree-walker": "2.0.2",
78
+ "get-east-asian-width": "1.6.0",
79
+ "is-fullwidth-code-point": "5.1.0",
80
+ "jiti": "2.7.0",
81
+ "patch-console": "2.0.0",
82
+ "signal-exit": "4.1.0",
83
+ "slice-ansi": "9.0.0",
84
+ "source-map-js": "1.2.1",
85
+ "string-width": "8.2.2",
86
+ "vue": "3.5.41",
87
+ "wrap-ansi": "10.0.1",
88
+ "yoga-layout": "3.2.1"
89
+ },
35
90
  "scripts": {
36
91
  "build": "vp pack",
37
92
  "dev": "vp pack --watch",
@@ -0,0 +1,16 @@
1
+ # generated by zpc deploy
2
+ # owned-by: {{ownerId}}
3
+ location {{loc}} {
4
+ proxy_pass http://127.0.0.1:{{port}}/;
5
+ proxy_http_version 1.1;
6
+ proxy_set_header Host $host;
7
+ proxy_set_header X-Real-IP $remote_addr;
8
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9
+ proxy_set_header X-Forwarded-Proto $scheme;
10
+ proxy_set_header Upgrade $http_upgrade;
11
+ proxy_set_header Connection "upgrade";
12
+ }
13
+
14
+ location = {{exact}} {
15
+ return 301 {{loc}};
16
+ }
@@ -0,0 +1,20 @@
1
+ /** @type {import("@zhipu/zp-cli").ZpcConfig} */
2
+ module.exports = {
3
+ projectName: "my-project",
4
+ api: [
5
+ {
6
+ path: "api",
7
+ name: "api",
8
+ proxy: "/api",
9
+ env: {
10
+ PORT: 3000,
11
+ },
12
+ },
13
+ ],
14
+ web: [
15
+ {
16
+ path: "web",
17
+ name: "web",
18
+ },
19
+ ],
20
+ };
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from "@zhipu/zp-cli";
2
+
3
+ export default defineConfig({
4
+ projectName: "my-project",
5
+ api: [
6
+ {
7
+ path: "api",
8
+ name: "api",
9
+ proxy: "/api",
10
+ env: {
11
+ PORT: 3000,
12
+ },
13
+ },
14
+ ],
15
+ web: [
16
+ {
17
+ path: "web",
18
+ name: "web",
19
+ },
20
+ ],
21
+ });
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from "@zhipu/zp-cli";
2
+
3
+ export default defineConfig({
4
+ projectName: "my-project",
5
+ api: [
6
+ {
7
+ path: "api",
8
+ name: "api",
9
+ proxy: "/api",
10
+ env: {
11
+ PORT: 3000,
12
+ },
13
+ },
14
+ ],
15
+ web: [
16
+ {
17
+ path: "web",
18
+ name: "web",
19
+ },
20
+ ],
21
+ });
@@ -0,0 +1,20 @@
1
+ {
2
+ "$schema": "./zpc.config.schema.json",
3
+ "projectName": "my-project",
4
+ "api": [
5
+ {
6
+ "path": "api",
7
+ "name": "api",
8
+ "proxy": "/api",
9
+ "env": {
10
+ "PORT": 3000
11
+ }
12
+ }
13
+ ],
14
+ "web": [
15
+ {
16
+ "path": "web",
17
+ "name": "web"
18
+ }
19
+ ]
20
+ }
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from "@zhipu/zp-cli";
2
+
3
+ export default defineConfig({
4
+ projectName: "my-project",
5
+ api: [
6
+ {
7
+ path: "api",
8
+ name: "api",
9
+ proxy: "/api",
10
+ env: {
11
+ PORT: 3000,
12
+ },
13
+ },
14
+ ],
15
+ web: [
16
+ {
17
+ path: "web",
18
+ name: "web",
19
+ },
20
+ ],
21
+ });
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from "@zhipu/zp-cli";
2
+
3
+ export default defineConfig({
4
+ projectName: "my-project",
5
+ api: [
6
+ {
7
+ path: "api",
8
+ name: "api",
9
+ proxy: "/api",
10
+ env: {
11
+ PORT: 3000,
12
+ },
13
+ },
14
+ ],
15
+ web: [
16
+ {
17
+ path: "web",
18
+ name: "web",
19
+ },
20
+ ],
21
+ });
@@ -0,0 +1,67 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "title": "zpc.config",
4
+ "type": "object",
5
+ "additionalProperties": false,
6
+ "required": ["projectName"],
7
+ "anyOf": [{ "required": ["api"] }, { "required": ["web"] }],
8
+ "properties": {
9
+ "$schema": {
10
+ "type": "string"
11
+ },
12
+ "projectName": {
13
+ "type": "string",
14
+ "minLength": 1,
15
+ "description": "项目名,部署路径为 <root>/<projectName>/<name>"
16
+ },
17
+ "api": {
18
+ "$ref": "#/definitions/appList"
19
+ },
20
+ "web": {
21
+ "$ref": "#/definitions/appList"
22
+ }
23
+ },
24
+ "definitions": {
25
+ "appList": {
26
+ "type": "array",
27
+ "items": {
28
+ "$ref": "#/definitions/app"
29
+ }
30
+ },
31
+ "app": {
32
+ "type": "object",
33
+ "additionalProperties": false,
34
+ "required": ["path", "name"],
35
+ "properties": {
36
+ "path": {
37
+ "type": "string",
38
+ "minLength": 1,
39
+ "description": "相对项目根的源码目录"
40
+ },
41
+ "name": {
42
+ "type": "string",
43
+ "minLength": 1,
44
+ "description": "打包 / 部署目录名"
45
+ },
46
+ "proxy": {
47
+ "type": "string",
48
+ "description": "api 对外路径前缀;web 未写 buildEnv.VITE_API_PROXY 时也可作回退"
49
+ },
50
+ "buildEnv": {
51
+ "$ref": "#/definitions/envMap",
52
+ "description": "仅构建期注入"
53
+ },
54
+ "env": {
55
+ "$ref": "#/definitions/envMap",
56
+ "description": "仅运行时注入(pm2)"
57
+ }
58
+ }
59
+ },
60
+ "envMap": {
61
+ "type": "object",
62
+ "additionalProperties": {
63
+ "type": ["string", "number", "boolean"]
64
+ }
65
+ }
66
+ }
67
+ }
@@ -0,0 +1,21 @@
1
+ import { defineConfig } from "@zhipu/zp-cli";
2
+
3
+ export default defineConfig({
4
+ projectName: "my-project",
5
+ api: [
6
+ {
7
+ path: "api",
8
+ name: "api",
9
+ proxy: "/api",
10
+ env: {
11
+ PORT: 3000,
12
+ },
13
+ },
14
+ ],
15
+ web: [
16
+ {
17
+ path: "web",
18
+ name: "web",
19
+ },
20
+ ],
21
+ });