@vtj/local 0.7.5 → 0.7.6

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.
@@ -59,7 +59,7 @@ const controller = {
59
59
  },
60
60
  getRaw: async (req) => {
61
61
  const { project, dsl } = req.data || {};
62
- return service.getRaw(project, dsl);
62
+ return service.genVueContent(project, dsl);
63
63
  },
64
64
  createRawPage: async (req) => {
65
65
  const file = req.data;
package/dist/plugin.mjs CHANGED
@@ -117,7 +117,7 @@ const aliasPlugin = function(options) {
117
117
  };
118
118
  export function createDevPlugin(options = {}) {
119
119
  const opts = {
120
- baseURL: "/vtj/local/api",
120
+ baseURL: "/vtj/local/repository",
121
121
  copy: true,
122
122
  server: true,
123
123
  staticBase: "/",
@@ -0,0 +1,2 @@
1
+ export * from './json';
2
+ export * from './vue';
@@ -0,0 +1,2 @@
1
+ export * from "./json.mjs";
2
+ export * from "./vue.mjs";
@@ -0,0 +1,9 @@
1
+ export declare class JsonRepository {
2
+ private path;
3
+ constructor(path: string);
4
+ exist(name: string): any;
5
+ save(name: string, json: any): boolean;
6
+ get(name: string): any;
7
+ remove(name: string): boolean;
8
+ clear(): boolean;
9
+ }
@@ -0,0 +1,49 @@
1
+ import { resolve, join } from "path";
2
+ import {
3
+ writeJsonSync,
4
+ pathExistsSync,
5
+ readJsonSync,
6
+ removeSync,
7
+ ensureFileSync
8
+ } from "@vtj/node";
9
+ export class JsonRepository {
10
+ path;
11
+ constructor(path) {
12
+ this.path = resolve(".vtj", path);
13
+ }
14
+ exist(name) {
15
+ const filePath = join(this.path, `${name}.json`);
16
+ return pathExistsSync(filePath);
17
+ }
18
+ save(name, json) {
19
+ const filePath = join(this.path, `${name}.json`);
20
+ if (!this.exist(name)) {
21
+ ensureFileSync(filePath);
22
+ }
23
+ writeJsonSync(filePath, json);
24
+ return true;
25
+ }
26
+ get(name) {
27
+ const filePath = join(this.path, `${name}.json`);
28
+ if (pathExistsSync(filePath)) {
29
+ return readJsonSync(filePath);
30
+ } else {
31
+ return void 0;
32
+ }
33
+ }
34
+ remove(name) {
35
+ const filePath = join(this.path, `${name}.json`);
36
+ if (pathExistsSync(filePath)) {
37
+ removeSync(filePath);
38
+ return true;
39
+ }
40
+ return false;
41
+ }
42
+ clear() {
43
+ if (pathExistsSync(this.path)) {
44
+ removeSync(this.path);
45
+ return true;
46
+ }
47
+ return false;
48
+ }
49
+ }
@@ -0,0 +1,8 @@
1
+ export declare class VueRepository {
2
+ private path;
3
+ constructor();
4
+ exist(name: string): any;
5
+ save(name: string, content: any): boolean;
6
+ remove(name: string): boolean;
7
+ clear(): boolean;
8
+ }
@@ -0,0 +1,40 @@
1
+ import { resolve, join } from "path";
2
+ import {
3
+ pathExistsSync,
4
+ removeSync,
5
+ outputFileSync,
6
+ ensureFileSync
7
+ } from "@vtj/node";
8
+ export class VueRepository {
9
+ path;
10
+ constructor() {
11
+ this.path = resolve(".vtj/vue");
12
+ }
13
+ exist(name) {
14
+ const filePath = join(this.path, `${name}.vue`);
15
+ return pathExistsSync(filePath);
16
+ }
17
+ save(name, content) {
18
+ const filePath = join(this.path, `${name}.vue`);
19
+ if (!this.exist(name)) {
20
+ ensureFileSync(filePath);
21
+ }
22
+ outputFileSync(filePath, content, "utf-8");
23
+ return true;
24
+ }
25
+ remove(name) {
26
+ const filePath = join(this.path, `${name}.vue`);
27
+ if (pathExistsSync(filePath)) {
28
+ removeSync(filePath);
29
+ return true;
30
+ }
31
+ return false;
32
+ }
33
+ clear() {
34
+ if (pathExistsSync(this.path)) {
35
+ removeSync(this.path);
36
+ return true;
37
+ }
38
+ return false;
39
+ }
40
+ }
package/dist/service.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { type ProjectSchema, type BlockSchema, type HistorySchema, type HistoryItem, type MaterialDescription, type PageFile, type BlockFile } from '@vtj/core';
2
2
  import { type ApiRequest } from './shared';
3
3
  export declare function notMatch(_req: ApiRequest): Promise<import("./shared").ApiResponse>;
4
- export declare function saveLogs(e: any): Promise<void>;
4
+ export declare function saveLogs(e: any): Promise<boolean>;
5
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>;
6
+ export declare function saveProject(dsl: ProjectSchema): Promise<import("./shared").ApiResponse>;
7
+ export declare function saveFile(dsl: BlockSchema): Promise<import("./shared").ApiResponse>;
8
8
  export declare function getFile(id: string): Promise<import("./shared").ApiResponse>;
9
9
  export declare function removeFile(id: string): Promise<import("./shared").ApiResponse>;
10
10
  export declare function getHistory(id: string): Promise<import("./shared").ApiResponse>;
@@ -16,6 +16,6 @@ export declare function removeHistoryItem(fId: string, ids: string[]): Promise<i
16
16
  export declare function saveMaterials(project: ProjectSchema, materials: Record<string, MaterialDescription>): Promise<import("./shared").ApiResponse>;
17
17
  export declare function publishFile(project: ProjectSchema, file: PageFile | BlockFile, componentMap?: Map<string, MaterialDescription>): Promise<import("./shared").ApiResponse>;
18
18
  export declare function publish(project: ProjectSchema): Promise<import("./shared").ApiResponse>;
19
- export declare function getRaw(project: ProjectSchema, dsl: BlockSchema): Promise<import("./shared").ApiResponse>;
19
+ export declare function genVueContent(project: ProjectSchema, dsl: BlockSchema): Promise<import("./shared").ApiResponse>;
20
20
  export declare function createRawPage(file: PageFile): Promise<import("./shared").ApiResponse>;
21
21
  export declare function removeRawPage(id: string): Promise<import("./shared").ApiResponse>;
package/dist/service.mjs CHANGED
@@ -2,182 +2,132 @@ import {
2
2
  ProjectModel
3
3
  } from "@vtj/core";
4
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";
5
+ import { readJsonSync, upperFirstCamelCase, timestamp } from "@vtj/node";
15
6
  import { generator, createEmptyPage } from "@vtj/coder";
16
7
  import { fail, success } from "./shared.mjs";
17
- const root = resolve("./");
18
- const vtjDir = resolve(".vtj");
19
- const viewsDir = resolve("src/views");
20
- const getProjectFilePath = (_name) => {
21
- return resolve(vtjDir, "project.json");
22
- };
23
- const getMaterialsFilePath = (_name) => {
24
- return resolve(vtjDir, "materials.json");
25
- };
26
- const getFilePath = (id) => {
27
- return resolve(vtjDir, `files/${id}.json`);
28
- };
29
- const getHistoryFilePath = (id) => {
30
- return resolve(vtjDir, `histories/${id}.json`);
31
- };
32
- const getHistoryItemFilePath = (fId, id) => {
33
- return resolve(vtjDir, `histories/${fId}/${id}.json`);
34
- };
35
- const getHistoryItemDir = (fId) => {
36
- return resolve(vtjDir, `histories/${fId}`);
37
- };
38
- const getRawFilePath = (id) => {
39
- return resolve(vtjDir, `raw/${id}.vue`);
40
- };
41
- const getRawPagePath = (id) => {
42
- return resolve(viewsDir, `${id}.vue`);
43
- };
44
- const getLogsDir = () => {
45
- return resolve(vtjDir, `logs`);
46
- };
8
+ import { JsonRepository, VueRepository } from "./repository/index.mjs";
47
9
  export async function notMatch(_req) {
48
10
  return fail("\u627E\u4E0D\u5230\u5904\u7406\u7A0B\u5E8F");
49
11
  }
50
12
  export async function saveLogs(e) {
51
13
  const name = `error-${timestamp()}.json`;
52
- outputFileSync(resolve(getLogsDir(), name), JSON.stringify(e), "utf-8");
14
+ const logs = new JsonRepository("logs");
15
+ const json = JSON.parse(JSON.stringify(e));
16
+ return logs.save(name, json);
53
17
  }
54
18
  export async function init() {
19
+ const root = resolve("./");
55
20
  const pkg = readJsonSync(resolve(root, "package.json"));
21
+ const repository = new JsonRepository("projects");
56
22
  const { name, description } = pkg || {};
57
- const filePath = getProjectFilePath(name);
58
- let schema = {};
59
- if (pathExistsSync(filePath)) {
60
- schema = readJsonSync(filePath);
23
+ let dsl = repository.get(name);
24
+ if (dsl) {
25
+ return success(dsl);
26
+ } else {
27
+ const model = new ProjectModel({
28
+ id: name,
29
+ name: description || upperFirstCamelCase(name),
30
+ description
31
+ });
32
+ dsl = model.toDsl();
33
+ repository.save(name, dsl);
34
+ return success(dsl);
61
35
  }
62
- const project = new ProjectModel({
63
- ...schema,
64
- id: name,
65
- name: description || upperFirstCamelCase(name),
66
- description
67
- });
68
- const dsl = project.toDsl(schema.__VERSION__);
69
- ensureFileSync(filePath);
70
- writeJsonSync(filePath, dsl);
71
- return success(dsl);
72
- }
73
- export async function saveProject(project) {
74
- const filePath = getProjectFilePath(project.id);
75
- if (pathExistsSync(filePath)) {
76
- writeJsonSync(filePath, project);
77
- return success(true);
36
+ }
37
+ export async function saveProject(dsl) {
38
+ const repository = new JsonRepository("projects");
39
+ if (repository.exist(dsl.id)) {
40
+ const ret = repository.save(dsl.id, dsl);
41
+ return success(ret);
78
42
  } else {
79
43
  return fail("\u9879\u76EE\u6587\u4EF6\u4E0D\u5B58\u5728");
80
44
  }
81
45
  }
82
- export async function saveFile(file) {
83
- const filePath = getFilePath(file.id);
84
- if (!pathExistsSync(filePath)) {
85
- ensureFileSync(filePath);
86
- }
87
- writeJsonSync(filePath, file);
88
- return success(true);
46
+ export async function saveFile(dsl) {
47
+ const repository = new JsonRepository("files");
48
+ const ret = repository.save(dsl.id, dsl);
49
+ return success(ret);
89
50
  }
90
51
  export async function getFile(id) {
91
- const filePath = getFilePath(id);
92
- if (pathExistsSync(filePath)) {
93
- const json = readJsonSync(filePath);
52
+ const repository = new JsonRepository("files");
53
+ const json = repository.get(id);
54
+ if (json) {
94
55
  return success(json);
95
56
  } else {
96
57
  return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
97
58
  }
98
59
  }
99
60
  export async function removeFile(id) {
100
- const filePath = getFilePath(id);
101
- if (pathExistsSync(filePath)) {
102
- removeSync(filePath);
103
- return success(true);
104
- } else {
105
- return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
106
- }
61
+ const repository = new JsonRepository("files");
62
+ const ret = repository.remove(id);
63
+ return success(ret);
107
64
  }
108
65
  export async function getHistory(id) {
109
- const filePath = getHistoryFilePath(id);
110
- if (pathExistsSync(filePath)) {
111
- const json = readJsonSync(filePath);
66
+ const repository = new JsonRepository("histories");
67
+ const json = repository.get(id);
68
+ if (json) {
112
69
  return success(json);
113
70
  } else {
114
71
  return success({});
115
72
  }
116
73
  }
117
74
  export async function saveHistory(file) {
118
- const filePath = getHistoryFilePath(file.id);
119
- if (!pathExistsSync(filePath)) {
120
- ensureFileSync(filePath);
121
- }
122
- writeJsonSync(filePath, file);
123
- return success(true);
75
+ const repository = new JsonRepository("histories");
76
+ const ret = repository.save(file.id, file);
77
+ return success(ret);
124
78
  }
125
79
  export async function removeHistory(id) {
126
- const filePath = getHistoryFilePath(id);
127
- const dir = getHistoryItemDir(id);
128
- if (pathExistsSync(filePath)) {
129
- removeSync(filePath);
130
- removeSync(dir);
131
- return success(true);
132
- } else {
133
- return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
134
- }
80
+ const repository = new JsonRepository("histories");
81
+ const items = new JsonRepository(`histories/${id}`);
82
+ items.clear();
83
+ repository.remove(id);
84
+ return success(true);
135
85
  }
136
86
  export async function getHistoryItem(fId, id) {
137
- const filePath = getHistoryItemFilePath(fId, id);
138
- if (pathExistsSync(filePath)) {
139
- const json = readJsonSync(filePath);
87
+ const repository = new JsonRepository(`histories/${fId}`);
88
+ const json = repository.get(id);
89
+ if (json) {
140
90
  return success(json);
141
91
  } else {
142
92
  return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
143
93
  }
144
94
  }
145
95
  export async function saveHistoryItem(fId, item) {
146
- const filePath = getHistoryItemFilePath(fId, item.id);
147
- if (!pathExistsSync(filePath)) {
148
- ensureFileSync(filePath);
149
- }
150
- writeJsonSync(filePath, item);
96
+ const repository = new JsonRepository(`histories/${fId}`);
97
+ repository.save(item.id, item);
151
98
  return success(true);
152
99
  }
153
100
  export async function removeHistoryItem(fId, ids) {
101
+ const repository = new JsonRepository(`histories/${fId}`);
154
102
  ids.forEach((id) => {
155
- const filePath = getHistoryItemFilePath(fId, id);
156
- removeSync(filePath);
103
+ repository.remove(id);
157
104
  });
158
105
  return success(true);
159
106
  }
160
107
  export async function saveMaterials(project, materials) {
161
- const filePath = getMaterialsFilePath(project.id);
162
- ensureFileSync(filePath);
163
- writeJsonSync(filePath, materials);
108
+ const repository = new JsonRepository("materials");
109
+ repository.save(project.id, materials);
164
110
  return success(true);
165
111
  }
166
112
  export async function publishFile(project, file, componentMap) {
167
- const materialsPath = getMaterialsFilePath(project.id);
168
- const materials = await readJsonSync(materialsPath);
169
- componentMap = componentMap || new Map(Object.entries(materials));
170
- const filePath = getFilePath(file.id);
171
- const dsl = readJsonSync(filePath);
172
- const content = await generator(dsl, componentMap, project.dependencies);
173
- const rawPath = getRawFilePath(file.id);
174
- outputFileSync(rawPath, content, "utf-8");
175
- return success(true);
113
+ const materialsRepository = new JsonRepository("materials");
114
+ const materials = materialsRepository.get(project.id);
115
+ componentMap = componentMap || new Map(Object.entries(materials || {}));
116
+ const fileRepository = new JsonRepository("files");
117
+ const dsl = fileRepository.get(file.id);
118
+ if (dsl) {
119
+ const content = await generator(dsl, componentMap, project.dependencies);
120
+ const vueRepository = new VueRepository();
121
+ vueRepository.save(file.id, content);
122
+ return success(true);
123
+ } else {
124
+ return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
125
+ }
176
126
  }
177
127
  export async function publish(project) {
178
128
  const { pages = [], blocks = [] } = project;
179
- const materialsPath = getMaterialsFilePath(project.id);
180
- const materials = await readJsonSync(materialsPath);
129
+ const materialsRepository = new JsonRepository("materials");
130
+ const materials = materialsRepository.get(project.id);
181
131
  const componentMap = new Map(
182
132
  Object.entries(materials)
183
133
  );
@@ -191,9 +141,9 @@ export async function publish(project) {
191
141
  }
192
142
  return success(true);
193
143
  }
194
- export async function getRaw(project, dsl) {
195
- const materialsPath = getMaterialsFilePath(project.id);
196
- const materials = await readJsonSync(materialsPath);
144
+ export async function genVueContent(project, dsl) {
145
+ const materialsRepository = new JsonRepository("materials");
146
+ const materials = materialsRepository.get(project.id);
197
147
  const componentMap = new Map(
198
148
  Object.entries(materials)
199
149
  );
@@ -201,17 +151,13 @@ export async function getRaw(project, dsl) {
201
151
  return success(content);
202
152
  }
203
153
  export async function createRawPage(file) {
204
- const filePath = getRawPagePath(file.id);
154
+ const repository = new VueRepository();
205
155
  const page = await createEmptyPage(file);
206
- outputFileSync(filePath, page, "utf-8");
156
+ repository.save(file.id, page);
207
157
  return success(true);
208
158
  }
209
159
  export async function removeRawPage(id) {
210
- const filePath = getRawPagePath(id);
211
- if (pathExistsSync(filePath)) {
212
- removeSync(filePath);
213
- return success(true);
214
- } else {
215
- return fail("\u6587\u4EF6\u4E0D\u5B58\u5728");
216
- }
160
+ const repository = new VueRepository();
161
+ repository.remove(id);
162
+ return success(true);
217
163
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vtj/local",
3
3
  "private": false,
4
- "version": "0.7.5",
4
+ "version": "0.7.6",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "unbuild",
@@ -10,14 +10,14 @@
10
10
  "coverage": "vitest run --coverage"
11
11
  },
12
12
  "dependencies": {
13
- "@vtj/coder": "^0.7.5",
14
- "@vtj/core": "^0.7.5",
13
+ "@vtj/coder": "^0.7.6",
14
+ "@vtj/core": "^0.7.6",
15
15
  "@vtj/node": "^0.7.2"
16
16
  },
17
17
  "devDependencies": {
18
- "@vtj/cli": "^0.7.3",
18
+ "@vtj/cli": "^0.7.4",
19
19
  "unbuild": "~2.0.0",
20
- "vite": "~5.0.11",
20
+ "vite": "~5.0.12",
21
21
  "vitest": "~1.2.1"
22
22
  },
23
23
  "exports": {
@@ -34,7 +34,7 @@
34
34
  "files": [
35
35
  "dist"
36
36
  ],
37
- "gitHead": "5c96cf32296d475fc39dd873801e4f035e6b462b",
37
+ "gitHead": "17389ef7d1daa18d33ba6cd57197219223237209",
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  }