@vtj/local 0.7.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.
- package/LICENSE +21 -0
- package/dist/controller.d.ts +5 -0
- package/dist/controller.mjs +74 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +1 -0
- package/dist/plugin.d.ts +11 -0
- package/dist/plugin.mjs +177 -0
- package/dist/service.d.ts +19 -0
- package/dist/service.mjs +198 -0
- package/dist/shared.d.ts +16 -0
- package/dist/shared.mjs +21 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 陈华春
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { fail } from "./shared.mjs";
|
|
2
|
+
import * as service from "./service.mjs";
|
|
3
|
+
const controller = {
|
|
4
|
+
notMatch: async (_req) => {
|
|
5
|
+
return fail("\u627E\u4E0D\u5230\u5904\u7406\u7A0B\u5E8F");
|
|
6
|
+
},
|
|
7
|
+
init: service.init,
|
|
8
|
+
saveProject: async (req) => {
|
|
9
|
+
const project = req.data;
|
|
10
|
+
return service.saveProject(project);
|
|
11
|
+
},
|
|
12
|
+
saveFile: async (req) => {
|
|
13
|
+
const file = req.data;
|
|
14
|
+
return service.saveFile(file);
|
|
15
|
+
},
|
|
16
|
+
getFile: async (req) => {
|
|
17
|
+
const id = req.data;
|
|
18
|
+
return service.getFile(id);
|
|
19
|
+
},
|
|
20
|
+
removeFile: async (req) => {
|
|
21
|
+
const id = req.data;
|
|
22
|
+
return service.removeFile(id);
|
|
23
|
+
},
|
|
24
|
+
getHistory: async (req) => {
|
|
25
|
+
const id = req.data;
|
|
26
|
+
return service.getHistory(id);
|
|
27
|
+
},
|
|
28
|
+
saveHistory: async (req) => {
|
|
29
|
+
const file = req.data;
|
|
30
|
+
return service.saveHistory(file);
|
|
31
|
+
},
|
|
32
|
+
removeHistory: async (req) => {
|
|
33
|
+
const id = req.data;
|
|
34
|
+
return service.removeHistory(id);
|
|
35
|
+
},
|
|
36
|
+
getHistoryItem: async (req) => {
|
|
37
|
+
const { fId, id } = req.data || {};
|
|
38
|
+
return service.getHistoryItem(fId, id);
|
|
39
|
+
},
|
|
40
|
+
saveHistoryItem: async (req) => {
|
|
41
|
+
const { fId, item } = req.data || {};
|
|
42
|
+
return service.saveHistoryItem(fId, item);
|
|
43
|
+
},
|
|
44
|
+
removeHistoryItem: async (req) => {
|
|
45
|
+
const { fId, ids = [] } = req.data || {};
|
|
46
|
+
return service.removeHistoryItem(fId, ids);
|
|
47
|
+
},
|
|
48
|
+
saveMaterials: async (req) => {
|
|
49
|
+
const { project, materials } = req.data || {};
|
|
50
|
+
return service.saveMaterials(project, materials);
|
|
51
|
+
},
|
|
52
|
+
publishFile: async (req) => {
|
|
53
|
+
const { project, file } = req.data || {};
|
|
54
|
+
return service.publishFile(project, file);
|
|
55
|
+
},
|
|
56
|
+
publish: async (req) => {
|
|
57
|
+
const project = req.data || {};
|
|
58
|
+
return service.publish(project);
|
|
59
|
+
},
|
|
60
|
+
getRaw: async (req) => {
|
|
61
|
+
const { project, dsl } = req.data || {};
|
|
62
|
+
return service.getRaw(project, dsl);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
export const router = async (req) => {
|
|
66
|
+
const body = req.body || {};
|
|
67
|
+
const handler = controller[body.type] || controller.notMatch;
|
|
68
|
+
try {
|
|
69
|
+
return await handler(body);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
await service.saveLogs(e);
|
|
72
|
+
return fail("\u5F02\u5E38\u9519\u8BEF", e);
|
|
73
|
+
}
|
|
74
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './plugin';
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./plugin.mjs";
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type Plugin } from 'vite';
|
|
2
|
+
export interface DevPluginOptions {
|
|
3
|
+
baseURL: string;
|
|
4
|
+
copy: boolean;
|
|
5
|
+
server: boolean;
|
|
6
|
+
link: boolean | string;
|
|
7
|
+
vtjDir: string;
|
|
8
|
+
packagesDir: string;
|
|
9
|
+
devMode: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function createDevPlugin(options?: Partial<DevPluginOptions>): Plugin<any>[];
|
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import {
|
|
2
|
+
copyPlugin,
|
|
3
|
+
staticPlugin
|
|
4
|
+
} from "@vtj/cli";
|
|
5
|
+
import { pathExistsSync } from "@vtj/node";
|
|
6
|
+
import { join } from "path";
|
|
7
|
+
import bodyParser from "body-parser";
|
|
8
|
+
import { router } from "./controller.mjs";
|
|
9
|
+
const setApis = (server, options) => {
|
|
10
|
+
server.middlewares.use(
|
|
11
|
+
bodyParser.json({ type: "application/json", limit: "50000kb" })
|
|
12
|
+
);
|
|
13
|
+
server.middlewares.use(async (req, res, next) => {
|
|
14
|
+
const reqUrl = req.url || "";
|
|
15
|
+
if (reqUrl.startsWith(options.baseURL)) {
|
|
16
|
+
const data = await router(req);
|
|
17
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
18
|
+
res.end(JSON.stringify(data));
|
|
19
|
+
} else {
|
|
20
|
+
next();
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
const apiServerPlugin = function(options) {
|
|
25
|
+
return {
|
|
26
|
+
name: "vtj-api-plugin",
|
|
27
|
+
apply: "serve",
|
|
28
|
+
configureServer(server) {
|
|
29
|
+
setApis(server, options);
|
|
30
|
+
},
|
|
31
|
+
configurePreviewServer(server) {
|
|
32
|
+
return () => {
|
|
33
|
+
setApis(server, options);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
const linkPlugin = function(options) {
|
|
39
|
+
let config;
|
|
40
|
+
return {
|
|
41
|
+
name: "vtj-link-plugin",
|
|
42
|
+
apply: "serve",
|
|
43
|
+
configResolved(resolvedConfig) {
|
|
44
|
+
config = resolvedConfig;
|
|
45
|
+
},
|
|
46
|
+
transformIndexHtml(html) {
|
|
47
|
+
if (html.includes("VTJ-LINK")) {
|
|
48
|
+
return html;
|
|
49
|
+
}
|
|
50
|
+
if (options.link) {
|
|
51
|
+
const link = typeof options.link === "string" ? options.link : "@vtj/pro/link.js";
|
|
52
|
+
const url = `${config.base}${link}`;
|
|
53
|
+
return html.replace(
|
|
54
|
+
/<\/body>/,
|
|
55
|
+
`<script src="${url}"><\/script></body>`
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
return html;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const aliasPlugin = function(options) {
|
|
63
|
+
return {
|
|
64
|
+
name: "vtj-alias-plugin",
|
|
65
|
+
config(config) {
|
|
66
|
+
const { root = process.cwd() } = config || {};
|
|
67
|
+
const vtjDir = join(root, options.vtjDir);
|
|
68
|
+
const packagesDir = join(root, options.packagesDir);
|
|
69
|
+
const devAlias = options.devMode && process.env.NODE_ENV === "development" ? {
|
|
70
|
+
"@vtj/ui/dist/style.css": join(
|
|
71
|
+
packagesDir,
|
|
72
|
+
"ui/src/style/index.scss"
|
|
73
|
+
),
|
|
74
|
+
"@vtj/icons/dist/style.css": join(
|
|
75
|
+
packagesDir,
|
|
76
|
+
"icons/src/style.scss"
|
|
77
|
+
),
|
|
78
|
+
"@vtj/designer/dist/style.css": join(
|
|
79
|
+
packagesDir,
|
|
80
|
+
"designer/src/style/index.scss"
|
|
81
|
+
),
|
|
82
|
+
"@vtj/base": join(packagesDir, "base/src"),
|
|
83
|
+
"@vtj/utils": join(packagesDir, "utils/src/index.ts"),
|
|
84
|
+
"@vtj/icons": join(packagesDir, "icons/src"),
|
|
85
|
+
"@vtj/ui": join(packagesDir, "ui/src"),
|
|
86
|
+
"@vtj/core": join(packagesDir, "core/src"),
|
|
87
|
+
"@vtj/designer": join(packagesDir, "designer/src"),
|
|
88
|
+
"@vtj/renderer": join(packagesDir, "renderer/src"),
|
|
89
|
+
"@vtj/coder": join(packagesDir, "coder/src")
|
|
90
|
+
} : {};
|
|
91
|
+
if (config.resolve) {
|
|
92
|
+
config.resolve.alias = Object.assign(config.resolve.alias || {}, {
|
|
93
|
+
$vtj: vtjDir,
|
|
94
|
+
...devAlias
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
config.resolve = {
|
|
98
|
+
alias: {
|
|
99
|
+
$vtj: vtjDir,
|
|
100
|
+
...devAlias
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
};
|
|
107
|
+
export function createDevPlugin(options = {}) {
|
|
108
|
+
const opts = {
|
|
109
|
+
baseURL: "/vtj/local/api",
|
|
110
|
+
copy: true,
|
|
111
|
+
server: true,
|
|
112
|
+
link: true,
|
|
113
|
+
vtjDir: ".vtj",
|
|
114
|
+
packagesDir: "../../packages",
|
|
115
|
+
devMode: false,
|
|
116
|
+
...options
|
|
117
|
+
};
|
|
118
|
+
const plugins = [aliasPlugin(opts)];
|
|
119
|
+
const proPath = "node_modules/@vtj/pro/dist";
|
|
120
|
+
const materialsPath1 = "node_modules/@vtj/materials/dist";
|
|
121
|
+
const materialsPath2 = "node_modules/@vtj/pro/" + materialsPath1;
|
|
122
|
+
if (opts.copy) {
|
|
123
|
+
const copyOptions = [];
|
|
124
|
+
if (pathExistsSync(materialsPath1)) {
|
|
125
|
+
copyOptions.push({
|
|
126
|
+
from: materialsPath1,
|
|
127
|
+
to: "@vtj/materials",
|
|
128
|
+
emptyDir: true
|
|
129
|
+
});
|
|
130
|
+
} else if (pathExistsSync(materialsPath2)) {
|
|
131
|
+
copyOptions.push({
|
|
132
|
+
from: materialsPath2,
|
|
133
|
+
to: "@vtj/materials",
|
|
134
|
+
emptyDir: true
|
|
135
|
+
});
|
|
136
|
+
} else {
|
|
137
|
+
console.warn(
|
|
138
|
+
"\n @vtj/materials is not installed, please install it first.\n"
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
if (copyOptions.length > 0) {
|
|
142
|
+
plugins.push(copyPlugin(copyOptions));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (opts.server) {
|
|
146
|
+
plugins.push(apiServerPlugin(opts));
|
|
147
|
+
const staticOptions = [];
|
|
148
|
+
if (pathExistsSync(proPath)) {
|
|
149
|
+
staticOptions.push({
|
|
150
|
+
path: "/@vtj/pro",
|
|
151
|
+
dir: proPath
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
if (pathExistsSync(materialsPath1)) {
|
|
155
|
+
staticOptions.push({
|
|
156
|
+
path: "/@vtj/materials",
|
|
157
|
+
dir: materialsPath1
|
|
158
|
+
});
|
|
159
|
+
} else if (pathExistsSync(materialsPath2)) {
|
|
160
|
+
staticOptions.push({
|
|
161
|
+
path: "/@vtj/materials",
|
|
162
|
+
dir: materialsPath2
|
|
163
|
+
});
|
|
164
|
+
} else {
|
|
165
|
+
console.warn(
|
|
166
|
+
"\n @vtj/materials is not installed, please install it first.\n"
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
if (staticOptions.length > 0) {
|
|
170
|
+
plugins.push(staticPlugin(staticOptions));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (!!opts.link) {
|
|
174
|
+
plugins.push(linkPlugin(opts));
|
|
175
|
+
}
|
|
176
|
+
return plugins;
|
|
177
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type ProjectSchema, type BlockSchema, type HistorySchema, type HistoryItem, type MaterialDescription, type PageFile, type BlockFile } from '@vtj/core';
|
|
2
|
+
import { type ApiRequest } from './shared';
|
|
3
|
+
export declare function notMatch(_req: ApiRequest): Promise<import("./shared").ApiResponse>;
|
|
4
|
+
export declare function saveLogs(e: any): Promise<void>;
|
|
5
|
+
export declare function init(): Promise<import("./shared").ApiResponse>;
|
|
6
|
+
export declare function saveProject(project: ProjectSchema): Promise<import("./shared").ApiResponse>;
|
|
7
|
+
export declare function saveFile(file: BlockSchema): Promise<import("./shared").ApiResponse>;
|
|
8
|
+
export declare function getFile(id: string): Promise<import("./shared").ApiResponse>;
|
|
9
|
+
export declare function removeFile(id: string): Promise<import("./shared").ApiResponse>;
|
|
10
|
+
export declare function getHistory(id: string): Promise<import("./shared").ApiResponse>;
|
|
11
|
+
export declare function saveHistory(file: HistorySchema): Promise<import("./shared").ApiResponse>;
|
|
12
|
+
export declare function removeHistory(id: string): Promise<import("./shared").ApiResponse>;
|
|
13
|
+
export declare function getHistoryItem(fId: string, id: string): Promise<import("./shared").ApiResponse>;
|
|
14
|
+
export declare function saveHistoryItem(fId: string, item: HistoryItem): Promise<import("./shared").ApiResponse>;
|
|
15
|
+
export declare function removeHistoryItem(fId: string, ids: string[]): Promise<import("./shared").ApiResponse>;
|
|
16
|
+
export declare function saveMaterials(project: ProjectSchema, materials: Record<string, MaterialDescription>): Promise<import("./shared").ApiResponse>;
|
|
17
|
+
export declare function publishFile(project: ProjectSchema, file: PageFile | BlockFile, componentMap?: Map<string, MaterialDescription>): Promise<import("./shared").ApiResponse>;
|
|
18
|
+
export declare function publish(project: ProjectSchema): Promise<import("./shared").ApiResponse>;
|
|
19
|
+
export declare function getRaw(project: ProjectSchema, dsl: BlockSchema): Promise<import("./shared").ApiResponse>;
|
package/dist/service.mjs
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ProjectModel
|
|
3
|
+
} from "@vtj/core";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
import {
|
|
6
|
+
readJsonSync,
|
|
7
|
+
pathExistsSync,
|
|
8
|
+
writeJsonSync,
|
|
9
|
+
ensureFileSync,
|
|
10
|
+
removeSync,
|
|
11
|
+
outputFileSync,
|
|
12
|
+
upperFirstCamelCase,
|
|
13
|
+
timestamp
|
|
14
|
+
} from "@vtj/node";
|
|
15
|
+
import { generator } from "@vtj/coder";
|
|
16
|
+
import { fail, success } from "./shared.mjs";
|
|
17
|
+
const root = resolve("./");
|
|
18
|
+
const vtjDir = resolve(".vtj");
|
|
19
|
+
const getProjectFilePath = (_name) => {
|
|
20
|
+
return resolve(vtjDir, "project.json");
|
|
21
|
+
};
|
|
22
|
+
const getMaterialsFilePath = (_name) => {
|
|
23
|
+
return resolve(vtjDir, "materials.json");
|
|
24
|
+
};
|
|
25
|
+
const getFilePath = (id) => {
|
|
26
|
+
return resolve(vtjDir, `files/${id}.json`);
|
|
27
|
+
};
|
|
28
|
+
const getHistoryFilePath = (id) => {
|
|
29
|
+
return resolve(vtjDir, `histories/${id}.json`);
|
|
30
|
+
};
|
|
31
|
+
const getHistoryItemFilePath = (fId, id) => {
|
|
32
|
+
return resolve(vtjDir, `histories/${fId}/${id}.json`);
|
|
33
|
+
};
|
|
34
|
+
const getHistoryItemDir = (fId) => {
|
|
35
|
+
return resolve(vtjDir, `histories/${fId}`);
|
|
36
|
+
};
|
|
37
|
+
const getRawFilePath = (id) => {
|
|
38
|
+
return resolve(vtjDir, `raw/${id}.vue`);
|
|
39
|
+
};
|
|
40
|
+
const getLogsDir = () => {
|
|
41
|
+
return resolve(vtjDir, `logs`);
|
|
42
|
+
};
|
|
43
|
+
export async function notMatch(_req) {
|
|
44
|
+
return fail("\u627E\u4E0D\u5230\u5904\u7406\u7A0B\u5E8F");
|
|
45
|
+
}
|
|
46
|
+
export async function saveLogs(e) {
|
|
47
|
+
const name = `error-${timestamp()}.json`;
|
|
48
|
+
outputFileSync(resolve(getLogsDir(), name), JSON.stringify(e), "utf-8");
|
|
49
|
+
}
|
|
50
|
+
export async function init() {
|
|
51
|
+
const pkg = readJsonSync(resolve(root, "package.json"));
|
|
52
|
+
const { name, description } = pkg || {};
|
|
53
|
+
const filePath = getProjectFilePath(name);
|
|
54
|
+
let schema = {};
|
|
55
|
+
if (pathExistsSync(filePath)) {
|
|
56
|
+
schema = readJsonSync(filePath);
|
|
57
|
+
}
|
|
58
|
+
const project = new ProjectModel({
|
|
59
|
+
...schema,
|
|
60
|
+
id: name,
|
|
61
|
+
name: description || upperFirstCamelCase(name),
|
|
62
|
+
description
|
|
63
|
+
});
|
|
64
|
+
const dsl = project.toDsl();
|
|
65
|
+
ensureFileSync(filePath);
|
|
66
|
+
writeJsonSync(filePath, dsl);
|
|
67
|
+
return success(dsl);
|
|
68
|
+
}
|
|
69
|
+
export async function saveProject(project) {
|
|
70
|
+
const filePath = getProjectFilePath(project.id);
|
|
71
|
+
if (pathExistsSync(filePath)) {
|
|
72
|
+
writeJsonSync(filePath, project);
|
|
73
|
+
return success(true);
|
|
74
|
+
} else {
|
|
75
|
+
return fail("\u9879\u76EE\u6587\u4EF6\u4E0D\u5B58\u5728");
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export async function saveFile(file) {
|
|
79
|
+
const filePath = getFilePath(file.id);
|
|
80
|
+
if (!pathExistsSync(filePath)) {
|
|
81
|
+
ensureFileSync(filePath);
|
|
82
|
+
}
|
|
83
|
+
writeJsonSync(filePath, file);
|
|
84
|
+
return success(true);
|
|
85
|
+
}
|
|
86
|
+
export async function getFile(id) {
|
|
87
|
+
const filePath = getFilePath(id);
|
|
88
|
+
if (pathExistsSync(filePath)) {
|
|
89
|
+
const json = readJsonSync(filePath);
|
|
90
|
+
return success(json);
|
|
91
|
+
} else {
|
|
92
|
+
return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export async function removeFile(id) {
|
|
96
|
+
const filePath = getFilePath(id);
|
|
97
|
+
if (pathExistsSync(filePath)) {
|
|
98
|
+
removeSync(filePath);
|
|
99
|
+
return success(true);
|
|
100
|
+
} else {
|
|
101
|
+
return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export async function getHistory(id) {
|
|
105
|
+
const filePath = getHistoryFilePath(id);
|
|
106
|
+
if (pathExistsSync(filePath)) {
|
|
107
|
+
const json = readJsonSync(filePath);
|
|
108
|
+
return success(json);
|
|
109
|
+
} else {
|
|
110
|
+
return success({});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
export async function saveHistory(file) {
|
|
114
|
+
const filePath = getHistoryFilePath(file.id);
|
|
115
|
+
if (!pathExistsSync(filePath)) {
|
|
116
|
+
ensureFileSync(filePath);
|
|
117
|
+
}
|
|
118
|
+
writeJsonSync(filePath, file);
|
|
119
|
+
return success(true);
|
|
120
|
+
}
|
|
121
|
+
export async function removeHistory(id) {
|
|
122
|
+
const filePath = getHistoryFilePath(id);
|
|
123
|
+
const dir = getHistoryItemDir(id);
|
|
124
|
+
if (pathExistsSync(filePath)) {
|
|
125
|
+
removeSync(filePath);
|
|
126
|
+
removeSync(dir);
|
|
127
|
+
return success(true);
|
|
128
|
+
} else {
|
|
129
|
+
return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export async function getHistoryItem(fId, id) {
|
|
133
|
+
const filePath = getHistoryItemFilePath(fId, id);
|
|
134
|
+
if (pathExistsSync(filePath)) {
|
|
135
|
+
const json = readJsonSync(filePath);
|
|
136
|
+
return success(json);
|
|
137
|
+
} else {
|
|
138
|
+
return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
export async function saveHistoryItem(fId, item) {
|
|
142
|
+
const filePath = getHistoryItemFilePath(fId, item.id);
|
|
143
|
+
if (!pathExistsSync(filePath)) {
|
|
144
|
+
ensureFileSync(filePath);
|
|
145
|
+
}
|
|
146
|
+
writeJsonSync(filePath, item);
|
|
147
|
+
return success(true);
|
|
148
|
+
}
|
|
149
|
+
export async function removeHistoryItem(fId, ids) {
|
|
150
|
+
ids.forEach((id) => {
|
|
151
|
+
const filePath = getHistoryItemFilePath(fId, id);
|
|
152
|
+
removeSync(filePath);
|
|
153
|
+
});
|
|
154
|
+
return success(true);
|
|
155
|
+
}
|
|
156
|
+
export async function saveMaterials(project, materials) {
|
|
157
|
+
const filePath = getMaterialsFilePath(project.id);
|
|
158
|
+
ensureFileSync(filePath);
|
|
159
|
+
writeJsonSync(filePath, materials);
|
|
160
|
+
return success(true);
|
|
161
|
+
}
|
|
162
|
+
export async function publishFile(project, file, componentMap) {
|
|
163
|
+
const materialsPath = getMaterialsFilePath(project.id);
|
|
164
|
+
const materials = await readJsonSync(materialsPath);
|
|
165
|
+
componentMap = componentMap || new Map(Object.entries(materials));
|
|
166
|
+
const filePath = getFilePath(file.id);
|
|
167
|
+
const dsl = readJsonSync(filePath);
|
|
168
|
+
const content = await generator(dsl, componentMap, project.dependencies);
|
|
169
|
+
const rawPath = getRawFilePath(file.id);
|
|
170
|
+
outputFileSync(rawPath, content, "utf-8");
|
|
171
|
+
return success(true);
|
|
172
|
+
}
|
|
173
|
+
export async function publish(project) {
|
|
174
|
+
const { pages = [], blocks = [] } = project;
|
|
175
|
+
const materialsPath = getMaterialsFilePath(project.id);
|
|
176
|
+
const materials = await readJsonSync(materialsPath);
|
|
177
|
+
const componentMap = new Map(
|
|
178
|
+
Object.entries(materials)
|
|
179
|
+
);
|
|
180
|
+
for (const block of blocks) {
|
|
181
|
+
await publishFile(project, block, componentMap);
|
|
182
|
+
}
|
|
183
|
+
for (const page of pages) {
|
|
184
|
+
if (!page.raw) {
|
|
185
|
+
await publishFile(project, page, componentMap);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
return success(true);
|
|
189
|
+
}
|
|
190
|
+
export async function getRaw(project, dsl) {
|
|
191
|
+
const materialsPath = getMaterialsFilePath(project.id);
|
|
192
|
+
const materials = await readJsonSync(materialsPath);
|
|
193
|
+
const componentMap = new Map(
|
|
194
|
+
Object.entries(materials)
|
|
195
|
+
);
|
|
196
|
+
const content = await generator(dsl, componentMap, project.dependencies);
|
|
197
|
+
return success(content);
|
|
198
|
+
}
|
package/dist/shared.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ApiRequest {
|
|
2
|
+
type: string;
|
|
3
|
+
data: any;
|
|
4
|
+
}
|
|
5
|
+
export interface ApiResponse {
|
|
6
|
+
code: number;
|
|
7
|
+
msg: string;
|
|
8
|
+
data: any;
|
|
9
|
+
success: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare enum Result {
|
|
12
|
+
Success = 0,
|
|
13
|
+
Fail = 1
|
|
14
|
+
}
|
|
15
|
+
export declare const success: (data: any) => ApiResponse;
|
|
16
|
+
export declare const fail: (msg: string, data?: any) => ApiResponse;
|
package/dist/shared.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export var Result = /* @__PURE__ */ ((Result2) => {
|
|
2
|
+
Result2[Result2["Success"] = 0] = "Success";
|
|
3
|
+
Result2[Result2["Fail"] = 1] = "Fail";
|
|
4
|
+
return Result2;
|
|
5
|
+
})(Result || {});
|
|
6
|
+
export const success = (data) => {
|
|
7
|
+
return {
|
|
8
|
+
code: 0 /* Success */,
|
|
9
|
+
msg: "success",
|
|
10
|
+
data,
|
|
11
|
+
success: true
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export const fail = (msg, data = null) => {
|
|
15
|
+
return {
|
|
16
|
+
code: 1 /* Fail */,
|
|
17
|
+
msg,
|
|
18
|
+
data,
|
|
19
|
+
success: false
|
|
20
|
+
};
|
|
21
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vtj/local",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "0.7.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "unbuild",
|
|
8
|
+
"test": "vitest run",
|
|
9
|
+
"vitest": "vitest",
|
|
10
|
+
"coverage": "vitest run --coverage"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@vtj/coder": "^0.7.0",
|
|
14
|
+
"@vtj/core": "^0.7.0",
|
|
15
|
+
"@vtj/node": "^0.7.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@vtj/cli": "^0.7.0",
|
|
19
|
+
"unbuild": "~2.0.0",
|
|
20
|
+
"vite": "~5.0.11",
|
|
21
|
+
"vitest": "~1.2.0"
|
|
22
|
+
},
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.mjs",
|
|
27
|
+
"require": "./dist/index.mjs"
|
|
28
|
+
},
|
|
29
|
+
"./*": "./*"
|
|
30
|
+
},
|
|
31
|
+
"main": "./dist/index.mjs",
|
|
32
|
+
"module": "./dist/index.mjs",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"gitHead": "b65d1b19322cbd0721173d094db1b5ece018d980",
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|