Skip to content

Commit 3eeaf5a

Browse files
committed
Fixed parsing of dependencies where original name has underscores and adding argument so users can skip dependency parsing altogether
1 parent d0a8721 commit 3eeaf5a

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polyapi",
3-
"version": "0.25.7",
3+
"version": "0.25.8",
44
"description": "Poly is a CLI tool to help create and manage your Poly definitions.",
55
"license": "MIT",
66
"repository": {

src/cli.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ void yargs
245245
.option('visibility', {
246246
describe: 'Specifies the visibility of a function. Options: PUBLIC, TENANT, ENVIRONMENT. Case insensitive',
247247
type: 'string',
248+
})
249+
.option('ignore-dependencies', {
250+
describe: 'Skip parsing internal and external dependencies referenced within function. It\'s best not to use this option if you can help it.',
251+
type: 'boolean',
248252
}),
249253
async ({
250254
name,
@@ -258,6 +262,7 @@ void yargs
258262
executionApiKey,
259263
cachePolyLibrary,
260264
visibility,
265+
ignoreDependencies,
261266
}) => {
262267
const logsEnabled =
263268
logs === 'enabled' ? true : logs === 'disabled' ? false : undefined;
@@ -304,6 +309,7 @@ void yargs
304309
executionApiKey,
305310
cachePolyLibrary,
306311
visibility,
312+
ignoreDependencies,
307313
);
308314
},
309315
);

src/commands/function.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const addOrUpdateCustomFunction = async (
3333
executionApiKey: string | null | undefined,
3434
cachePolyLibrary: boolean | undefined,
3535
visibility: string | undefined,
36+
ignoreDependencies: boolean | undefined
3637
) => {
3738
loadConfig(polyPath);
3839

@@ -79,7 +80,7 @@ export const addOrUpdateCustomFunction = async (
7980
}
8081

8182
const typeSchemas = generateTypeSchemas(file, DeployableTypeEntries.map(d => d[0]), name);
82-
const [externalDependencies, internalDependencies] = await getDependencies(code, file, tsConfigBaseUrl);
83+
const [externalDependencies, internalDependencies] = ignoreDependencies ? [undefined, undefined] : await getDependencies(code, file, tsConfigBaseUrl);
8384

8485
if (server) {
8586
shell.echo(

src/transpiler.ts

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { getCachedSpecs, getPolyLibPath, writeCachedSpecs } from './utils';
1717
import { DEFAULT_POLY_PATH } from './constants';
1818
import { Specification } from './types';
1919
import { getSpecs } from './api';
20+
import { toPascalCase } from '@guanghechen/helper-string';
2021

2122
// NodeJS built-in libraries + polyapi
2223
// https://www.w3schools.com/nodejs/ref_modules.asp
@@ -94,7 +95,7 @@ export const getDependencies = async (
9495
let polyImportIdentifier: string | null = null;
9596
let variImportIdentifier: string | null = null;
9697
let tabiImportIdentifier: string | null = null;
97-
let schemasImportIdentifier: string | null = null;
98+
let schemasImportIdentifier = 'schemas.';
9899
const otherImportIdentifiers: string[] = [];
99100
let lookForInternalDependencies = false;
100101
// Users can alias references to parts of the poly tree by assigning to a variable
@@ -233,7 +234,7 @@ export const getDependencies = async (
233234
(tabiImportIdentifier && path.startsWith(tabiImportIdentifier)) ||
234235
// Capture other top-level namespace reference aliases
235236
(otherImportIdentifiers.length && otherImportIdentifiers.some(other => path.startsWith(other)))
236-
) {
237+
) {
237238
if (node.name && ts.isIdentifier(node.name)) {
238239
aliasMap.set(node.name.text, path);
239240
return node; // Don't recurse into assignment, just move on
@@ -249,7 +250,7 @@ export const getDependencies = async (
249250
basePath = initializer.text;
250251
}
251252
if (!basePath) return node;
252-
253+
253254
for (const element of node.name.elements) {
254255
if (!ts.isBindingElement(element)) continue;
255256

@@ -262,8 +263,8 @@ export const getDependencies = async (
262263
const root = path.split('.')[0];
263264
// Check for alias to handle case where we're destructuring something from an aliased import
264265
if (aliasMap.has(root)) {
265-
const aliasBase = aliasMap.get(root);
266-
path = aliasBase.split(".").concat(path.split('.').slice(1)).join('.');
266+
const aliasBase = aliasMap.get(root);
267+
path = aliasBase.split(".").concat(path.split('.').slice(1)).join('.');
267268
}
268269
if (
269270
(polyImportIdentifier && path.startsWith(polyImportIdentifier)) ||
@@ -288,15 +289,15 @@ export const getDependencies = async (
288289
}
289290
// Capture poly references (all function types, webhooks, and subscriptions)
290291
if (polyImportIdentifier && path.startsWith(polyImportIdentifier)) {
291-
internalReferences.add(path.replace(polyImportIdentifier, ''));
292+
internalReferences.add(path);
292293
}
293294
// Capture vari references
294295
else if (variImportIdentifier && path.startsWith(variImportIdentifier)) {
295-
internalReferences.add(path.replace(VariMethods, '').replace(variImportIdentifier, ''));
296+
internalReferences.add(path.replace(VariMethods, ''));
296297
}
297298
// Capture tabi references
298299
else if (tabiImportIdentifier && path.startsWith(tabiImportIdentifier)) {
299-
internalReferences.add(path.replace(TabiMethods, '').replace(tabiImportIdentifier, ''));
300+
internalReferences.add(path.replace(TabiMethods, ''));
300301
}
301302
// Capture other top-level namespace references
302303
else if (otherImportIdentifiers.length) {
@@ -310,7 +311,7 @@ export const getDependencies = async (
310311
if (schemasImportIdentifier && ts.isTypeReferenceNode(node)) {
311312
const path = flattenTypeName(node.typeName);
312313
if (path.startsWith(schemasImportIdentifier)) {
313-
internalReferences.add(path.replace(schemasImportIdentifier, ''));
314+
internalReferences.add(path);
314315
return node;
315316
}
316317
}
@@ -343,7 +344,7 @@ export const getDependencies = async (
343344
} catch (error) {
344345
shell.echo(
345346
chalk.yellow('\nWarning:'),
346-
'Failed to parse package.json file in order to read dependencies, there could be issues with some dependencies at the time of deploying the server function.',
347+
'Failed to parse package.json file in order to read dependencies, there could be issues with some dependencies at the time of deploying the server function. Rerun command with \'--ignore-dependencies\' to skip parsing dependencies.',
347348
);
348349
}
349350

@@ -387,8 +388,30 @@ export const getDependencies = async (
387388
let missing: string[] = [];
388389

389390
const findReferencedSpecs = (toFind: string[] | Set<string>) => {
390-
for (const path of internalReferences) {
391-
const spec = specs.find(s => s.contextName.toLowerCase() === path.toLowerCase());
391+
for (let path of toFind) {
392+
let type;
393+
if (path.startsWith(polyImportIdentifier)) {
394+
path = path.replace(polyImportIdentifier, '');
395+
} else if (path.startsWith(variImportIdentifier)) {
396+
type = "serverVariable";
397+
path = path.replace(variImportIdentifier, '');
398+
} else if (path.startsWith(tabiImportIdentifier)) {
399+
type = "table";
400+
path = path.replace(tabiImportIdentifier, '');
401+
} else if (path.startsWith(schemasImportIdentifier)) {
402+
type = "schema";
403+
path = path.replace(schemasImportIdentifier, '');
404+
}
405+
const spec = specs.find(s => {
406+
if (type) {
407+
if (type !== s.type) return false;
408+
} else if (['serverVariable', 'table', 'schema'].includes(s.type)) return false;
409+
if (s.type === 'schema') {
410+
// Schema names are munged too much to just compare by lowercase
411+
return s.contextName.split('.').map(s => toPascalCase(s)).join('.') === path;
412+
}
413+
return s.contextName.toLowerCase() === path.toLowerCase();
414+
});
392415
if (spec) {
393416
found.push(spec);
394417
let type: string = spec.type;
@@ -420,7 +443,7 @@ export const getDependencies = async (
420443
}
421444

422445
if (missing.length) {
423-
throw new Error(`Cannot resolve all poly resources referenced within function.\nMissing:\n${missing.map(n => `'${n}'`).join('\n')}`)
446+
throw new Error(`Cannot resolve all poly resources referenced within function.\n\nMissing:\n${missing.map(n => ` '${n}'`).join('\n')}\n\nRerun command with '--ignore-dependencies' to skip resolving dependencies.`)
424447
}
425448
}
426449

0 commit comments

Comments
 (0)