nsgm-cli 2.0.25 → 2.1.1

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,112 +1,175 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
16
5
  Object.defineProperty(exports, "__esModule", { value: true });
17
- var express_graphql_1 = require("express-graphql");
18
- var graphql_1 = require("graphql");
19
- var fs_1 = __importDefault(require("fs"));
20
- var lodash_1 = __importDefault(require("lodash"));
21
- var path_1 = require("path");
22
- var date_1 = __importDefault(require("./plugins/date"));
23
- var defaultPath = (0, path_1.resolve)(__dirname, './modules/');
24
- var typeDefFileName = 'schema.js';
25
- var resolverFileName = 'resolver.js';
26
- var curFolder = process.cwd();
27
- var outModulesPath = (0, path_1.resolve)(curFolder + '/server/modules/');
28
- var handleOutPlugins = function () {
29
- var result = {};
30
- var outPluginsPath = (0, path_1.resolve)(curFolder + '/server/plugins/');
31
- if (fs_1.default.existsSync(outPluginsPath)) {
32
- var list = fs_1.default.readdirSync(outPluginsPath);
33
- list.forEach(function (item) {
34
- var resolverPath = (0, path_1.resolve)(outPluginsPath + '/' + item);
35
- var stat = fs_1.default.statSync(resolverPath);
36
- var isFile = stat.isFile();
37
- if (isFile) {
38
- result = __assign({}, require(resolverPath));
6
+ const express_graphql_1 = require("express-graphql");
7
+ const graphql_1 = require("graphql");
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const lodash_1 = __importDefault(require("lodash"));
10
+ const path_1 = require("path");
11
+ const date_1 = __importDefault(require("./plugins/date"));
12
+ // 缓存已生成的 schema resolvers
13
+ let cachedSchema = null;
14
+ let cachedResolvers = null;
15
+ const defaultPath = (0, path_1.resolve)(__dirname, './modules/');
16
+ const typeDefFileName = 'schema.js';
17
+ const resolverFileName = 'resolver.js';
18
+ const curFolder = process.cwd();
19
+ const outModulesPath = (0, path_1.resolve)(`${curFolder}/server/modules/`);
20
+ const handleOutPlugins = () => {
21
+ let result = {};
22
+ const outPluginsPath = (0, path_1.resolve)(`${curFolder}/server/plugins/`);
23
+ if (!fs_1.default.existsSync(outPluginsPath)) {
24
+ return result;
25
+ }
26
+ try {
27
+ const list = fs_1.default.readdirSync(outPluginsPath);
28
+ list.forEach((item) => {
29
+ const resolverPath = (0, path_1.resolve)(`${outPluginsPath}/${item}`);
30
+ if (!fs_1.default.existsSync(resolverPath))
31
+ return;
32
+ const stat = fs_1.default.statSync(resolverPath);
33
+ const isFile = stat.isFile();
34
+ if (isFile && (item.endsWith('.js') || item.endsWith('.ts'))) {
35
+ const pluginModule = require(resolverPath);
36
+ result = {
37
+ ...result,
38
+ ...(pluginModule.default || pluginModule)
39
+ };
39
40
  }
40
41
  });
41
42
  }
43
+ catch (error) {
44
+ console.warn('⚠️ Error loading plugins:', error);
45
+ }
42
46
  return result;
43
47
  };
44
48
  function generateTypeDefsAndResolvers() {
45
- var querySchemes = [
46
- "\n _: Boolean\n "
47
- ];
48
- var mutationSchemes = [
49
- "\n _: Boolean\n "
50
- ];
51
- var subscriptionSchemes = [
52
- "\n _: Boolean\n "
53
- ];
54
- var typeSchemes = [];
55
- var resolvers = __assign(__assign({}, date_1.default), handleOutPlugins());
56
- var scalars = lodash_1.default.keys(resolvers);
57
- var scalarStr = '';
58
- lodash_1.default.each(scalars, function (item, index) {
59
- scalarStr += 'scalar ' + item + '\n ';
49
+ // 如果已有缓存且在生产环境,直接返回缓存
50
+ if (process.env.NODE_ENV === 'production' && cachedSchema && cachedResolvers) {
51
+ return {
52
+ schemaStr: cachedSchema,
53
+ resolvers: cachedResolvers
54
+ };
55
+ }
56
+ const querySchemes = ['_: Boolean'];
57
+ const mutationSchemes = ['_: Boolean'];
58
+ const subscriptionSchemes = ['_: Boolean'];
59
+ const typeSchemes = [];
60
+ const resolvers = {
61
+ ...date_1.default,
62
+ ...handleOutPlugins()
63
+ };
64
+ const scalars = lodash_1.default.keys(resolvers);
65
+ let scalarStr = '';
66
+ lodash_1.default.each(scalars, (item) => {
67
+ scalarStr += `scalar ${item}\n `;
60
68
  });
61
69
  // console.log('resolvers', resolvers, _.keys(resolvers), scalarStr)
62
- var _generateAllComponentRecursive = function (path) {
63
- if (path === void 0) { path = defaultPath; }
64
- if (fs_1.default.existsSync(path)) {
65
- var list = fs_1.default.readdirSync(path);
66
- list.forEach(function (item) {
67
- var resolverPath = path + '/' + item;
68
- var stat = fs_1.default.statSync(resolverPath);
69
- var isDir = stat.isDirectory();
70
- var isFile = stat.isFile();
70
+ const _generateAllComponentRecursive = (path = defaultPath) => {
71
+ if (!fs_1.default.existsSync(path))
72
+ return;
73
+ try {
74
+ const list = fs_1.default.readdirSync(path);
75
+ list.forEach((item) => {
76
+ const resolverPath = `${path}/${item}`;
77
+ if (!fs_1.default.existsSync(resolverPath))
78
+ return;
79
+ const stat = fs_1.default.statSync(resolverPath);
80
+ const isDir = stat.isDirectory();
81
+ const isFile = stat.isFile();
71
82
  if (isDir) {
72
83
  _generateAllComponentRecursive(resolverPath);
73
84
  }
74
85
  else if (isFile && item === typeDefFileName) {
75
- // console.log('resolverPath1', resolverPath)
76
- var schemaObj = require(resolverPath);
77
- if (schemaObj.default !== undefined)
78
- schemaObj = schemaObj.default;
79
- var query = schemaObj.query, mutation = schemaObj.mutation, subscription = schemaObj.subscription, type = schemaObj.type;
80
- if (query !== '')
81
- querySchemes.push(query);
82
- if (mutation !== '')
83
- mutationSchemes.push(mutation);
84
- if (subscription !== '')
85
- subscriptionSchemes.push(subscription);
86
- if (type !== '')
87
- typeSchemes.push(type);
86
+ try {
87
+ let schemaObj = require(resolverPath);
88
+ if (schemaObj.default !== undefined)
89
+ schemaObj = schemaObj.default;
90
+ const { query, mutation, subscription, type } = schemaObj;
91
+ if (query && query !== '')
92
+ querySchemes.push(query);
93
+ if (mutation && mutation !== '')
94
+ mutationSchemes.push(mutation);
95
+ if (subscription && subscription !== '')
96
+ subscriptionSchemes.push(subscription);
97
+ if (type && type !== '')
98
+ typeSchemes.push(type);
99
+ }
100
+ catch (error) {
101
+ console.warn(`⚠️ Error loading schema from ${resolverPath}:`, error);
102
+ }
88
103
  }
89
104
  else if (isFile && item === resolverFileName) {
90
- // console.log('resolverPath2', resolverPath)
91
- var resolversObj_1 = require(resolverPath);
92
- if (resolversObj_1.default !== undefined)
93
- resolversObj_1 = resolversObj_1.default;
94
- Object.keys(resolversObj_1).forEach(function (k) {
95
- if (!resolvers[k])
96
- resolvers[k] = {};
97
- resolvers[k] = resolversObj_1[k];
98
- });
105
+ try {
106
+ let resolversObj = require(resolverPath);
107
+ if (resolversObj.default !== undefined)
108
+ resolversObj = resolversObj.default;
109
+ Object.keys(resolversObj).forEach((k) => {
110
+ if (!resolvers[k])
111
+ resolvers[k] = {};
112
+ resolvers[k] = resolversObj[k];
113
+ });
114
+ }
115
+ catch (error) {
116
+ console.warn(`⚠️ Error loading resolver from ${resolverPath}:`, error);
117
+ }
99
118
  }
100
119
  });
101
120
  }
121
+ catch (error) {
122
+ console.warn(`⚠️ Error reading directory ${path}:`, error);
123
+ }
102
124
  };
103
125
  _generateAllComponentRecursive();
104
126
  _generateAllComponentRecursive(outModulesPath);
105
- return { querySchemes: querySchemes, mutationSchemes: mutationSchemes, subscriptionSchemes: subscriptionSchemes, typeSchemes: typeSchemes, resolvers: resolvers, scalarStr: scalarStr };
127
+ const schemaStr = `
128
+ ${scalarStr}
129
+
130
+ type Query {
131
+ ${querySchemes.join('\n')}
132
+ }
133
+
134
+ type Mutation {
135
+ ${mutationSchemes.join('\n')}
136
+ }
137
+
138
+ type Subscription {
139
+ ${subscriptionSchemes.join('\n')}
140
+ }
141
+
142
+ ${typeSchemes.join('\n')}
143
+ `;
144
+ // 在生产环境中缓存结果
145
+ if (process.env.NODE_ENV === 'production') {
146
+ cachedSchema = schemaStr;
147
+ cachedResolvers = resolvers;
148
+ }
149
+ return { querySchemes, mutationSchemes, subscriptionSchemes, typeSchemes, resolvers, scalarStr, schemaStr };
106
150
  }
107
- var _a = generateTypeDefsAndResolvers(), querySchemesV = _a.querySchemes, mutationSchemesV = _a.mutationSchemes, subscriptionSchemesV = _a.subscriptionSchemes, typeSchemesV = _a.typeSchemes, resolversV = _a.resolvers, scalarStrV = _a.scalarStr;
108
- var schemaStr = "\n ".concat(scalarStrV, "\n\n type Query { \n ").concat(querySchemesV.join('\n'), " \n }\n\n type Mutation { \n ").concat(mutationSchemesV.join('\n'), " \n }\n\n type Subscription { \n ").concat(subscriptionSchemesV.join('\n'), " \n }\n\n ").concat(typeSchemesV.join('\n'), "\n");
109
- exports.default = (function (command) {
151
+ const generateResult = generateTypeDefsAndResolvers();
152
+ const { querySchemes: querySchemesV, mutationSchemes: mutationSchemesV, subscriptionSchemes: subscriptionSchemesV, typeSchemes: typeSchemesV, resolvers: resolversV, scalarStr: scalarStrV, schemaStr: generatedSchemaStr } = generateResult;
153
+ // 使用生成的 schema 或构建新的 schema
154
+ const schemaStr = generatedSchemaStr ||
155
+ `
156
+ ${scalarStrV}
157
+
158
+ type Query {
159
+ ${querySchemesV?.join('\n') || '_: Boolean'}
160
+ }
161
+
162
+ type Mutation {
163
+ ${mutationSchemesV?.join('\n') || '_: Boolean'}
164
+ }
165
+
166
+ type Subscription {
167
+ ${subscriptionSchemesV?.join('\n') || '_: Boolean'}
168
+ }
169
+
170
+ ${typeSchemesV?.join('\n') || ''}
171
+ `;
172
+ exports.default = (command) => {
110
173
  if (command === 'dev') {
111
174
  console.log('schemaStr', schemaStr);
112
175
  console.log('resolvers', resolversV);
@@ -116,4 +179,4 @@ exports.default = (function (command) {
116
179
  rootValue: resolversV,
117
180
  graphiql: true
118
181
  });
119
- });
182
+ };
@@ -3,32 +3,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- var moment_1 = __importDefault(require("moment"));
7
- var language_1 = require("graphql/language");
8
- var graphql_1 = require("graphql");
9
- var customScalarDate = new graphql_1.GraphQLScalarType({
6
+ const dayjs_1 = __importDefault(require("dayjs"));
7
+ const language_1 = require("graphql/language");
8
+ const graphql_1 = require("graphql");
9
+ const customScalarDate = new graphql_1.GraphQLScalarType({
10
10
  name: 'Date',
11
11
  description: 'Date custom scalar type',
12
- parseValue: function (value) {
13
- var date = (0, moment_1.default)(value);
12
+ parseValue: (value) => {
13
+ const date = (0, dayjs_1.default)(value);
14
14
  if (!date.isValid()) {
15
15
  throw new Error('Invalid date format');
16
16
  }
17
17
  return date.valueOf(); // Ensure this returns a number
18
18
  },
19
- serialize: function (value) {
20
- var date = (0, moment_1.default)(value);
19
+ serialize: (value) => {
20
+ const date = (0, dayjs_1.default)(value);
21
21
  if (!date.isValid()) {
22
22
  throw new Error('Invalid date format');
23
23
  }
24
24
  return date.format('YYYY-MM-DD HH:mm:ss:SSS'); // Ensure this returns a string
25
25
  },
26
- parseLiteral: function (ast) {
26
+ parseLiteral: (ast) => {
27
27
  if (ast.kind === language_1.Kind.INT) {
28
28
  return parseInt(ast.value, 10);
29
29
  }
30
30
  else if (ast.kind === language_1.Kind.STRING) {
31
- var date = (0, moment_1.default)(ast.value);
31
+ const date = (0, dayjs_1.default)(ast.value);
32
32
  if (!date.isValid()) {
33
33
  throw new Error('Invalid date format');
34
34
  }
@@ -0,0 +1,9 @@
1
+ export declare const graphqlCacheMiddleware: (req: any, res: any, next: any) => any;
2
+ export declare const clearGraphQLCache: () => void;
3
+ export declare const getCacheStats: () => {
4
+ size: number;
5
+ entries: {
6
+ query: string;
7
+ age: number;
8
+ }[];
9
+ };
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCacheStats = exports.clearGraphQLCache = exports.graphqlCacheMiddleware = void 0;
4
+ // GraphQL 查询缓存中间件
5
+ const queryCache = new Map();
6
+ const CACHE_TTL = 5 * 60 * 1000; // 5分钟
7
+ const graphqlCacheMiddleware = (req, res, next) => {
8
+ const { query, variables } = req.body || {};
9
+ // 只缓存查询操作,不缓存变更操作
10
+ if (!query || query.trim().startsWith('mutation')) {
11
+ return next();
12
+ }
13
+ const cacheKey = JSON.stringify({ query, variables });
14
+ const now = Date.now();
15
+ // 检查缓存
16
+ const cached = queryCache.get(cacheKey);
17
+ if (cached && now - cached.timestamp < CACHE_TTL) {
18
+ console.log('🚀 GraphQL cache hit for:', `${query.substring(0, 50)}...`);
19
+ return res.json(cached.data);
20
+ }
21
+ // 拦截响应以存储缓存
22
+ const originalSend = res.send;
23
+ res.send = function (body) {
24
+ try {
25
+ const data = typeof body === 'string' ? JSON.parse(body) : body;
26
+ // 只缓存成功的查询结果
27
+ if (!data.errors) {
28
+ queryCache.set(cacheKey, {
29
+ data,
30
+ timestamp: now
31
+ });
32
+ // 清理过期缓存
33
+ cleanExpiredCache();
34
+ }
35
+ }
36
+ catch (error) {
37
+ console.warn('⚠️ GraphQL cache error:', error);
38
+ }
39
+ originalSend.call(this, body);
40
+ };
41
+ next();
42
+ };
43
+ exports.graphqlCacheMiddleware = graphqlCacheMiddleware;
44
+ // 清理过期缓存
45
+ function cleanExpiredCache() {
46
+ const now = Date.now();
47
+ for (const [key, entry] of queryCache.entries()) {
48
+ if (now - entry.timestamp > CACHE_TTL) {
49
+ queryCache.delete(key);
50
+ }
51
+ }
52
+ }
53
+ // 清空缓存的函数
54
+ const clearGraphQLCache = () => {
55
+ queryCache.clear();
56
+ console.log('🧹 GraphQL cache cleared');
57
+ };
58
+ exports.clearGraphQLCache = clearGraphQLCache;
59
+ // 获取缓存统计
60
+ const getCacheStats = () => {
61
+ return {
62
+ size: queryCache.size,
63
+ entries: Array.from(queryCache.keys()).map((key) => ({
64
+ query: `${key.substring(0, 100)}...`,
65
+ age: Date.now() - queryCache.get(key).timestamp
66
+ }))
67
+ };
68
+ };
69
+ exports.getCacheStats = getCacheStats;