-
Notifications
You must be signed in to change notification settings - Fork 974
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init dataconnect:sdk and other SDK onboarding improvements (#7299)
* Draft of init dataconnect:sdk * Polishing * Recommend command during dataconnect:sdk:generate * Added changelog * PR fixes * Fixing link
- Loading branch information
Showing
12 changed files
with
222 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
- Moved `dataconnect.location` key in `firebase.json` to `dataconnect.yaml`. | ||
- Fixes issue where files were not properly being discovered and deployed to Firebase Hosting (#7363, #7378) | ||
- Added new command `init dataconnect:sdk`, which interactively configures a generated SDK for a Data Connect connector. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import * as yaml from "yaml"; | ||
import * as fs from "fs-extra"; | ||
import { confirm, promptOnce } from "../../../prompt"; | ||
import * as clc from "colorette"; | ||
import * as path from "path"; | ||
import { readFirebaseJson } from "../../../dataconnect/fileUtils"; | ||
import { Config } from "../../../config"; | ||
import { Setup } from "../.."; | ||
import { load } from "../../../dataconnect/load"; | ||
import { logger } from "../../../logger"; | ||
import { ConnectorInfo, ConnectorYaml, JavascriptSDK, KotlinSDK } from "../../../dataconnect/types"; | ||
import { DataConnectEmulator } from "../../../emulator/dataconnectEmulator"; | ||
|
||
const IOS = "ios"; | ||
const WEB = "web"; | ||
const ANDROID = "android"; | ||
export async function doSetup(setup: Setup, config: Config): Promise<void> { | ||
const serviceCfgs = readFirebaseJson(config); | ||
const serviceInfos = await Promise.all( | ||
serviceCfgs.map((c) => load(setup.projectId || "", path.join(process.cwd(), c.source))), | ||
); | ||
const connectorChoices: { name: string; value: ConnectorInfo }[] = serviceInfos | ||
.map((si) => { | ||
return si.connectorInfo.map((ci) => { | ||
return { | ||
name: `${si.dataConnectYaml.serviceId}/${ci.connectorYaml.connectorId}`, | ||
value: ci, | ||
}; | ||
}); | ||
}) | ||
.flat(); | ||
if (!connectorChoices.length) { | ||
logger.info( | ||
`Your config has no connectors to set up SDKs for. Run ${clc.bold( | ||
"firebase init dataconnect", | ||
)} to set up a service and conenctors.`, | ||
); | ||
return; | ||
} | ||
const connectorInfo: ConnectorInfo = await promptOnce({ | ||
message: "Which connector do you want set up a generated SDK for?", | ||
type: "list", | ||
choices: connectorChoices, | ||
}); | ||
|
||
const platforms = await promptOnce({ | ||
message: "Which platforms do you want to set up a generated SDK for?", | ||
type: "checkbox", | ||
choices: [ | ||
{ name: "iOS (Swift)", value: IOS }, | ||
{ name: "Web (JavaScript)", value: WEB }, | ||
{ name: "Androd (Kotlin)", value: ANDROID }, | ||
], | ||
}); | ||
|
||
const newConnectorYaml = JSON.parse(JSON.stringify(connectorInfo.connectorYaml)) as ConnectorYaml; | ||
if (!newConnectorYaml.generate) { | ||
newConnectorYaml.generate = {}; | ||
} | ||
|
||
if (platforms.includes(IOS)) { | ||
const defaultOutputDir = newConnectorYaml.generate.swiftSdk?.outputDir; | ||
const outputDir = await promptOnce({ | ||
message: `What directory do you want to write your Swift SDK code to? (If not absolute, path will be relative to '${connectorInfo.directory}')`, | ||
type: "input", | ||
default: defaultOutputDir, | ||
}); | ||
const swiftSdk = { outputDir }; | ||
newConnectorYaml.generate.swiftSdk = swiftSdk; | ||
} | ||
if (platforms.includes(WEB)) { | ||
const outputDir = await promptOnce({ | ||
message: `What directory do you want to write your JavaScript SDK code to? (If not absolute, path will be relative to '${connectorInfo.directory}')`, | ||
type: "input", | ||
default: newConnectorYaml.generate.javascriptSdk?.outputDir, | ||
}); | ||
const pkg = await promptOnce({ | ||
message: "What package name do you want to use for your JavaScript SDK?", | ||
type: "input", | ||
default: | ||
newConnectorYaml.generate.javascriptSdk?.package ?? | ||
`@firebasegen/${connectorInfo.connectorYaml.connectorId}`, | ||
}); | ||
const packageJSONDir = await promptOnce({ | ||
message: | ||
"Which directory contains the package.json that you would like to add the JavaScript SDK dependency to? (Leave blank to skip)", | ||
type: "input", | ||
default: newConnectorYaml.generate.javascriptSdk?.packageJSONDir, | ||
}); | ||
// ../.. since we ask relative to connector.yaml | ||
const javascriptSdk: JavascriptSDK = { | ||
outputDir, | ||
package: pkg, | ||
}; | ||
if (packageJSONDir) { | ||
javascriptSdk.packageJSONDir = packageJSONDir; | ||
} | ||
newConnectorYaml.generate.javascriptSdk = javascriptSdk; | ||
} | ||
if (platforms.includes(ANDROID)) { | ||
const outputDir = await promptOnce({ | ||
message: `What directory do you want to write your Kotlin SDK code to? (If not absolute, path will be relative to '${connectorInfo.directory}')`, | ||
type: "input", | ||
default: newConnectorYaml.generate.kotlinSdk?.outputDir, | ||
}); | ||
const pkg = await promptOnce({ | ||
message: "What package name do you want to use for your Kotlin SDK?", | ||
type: "input", | ||
default: | ||
newConnectorYaml.generate.kotlinSdk?.package ?? | ||
`com.google.firebase.dataconnect.connectors.${connectorInfo.connectorYaml.connectorId}`, | ||
}); | ||
const kotlinSdk: KotlinSDK = { | ||
outputDir, | ||
package: pkg, | ||
}; | ||
newConnectorYaml.generate.kotlinSdk = kotlinSdk; | ||
} | ||
// TODO: Prompt user about adding generated paths to .gitignore | ||
const connectorYamlContents = yaml.stringify(newConnectorYaml); | ||
const connectorYamlPath = `${connectorInfo.directory}/connector.yaml`; | ||
fs.writeFileSync(connectorYamlPath, connectorYamlContents, "utf8"); | ||
logger.info(`Wrote new config to ${connectorYamlPath}`); | ||
if ( | ||
setup.projectId && | ||
(await confirm({ | ||
message: "Would you like to generate SDK code now?", | ||
default: true, | ||
})) | ||
) { | ||
await DataConnectEmulator.generate({ | ||
configDir: connectorInfo.directory, | ||
connectorId: connectorInfo.connectorYaml.connectorId, | ||
}); | ||
logger.info(`Generated SDK code for ${connectorInfo.connectorYaml.connectorId}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,3 +64,6 @@ node_modules/ | |
|
||
# dotenv environment variables file | ||
.env | ||
|
||
# dataconnect generated files | ||
.dataconnect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,13 @@ | ||
connectorId: "__connectorId__" | ||
authMode: "PUBLIC" | ||
## ## Here's an example of how to add generated SDKs. | ||
## ## You'll need to replace the outputDirs with ones pointing to where you want the generated code in your app. | ||
# generate: | ||
# javascriptSdk: | ||
# outputDir: <Path where you want the generated SDK to be written to, relative to this file> | ||
# package: "@firebasegen/my-connector" | ||
# packageJSONDir: < Optional. Path to your Javascript app's package.json> | ||
# swiftSdk: | ||
# outputDir: <Path where you want the generated SDK to be written to, relative to this file> | ||
# kotlinSdk: | ||
# outputDir: <Path where you want the generated SDK to be written to, relative to this file> |