|
| 1 | +import { chain, Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics'; |
| 2 | +import * as ts from 'typescript'; |
| 3 | +import { addPackageToPackageJson } from '../utils/package'; |
| 4 | +import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; |
| 5 | +import { angularfireVersion, firebaseVersion } from '../utils/libs-version'; |
| 6 | +import { addEnvironmentEntry } from '../utils/environment'; |
| 7 | +import { getProjectPath } from '../utils/project'; |
| 8 | +import { Schema } from './schema'; |
| 9 | +import { parseName } from '../schematics-core/utility/parse-name'; |
| 10 | +import { buildRelativePath, findModuleFromOptions } from '../schematics-core/utility/find-module'; |
| 11 | +import { addImportToModule, insertImport } from '../schematics-core/utility/ast-utils'; |
| 12 | +import { InsertChange } from '../schematics-core/utility/change'; |
| 13 | + |
| 14 | +export default function add(options: Schema): Rule { |
| 15 | + return chain([ |
| 16 | + install(), |
| 17 | + addConfig(), |
| 18 | + addToNgModule(options), |
| 19 | + ]) |
| 20 | +} |
| 21 | + |
| 22 | +export function install(): Rule { |
| 23 | + return (host: Tree, context: SchematicContext) => { |
| 24 | + addPackageToPackageJson(host, 'dependencies', 'firebase', firebaseVersion); |
| 25 | + addPackageToPackageJson(host, 'dependencies', '@angular/fire', angularfireVersion); |
| 26 | + context.addTask(new NodePackageInstallTask()); |
| 27 | + return host; |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +function addConfig(): Rule { |
| 32 | + return (host: Tree) => { |
| 33 | + const firebaseConfig = ` |
| 34 | + firebase: { |
| 35 | + apiKey: '<your-key>', |
| 36 | + authDomain: '<your-project-authdomain>', |
| 37 | + databaseURL: '<your-database-URL>', |
| 38 | + projectId: '<your-project-id>', |
| 39 | + storageBucket: '<your-storage-bucket>', |
| 40 | + messagingSenderId: '<your-messaging-sender-id>' |
| 41 | + },`; |
| 42 | + addEnvironmentEntry(host, 'environment.ts', firebaseConfig); |
| 43 | + return host; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +function addToNgModule(options: Schema): Rule { |
| 48 | + return (host: Tree) => { |
| 49 | + options.path = parseName(getProjectPath(host, options), '').path; |
| 50 | + |
| 51 | + if (options.module) { |
| 52 | + options.module = findModuleFromOptions(host, { |
| 53 | + name: '', |
| 54 | + module: options.module, |
| 55 | + path: options.path, |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + const modulePath = options.module; |
| 60 | + |
| 61 | + if (!modulePath) { |
| 62 | + return host; |
| 63 | + } |
| 64 | + |
| 65 | + if (!host.exists(modulePath)) { |
| 66 | + throw new Error(`Specified module path ${modulePath} does not exist`); |
| 67 | + } |
| 68 | + |
| 69 | + const text = host.read(modulePath); |
| 70 | + if (text === null) { |
| 71 | + throw new SchematicsException(`File ${modulePath} does not exist.`); |
| 72 | + } |
| 73 | + const sourceText = text.toString('utf-8'); |
| 74 | + |
| 75 | + const source = ts.createSourceFile( |
| 76 | + modulePath, |
| 77 | + sourceText, |
| 78 | + ts.ScriptTarget.Latest, |
| 79 | + true |
| 80 | + ); |
| 81 | + |
| 82 | + const environmentsPath = buildRelativePath( |
| 83 | + modulePath, |
| 84 | + `${options.path}/environments/environment` |
| 85 | + ); |
| 86 | + |
| 87 | + const AngularFireNgModuleImport = addImportToModule( |
| 88 | + source, |
| 89 | + modulePath, |
| 90 | + options.firebaseApp |
| 91 | + ? `AngularFireModule.initializeApp(environment.firebase, '${options.firebaseApp}')` |
| 92 | + : `AngularFireModule.initializeApp(environment.firebase)`, |
| 93 | + '' |
| 94 | + ).shift(); |
| 95 | + |
| 96 | + const coreImports = [ |
| 97 | + insertImport(source, modulePath, 'AngularFireModule', '@angular/fire'), |
| 98 | + insertImport(source, modulePath, 'environment', environmentsPath), |
| 99 | + AngularFireNgModuleImport, |
| 100 | + ]; |
| 101 | + |
| 102 | + const individualImports = options.all |
| 103 | + ? [ |
| 104 | + insertImport(source, modulePath, 'AngularFirestoreModule', '@angular/fire/firestore'), |
| 105 | + insertImport(source, modulePath, 'AngularFireStorageModule', '@angular/fire/storage'), |
| 106 | + insertImport(source, modulePath, 'AngularFireAuthModule', '@angular/fire/auth'), |
| 107 | + addImportToModule(source, modulePath, 'AngularFirestoreModule', '').shift(), |
| 108 | + addImportToModule(source, modulePath, 'AngularFireStorageModule', '').shift(), |
| 109 | + addImportToModule(source, modulePath, 'AngularFireAuthModule', '').shift(), |
| 110 | + ] |
| 111 | + : []; |
| 112 | + |
| 113 | + const changes = [...coreImports, ...individualImports]; |
| 114 | + |
| 115 | + const recorder = host.beginUpdate(modulePath); |
| 116 | + for (const change of changes) { |
| 117 | + if (change instanceof InsertChange) { |
| 118 | + recorder.insertLeft(change.pos, change.toAdd); |
| 119 | + } |
| 120 | + } |
| 121 | + host.commitUpdate(recorder); |
| 122 | + |
| 123 | + return host; |
| 124 | + } |
| 125 | +} |
0 commit comments