@@ -17,6 +17,7 @@ import { getCachedSpecs, getPolyLibPath, writeCachedSpecs } from './utils';
1717import { DEFAULT_POLY_PATH } from './constants' ;
1818import { Specification } from './types' ;
1919import { 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