@wzyjs/next 0.2.55 → 0.2.57
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/dist/Base.zmodel
CHANGED
|
@@ -39,10 +39,11 @@ plugin trpc {
|
|
|
39
39
|
generateModelActions = 'findMany, findUnique, create, update, delete, deleteMany'
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
plugin
|
|
42
|
+
plugin crud_tags {
|
|
43
43
|
provider = '@wzyjs/next'
|
|
44
|
-
title = 'Generate
|
|
45
|
-
output = 'src/api/generated
|
|
44
|
+
title = 'Generate Some'
|
|
45
|
+
output = 'src/api/generated'
|
|
46
|
+
generateActions = 'crud, tags'
|
|
46
47
|
}
|
|
47
48
|
|
|
48
49
|
abstract model Base {
|
package/dist/index.js
CHANGED
|
@@ -38,44 +38,36 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.name = void 0;
|
|
40
40
|
exports.default = run;
|
|
41
|
-
var
|
|
42
|
-
var
|
|
43
|
-
exports.name = 'Generate
|
|
44
|
-
var config = {
|
|
45
|
-
template: {
|
|
46
|
-
content: fs.readFileSync(path.resolve(__dirname, './useHabitGroupCRUD.ts'), 'utf-8'),
|
|
47
|
-
},
|
|
48
|
-
};
|
|
41
|
+
var crud_1 = require("./plugin/crud");
|
|
42
|
+
var tags_1 = require("./plugin/tags");
|
|
43
|
+
exports.name = 'Generate CRUD and Tags';
|
|
49
44
|
function run(model, options) {
|
|
50
45
|
return __awaiter(this, void 0, void 0, function () {
|
|
51
|
-
var
|
|
46
|
+
var outputPath, generateActions, actions;
|
|
52
47
|
return __generator(this, function (_a) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
48
|
+
switch (_a.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
// 检查配置
|
|
51
|
+
if (!options.output) {
|
|
52
|
+
throw new Error('output is required');
|
|
53
|
+
}
|
|
54
|
+
outputPath = String(options.output);
|
|
55
|
+
generateActions = String(options.generateActions || 'crud,tags');
|
|
56
|
+
actions = generateActions.split(',').map(function (action) { return action.trim().toLowerCase(); });
|
|
57
|
+
console.log();
|
|
58
|
+
if (!actions.includes('crud')) return [3 /*break*/, 2];
|
|
59
|
+
return [4 /*yield*/, (0, crud_1.generateCRUD)(model, outputPath)];
|
|
60
|
+
case 1:
|
|
61
|
+
_a.sent();
|
|
62
|
+
_a.label = 2;
|
|
63
|
+
case 2:
|
|
64
|
+
if (!actions.includes('tags')) return [3 /*break*/, 4];
|
|
65
|
+
return [4 /*yield*/, (0, tags_1.generateTags)(model, outputPath)];
|
|
66
|
+
case 3:
|
|
67
|
+
_a.sent();
|
|
68
|
+
_a.label = 4;
|
|
69
|
+
case 4: return [2 /*return*/];
|
|
63
70
|
}
|
|
64
|
-
// 生成模板
|
|
65
|
-
modelNames.forEach(function (_a) {
|
|
66
|
-
var ModelName = _a.name;
|
|
67
|
-
var templateContent = config.template.content;
|
|
68
|
-
var modelName = ModelName.replace(/^./, function (s) { return s.toLowerCase(); });
|
|
69
|
-
templateContent = templateContent.replace(/habitGroup/g, modelName);
|
|
70
|
-
templateContent = templateContent.replace(/HabitGroup/g, ModelName);
|
|
71
|
-
fs.writeFileSync(options.output + "/use".concat(ModelName, "CRUD.ts"), templateContent);
|
|
72
|
-
});
|
|
73
|
-
indexContent = modelNames.map(function (_a) {
|
|
74
|
-
var ModelName = _a.name;
|
|
75
|
-
return "export * from './use".concat(ModelName, "CRUD'");
|
|
76
|
-
}).join('\n');
|
|
77
|
-
fs.writeFileSync(options.output + '/index.ts', indexContent + '\n');
|
|
78
|
-
return [2 /*return*/];
|
|
79
71
|
});
|
|
80
72
|
});
|
|
81
73
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.generateCRUD = void 0;
|
|
40
|
+
var fs = require("node:fs");
|
|
41
|
+
var path = require("node:path");
|
|
42
|
+
var crudConfig = {
|
|
43
|
+
template: {
|
|
44
|
+
content: fs.readFileSync(path.resolve(__dirname, './useHabitGroupCRUD.ts'), 'utf-8'),
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
var generateCRUD = function (model, outputPath) { return __awaiter(void 0, void 0, void 0, function () {
|
|
48
|
+
var crudDirPath, modelNames, indexContent;
|
|
49
|
+
return __generator(this, function (_a) {
|
|
50
|
+
crudDirPath = path.join(outputPath, 'crud');
|
|
51
|
+
modelNames = model.declarations.filter(function (item) { return item.$type === 'DataModel'; });
|
|
52
|
+
// 创建目录
|
|
53
|
+
if (modelNames.length) {
|
|
54
|
+
if (!fs.existsSync(crudDirPath)) {
|
|
55
|
+
fs.mkdirSync(crudDirPath, { recursive: true });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// 生成模板
|
|
59
|
+
modelNames.forEach(function (_a) {
|
|
60
|
+
var ModelName = _a.name;
|
|
61
|
+
var templateContent = crudConfig.template.content;
|
|
62
|
+
var modelName = String(ModelName).replace(/^./, function (s) { return s.toLowerCase(); });
|
|
63
|
+
templateContent = templateContent.replace(/habitGroup/g, modelName);
|
|
64
|
+
templateContent = templateContent.replace(/HabitGroup/g, String(ModelName));
|
|
65
|
+
fs.writeFileSync(path.join(crudDirPath, "use".concat(ModelName, "CRUD.ts")), templateContent);
|
|
66
|
+
});
|
|
67
|
+
indexContent = modelNames.map(function (_a) {
|
|
68
|
+
var ModelName = _a.name;
|
|
69
|
+
return "export * from './use".concat(ModelName, "CRUD'");
|
|
70
|
+
}).join('\n');
|
|
71
|
+
fs.writeFileSync(path.join(crudDirPath, 'index.ts'), indexContent + '\n');
|
|
72
|
+
console.log("\u2705 CRUD \u529F\u80FD\u751F\u6210\u5B8C\u6210\uFF0C\u5171\u5904\u7406 ".concat(modelNames.length, " \u4E2A\u6A21\u578B"));
|
|
73
|
+
return [2 /*return*/];
|
|
74
|
+
});
|
|
75
|
+
}); };
|
|
76
|
+
exports.generateCRUD = generateCRUD;
|
|
@@ -12,13 +12,18 @@ interface Option {
|
|
|
12
12
|
query?: Parameters<typeof api.habitGroup.findMany.useQuery>[0],
|
|
13
13
|
option?: Parameters<typeof api.habitGroup.findMany.useQuery>[1],
|
|
14
14
|
},
|
|
15
|
+
info?: false | {
|
|
16
|
+
query?: Parameters<typeof api.habitGroup.findUnique.useQuery>[0],
|
|
17
|
+
option?: Parameters<typeof api.habitGroup.findUnique.useQuery>[1],
|
|
18
|
+
},
|
|
15
19
|
create?: Parameters<typeof api.habitGroup.create.useMutation>[0],
|
|
16
20
|
update?: Parameters<typeof api.habitGroup.update.useMutation>[0],
|
|
17
21
|
remove?: Parameters<typeof api.habitGroup.update.useMutation>[0],
|
|
22
|
+
delete?: Parameters<typeof api.habitGroup.delete.useMutation>[0],
|
|
18
23
|
}
|
|
19
24
|
|
|
20
25
|
export const useHabitGroupCRUD = (option: Option = {}) => {
|
|
21
|
-
const { list, create, update, remove, showTip = true } = option
|
|
26
|
+
const { list, info, create, update, remove, delete: deleteOption, showTip = true } = option
|
|
22
27
|
|
|
23
28
|
const apiUtils = api.useUtils()
|
|
24
29
|
|
|
@@ -27,6 +32,11 @@ export const useHabitGroupCRUD = (option: Option = {}) => {
|
|
|
27
32
|
...list?.option,
|
|
28
33
|
})
|
|
29
34
|
|
|
35
|
+
const infoState = api.habitGroup.findUnique.useQuery(info ? info.query : { where: { id: '' } }, {
|
|
36
|
+
enabled: !!(info && info.query),
|
|
37
|
+
...info?.option,
|
|
38
|
+
})
|
|
39
|
+
|
|
30
40
|
const onSuccess = (tip: string) => {
|
|
31
41
|
message.destroy()
|
|
32
42
|
if (showTip) {
|
|
@@ -109,11 +119,37 @@ export const useHabitGroupCRUD = (option: Option = {}) => {
|
|
|
109
119
|
},
|
|
110
120
|
}
|
|
111
121
|
|
|
122
|
+
const baseDeleteState = api.habitGroup.delete.useMutation({
|
|
123
|
+
...deleteOption,
|
|
124
|
+
onSuccess: (...args) => {
|
|
125
|
+
if (deleteOption?.onSuccess) {
|
|
126
|
+
deleteOption.onSuccess(...args)
|
|
127
|
+
}
|
|
128
|
+
onSuccess('删除成功')
|
|
129
|
+
},
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
const deleteState = {
|
|
133
|
+
...baseDeleteState as unknown as typeof baseDeleteState,
|
|
134
|
+
mutate: (id: string) => {
|
|
135
|
+
return baseDeleteState.mutate({
|
|
136
|
+
where: { id },
|
|
137
|
+
})
|
|
138
|
+
},
|
|
139
|
+
mutateAsync: (id: string) => {
|
|
140
|
+
return baseDeleteState.mutateAsync({
|
|
141
|
+
where: { id },
|
|
142
|
+
})
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
|
|
112
146
|
return {
|
|
113
147
|
listState,
|
|
148
|
+
infoState,
|
|
114
149
|
createState,
|
|
115
150
|
removeState,
|
|
116
151
|
updateState,
|
|
152
|
+
deleteState,
|
|
117
153
|
apiUtils,
|
|
118
154
|
}
|
|
119
155
|
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.generateTags = void 0;
|
|
40
|
+
var fs = require("node:fs");
|
|
41
|
+
var path = require("node:path");
|
|
42
|
+
var generateTags = function (model, outputPath) { return __awaiter(void 0, void 0, void 0, function () {
|
|
43
|
+
var dataModels, modelsWithTags, _i, modelsWithTags_1, dataModel, modelName, routerFilePath, fileContent, getAllTagsInterface, lastCommaIndex, closingBraceIndex, beforeInsertion, afterInsertion, newContent;
|
|
44
|
+
return __generator(this, function (_a) {
|
|
45
|
+
dataModels = model.declarations.filter(function (item) { return item.$type === 'DataModel'; });
|
|
46
|
+
modelsWithTags = dataModels.filter(function (dataModel) {
|
|
47
|
+
return dataModel.fields.some(function (field) { return field.name === 'tags'; });
|
|
48
|
+
});
|
|
49
|
+
if (modelsWithTags.length === 0) {
|
|
50
|
+
return [2 /*return*/];
|
|
51
|
+
}
|
|
52
|
+
// 为每个有 tags 字段的模型添加 getAllTags 接口
|
|
53
|
+
for (_i = 0, modelsWithTags_1 = modelsWithTags; _i < modelsWithTags_1.length; _i++) {
|
|
54
|
+
dataModel = modelsWithTags_1[_i];
|
|
55
|
+
modelName = String(dataModel.name);
|
|
56
|
+
routerFilePath = path.join(outputPath, 'trpc', 'routers', "".concat(modelName, ".router.ts"));
|
|
57
|
+
// 检查文件是否存在
|
|
58
|
+
if (!fs.existsSync(routerFilePath)) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
fileContent = fs.readFileSync(routerFilePath, 'utf-8');
|
|
62
|
+
// 检查是否已经添加了 getAllTags 接口
|
|
63
|
+
if (fileContent.includes('getAllTags:')) {
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
getAllTagsInterface = "\n getAllTags: procedure.query(async ({ ctx }) => {\n const records = await checkRead(db(ctx).".concat(modelName.charAt(0).toLowerCase() + modelName.slice(1), ".findMany({\n select: { tags: true },\n }))\n\n // \u6241\u5E73\u5316 + \u53BB\u91CD\n const uniqueTags = Array.from(new Set(records.flatMap(record => record.tags || [])))\n\n return uniqueTags\n }),");
|
|
67
|
+
lastCommaIndex = fileContent.lastIndexOf(',');
|
|
68
|
+
closingBraceIndex = fileContent.indexOf('\n }\n );', lastCommaIndex);
|
|
69
|
+
if (closingBraceIndex === -1) {
|
|
70
|
+
console.warn("\u26A0\uFE0F Could not find insertion point in ".concat(modelName, ".router.ts"));
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
beforeInsertion = fileContent.substring(0, closingBraceIndex);
|
|
74
|
+
afterInsertion = fileContent.substring(closingBraceIndex);
|
|
75
|
+
newContent = beforeInsertion + getAllTagsInterface + afterInsertion;
|
|
76
|
+
// 写入文件
|
|
77
|
+
fs.writeFileSync(routerFilePath, newContent, 'utf-8');
|
|
78
|
+
}
|
|
79
|
+
console.log("\u2705 Tags \u529F\u80FD\u751F\u6210\u5B8C\u6210\uFF0C\u5171\u5904\u7406 ".concat(modelsWithTags.length, " \u4E2A\u5305\u542B tags \u5B57\u6BB5\u7684\u6A21\u578B"));
|
|
80
|
+
return [2 /*return*/];
|
|
81
|
+
});
|
|
82
|
+
}); };
|
|
83
|
+
exports.generateTags = generateTags;
|
package/package.json
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wzyjs/next",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.57",
|
|
4
4
|
"description": "description",
|
|
5
5
|
"author": "wzy",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"scripts": {
|
|
8
|
-
"build:plugin": "tsc
|
|
8
|
+
"build:plugin": "tsc",
|
|
9
|
+
"rm": "rm -rf dist",
|
|
9
10
|
"cp1": "cp src/prisma/Base.zmodel dist/Base.zmodel",
|
|
10
|
-
"cp2": "cp src/plugin/crud/useHabitGroupCRUD.ts dist/useHabitGroupCRUD.ts",
|
|
11
|
-
"build": "npm run build:plugin && npm run cp1 && npm run cp2"
|
|
11
|
+
"cp2": "cp src/plugin/crud/useHabitGroupCRUD.ts dist/plugin/crud/useHabitGroupCRUD.ts",
|
|
12
|
+
"build": "npm run rm && npm run build:plugin && npm run cp1 && npm run cp2"
|
|
12
13
|
},
|
|
13
14
|
"files": [
|
|
14
15
|
"dist"
|
|
@@ -16,7 +17,7 @@
|
|
|
16
17
|
"dependencies": {
|
|
17
18
|
"@zenstackhq/sdk": "^2.12.3"
|
|
18
19
|
},
|
|
19
|
-
"gitHead": "
|
|
20
|
+
"gitHead": "7603b720b28050ff484e841ae23e60a330db8956",
|
|
20
21
|
"publishConfig": {
|
|
21
22
|
"access": "public"
|
|
22
23
|
}
|