Skip to content

Commit

Permalink
fixes(schematics): ng add/deploy universal fixes (#2954)
Browse files Browse the repository at this point in the history
* `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
  • Loading branch information
jamesdaniels authored Sep 14, 2021
1 parent b1eb567 commit 73bde38
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"node-fetch": "^2.6.1",
"semver": "^7.1.3",
"inquirer": "^8.1.1",
"fs-extra": "^8.0.1"
"fs-extra": "^8.0.1",
"file-loader": "^6.2.0"
},
"ngPackage": {
"lib": {
Expand All @@ -64,7 +65,7 @@
"fuzzy", "inquirer-autocomplete-prompt",
"open", "jsonc-parser", "ora", "winston",
"triple-beam", "@schematics/angular", "node-fetch",
"semver", "inquirer", "fs-extra"
"semver", "inquirer", "fs-extra", "file-loader"
]
},
"ng-update": {
Expand Down
5 changes: 4 additions & 1 deletion src/schematics/deploy/actions.jasmine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const SERVER_BUILD_TARGET: BuildTarget = {
name: `${PROJECT}:server:production`
};

const login = () => Promise.resolve();
login.list = () => Promise.resolve([{ user: { email: 'foo@bar.baz' }}]);

const initMocks = () => {
fsHost = {
moveSync(_: string, __: string) {
Expand All @@ -36,7 +39,7 @@ const initMocks = () => {
};

firebaseMock = {
login: () => Promise.resolve(),
login,
projects: {
list: () => Promise.resolve([]),
create: () => Promise.reject(),
Expand Down
2 changes: 2 additions & 0 deletions src/schematics/deploy/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ export default async function deploy(
) {
if (!firebaseToken) {
await firebaseTools.login();
const users = await firebaseTools.login.list();
console.log(`Logged into Firebase as ${users.map(it => it.user.email).join(', ')}.`);
}

if (prerenderBuildTarget) {
Expand Down
4 changes: 3 additions & 1 deletion src/schematics/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ export interface FirebaseTools {
version(): string;
};

login(): Promise<void>;
login: {
list(): Promise<{user: Record<string, any>}[]>;
} & (() => Promise<void>);

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

Expand Down
8 changes: 7 additions & 1 deletion src/schematics/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics';
import { getWorkspace, getProject, getFirebaseProjectNameFromHost, addEnvironmentEntry, addToNgModule, addIgnoreFiles } from '../utils';
import {
getWorkspace, getProject, getFirebaseProjectNameFromHost, addEnvironmentEntry,
addToNgModule, addIgnoreFiles, addFixesToServer
} from '../utils';
import { projectTypePrompt, appPrompt, sitePrompt, projectPrompt, featuresPrompt } from './prompts';
import { setupUniversalDeployment } from './ssr';
import { setupStaticDeployment } from './static';
Expand Down Expand Up @@ -34,6 +37,7 @@ export const setupProject =
const featuresToImport = features.filter(it => it !== FEATURES.Hosting);
if (featuresToImport.length > 0) {
addToNgModule(tree, { features: featuresToImport, sourcePath });
addFixesToServer(tree, { features: featuresToImport, sourcePath });
}

if (config.sdkConfig) {
Expand Down Expand Up @@ -112,6 +116,8 @@ export const ngAddSetupProject = (
const firebaseTools = await getFirebaseTools();

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

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

Expand Down
6 changes: 5 additions & 1 deletion src/schematics/setup/ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ export const setupUniversalDeployment = (config: {

addDependencies(
tree,
firebaseFunctionsDependencies,
Object.entries(firebaseFunctionsDependencies).reduce((acc, [dep, deets]) => {
deets.dev = true;
acc[dep] = deets;
return acc;
}, {}),
config.context
);

Expand Down
35 changes: 27 additions & 8 deletions src/schematics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SchematicsException, Tree } from '@angular-devkit/schematics';
import ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
import { findNode, addImportToModule, insertImport } from '@schematics/angular/utility/ast-utils';
import { InsertChange, ReplaceChange, applyToUpdateRecorder, Change } from '@schematics/angular/utility/change';
import { findModuleFromOptions, buildRelativePath } from '@schematics/angular/utility/find-module';
import { buildRelativePath } from '@schematics/angular/utility/find-module';
import { overwriteIfExists } from './common';

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

export function addToNgModule(host: Tree, options: { sourcePath: string, features: FEATURES[]}) {

const modulePath = findModuleFromOptions(host, {
name: 'app',
path: options.sourcePath,
});
// TODO rewrite using typescript
export function addFixesToServer(host: Tree, options: { sourcePath: string, features: FEATURES[]}) {
const serverPath = `/server.ts`;

if (!modulePath) {
if (!host.exists(serverPath)) {
return host;
}

const text = host.read(serverPath);
if (text === null) {
throw new SchematicsException(`File ${serverPath} does not exist.`);
}
const sourceText = text.toString('utf-8');
const addZonePatch = !sourceText.includes('import \'zone.js/dist/zone-patch-rxjs\';');
const addFirestorePatch = options.features.includes(FEATURES.Firestore) &&
!sourceText.includes('import \'@angular/fire/firestore-protos\';');

if (addZonePatch || addFirestorePatch) {
overwriteIfExists(host, serverPath, sourceText.replace('import \'zone.js/dist/zone-node\';', `import 'zone.js/dist/zone-node';
${addZonePatch ? 'import \'zone.js/dist/zone-patch-rxjs\';' : ''}
${addFirestorePatch ? 'import \'@angular/fire/firestore-protos\';' : ''}`));
}

return host;
}

export function addToNgModule(host: Tree, options: { sourcePath: string, features: FEATURES[]}) {

const modulePath = `/${options.sourcePath}/app/app.module.ts`;

if (!host.exists(modulePath)) {
throw new Error(`Specified module path ${modulePath} does not exist`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/schematics/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"rxfire": { "dev": false, "version": "0.0.0" }
},
"firebaseFunctionsDependencies": {
"firebase-admin": { "dev": true, "version": "0.0.0" },
"firebase-functions": { "dev": true, "version": "0.0.0" }
"firebase-admin": { "dev": false, "version": "0.0.0" },
"firebase-functions": { "dev": false, "version": "0.0.0" }
}
}

0 comments on commit 73bde38

Please sign in to comment.