Skip to content

Commit 73bde38

Browse files
authored
fixes(schematics): ng add/deploy universal fixes (#2954)
* `findModuleFromOptions` wasn't working well when SSR was added to the mix, drop * shell out the logged in user(s) * fixed Cloud Functions dependencies all being dev * added `file-loader` dep so the Firestore loaders work out of box
1 parent b1eb567 commit 73bde38

File tree

8 files changed

+53
-16
lines changed

8 files changed

+53
-16
lines changed

src/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
"node-fetch": "^2.6.1",
4747
"semver": "^7.1.3",
4848
"inquirer": "^8.1.1",
49-
"fs-extra": "^8.0.1"
49+
"fs-extra": "^8.0.1",
50+
"file-loader": "^6.2.0"
5051
},
5152
"ngPackage": {
5253
"lib": {
@@ -64,7 +65,7 @@
6465
"fuzzy", "inquirer-autocomplete-prompt",
6566
"open", "jsonc-parser", "ora", "winston",
6667
"triple-beam", "@schematics/angular", "node-fetch",
67-
"semver", "inquirer", "fs-extra"
68+
"semver", "inquirer", "fs-extra", "file-loader"
6869
]
6970
},
7071
"ng-update": {

src/schematics/deploy/actions.jasmine.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ const SERVER_BUILD_TARGET: BuildTarget = {
2121
name: `${PROJECT}:server:production`
2222
};
2323

24+
const login = () => Promise.resolve();
25+
login.list = () => Promise.resolve([{ user: { email: '[email protected]' }}]);
26+
2427
const initMocks = () => {
2528
fsHost = {
2629
moveSync(_: string, __: string) {
@@ -36,7 +39,7 @@ const initMocks = () => {
3639
};
3740

3841
firebaseMock = {
39-
login: () => Promise.resolve(),
42+
login,
4043
projects: {
4144
list: () => Promise.resolve([]),
4245
create: () => Promise.reject(),

src/schematics/deploy/actions.ts

+2
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ export default async function deploy(
367367
) {
368368
if (!firebaseToken) {
369369
await firebaseTools.login();
370+
const users = await firebaseTools.login.list();
371+
console.log(`Logged into Firebase as ${users.map(it => it.user.email).join(', ')}.`);
370372
}
371373

372374
if (prerenderBuildTarget) {

src/schematics/interfaces.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ export interface FirebaseTools {
118118
version(): string;
119119
};
120120

121-
login(): Promise<void>;
121+
login: {
122+
list(): Promise<{user: Record<string, any>}[]>;
123+
} & (() => Promise<void>);
122124

123125
deploy(config: FirebaseDeployConfig): Promise<any>;
124126

src/schematics/setup/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics';
2-
import { getWorkspace, getProject, getFirebaseProjectNameFromHost, addEnvironmentEntry, addToNgModule, addIgnoreFiles } from '../utils';
2+
import {
3+
getWorkspace, getProject, getFirebaseProjectNameFromHost, addEnvironmentEntry,
4+
addToNgModule, addIgnoreFiles, addFixesToServer
5+
} from '../utils';
36
import { projectTypePrompt, appPrompt, sitePrompt, projectPrompt, featuresPrompt } from './prompts';
47
import { setupUniversalDeployment } from './ssr';
58
import { setupStaticDeployment } from './static';
@@ -34,6 +37,7 @@ export const setupProject =
3437
const featuresToImport = features.filter(it => it !== FEATURES.Hosting);
3538
if (featuresToImport.length > 0) {
3639
addToNgModule(tree, { features: featuresToImport, sourcePath });
40+
addFixesToServer(tree, { features: featuresToImport, sourcePath });
3741
}
3842

3943
if (config.sdkConfig) {
@@ -112,6 +116,8 @@ export const ngAddSetupProject = (
112116
const firebaseTools = await getFirebaseTools();
113117

114118
await firebaseTools.login();
119+
const users = await firebaseTools.login.list();
120+
console.log(`Logged into Firebase as ${users.map(it => it.user.email).join(', ')}.`);
115121

116122
const { project: ngProject, projectName: ngProjectName } = getProject(options, host);
117123

src/schematics/setup/ssr.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ export const setupUniversalDeployment = (config: {
131131

132132
addDependencies(
133133
tree,
134-
firebaseFunctionsDependencies,
134+
Object.entries(firebaseFunctionsDependencies).reduce((acc, [dep, deets]) => {
135+
deets.dev = true;
136+
acc[dep] = deets;
137+
return acc;
138+
}, {}),
135139
config.context
136140
);
137141

src/schematics/utils.ts

+27-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { SchematicsException, Tree } from '@angular-devkit/schematics';
55
import ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
66
import { findNode, addImportToModule, insertImport } from '@schematics/angular/utility/ast-utils';
77
import { InsertChange, ReplaceChange, applyToUpdateRecorder, Change } from '@schematics/angular/utility/change';
8-
import { findModuleFromOptions, buildRelativePath } from '@schematics/angular/utility/find-module';
8+
import { buildRelativePath } from '@schematics/angular/utility/find-module';
99
import { overwriteIfExists } from './common';
1010

1111
// We consider a project to be a universal project if it has a `server` architect
@@ -151,17 +151,36 @@ export function addEnvironmentEntry(
151151
return host;
152152
}
153153

154-
export function addToNgModule(host: Tree, options: { sourcePath: string, features: FEATURES[]}) {
155-
156-
const modulePath = findModuleFromOptions(host, {
157-
name: 'app',
158-
path: options.sourcePath,
159-
});
154+
// TODO rewrite using typescript
155+
export function addFixesToServer(host: Tree, options: { sourcePath: string, features: FEATURES[]}) {
156+
const serverPath = `/server.ts`;
160157

161-
if (!modulePath) {
158+
if (!host.exists(serverPath)) {
162159
return host;
163160
}
164161

162+
const text = host.read(serverPath);
163+
if (text === null) {
164+
throw new SchematicsException(`File ${serverPath} does not exist.`);
165+
}
166+
const sourceText = text.toString('utf-8');
167+
const addZonePatch = !sourceText.includes('import \'zone.js/dist/zone-patch-rxjs\';');
168+
const addFirestorePatch = options.features.includes(FEATURES.Firestore) &&
169+
!sourceText.includes('import \'@angular/fire/firestore-protos\';');
170+
171+
if (addZonePatch || addFirestorePatch) {
172+
overwriteIfExists(host, serverPath, sourceText.replace('import \'zone.js/dist/zone-node\';', `import 'zone.js/dist/zone-node';
173+
${addZonePatch ? 'import \'zone.js/dist/zone-patch-rxjs\';' : ''}
174+
${addFirestorePatch ? 'import \'@angular/fire/firestore-protos\';' : ''}`));
175+
}
176+
177+
return host;
178+
}
179+
180+
export function addToNgModule(host: Tree, options: { sourcePath: string, features: FEATURES[]}) {
181+
182+
const modulePath = `/${options.sourcePath}/app/app.module.ts`;
183+
165184
if (!host.exists(modulePath)) {
166185
throw new Error(`Specified module path ${modulePath} does not exist`);
167186
}

src/schematics/versions.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"rxfire": { "dev": false, "version": "0.0.0" }
55
},
66
"firebaseFunctionsDependencies": {
7-
"firebase-admin": { "dev": true, "version": "0.0.0" },
8-
"firebase-functions": { "dev": true, "version": "0.0.0" }
7+
"firebase-admin": { "dev": false, "version": "0.0.0" },
8+
"firebase-functions": { "dev": false, "version": "0.0.0" }
99
}
1010
}

0 commit comments

Comments
 (0)