datagrok-tools 4.14.66 → 4.14.68

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.
File without changes
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.stressTests = stressTests;
8
+ var _fs = _interopRequireDefault(require("fs"));
9
+ var _path = _interopRequireDefault(require("path"));
10
+ var utils = _interopRequireWildcard(require("../utils/utils"));
11
+ var _testUtils = _interopRequireWildcard(require("../utils/test-utils"));
12
+ var testUtils = _testUtils;
13
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
14
+ const cwd = process.cwd();
15
+ async function stressTests(args) {
16
+ const pkgPath = _path.default.join(cwd, 'package.json');
17
+ if (!_fs.default.existsSync(pkgPath)) {
18
+ console.error('❌ Error: This command must be executed from the ApiTests package folder (package.json not found).');
19
+ process.exit(1);
20
+ }
21
+ let pkg;
22
+ try {
23
+ pkg = JSON.parse(_fs.default.readFileSync(pkgPath, 'utf8'));
24
+ } catch (e) {
25
+ console.error('❌ Error: Failed to read package.json. Make sure it is valid JSON.');
26
+ process.exit(1);
27
+ }
28
+ if (pkg.name !== '@datagrok/api-tests') {
29
+ console.error(`❌ Error: This command must be executed from the ApiTests package folder. Found package name: '${pkg.name ?? 'undefined'}'`);
30
+ process.exit(1);
31
+ }
32
+ const config = (0, _testUtils.getDevKey)(args.host);
33
+ await testUtils.loadPackage('', 'ApiTests', args.host, args['skip-publish'], args['skip-build']);
34
+ await utils.runScript(`npm run build-node`, '');
35
+ try {
36
+ await utils.runScript(`node -r ./tsconfig-paths-bootstrap.js dist-node/package-test-node.js --apiUrl=${config.url} --devKey=${config.key} --concurrentRuns=${args["concurrent-runs"] ?? 1}${args.loop ? ' --loop' : ''}`, '');
37
+ return true;
38
+ } catch (e) {
39
+ console.error(`❌ Error: Something went wrong: ${e}`);
40
+ return false;
41
+ }
42
+ }
package/bin/grok.js CHANGED
@@ -16,6 +16,7 @@ const commands = {
16
16
  publish: require('./commands/publish').publish,
17
17
  test: require('./commands/test').test,
18
18
  testall: require('./commands/test-all').testAll,
19
+ stresstest: require('./commands/stress-tests').stressTests
19
20
  };
20
21
 
21
22
  const onPackageCommandNames = ['api', 'check', 'link', 'publish', 'test'];
@@ -136,7 +136,13 @@ function getFuncAnnotation(data, comment = '//', sep = '\n') {
136
136
  }
137
137
  for (const input of data.inputs ?? []) {
138
138
  if (!input) continue;
139
- let type = input?.type;
139
+ let type = input?.type?.replaceAll(" ", "");
140
+ if (type?.includes(`|undefined`)) {
141
+ type = type.replaceAll(`|undefined`, '');
142
+ // modify original payload
143
+ input.optional = true;
144
+ input.type = type;
145
+ }
140
146
  let isArray = false;
141
147
  if (type?.includes(`[]`)) {
142
148
  type = type.replace(/\[\]$/, '');
@@ -470,8 +476,19 @@ const primitives = exports.primitives = new Set(['string', 'string[]', 'number',
470
476
 
471
477
  /** Generates a DG function. */
472
478
  function generateFunc(annotation, funcName, sep = '\n', className = '', inputs = [], output, isAsync = false) {
473
- // eslint-disable-next-line max-len
474
- const funcSigNature = inputs.map(e => `${e.name}${e.optional ? '?' : ''}: ${primitives.has(e.type ?? '') && !typesToAny.includes(e.type ?? '') ? e.type : typesToAnnotation[e.type?.replace('[]', '') ?? ''] && !typesToAny.includes(e.type ?? '') ? e.type : 'any'}`).join(', ');
479
+ // FuncCall can have an optional followed by mandatory args for ui reasons, typescript cannot
480
+ let firstTsValidOptionalIdx = inputs.length - 1;
481
+ for (; firstTsValidOptionalIdx >= 0; firstTsValidOptionalIdx--) {
482
+ const e = inputs[firstTsValidOptionalIdx];
483
+ if (!e.optional) break;
484
+ }
485
+ const funcSigNature = inputs.map((e, idx) => {
486
+ const namePart = `${e.name}${e.optional && idx >= firstTsValidOptionalIdx ? '?' : ''}`;
487
+ // eslint-disable-next-line max-len
488
+ let typePart = `${primitives.has(e.type ?? '') && !typesToAny.includes(e.type ?? '') ? e.type : typesToAnnotation[e.type?.replace('[]', '') ?? ''] && !typesToAny.includes(e.type ?? '') ? e.type : 'any'}`;
489
+ if (e.optional && idx < firstTsValidOptionalIdx) typePart += ' | undefined';
490
+ return `${namePart}: ${typePart}`;
491
+ }).join(', ');
475
492
  const funcArguments = inputs.map(e => e.name).join(', ');
476
493
  const returnType = output ? primitives.has(output) ? !isAsync ? `: ${output} ` : `: Promise<${output}> ` : !isAsync ? `: any ` : `: Promise<any> ` : '';
477
494
  // eslint-disable-next-line max-len
@@ -14,6 +14,7 @@ exports.getBrowserPage = getBrowserPage;
14
14
  exports.getDevKey = getDevKey;
15
15
  exports.getToken = getToken;
16
16
  exports.getWebUrl = getWebUrl;
17
+ exports.loadPackage = loadPackage;
17
18
  exports.loadPackages = loadPackages;
18
19
  exports.loadTestsList = loadTestsList;
19
20
  exports.mergeBrowsersResults = mergeBrowsersResults;
@@ -181,6 +182,20 @@ const recorderConfig = exports.recorderConfig = {
181
182
  }
182
183
  // aspectRatio: '16:9',
183
184
  };
185
+ async function loadPackage(packageDir, dirName, hostString, skipPublish, skipBuild, linkPackage, release) {
186
+ try {
187
+ if (skipPublish != true) {
188
+ process.stdout.write(`Building and publishing ${dirName}...`);
189
+ await utils.runScript(`npm install`, packageDir);
190
+ if (linkPackage) await utils.runScript(`grok link`, packageDir);
191
+ if (skipBuild != true) await utils.runScript(`npm run build`, packageDir);
192
+ await utils.runScript(`grok publish ${hostString}${release ? ' --release' : ''}`, packageDir);
193
+ process.stdout.write(` success!\n`);
194
+ }
195
+ } catch (e) {
196
+ process.stdout.write(` failed to load package ${dirName}!\n`);
197
+ }
198
+ }
184
199
  async function loadPackages(packagesDir, packagesToLoad, host, skipPublish, skipBuild, linkPackage, release) {
185
200
  const packagesToRun = new Map();
186
201
  const hostString = host === undefined ? `` : `${host}`;
@@ -198,19 +213,8 @@ async function loadPackages(packagesDir, packagesToLoad, host, skipPublish, skip
198
213
  }));
199
214
  const packageFriendlyName = packagesToRun.get((0, _utils.spaceToCamelCase)(packageJsonData['friendlyName'] ?? packageJsonData['name'].split('/')[1] ?? packageJsonData['name'] ?? '').toLocaleLowerCase() ?? '') ?? packagesToRun.get(dirName);
200
215
  if (utils.isPackageDir(packageDir) && (packageFriendlyName !== undefined || packagesToLoad === 'all')) {
201
- try {
202
- process.stdout.write(`Building and publishing ${dirName}...`);
203
- if (skipPublish != true) {
204
- await utils.runScript(`npm install`, packageDir);
205
- if (linkPackage) await utils.runScript(`grok link`, packageDir);
206
- if (skipBuild != true) await utils.runScript(`npm run build`, packageDir);
207
- await utils.runScript(`grok publish ${hostString}${release ? ' --release' : ''}`, packageDir);
208
- }
209
- packagesToRun.set(dirName, true);
210
- process.stdout.write(` success!\n`);
211
- } catch (e) {
212
- process.stdout.write(` fail!\n`);
213
- }
216
+ await loadPackage(packageDir, dirName, hostString, skipPublish, skipBuild, linkPackage, release);
217
+ packagesToRun.set(dirName, true);
214
218
  }
215
219
  } catch (e) {
216
220
  if (utils.isPackageDir(packageDir) && (packagesToRun.get((0, _utils.spaceToCamelCase)(dirName).toLocaleLowerCase()) !== undefined || packagesToLoad === 'all')) console.log(`Couldn't read package.json ${dirName}`);
@@ -110,7 +110,7 @@ const replacers = exports.replacers = {
110
110
  FUNC_NAME_LOWERCASE: (s, name) => s.replace(/#{FUNC_NAME_LOWERCASE}/g, friendlyNameToName(name, false)),
111
111
  PARAMS_OBJECT: (s, params) => s.replace(/#{PARAMS_OBJECT}/g, params.length ? `{ ${params.map(p => p.name).join(', ')} }` : `{}`),
112
112
  OUTPUT_TYPE: (s, type) => s.replace(/#{OUTPUT_TYPE}/g, type),
113
- TYPED_PARAMS: (s, params) => s.replace(/#{TYPED_PARAMS}/g, params.map(p => `${p.name}${p.isOptional ? '?' : ''}: ${p.type} ${p.nullable ? '| null' : ''}`).join(', '))
113
+ TYPED_PARAMS: (s, params) => s.replace(/#{TYPED_PARAMS}/g, params.map(p => `${p.name}${p.isOptional ? '?' : ''}: ${p.type} ${p.undefinable ? '| undefined' : ''}${p.nullable ? '| null' : ''}`).join(', '))
114
114
  };
115
115
  class TemplateBuilder {
116
116
  static sep = '\n';
@@ -219,9 +219,18 @@ function getScriptOutputType(script, comment = '#') {
219
219
  ;
220
220
  function getScriptInputs(script, comment = '#') {
221
221
  const regex = new RegExp(`${comment}\\s*input:\\s?([a-z_]+)(?:<[^>]*>)?\\s+(\\w+)(?:[^{\\n]*{[^}\\n]*})?`, 'g');
222
+ const testOptional = inputAnnotation => /isOptional\s*:\s*true/.test(inputAnnotation) || /optional\s*:\s*true/.test(inputAnnotation);
223
+ const inputAnnotations = [...script.matchAll(regex)];
224
+ let firstTsValidOptionalIdx = inputAnnotations.length - 1;
225
+ for (; firstTsValidOptionalIdx >= 0; firstTsValidOptionalIdx--) {
226
+ const annotation = inputAnnotations[firstTsValidOptionalIdx][0];
227
+ if (!testOptional(annotation)) break;
228
+ }
222
229
  const inputs = [];
223
- for (const match of script.matchAll(regex)) {
224
- const isOptional = /isOptional\s*:\s*true/.test(match[0]) || /optional\s*:\s*true/.test(match[0]);
230
+ for (const [idx, match] of inputAnnotations.entries()) {
231
+ const hasOptionalAnnotation = testOptional(match[0]);
232
+ const isOptional = hasOptionalAnnotation && idx >= firstTsValidOptionalIdx;
233
+ const undefinable = hasOptionalAnnotation && idx < firstTsValidOptionalIdx;
225
234
  const nullable = /nullable\s*:\s*true/.test(match[0]);
226
235
  const type = dgToTsTypeMap[match[1]] || 'any';
227
236
  const name = match[2];
@@ -229,6 +238,7 @@ function getScriptInputs(script, comment = '#') {
229
238
  type,
230
239
  name,
231
240
  isOptional,
241
+ undefinable,
232
242
  nullable
233
243
  });
234
244
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datagrok-tools",
3
- "version": "4.14.66",
3
+ "version": "4.14.68",
4
4
  "description": "Utility to upload and publish packages to Datagrok",
5
5
  "homepage": "https://github.com/datagrok-ai/public/tree/master/tools#readme",
6
6
  "dependencies": {