Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added data connect to schematics #3638

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"test:chrome-headless": "npx --yes firebase-tools@latest emulators:exec --project=demo-123 \"ng test --watch=false --browsers=ChromeHeadless\"",
"test:firefox-headless": "npx --yes firebase-tools@latest emulators:exec --project=demo-123 \"ng test --watch=false --browsers=FirefoxHeadless\"",
"lint": "ng lint",
"lint:fix": "ng lint --fix",
"test:node": "node -r tsconfig-paths/register ./dist/out-tsc/jasmine/tools/jasmine.mjs --input-type=commonjs",
"test:node-esm": "node -r tsconfig-paths/register ./dist/out-tsc/jasmine/tools/jasmine.mjs",
"test:typings": "node ./tools/run-typings-test.js",
Expand Down Expand Up @@ -116,7 +117,8 @@
"ts-patch": "^3.2.1",
"ts-transformer-keys": "^0.4.4",
"tslint": "~6.1.0",
"typescript": ">=5.5 <5.7"
"typescript": ">=5.5 <5.7",
"yaml": "^2.7.0"
},
"typings": "index.d.ts"
}
3 changes: 3 additions & 0 deletions src/schematics/deploy/actions.jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const initMocks = () => {
create: () => Promise.reject(),
}
},
init() {
return Promise.resolve()
},
deploy: (_: FirebaseDeployConfig) => Promise.resolve(),
use: () => Promise.resolve(),
logger: {
Expand Down
42 changes: 41 additions & 1 deletion src/schematics/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const featureOptions = [
{ name: 'App Check', value: FEATURES.AppCheck },
{ name: 'Firestore', value: FEATURES.Firestore },
{ name: 'Realtime Database', value: FEATURES.Database },
// { name: 'Data Connect', value: FEATURES.DataConnect },
{ name: 'Data Connect', value: FEATURES.DataConnect },
{ name: 'Cloud Functions (callable)', value: FEATURES.Functions },
{ name: 'Cloud Messaging', value: FEATURES.Messaging },
{ name: 'Performance Monitoring', value: FEATURES.Performance },
Expand Down Expand Up @@ -132,6 +132,8 @@ export interface FirebaseTools {
serve(options: any): Promise<any>;

use(options: any, lol: any): Promise<any>;

init(feature: string, options: any): Promise<any>;
}

export interface FirebaseHostingRewrite {
Expand All @@ -149,9 +151,14 @@ export interface FirebaseHostingConfig {

export type FirebaseFunctionsConfig = Record<string, any>;

export interface DataConnectConfig {
source?: string;
}

export interface FirebaseJSON {
hosting?: FirebaseHostingConfig[] | FirebaseHostingConfig;
functions?: FirebaseFunctionsConfig;
dataconnect?: DataConnectConfig;
}

export interface FirebaseRcTarget {
Expand Down Expand Up @@ -225,3 +232,36 @@ export interface Workspace {
defaultProject?: string;
projects: Record<string, WorkspaceProject>;
}

export interface ConnectorConfig {
location: string;
connector: string;
service: string;
}
export interface ConnectorYaml {
connectorId: string;
generate?: {
javascriptSdk?: {
package: string;
outputDir: string;
packageJsonDir?: string;
angular?: boolean;
}
}
}
export interface DataConnectYaml {
location: string;
serviceId: string;
connectorDirs: string[];
}
export interface DataConnectConnectorConfig {
connectorYaml: ConnectorYaml;
connectorConfig?: ConnectorConfig;
angular?: boolean;
package?: string;
}

export interface PackageJson {
dependencies: Record<string, string>;
devDependencies: Record<string, string>;
}
64 changes: 56 additions & 8 deletions src/schematics/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { writeFileSync } from 'fs';
import { readFileSync, writeFileSync } from 'fs';
import { join } from 'path';
import { asWindowsPath, normalize } from '@angular-devkit/core';
import { SchematicContext, Tree, chain } from '@angular-devkit/schematics';
import { addRootProvider } from '@schematics/angular/utility';
import { getFirebaseTools } from '../firebaseTools';
import {
DeployOptions, FEATURES, FirebaseApp, FirebaseProject,
DataConnectConnectorConfig,
DeployOptions, FEATURES, FirebaseApp, FirebaseJSON, FirebaseProject,
} from '../interfaces';
import {
addIgnoreFiles,
featureToRules,
getFirebaseProjectNameFromHost,
getProject,
parseDataConnectConfig,
setupTanstackDependencies,
} from '../utils';
import { appPrompt, featuresPrompt, projectPrompt, userPrompt } from './prompts';

export interface SetupConfig extends DeployOptions {
firebaseProject: FirebaseProject,
firebaseApp?: FirebaseApp,
sdkConfig?: Record<string, string>,
firebaseJsonConfig?: FirebaseJSON;
dataConnectConfig?: DataConnectConnectorConfig | null;
firebaseJsonPath: string;
}

export const setupProject =
Expand All @@ -35,7 +41,7 @@ export const setupProject =
config.sdkConfig ? `{ ${Object.entries(config.sdkConfig).map(([k, v]) => `${k}: ${JSON.stringify(v)}`).join(", ")} }` : ""
}))`;
}),
...featureToRules(features, projectName),
...featureToRules(features, projectName, config.dataConnectConfig),
]);
}
};
Expand All @@ -56,6 +62,10 @@ export const ngAddSetupProject = (

// Add the firebase files if they don't exist already so login.use works
if (!host.exists('/firebase.json')) { writeFileSync(join(projectRoot, 'firebase.json'), '{}'); }

let firebaseJson: FirebaseJSON = JSON.parse(
readFileSync(join(projectRoot, "firebase.json")).toString()
);

const user = await userPrompt({ projectRoot });
const defaultUser = await firebaseTools.login(options);
Expand All @@ -72,19 +82,57 @@ export const ngAddSetupProject = (
let firebaseApp: FirebaseApp|undefined;
let sdkConfig: Record<string, string>|undefined;

const setupConfig: SetupConfig = {
...options, firebaseProject, firebaseApp, sdkConfig,
firebaseJsonConfig: firebaseJson,
firebaseJsonPath: projectRoot
};
if (features.length) {

firebaseApp = await appPrompt(firebaseProject, undefined, { projectRoot });

const result = await firebaseTools.apps.sdkconfig('web', firebaseApp.appId, { nonInteractive: true, projectRoot });
sdkConfig = result.sdkConfig;
delete sdkConfig.locationId;

setupConfig.sdkConfig = sdkConfig;
setupConfig.firebaseApp = firebaseApp;
// set up data connect locally if data connect hasn't already been initialized.
if(features.includes(FEATURES.DataConnect)) {
if (!firebaseJson.dataconnect) {
try {
await firebaseTools.init("dataconnect", {
projectRoot,
project: firebaseProject.projectId,
});
// Update firebaseJson values to include newly added dataconnect field in firebase.json.
firebaseJson = JSON.parse(
readFileSync(join(projectRoot, "firebase.json")).toString()
);
setupConfig.firebaseJsonConfig = firebaseJson;
} catch (e) {
console.error(e);
}
}
let dataConnectConfig = parseDataConnectConfig(setupConfig);
if(!dataConnectConfig?.connectorYaml.generate?.javascriptSdk) {
await firebaseTools.init("dataconnect:sdk", {
projectRoot,
project: firebaseProject.projectId,
});
}
// Parse through sdk again
dataConnectConfig = parseDataConnectConfig(setupConfig);
if(dataConnectConfig?.angular) {
context.logger.info('Generated Angular SDK Enabled. Setting up Tanstack Dependencies.');
setupTanstackDependencies(host, context);
} else {
context.logger.info('Generated Angular SDK Disabled. Please add `angular: true` to your connector.yaml and re-run `ng add @angular/fire`');
}
setupConfig.dataConnectConfig = dataConnectConfig;
}

}

return setupProject(host, context, features, {
...options, firebaseProject, firebaseApp, sdkConfig,
});

return setupProject(host, context, features, setupConfig);
}
};
Loading