import { Serverless , ServerlessOptions } from "./types" ; import { PROVIDER_NAME } from "./constants" ; import TencentProvider from "./Provider" ; const logPrefix = `(${ PROVIDER_NAME }) ` ; function guid ( ) { function s4 ( ) { return Math . floor ( ( 1 + Math . random ( ) ) * 0x10000 ) . toString ( 16 ) . substring ( 1 ) ; } return ( s4 ( ) + s4 ( ) + "-" + s4 ( ) + "-" + s4 ( ) + "-" + s4 ( ) + "-" + s4 ( ) + s4 ( ) + s4 ( ) ) ; } const jsonBoundary = process . env . JSON_BOUNDARY || guid ( ) ; export default abstract class Plugin { private hasJsonOutput = false ; constructor ( public serverless , public options ) { } get provider ( ) : TencentProvider { return this . serverless . getProvider ( PROVIDER_NAME ) ; } log ( str ) { return this . serverless . cli . log ( logPrefix + str ) ; } debug ( str ) { if ( process . env . SLS_DEBUG ) { return this . serverless . cli . log ( logPrefix + "DEBUG " + str ) ; } } get jsonBoundary ( ) { return jsonBoundary ; } param data json ( data ) { if ( ! process . env . JSON_OUTPUT ) { return ; } if ( ! this . hasJsonOutput ) { this . hasJsonOutput = true ; this . log ( `jsonBoundary(${ this . jsonBoundary })` ) ; } return this . log ( `${ this . jsonBoundary }(${ JSON . stringify ( data ) })${ this . jsonBoundary }` ) ; } makeError ( err ) { if ( err instanceof Error ) { return err ; } const wrap = new this . serverless . classes . Error ( ) ; Object . assign ( wrap , err , { message : ( err && err . message ) || JSON . stringify ( err ) } ) ; return wrap ; } }