import * as fs from 'fs-extra' ; import * as path from 'path' ; import * as os from 'os' ; import { uniq } from 'ramda' ; import * as Serverless from 'serverless' ; import * as matchAll from 'string.prototype.matchall' ; import { JSONObject } from './types' ; export function extractFileNames ( cwd , provider , functions ? ) { if ( provider === 'google' ) { const packageFilePath = path . join ( cwd , 'package.json' ) ; if ( fs . existsSync ( packageFilePath ) ) { const packageFile = JSON . parse ( fs . readFileSync ( packageFilePath ) . toString ( ) ) ; const entry = packageFile . main ? packageFile . main . replace ( /\.js$/ , '.ts' ) : 'index.ts' ; if ( ! fs . existsSync ( path . join ( cwd , entry ) ) ) { console . log ( `Cannot locate entrypoint, ${ entry } not found` ) ; throw new Error ( 'Compilation failed' ) ; } return [ { entry , func : null } ] ; } } return Object . keys ( functions ) . map ( ( functionAlias ) => { const func = functions [ functionAlias ] ; const h = func . handler ; const fnName = path . extname ( h ) ; const fnNameLastAppearanceIndex = h . lastIndexOf ( fnName ) ; const fileName = h . substring ( 0 , fnNameLastAppearanceIndex ) ; const extensions = [ '.ts' , '.js' ] ; for ( const extension of extensions ) { if ( fs . existsSync ( path . join ( cwd , fileName + extension ) ) ) { const entry = path . relative ( cwd , fileName + extension ) ; return { entry : os . platform ( ) === 'win32' ? entry . replace ( /\\/g , '/' ) : entry , func , functionAlias , } ; } } console . log ( `Cannot locate handler - ${ fileName } not found` ) ; throw new Error ( 'Compilation failed. Please ensure you have an index file with ext .ts or .js, or have a path listed as main key in package.json' ) ; } ) ; } param deps param filter export const flatDep = ( deps , filter ? , originalObject ? ) => { if ( ! deps ) return [ ] ; if ( ! originalObject ) originalObject = deps ; return Object . entries ( deps ) . reduce ( ( acc , [ depName , details ] ) => { if ( filter && ! filter . includes ( depName ) ) return acc ; const detailsDeps = originalObject [ depName ] ?. dependencies || ( details as JSONObject ) . dependencies ; return uniq ( [ ... acc , depName , ... flatDep ( detailsDeps , undefined , originalObject ) ] ) ; } , [ ] ) ; } ; param bundlePath export const getDepsFromBundle = ( bundlePath ) => { const bundleContent = fs . readFileSync ( bundlePath , 'utf8' ) ; const requireMatch = matchAll ( bundleContent , /require\("(.*?)"\)/gim ) ; return uniq ( Array . from ( requireMatch ) . map ( ( match ) => match [ 1 ] ) ) ; } ; export const doSharePath = ( child , parent ) => { if ( child === parent ) return true ; const parentTokens = parent . split ( '/' ) ; const childToken = child . split ( '/' ) ; return parentTokens . every ( ( t , i ) => childToken [ i ] === t ) ; } ; export const providerRuntimeMatcher = Object . freeze ( { aws : { 'nodejs14.x' : 'node14' , 'nodejs12.x' : 'node12' , 'nodejs10.x' : 'node10' , } , } ) ;