@vtj/local 0.7.33 → 0.8.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.
@@ -1,5 +1,6 @@
1
1
  import { type ApiRequest, type ApiResponse } from './shared';
2
+ import type { DevToolsOptions } from './plugin';
2
3
  export interface Controller {
3
- [index: string]: (req: ApiRequest) => Promise<ApiResponse>;
4
+ [index: string]: (req: ApiRequest, opts?: DevToolsOptions) => Promise<ApiResponse>;
4
5
  }
5
- export declare const router: (req: any) => Promise<ApiResponse>;
6
+ export declare const router: (req: any, opts: DevToolsOptions) => Promise<ApiResponse>;
@@ -1,5 +1,7 @@
1
+ import formidable from "formidable";
1
2
  import { fail } from "./shared.mjs";
2
3
  import * as service from "./service.mjs";
4
+ import { resolve } from "path";
3
5
  const controller = {
4
6
  notMatch: async (_req) => {
5
7
  return fail("\u627E\u4E0D\u5230\u5904\u7406\u7A0B\u5E8F");
@@ -68,19 +70,58 @@ const controller = {
68
70
  removeRawPage: async (req) => {
69
71
  const id = req.data;
70
72
  return service.removeRawPage(id);
73
+ },
74
+ getStaticFiles: async (_req, opts) => {
75
+ return service.getStaticFiles(opts);
76
+ },
77
+ removeStaticFile: async (req, opts) => {
78
+ const name = req.data?.name;
79
+ return service.removeStaticFile(name, opts);
80
+ },
81
+ clearStaticFiles: async (_req, opts) => {
82
+ return service.clearStaticFiles(opts);
83
+ },
84
+ uploader: async (req, opts) => {
85
+ if (!opts)
86
+ return fail("\u5F02\u5E38\u9519\u8BEF");
87
+ const uploadDir = resolve(opts.staticDir, opts.vtjDir);
88
+ const form = formidable({
89
+ keepExtensions: true,
90
+ multiples: true,
91
+ createDirsFromUploads: true,
92
+ uploadDir
93
+ });
94
+ return await new Promise((reslove) => {
95
+ form.parse(req, (err, _fields, files) => {
96
+ if (err) {
97
+ reslove(fail("\u5F02\u5E38\u9519\u8BEF", err));
98
+ return;
99
+ }
100
+ const tempFiles = files.files || [];
101
+ const result = service.uploadStaticFiles(tempFiles, opts);
102
+ reslove(result);
103
+ });
104
+ });
71
105
  }
72
106
  };
73
- export const router = async (req) => {
107
+ export const router = async (req, opts) => {
74
108
  const body = req.body || {};
75
- const handler = controller[body.type] || controller.notMatch;
76
- try {
77
- return await handler(body);
78
- } catch (e) {
79
- const info = {
80
- input: body,
81
- error: e
82
- };
83
- await service.saveLogs(info);
84
- return fail("\u5F02\u5E38\u9519\u8BEF", e);
109
+ const reqUrl = req.url || "";
110
+ const uploaderPath = `${opts.baseURL}${opts.uploader}`;
111
+ const isUploader = reqUrl.startsWith(uploaderPath);
112
+ if (isUploader) {
113
+ return await controller.uploader(req, opts);
114
+ } else {
115
+ const handler = controller[body.type] || controller.notMatch;
116
+ try {
117
+ return await handler(body, opts);
118
+ } catch (e) {
119
+ const info = {
120
+ input: body,
121
+ error: e
122
+ };
123
+ await service.saveLogs(info);
124
+ return fail("\u5F02\u5E38\u9519\u8BEF", e);
125
+ }
85
126
  }
86
127
  };
package/dist/plugin.d.ts CHANGED
@@ -4,11 +4,13 @@ export interface DevToolsOptions {
4
4
  copy: boolean;
5
5
  server: boolean;
6
6
  staticBase: string;
7
+ staticDir: string;
7
8
  link: boolean | string;
8
9
  linkOptions: LinkOptions | null;
9
10
  vtjDir: string;
10
11
  packagesDir: string;
11
12
  devMode: boolean;
13
+ uploader: string;
12
14
  hm?: string;
13
15
  }
14
16
  export interface LinkOptions {
package/dist/plugin.mjs CHANGED
@@ -13,7 +13,7 @@ const setApis = (server, options) => {
13
13
  server.middlewares.use(async (req, res, next) => {
14
14
  const reqUrl = req.url || "";
15
15
  if (reqUrl.startsWith(options.baseURL)) {
16
- const data = await router(req);
16
+ const data = await router(req, options);
17
17
  res.writeHead(200, { "Content-Type": "application/json" });
18
18
  res.end(JSON.stringify(data));
19
19
  } else {
@@ -144,11 +144,13 @@ export function createDevTools(options = {}) {
144
144
  copy: true,
145
145
  server: true,
146
146
  staticBase: "/",
147
+ staticDir: "public",
147
148
  link: true,
148
149
  linkOptions: null,
149
150
  vtjDir: ".vtj",
150
151
  packagesDir: "../../packages",
151
152
  devMode: false,
153
+ uploader: "/uploader.json",
152
154
  hm: "42f2469b4aa27c3f8978f634c0c19d24",
153
155
  ...options
154
156
  };
@@ -1,2 +1,3 @@
1
1
  export * from './json';
2
2
  export * from './vue';
3
+ export * from './static';
@@ -1,2 +1,3 @@
1
1
  export * from "./json.mjs";
2
2
  export * from "./vue.mjs";
3
+ export * from "./static.mjs";
@@ -0,0 +1,17 @@
1
+ import formidable from 'formidable';
2
+ export interface StaticRepositoryOptions {
3
+ staticBase: string;
4
+ staticDir: string;
5
+ vtjDir: string;
6
+ }
7
+ export declare class StaticRepository {
8
+ private options;
9
+ private path;
10
+ constructor(options: StaticRepositoryOptions);
11
+ exist(name: string): any;
12
+ remove(name: string): boolean;
13
+ clear(): boolean;
14
+ getAllFiles(): any;
15
+ validate(files: formidable.File[]): false | never[];
16
+ save(files: formidable.File[]): never[];
17
+ }
@@ -0,0 +1,93 @@
1
+ import { resolve, join } from "path";
2
+ import { pathExistsSync, removeSync, readdirSync, moveSync } from "@vtj/node";
3
+ export class StaticRepository {
4
+ constructor(options) {
5
+ this.options = options;
6
+ this.path = resolve(this.options.staticDir, this.options.vtjDir);
7
+ }
8
+ path;
9
+ exist(name) {
10
+ const filePath = join(this.path, name);
11
+ return pathExistsSync(filePath);
12
+ }
13
+ remove(name) {
14
+ const filePath = join(this.path, name);
15
+ if (pathExistsSync(filePath)) {
16
+ removeSync(filePath);
17
+ return true;
18
+ }
19
+ return false;
20
+ }
21
+ clear() {
22
+ if (pathExistsSync(this.path)) {
23
+ removeSync(this.path);
24
+ return true;
25
+ }
26
+ return false;
27
+ }
28
+ getAllFiles() {
29
+ if (pathExistsSync(this.path)) {
30
+ const files = readdirSync(this.path) || [];
31
+ return files.map((name) => {
32
+ return {
33
+ filename: name,
34
+ filepath: join(
35
+ this.options.staticBase,
36
+ this.options.vtjDir,
37
+ name
38
+ ).replace(/\\/g, "/")
39
+ };
40
+ });
41
+ }
42
+ return [];
43
+ }
44
+ validate(files) {
45
+ let isExists = false;
46
+ const result = [];
47
+ for (let file of files) {
48
+ if (file.originalFilename) {
49
+ const filePath = join(this.path, file.originalFilename);
50
+ if (pathExistsSync(filePath)) {
51
+ isExists = true;
52
+ result.push({
53
+ filename: file.originalFilename,
54
+ filepath: join(
55
+ this.options.staticBase,
56
+ this.options.vtjDir,
57
+ file.originalFilename
58
+ ).replace(/\\/g, "/")
59
+ });
60
+ break;
61
+ }
62
+ }
63
+ }
64
+ if (isExists) {
65
+ for (let file of files) {
66
+ if (file.filepath) {
67
+ if (pathExistsSync(file.filepath)) {
68
+ removeSync(file.filepath);
69
+ }
70
+ }
71
+ }
72
+ }
73
+ return isExists ? result : false;
74
+ }
75
+ save(files) {
76
+ const result = [];
77
+ for (let file of files) {
78
+ if (file.filepath && file.originalFilename) {
79
+ const filePath = join(this.path, file.originalFilename);
80
+ moveSync(file.filepath, filePath, { overwrite: true });
81
+ result.push({
82
+ filename: file.originalFilename,
83
+ filepath: join(
84
+ this.options.staticBase,
85
+ this.options.vtjDir,
86
+ file.originalFilename
87
+ ).replace(/\\/g, "/")
88
+ });
89
+ }
90
+ }
91
+ return result;
92
+ }
93
+ }
package/dist/service.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  import { type ProjectSchema, type BlockSchema, type HistorySchema, type HistoryItem, type MaterialDescription, type PageFile, type BlockFile } from '@vtj/core';
2
+ import formidable from 'formidable';
2
3
  import { type ApiRequest } from './shared';
4
+ import { type StaticRepositoryOptions } from './repository';
3
5
  export declare function notMatch(_req: ApiRequest): Promise<import("./shared").ApiResponse>;
4
6
  export declare function saveLogs(e: any): Promise<boolean>;
5
7
  export declare function init(): Promise<import("./shared").ApiResponse>;
@@ -19,3 +21,7 @@ export declare function publish(project: ProjectSchema): Promise<import("./share
19
21
  export declare function genVueContent(project: ProjectSchema, dsl: BlockSchema): Promise<import("./shared").ApiResponse>;
20
22
  export declare function createRawPage(file: PageFile): Promise<import("./shared").ApiResponse>;
21
23
  export declare function removeRawPage(id: string): Promise<import("./shared").ApiResponse>;
24
+ export declare function uploadStaticFiles(files: formidable.File[], options: StaticRepositoryOptions): Promise<import("./shared").ApiResponse>;
25
+ export declare function removeStaticFile(filename: string, options: StaticRepositoryOptions): Promise<import("./shared").ApiResponse>;
26
+ export declare function getStaticFiles(options: StaticRepositoryOptions): Promise<import("./shared").ApiResponse>;
27
+ export declare function clearStaticFiles(options: StaticRepositoryOptions): Promise<import("./shared").ApiResponse>;
package/dist/service.mjs CHANGED
@@ -5,7 +5,11 @@ import { resolve } from "path";
5
5
  import { readJsonSync, upperFirstCamelCase, timestamp } from "@vtj/node";
6
6
  import { generator, createEmptyPage } from "@vtj/coder";
7
7
  import { fail, success } from "./shared.mjs";
8
- import { JsonRepository, VueRepository } from "./repository/index.mjs";
8
+ import {
9
+ JsonRepository,
10
+ VueRepository,
11
+ StaticRepository
12
+ } from "./repository/index.mjs";
9
13
  export async function notMatch(_req) {
10
14
  return fail("\u627E\u4E0D\u5230\u5904\u7406\u7A0B\u5E8F");
11
15
  }
@@ -132,7 +136,9 @@ export async function publish(project) {
132
136
  Object.entries(materials)
133
137
  );
134
138
  for (const block of blocks) {
135
- await publishFile(project, block, componentMap);
139
+ if (!block.fromType || block.fromType === "Schema") {
140
+ await publishFile(project, block, componentMap);
141
+ }
136
142
  }
137
143
  for (const page of pages) {
138
144
  if (!page.raw) {
@@ -161,3 +167,25 @@ export async function removeRawPage(id) {
161
167
  repository.remove(id);
162
168
  return success(true);
163
169
  }
170
+ export async function uploadStaticFiles(files, options) {
171
+ const repository = new StaticRepository(options);
172
+ const error = repository.validate(files);
173
+ if (error) {
174
+ return fail("\u6587\u4EF6\u540D\u79F0\u5DF2\u5B58\u5728", error);
175
+ }
176
+ const res = repository.save(files);
177
+ return success(res);
178
+ }
179
+ export async function removeStaticFile(filename, options) {
180
+ const repository = new StaticRepository(options);
181
+ const ret = repository.remove(filename);
182
+ return ret ? success(true) : fail("\u5220\u9664\u5931\u8D25");
183
+ }
184
+ export async function getStaticFiles(options) {
185
+ const repository = new StaticRepository(options);
186
+ return success(repository.getAllFiles());
187
+ }
188
+ export async function clearStaticFiles(options) {
189
+ const repository = new StaticRepository(options);
190
+ return success(repository.clear());
191
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vtj/local",
3
3
  "private": false,
4
- "version": "0.7.33",
4
+ "version": "0.8.0",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "build": "unbuild",
@@ -10,15 +10,17 @@
10
10
  "coverage": "vitest run --coverage"
11
11
  },
12
12
  "dependencies": {
13
- "@vtj/coder": "^0.7.33",
14
- "@vtj/core": "^0.7.33",
15
- "@vtj/node": "^0.7.8"
13
+ "@vtj/coder": "^0.8.0",
14
+ "@vtj/core": "^0.8.0",
15
+ "@vtj/node": "^0.8.0",
16
+ "formidable": "~3.5.1"
16
17
  },
17
18
  "devDependencies": {
18
- "@vtj/cli": "^0.7.21",
19
+ "@types/formidable": "~3.4.5",
20
+ "@vtj/cli": "^0.8.0",
19
21
  "unbuild": "~2.0.0",
20
22
  "vite": "~5.2.6",
21
- "vitest": "~1.4.0"
23
+ "vitest": "~1.5.0"
22
24
  },
23
25
  "exports": {
24
26
  ".": {
@@ -34,7 +36,7 @@
34
36
  "files": [
35
37
  "dist"
36
38
  ],
37
- "gitHead": "f8b4d10016f55e54c634f472fde12760d512e829",
39
+ "gitHead": "911c3a0e2bb60548affe5dcf5a496577809d63b8",
38
40
  "publishConfig": {
39
41
  "access": "public"
40
42
  }