Skip to content

Commit

Permalink
Custom SDK Agents + global sw.config.js (#605)
Browse files Browse the repository at this point in the history
* change logic for defining sdk agents, add global swConfig file

* add changeset

* change name
  • Loading branch information
framini authored Aug 11, 2022
1 parent 5402ffc commit 2f909c9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
13 changes: 13 additions & 0 deletions .changeset/serious-bananas-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
'@sw-internal/build': patch
'@signalwire/compatibility-api': patch
'@signalwire/core': patch
'@signalwire/js': patch
'@signalwire/node': patch
'@signalwire/react-native': patch
'@signalwire/realtime-api': patch
'@signalwire/web-api': patch
'@signalwire/webrtc': patch
---

[internal] change how the SDK agent is defined
32 changes: 29 additions & 3 deletions scripts/sw-build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import license from 'rollup-plugin-license'
import replace from '@rollup/plugin-replace'
import { visualizer } from 'rollup-plugin-visualizer'
import typescript from 'rollup-plugin-typescript2'
import swConfig from '../../sw.config.js'

const COMMON_NODE = {
entryPoints: ['./src/index.ts'],
Expand Down Expand Up @@ -122,9 +123,29 @@ const getWatchFormatFlag = (flags) => {
return flagWatchFormat.split('=')[1]
}
const getPackageAgentName = (pkgJson) => {
const name = pkgJson.agent || pkgJson.name
const pkgName = pkgJson.name
const agentName = swConfig.agents.byName[pkgName]

return `${name}/${pkgJson.version}`
/**
* Only private packages or utility packages are allowed
* to not have an agent.
*/
const requiresAgent =
!pkgJson.private && !swConfig.utilityPackages.includes(pkgName)

if (!requiresAgent) {
return ''
}

/**
* We'll break the build if we detect that a public
* package doesn't have an agent name defined.
*/
if (!agentName) {
throw new Error(`[getPackageAgentName] Missing agent name for ${pkgName}`)
}

return `@signalwire/${agentName}/${pkgJson.version}`
}
const getBuildOptions = ({ flags, pkgJson }) => {
const optionsFlags = flags.filter(
Expand All @@ -145,7 +166,12 @@ const getBuildOptions = ({ flags, pkgJson }) => {
const sdkEnvVariables = {
'process.env.SDK_PKG_NAME': JSON.stringify(pkgJson.name),
'process.env.SDK_PKG_DESCRIPTION': JSON.stringify(pkgJson.description),
'process.env.SDK_PKG_AGENT': JSON.stringify(getPackageAgentName(pkgJson)),
}
const sdkPackageAgent = getPackageAgentName(pkgJson)
if (sdkPackageAgent) {
sdkEnvVariables['process.env.SDK_PKG_AGENT'] = JSON.stringify(
getPackageAgentName(pkgJson)
)
}

/**
Expand Down
14 changes: 14 additions & 0 deletions sw.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
agents: {
// Maps packages to agent-name. Take into account that
// these names are just a part within the fully
// qualified agent sent to the server:
// @signalwire/<platform>/<sdk-name>/<x.y.x>
byName: {
'@signalwire/realtime-api': 'nodejs/realtime-api',
'@signalwire/js': 'js/browser',
'@signalwire/compatibility-api': 'nodejs/compatibility-api',
},
},
utilityPackages: ['@signalwire/core', '@signalwire/webrtc'],
}

0 comments on commit 2f909c9

Please sign in to comment.