├── sandbox ├── src │ ├── assets │ │ └── .gitkeep │ ├── app │ │ ├── app.component.scss │ │ ├── app.component.ts │ │ ├── app.module.ts │ │ ├── app.component.spec.ts │ │ └── app.component.html │ ├── environments │ │ ├── environment.prod.ts │ │ └── environment.ts │ ├── styles.scss │ ├── favicon.ico │ ├── tsconfig.app.json │ ├── tsconfig.spec.json │ ├── index.html │ ├── tslint.json │ ├── main.ts │ ├── browserslist │ ├── test.ts │ ├── karma.conf.js │ └── polyfills.ts ├── e2e │ ├── src │ │ ├── app.po.ts │ │ └── app.e2e-spec.ts │ ├── tsconfig.e2e.json │ └── protractor.conf.js ├── .editorconfig ├── tsconfig.json ├── README.md ├── package.json ├── tslint.json └── angular.json ├── .prettierrc.yml ├── docs └── ng-add-angular-fire-schematics.gif ├── .npmignore ├── .travis.yml ├── README.md ├── .gitignore ├── src ├── ng-add │ ├── schema.ts │ ├── index_spec.ts │ ├── schema.json │ ├── index.ts │ └── setup-project.ts ├── util │ ├── project-configurations.ts │ ├── npmjs.ts │ ├── project-environment-file.ts │ └── version-agnostic-typescript.ts └── collection.json ├── tsconfig.json ├── LICENSE ├── .github └── ISSUE_TEMPLATE │ └── bug_report.md └── package.json /sandbox/src/assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.prettierrc.yml: -------------------------------------------------------------------------------- 1 | singleQuote: true 2 | -------------------------------------------------------------------------------- /sandbox/src/app/app.component.scss: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /sandbox/src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /sandbox/src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | -------------------------------------------------------------------------------- /sandbox/src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blove/angular-fire-schematics/HEAD/sandbox/src/favicon.ico -------------------------------------------------------------------------------- /docs/ng-add-angular-fire-schematics.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/blove/angular-fire-schematics/HEAD/docs/ng-add-angular-fire-schematics.gif -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # Ignores TypeScript files, but keeps definitions. 2 | *.ts 3 | *.js.map 4 | !src/**/files/**/*.js 5 | !src/**/files/**/*.ts 6 | !*.d.ts 7 | 8 | .vscode/* 9 | sandbox 10 | docs 11 | *.circleci 12 | *.github -------------------------------------------------------------------------------- /sandbox/src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "types": [] 6 | }, 7 | "exclude": [ 8 | "test.ts", 9 | "**/*.spec.ts" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | 3 | node_js: 4 | - 8 5 | 6 | cache: 7 | yarn: true 8 | directories: 9 | - node_modules 10 | 11 | install: 12 | - travis_retry npm install 13 | 14 | before_script: 15 | - yarn link:schematic 16 | 17 | script: 18 | - yarn test:ci -------------------------------------------------------------------------------- /sandbox/e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get('/'); 6 | } 7 | 8 | getParagraphText() { 9 | return element(by.css('app-root h1')).getText(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /sandbox/src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.scss'] 7 | }) 8 | export class AppComponent { 9 | title = 'sandbox'; 10 | } 11 | -------------------------------------------------------------------------------- /sandbox/e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /sandbox/.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AngularFire Schematic 2 | 3 | This schematic adds AngularFire to an Angular project. 4 | 5 | ## Installation 6 | 7 | To install AngularFire into an Angular project simple run: 8 | 9 | ```shell 10 | ng add angular-fire-schematics 11 | ``` 12 | 13 | ## Demo 14 | 15 | ![ng-add-demo](docs/ng-add-angular-fire-schematics.gif) 16 | -------------------------------------------------------------------------------- /sandbox/src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "test.ts", 12 | "polyfills.ts" 13 | ], 14 | "include": [ 15 | "**/*.spec.ts", 16 | "**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /sandbox/src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { BrowserModule } from '@angular/platform-browser'; 3 | import { AppComponent } from './app.component'; 4 | 5 | @NgModule({ 6 | declarations: [AppComponent], 7 | imports: [BrowserModule], 8 | providers: [], 9 | bootstrap: [AppComponent] 10 | }) 11 | export class AppModule {} 12 | -------------------------------------------------------------------------------- /sandbox/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Sandbox 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /sandbox/e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | 3 | describe('workspace-project App', () => { 4 | let page: AppPage; 5 | 6 | beforeEach(() => { 7 | page = new AppPage(); 8 | }); 9 | 10 | it('should display welcome message', () => { 11 | page.navigateTo(); 12 | expect(page.getParagraphText()).toEqual('Welcome to sandbox!'); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /sandbox/src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "app", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "app", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /sandbox/src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /sandbox/src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # 5 | # For IE 9-11 support, please remove 'not' from the last line of the file and adjust as needed 6 | 7 | > 0.5% 8 | last 2 versions 9 | Firefox ESR 10 | not dead 11 | not IE 9-11 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Outputs 2 | src/**/*.js 3 | !src/**/files/**/*.js 4 | src/**/*.js.map 5 | src/**/*.d.ts 6 | 7 | # IDEs 8 | .idea/ 9 | jsconfig.json 10 | .vscode/ 11 | 12 | # Misc 13 | node_modules/ 14 | npm-debug.log* 15 | yarn-error.log* 16 | *.todo 17 | 18 | # Mac OSX Finder files. 19 | **/.DS_Store 20 | .DS_Store 21 | 22 | # IDE - VSCode 23 | .vscode/* 24 | !.vscode/settings.json 25 | !.vscode/tasks.json 26 | !.vscode/launch.json 27 | !.vscode/extensions.json -------------------------------------------------------------------------------- /sandbox/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "module": "es2015", 9 | "moduleResolution": "node", 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "target": "es5", 13 | "typeRoots": [ 14 | "node_modules/@types" 15 | ], 16 | "lib": [ 17 | "es2018", 18 | "dom" 19 | ] 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/ng-add/schema.ts: -------------------------------------------------------------------------------- 1 | export interface Schema { 2 | /** Firebase API key. */ 3 | apiKey: string; 4 | 5 | /** Firebase authorized domain. */ 6 | authDomain: string; 7 | 8 | /** Firebase db URL. */ 9 | databaseURL: string; 10 | 11 | /** Name of the project to target. */ 12 | project: string; 13 | 14 | /** Firebase project ID. */ 15 | projectId: string; 16 | 17 | /** Firebase storage bucket. */ 18 | storageBucket: string; 19 | 20 | /** Firebase messaging sender ID. */ 21 | messagingSenderId: string; 22 | } 23 | -------------------------------------------------------------------------------- /src/util/project-configurations.ts: -------------------------------------------------------------------------------- 1 | import { WorkspaceProject } from '@angular-devkit/core/src/workspace'; 2 | 3 | export function getProjectTargetConfigurations( 4 | project: WorkspaceProject, 5 | buildTarget = "build" 6 | ) { 7 | if ( 8 | project.architect && 9 | project.architect[buildTarget] && 10 | project.architect[buildTarget].options 11 | ) { 12 | return project.architect[buildTarget].configurations; 13 | } 14 | 15 | throw new Error( 16 | `Cannot determine project target configurations for: ${buildTarget}.` 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /src/ng-add/index_spec.ts: -------------------------------------------------------------------------------- 1 | import { Tree } from '@angular-devkit/schematics'; 2 | import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; 3 | import * as path from 'path'; 4 | 5 | 6 | const collectionPath = path.join(__dirname, '../collection.json'); 7 | 8 | 9 | describe('angular-fire-schematics', () => { 10 | it('works', () => { 11 | const runner = new SchematicTestRunner('schematics', collectionPath); 12 | const tree = runner.runSchematic('angular-fire-schematics', {}, Tree.empty()); 13 | 14 | expect(tree.files).toEqual([]); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /sandbox/src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /sandbox/src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "baseUrl": "tsconfig", 4 | "lib": [ 5 | "es2017", 6 | "dom" 7 | ], 8 | "declaration": true, 9 | "module": "commonjs", 10 | "moduleResolution": "node", 11 | "noEmitOnError": true, 12 | "noFallthroughCasesInSwitch": true, 13 | "noImplicitAny": true, 14 | "noImplicitThis": true, 15 | "noUnusedParameters": true, 16 | "noUnusedLocals": true, 17 | "rootDir": "src/", 18 | "skipDefaultLibCheck": true, 19 | "skipLibCheck": true, 20 | "sourceMap": true, 21 | "strictNullChecks": true, 22 | "target": "es6", 23 | "types": [ 24 | "jasmine", 25 | "node" 26 | ] 27 | }, 28 | "include": [ 29 | "src/**/*" 30 | ], 31 | "exclude": [ 32 | "src/*/files/**/*" 33 | ] 34 | } 35 | -------------------------------------------------------------------------------- /src/collection.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json", 3 | "schematics": { 4 | "add": { 5 | "description": "Adds Angular Firebase to the application without affecting any templates", 6 | "factory": "./ng-add/index", 7 | "schema": "./ng-add/schema.json" 8 | }, 9 | "ng-add": { 10 | "description": "Adds Angular Firebase to the application without affecting any templates", 11 | "factory": "./ng-add/index", 12 | "schema": "./ng-add/schema.json", 13 | "aliases": ["install"] 14 | }, 15 | "ng-add-setup-project": { 16 | "description": "Sets up the specified project after the ng-add dependencies have been installed.", 17 | "private": true, 18 | "factory": "./ng-add/setup-project", 19 | "schema": "./ng-add/schema.json" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /sandbox/e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: require('path').join(__dirname, './tsconfig.e2e.json') 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; -------------------------------------------------------------------------------- /sandbox/src/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage'), 20 | reports: ['html', 'lcovonly'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false 30 | }); 31 | }; -------------------------------------------------------------------------------- /src/util/npmjs.ts: -------------------------------------------------------------------------------- 1 | import { get } from 'http'; 2 | 3 | export interface NpmRegistryPackage { 4 | name: string; 5 | version: string; 6 | } 7 | 8 | export function getLatestNodeVersion( 9 | packageName: string 10 | ): Promise { 11 | const DEFAULT_VERSION = 'latest'; 12 | 13 | return new Promise(resolve => { 14 | return get(`http://registry.npmjs.org/${packageName}`, res => { 15 | let rawData = ''; 16 | res.on('data', chunk => (rawData += chunk)); 17 | res.on('end', () => { 18 | try { 19 | const response = JSON.parse(rawData); 20 | const version = (response && response['dist-tags']) || {}; 21 | 22 | resolve(buildPackage(response.name || packageName, version.latest)); 23 | } catch (e) { 24 | resolve(buildPackage(packageName)); 25 | } 26 | }); 27 | }).on('error', () => resolve(buildPackage(packageName))); 28 | }); 29 | 30 | function buildPackage( 31 | name: string, 32 | version: string = DEFAULT_VERSION 33 | ): NpmRegistryPackage { 34 | return { name, version }; 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /sandbox/README.md: -------------------------------------------------------------------------------- 1 | # Sandbox 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 7.0.2. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 8 | 9 | ## Code scaffolding 10 | 11 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. 12 | 13 | ## Build 14 | 15 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. Use the `--prod` flag for a production build. 16 | 17 | ## Running unit tests 18 | 19 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 20 | 21 | ## Running end-to-end tests 22 | 23 | Run `ng e2e` to execute the end-to-end tests via [Protractor](http://www.protractortest.org/). 24 | 25 | ## Further help 26 | 27 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI README](https://github.com/angular/angular-cli/blob/master/README.md). 28 | -------------------------------------------------------------------------------- /sandbox/src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { AppComponent } from './app.component'; 3 | 4 | describe('AppComponent', () => { 5 | beforeEach(async(() => { 6 | TestBed.configureTestingModule({ 7 | declarations: [ 8 | AppComponent 9 | ], 10 | }).compileComponents(); 11 | })); 12 | 13 | it('should create the app', () => { 14 | const fixture = TestBed.createComponent(AppComponent); 15 | const app = fixture.debugElement.componentInstance; 16 | expect(app).toBeTruthy(); 17 | }); 18 | 19 | it(`should have as title 'sandbox'`, () => { 20 | const fixture = TestBed.createComponent(AppComponent); 21 | const app = fixture.debugElement.componentInstance; 22 | expect(app.title).toEqual('sandbox'); 23 | }); 24 | 25 | it('should render title in a h1 tag', () => { 26 | const fixture = TestBed.createComponent(AppComponent); 27 | fixture.detectChanges(); 28 | const compiled = fixture.debugElement.nativeElement; 29 | expect(compiled.querySelector('h1').textContent).toContain('Welcome to sandbox!'); 30 | }); 31 | }); 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Kevin Schuchard 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /sandbox/src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |

4 | Welcome to {{ title }}! 5 |

6 | Angular Logo 7 |
8 |

Here are some links to help you start:

9 | 20 | 21 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | Issue tracker is **ONLY** used for reporting bugs. NO NEW FEATURE ACCEPTED! Use [stackoverflow](https://stackoverflow.com) for supporting issues. 2 | 3 | 4 | 5 | ## Expected Behavior 6 | 7 | 8 | 9 | ## Current Behavior 10 | 11 | 12 | 13 | ## Possible Solution 14 | 15 | 16 | 17 | ## Steps to Reproduce 18 | 19 | 20 | 21 | 22 | 1. 2. 3. 4. 23 | 24 | ## Context (Environment) 25 | 26 | 27 | 28 | 29 | 30 | 31 | ## Detailed Description 32 | 33 | 34 | 35 | ## Possible Implementation 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/util/project-environment-file.ts: -------------------------------------------------------------------------------- 1 | import { WorkspaceProject } from '@angular-devkit/core/src/workspace'; 2 | import { SchematicsException } from '@angular-devkit/schematics'; 3 | import { getProjectTargetConfigurations } from './project-configurations'; 4 | 5 | export function getProjectEnvironmentFile(project: WorkspaceProject): string { 6 | const configurations = getProjectTargetConfigurations(project, 'build'); 7 | 8 | if ( 9 | !configurations.production || 10 | !configurations.production.fileReplacements || 11 | configurations.production.fileReplacements.length === 0 12 | ) { 13 | throw new SchematicsException( 14 | `Could not find the configuration of the workspace config (${ 15 | project.sourceRoot 16 | })` 17 | ); 18 | } 19 | 20 | const fileReplacements: [{ replace: string; with: string }] = 21 | configurations.production.fileReplacements; 22 | const fileReplacement = fileReplacements.find(replacement => 23 | /environment\.ts$/.test(replacement.replace) 24 | ); 25 | 26 | if (fileReplacement === undefined) { 27 | throw new SchematicsException( 28 | `Could not find the environment file replacement configuration of the workspace config (${ 29 | project.sourceRoot 30 | })` 31 | ); 32 | } 33 | 34 | return fileReplacement.replace; 35 | } 36 | -------------------------------------------------------------------------------- /src/util/version-agnostic-typescript.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @license 3 | * Copyright Google LLC All Rights Reserved. 4 | * 5 | * Use of this source code is governed by an MIT-style license that can be 6 | * found in the LICENSE file at https://angular.io/license 7 | */ 8 | 9 | /** 10 | * This is just a type import and won't be generated in the release output. 11 | * 12 | * Note that we always need to adjust this type import based on the location of the Typescript 13 | * dependency that will be shipped with `@schematics/angular`. 14 | */ 15 | import typescript = require('typescript'); 16 | 17 | /** 18 | * This is an agnostic re-export of TypeScript. Depending on the context, this module file will 19 | * return the TypeScript version that is being shipped within the `@schematics/angular` package, 20 | * or fall back to the TypeScript version that has been flattened in the node modules. 21 | * 22 | * This is necessary because we parse TypeScript files and pass the resolved AST to the 23 | * `@schematics/angular` package which might have a different TypeScript version installed. 24 | */ 25 | let ts: typeof typescript; 26 | 27 | try { 28 | ts = require('@schematics/angular/node_modules/typescript'); 29 | } catch { 30 | try { 31 | ts = require('typescript'); 32 | } catch { 33 | throw new Error( 34 | 'Error: Could not find a TypeScript version for the schematics. ' + 35 | 'Please report an issue on the Angular Material repository.' 36 | ); 37 | } 38 | } 39 | 40 | export { ts }; 41 | -------------------------------------------------------------------------------- /sandbox/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sandbox", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "~7.0.0", 15 | "@angular/common": "~7.0.0", 16 | "@angular/compiler": "~7.0.0", 17 | "@angular/core": "~7.0.0", 18 | "@angular/forms": "~7.0.0", 19 | "@angular/http": "~7.0.0", 20 | "@angular/platform-browser": "~7.0.0", 21 | "@angular/platform-browser-dynamic": "~7.0.0", 22 | "@angular/router": "~7.0.0", 23 | "core-js": "^2.5.4", 24 | "rxjs": "~6.3.3", 25 | "zone.js": "~0.8.26" 26 | }, 27 | "devDependencies": { 28 | "@angular-devkit/build-angular": "~0.10.0", 29 | "@angular/cli": "~7.0.2", 30 | "@angular/compiler-cli": "~7.0.0", 31 | "@angular/language-service": "~7.0.0", 32 | "@types/node": "~8.9.4", 33 | "@types/jasmine": "~2.8.8", 34 | "@types/jasminewd2": "~2.0.3", 35 | "codelyzer": "~4.5.0", 36 | "jasmine-core": "~2.99.1", 37 | "jasmine-spec-reporter": "~4.2.1", 38 | "karma": "~3.0.0", 39 | "karma-chrome-launcher": "~2.2.0", 40 | "karma-coverage-istanbul-reporter": "~2.0.1", 41 | "karma-jasmine": "~1.1.2", 42 | "karma-jasmine-html-reporter": "^0.2.2", 43 | "protractor": "~5.4.0", 44 | "ts-node": "~7.0.0", 45 | "tslint": "~5.11.0", 46 | "typescript": "~3.1.1" 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/ng-add/schema.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json-schema.org/schema", 3 | "id": "angular-firebase-schematic-ng-add", 4 | "title": "Angular Firebase ng-add schematic", 5 | "type": "object", 6 | "properties": { 7 | "project": { 8 | "type": "string", 9 | "description": "The name of the project.", 10 | "$default": { 11 | "$source": "projectName" 12 | } 13 | }, 14 | "apiKey": { 15 | "type": "string", 16 | "default": "", 17 | "description": "Your Firebase API key", 18 | "x-prompt": "What is your project's apiKey?" 19 | }, 20 | "authDomain": { 21 | "type": "string", 22 | "default": "", 23 | "description": "Your Firebase domain", 24 | "x-prompt": "What is your project's authDomain?" 25 | }, 26 | "databaseURL": { 27 | "type": "string", 28 | "default": "", 29 | "description": "Your Firebase database URL", 30 | "x-prompt": "What is your project's databaseURL?" 31 | }, 32 | "projectId": { 33 | "type": "string", 34 | "default": "", 35 | "description": "Your Firebase project ID", 36 | "x-prompt": "What is your project's id?" 37 | }, 38 | "storageBucket": { 39 | "type": "string", 40 | "default": "", 41 | "description": "Your Firebase storage bucket", 42 | "x-prompt": "What is your project's storageBucket?" 43 | }, 44 | "messagingSenderId": { 45 | "type": "string", 46 | "default": "", 47 | "description": "Your Firebase message sender ID", 48 | "x-prompt": "What is your project's messagingSenderId?" 49 | } 50 | }, 51 | "required": [], 52 | "additionalProperties": false 53 | } 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-fire-schematics", 3 | "version": "1.0.0", 4 | "description": "AngularFire Schematics", 5 | "scripts": { 6 | "build": "tsc -p tsconfig.json", 7 | "clean": "git checkout HEAD -- sandbox && git clean -f -d sandbox", 8 | "commit": "git-cz", 9 | "link:schematic": "yarn link && cd sandbox && yarn link \"angular-fire-schematic\"", 10 | "sandbox:add": "cd sandbox && ng g angular-fire-schematic:add", 11 | "test": "yarn clean && yarn sandbox:add && yarn test:sandbox", 12 | "test:ci": "yarn clean && cd sandbox && yarn && ng g angular-fire-schematic:add --apiKey='' --authDomain='' --databaseURL='' --projectId='' --storageBucket='' --messagingSenderId='' && yarn lint && yarn build", 13 | "test:unit": "yarn build && jasmine src/**/*_spec.js", 14 | "test:sandbox": "cd sandbox && yarn lint && yarn test && yarn build", 15 | "semantic-release": "semantic-release" 16 | }, 17 | "keywords": [ 18 | "schematics", 19 | "AngularFire", 20 | "ng-add" 21 | ], 22 | "author": "Brian Love", 23 | "license": "MIT", 24 | "schematics": "./src/collection.json", 25 | "engines": { 26 | "node": ">=8.11.0" 27 | }, 28 | "dependencies": { 29 | "@angular-devkit/core": "^7.1.3", 30 | "@angular-devkit/schematics": "^7.1.3", 31 | "@angular/cdk": "^7.1.1", 32 | "@schematics/angular": "^7.1.3", 33 | "@types/jasmine": "^2.6.0", 34 | "@types/node": "^8.0.31", 35 | "jasmine": "^2.8.0", 36 | "tslint": "^5.11.0", 37 | "typescript": "^2.5.2" 38 | }, 39 | "devDependencies": { 40 | "@angular-devkit/schematics-cli": "^0.11.3", 41 | "commitizen": "^3.0.5", 42 | "cz-conventional-changelog": "2.1.0", 43 | "semantic-release": "^15.13.0" 44 | }, 45 | "repository": { 46 | "type": "git", 47 | "url": "https://github.com/blove/angular-fire-schematics.git" 48 | }, 49 | "config": { 50 | "commitizen": { 51 | "path": "./node_modules/cz-conventional-changelog" 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/ng-add/index.ts: -------------------------------------------------------------------------------- 1 | import { 2 | chain, 3 | Rule, 4 | SchematicContext, 5 | Tree 6 | } from '@angular-devkit/schematics'; 7 | import { NodePackageInstallTask, RunSchematicTask } from '@angular-devkit/schematics/tasks'; 8 | import { addPackageJsonDependency, NodeDependency, NodeDependencyType } from '@schematics/angular/utility/dependencies'; 9 | import { Observable, of } from 'rxjs'; 10 | import { concatMap, map } from 'rxjs/operators'; 11 | import { Schema } from './schema'; 12 | import { getLatestNodeVersion, NpmRegistryPackage } from '../util/npmjs'; 13 | 14 | export default function(options: Schema): Rule { 15 | return (tree: Tree, _context: SchematicContext) => { 16 | return chain([ 17 | addPackageJsonDependencies(), 18 | installDependencies(), 19 | setupProject(options) 20 | ])(tree, _context); 21 | }; 22 | } 23 | 24 | function addPackageJsonDependencies(): Rule { 25 | return (tree: Tree, _context: SchematicContext): Observable => { 26 | return of('firebase', '@angular/fire').pipe( 27 | concatMap(name => getLatestNodeVersion(name)), 28 | map((npmRegistryPackage: NpmRegistryPackage) => { 29 | const nodeDependency: NodeDependency = { 30 | type: NodeDependencyType.Default, 31 | name: npmRegistryPackage.name, 32 | version: npmRegistryPackage.version, 33 | overwrite: false 34 | }; 35 | addPackageJsonDependency(tree, nodeDependency); 36 | _context.logger.info( 37 | `✅️ Added dependency: ${npmRegistryPackage.name}@${ 38 | npmRegistryPackage.version 39 | }` 40 | ); 41 | return tree; 42 | }) 43 | ); 44 | }; 45 | } 46 | 47 | function installDependencies(): Rule { 48 | return (tree: Tree, _context: SchematicContext) => { 49 | _context.addTask(new NodePackageInstallTask()); 50 | _context.logger.info('✅️ Dependencies installed'); 51 | return tree; 52 | }; 53 | } 54 | 55 | function setupProject(options: Schema): Rule { 56 | return (tree: Tree, _context: SchematicContext) => { 57 | const installTaskId = _context.addTask(new NodePackageInstallTask()); 58 | _context.addTask(new RunSchematicTask('ng-add-setup-project', options), [ 59 | installTaskId 60 | ]); 61 | return tree; 62 | }; 63 | } 64 | -------------------------------------------------------------------------------- /sandbox/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rulesDirectory": [ 3 | "node_modules/codelyzer" 4 | ], 5 | "rules": { 6 | "arrow-return-shorthand": true, 7 | "callable-types": true, 8 | "class-name": true, 9 | "comment-format": [ 10 | true, 11 | "check-space" 12 | ], 13 | "curly": true, 14 | "deprecation": { 15 | "severity": "warn" 16 | }, 17 | "eofline": true, 18 | "forin": true, 19 | "import-blacklist": [ 20 | true, 21 | "rxjs/Rx" 22 | ], 23 | "import-spacing": true, 24 | "indent": [ 25 | true, 26 | "spaces" 27 | ], 28 | "interface-over-type-literal": true, 29 | "label-position": true, 30 | "max-line-length": [ 31 | true, 32 | 140 33 | ], 34 | "member-access": false, 35 | "member-ordering": [ 36 | true, 37 | { 38 | "order": [ 39 | "static-field", 40 | "instance-field", 41 | "static-method", 42 | "instance-method" 43 | ] 44 | } 45 | ], 46 | "no-arg": true, 47 | "no-bitwise": true, 48 | "no-console": [ 49 | true, 50 | "debug", 51 | "info", 52 | "time", 53 | "timeEnd", 54 | "trace" 55 | ], 56 | "no-construct": true, 57 | "no-debugger": true, 58 | "no-duplicate-super": true, 59 | "no-empty": false, 60 | "no-empty-interface": true, 61 | "no-eval": true, 62 | "no-inferrable-types": [ 63 | true, 64 | "ignore-params" 65 | ], 66 | "no-misused-new": true, 67 | "no-non-null-assertion": true, 68 | "no-redundant-jsdoc": true, 69 | "no-shadowed-variable": true, 70 | "no-string-literal": false, 71 | "no-string-throw": true, 72 | "no-switch-case-fall-through": true, 73 | "no-trailing-whitespace": true, 74 | "no-unnecessary-initializer": true, 75 | "no-unused-expression": true, 76 | "no-use-before-declare": true, 77 | "no-var-keyword": true, 78 | "object-literal-sort-keys": false, 79 | "one-line": [ 80 | true, 81 | "check-open-brace", 82 | "check-catch", 83 | "check-else", 84 | "check-whitespace" 85 | ], 86 | "prefer-const": true, 87 | "quotemark": [ 88 | true, 89 | "single" 90 | ], 91 | "radix": true, 92 | "semicolon": [ 93 | true, 94 | "always" 95 | ], 96 | "triple-equals": [ 97 | true, 98 | "allow-null-check" 99 | ], 100 | "typedef-whitespace": [ 101 | true, 102 | { 103 | "call-signature": "nospace", 104 | "index-signature": "nospace", 105 | "parameter": "nospace", 106 | "property-declaration": "nospace", 107 | "variable-declaration": "nospace" 108 | } 109 | ], 110 | "unified-signatures": true, 111 | "variable-name": false, 112 | "whitespace": [ 113 | true, 114 | "check-branch", 115 | "check-decl", 116 | "check-operator", 117 | "check-separator", 118 | "check-type" 119 | ], 120 | "no-output-on-prefix": true, 121 | "use-input-property-decorator": true, 122 | "use-output-property-decorator": true, 123 | "use-host-property-decorator": true, 124 | "no-input-rename": true, 125 | "no-output-rename": true, 126 | "use-life-cycle-interface": true, 127 | "use-pipe-transform-interface": true, 128 | "component-class-suffix": true, 129 | "directive-class-suffix": true 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /sandbox/src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE9, IE10 and IE11 requires all of the following polyfills. **/ 22 | // import 'core-js/es6/symbol'; 23 | // import 'core-js/es6/object'; 24 | // import 'core-js/es6/function'; 25 | // import 'core-js/es6/parse-int'; 26 | // import 'core-js/es6/parse-float'; 27 | // import 'core-js/es6/number'; 28 | // import 'core-js/es6/math'; 29 | // import 'core-js/es6/string'; 30 | // import 'core-js/es6/date'; 31 | // import 'core-js/es6/array'; 32 | // import 'core-js/es6/regexp'; 33 | // import 'core-js/es6/map'; 34 | // import 'core-js/es6/weak-map'; 35 | // import 'core-js/es6/set'; 36 | 37 | /** 38 | * If the application will be indexed by Google Search, the following is required. 39 | * Googlebot uses a renderer based on Chrome 41. 40 | * https://developers.google.com/search/docs/guides/rendering 41 | **/ 42 | // import 'core-js/es6/array'; 43 | 44 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 45 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 46 | 47 | /** IE10 and IE11 requires the following for the Reflect API. */ 48 | // import 'core-js/es6/reflect'; 49 | 50 | /** 51 | * Web Animations `@angular/platform-browser/animations` 52 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 53 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 54 | **/ 55 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 56 | 57 | /** 58 | * By default, zone.js will patch all possible macroTask and DomEvents 59 | * user can disable parts of macroTask/DomEvents patch by setting following flags 60 | */ 61 | 62 | // (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 63 | // (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 64 | // (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 65 | 66 | /* 67 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 68 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 69 | */ 70 | // (window as any).__Zone_enable_cross_context_check = true; 71 | 72 | /*************************************************************************************************** 73 | * Zone JS is required by default for Angular itself. 74 | */ 75 | import 'zone.js/dist/zone'; // Included with Angular CLI. 76 | 77 | 78 | /*************************************************************************************************** 79 | * APPLICATION IMPORTS 80 | */ 81 | -------------------------------------------------------------------------------- /sandbox/angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "sandbox": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "app", 11 | "schematics": { 12 | "@schematics/angular:component": { 13 | "styleext": "scss" 14 | } 15 | }, 16 | "architect": { 17 | "build": { 18 | "builder": "@angular-devkit/build-angular:browser", 19 | "options": { 20 | "outputPath": "dist/sandbox", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "src/tsconfig.app.json", 25 | "assets": [ 26 | "src/favicon.ico", 27 | "src/assets" 28 | ], 29 | "styles": [ 30 | "src/styles.scss" 31 | ], 32 | "scripts": [] 33 | }, 34 | "configurations": { 35 | "production": { 36 | "fileReplacements": [ 37 | { 38 | "replace": "src/environments/environment.ts", 39 | "with": "src/environments/environment.prod.ts" 40 | } 41 | ], 42 | "optimization": true, 43 | "outputHashing": "all", 44 | "sourceMap": false, 45 | "extractCss": true, 46 | "namedChunks": false, 47 | "aot": true, 48 | "extractLicenses": true, 49 | "vendorChunk": false, 50 | "buildOptimizer": true, 51 | "budgets": [ 52 | { 53 | "type": "initial", 54 | "maximumWarning": "2mb", 55 | "maximumError": "5mb" 56 | } 57 | ] 58 | } 59 | } 60 | }, 61 | "serve": { 62 | "builder": "@angular-devkit/build-angular:dev-server", 63 | "options": { 64 | "browserTarget": "sandbox:build" 65 | }, 66 | "configurations": { 67 | "production": { 68 | "browserTarget": "sandbox:build:production" 69 | } 70 | } 71 | }, 72 | "extract-i18n": { 73 | "builder": "@angular-devkit/build-angular:extract-i18n", 74 | "options": { 75 | "browserTarget": "sandbox:build" 76 | } 77 | }, 78 | "test": { 79 | "builder": "@angular-devkit/build-angular:karma", 80 | "options": { 81 | "main": "src/test.ts", 82 | "polyfills": "src/polyfills.ts", 83 | "tsConfig": "src/tsconfig.spec.json", 84 | "karmaConfig": "src/karma.conf.js", 85 | "styles": [ 86 | "src/styles.scss" 87 | ], 88 | "scripts": [], 89 | "assets": [ 90 | "src/favicon.ico", 91 | "src/assets" 92 | ] 93 | } 94 | }, 95 | "lint": { 96 | "builder": "@angular-devkit/build-angular:tslint", 97 | "options": { 98 | "tsConfig": [ 99 | "src/tsconfig.app.json", 100 | "src/tsconfig.spec.json" 101 | ], 102 | "exclude": [ 103 | "**/node_modules/**" 104 | ] 105 | } 106 | } 107 | } 108 | }, 109 | "sandbox-e2e": { 110 | "root": "e2e/", 111 | "projectType": "application", 112 | "prefix": "", 113 | "architect": { 114 | "e2e": { 115 | "builder": "@angular-devkit/build-angular:protractor", 116 | "options": { 117 | "protractorConfig": "e2e/protractor.conf.js", 118 | "devServerTarget": "sandbox:serve" 119 | }, 120 | "configurations": { 121 | "production": { 122 | "devServerTarget": "sandbox:serve:production" 123 | } 124 | } 125 | }, 126 | "lint": { 127 | "builder": "@angular-devkit/build-angular:tslint", 128 | "options": { 129 | "tsConfig": "e2e/tsconfig.e2e.json", 130 | "exclude": [ 131 | "**/node_modules/**" 132 | ] 133 | } 134 | } 135 | } 136 | } 137 | }, 138 | "defaultProject": "sandbox" 139 | } -------------------------------------------------------------------------------- /src/ng-add/setup-project.ts: -------------------------------------------------------------------------------- 1 | import { bold, red } from '@angular-devkit/core/src/terminal'; 2 | import { 3 | chain, 4 | Rule, 5 | SchematicContext, 6 | SchematicsException, 7 | Tree 8 | } from '@angular-devkit/schematics'; 9 | import { 10 | addModuleImportToRootModule, 11 | getProjectFromWorkspace, 12 | getProjectMainFile, 13 | hasNgModuleImport 14 | } from '@angular/cdk/schematics'; 15 | import { getSourceNodes, insertImport, isImported } from '@schematics/angular/utility/ast-utils'; 16 | import { InsertChange } from '@schematics/angular/utility/change'; 17 | import { getWorkspace } from '@schematics/angular/utility/config'; 18 | import { getAppModulePath } from '@schematics/angular/utility/ng-ast-utils'; 19 | import { SourceFile } from 'typescript'; 20 | import { Schema } from './schema'; 21 | import { getProjectEnvironmentFile } from '../util/project-environment-file'; 22 | import { ts } from '../util/version-agnostic-typescript'; 23 | 24 | export default function(options: Schema): Rule { 25 | return (tree: Tree, _context: SchematicContext) => { 26 | return chain([ 27 | addEnvironmentConfig(options), 28 | importEnvironemntIntoRootModule(options), 29 | addAngularFireModule(options) 30 | ])(tree, _context); 31 | }; 32 | } 33 | 34 | function addEnvironmentConfig(options: Schema): Rule { 35 | return (tree: Tree, context: SchematicContext) => { 36 | const workspace = getWorkspace(tree); 37 | const project = getProjectFromWorkspace(workspace, options.project); 38 | const envPath = getProjectEnvironmentFile(project); 39 | 40 | // verify environment.ts file exists 41 | if (!envPath) { 42 | return context.logger.warn( 43 | `❌ Could not find environment file: "${envPath}". Skipping firebase configuration.` 44 | ); 45 | } 46 | 47 | // firebase config to add to environment.ts file 48 | const insertion = 49 | ',\n' + 50 | ` firebase: {\n` + 51 | ` apiKey: '${options.apiKey}',\n` + 52 | ` authDomain: '${options.authDomain}',\n` + 53 | ` databaseURL: '${options.databaseURL}',\n` + 54 | ` projectId: '${options.projectId}',\n` + 55 | ` storageBucket: '${options.storageBucket}',\n` + 56 | ` messagingSenderId: '${options.messagingSenderId}',\n` + 57 | ` }`; 58 | const sourceFile = readIntoSourceFile(tree, envPath); 59 | 60 | // verify firebase config does not already exist 61 | const sourceFileText = sourceFile.getText(); 62 | if (sourceFileText.includes(insertion)) { 63 | return; 64 | } 65 | 66 | // get the array of top-level Node objects in the AST from the SourceFile 67 | const nodes = getSourceNodes(sourceFile as any); 68 | const start = nodes.find( 69 | node => node.kind === ts.SyntaxKind.OpenBraceToken 70 | )!; 71 | const end = nodes.find( 72 | node => node.kind === ts.SyntaxKind.CloseBraceToken, 73 | start.end 74 | )!; 75 | 76 | const recorder = tree.beginUpdate(envPath); 77 | recorder.insertLeft(end.pos, insertion); 78 | tree.commitUpdate(recorder); 79 | 80 | context.logger.info('✅️ Environment configuration'); 81 | return tree; 82 | }; 83 | } 84 | 85 | function addAngularFireModule(options: Schema): Rule { 86 | return (tree: Tree, context: SchematicContext) => { 87 | const MODULE_NAME = 'AngularFireModule.initializeApp(environment.firebase)'; 88 | const workspace = getWorkspace(tree); 89 | const project = getProjectFromWorkspace(workspace, options.project); 90 | const appModulePath = getAppModulePath(tree, getProjectMainFile(project)); 91 | 92 | // verify module has not already been imported 93 | if (hasNgModuleImport(tree, appModulePath, MODULE_NAME)) { 94 | return console.warn( 95 | red( 96 | `Could not import "${bold(MODULE_NAME)}" because "${bold( 97 | MODULE_NAME 98 | )}" is already imported.` 99 | ) 100 | ); 101 | } 102 | 103 | // add NgModule to root NgModule imports 104 | addModuleImportToRootModule(tree, MODULE_NAME, '@angular/fire', project); 105 | 106 | context.logger.info('✅️ Import AngularFireModule into root module'); 107 | return tree; 108 | }; 109 | } 110 | 111 | function importEnvironemntIntoRootModule(options: Schema): Rule { 112 | return (tree: Tree, context: SchematicContext) => { 113 | const IMPORT_IDENTIFIER = 'environment'; 114 | const workspace = getWorkspace(tree); 115 | const project = getProjectFromWorkspace(workspace, options.project); 116 | const appModulePath = getAppModulePath(tree, getProjectMainFile(project)); 117 | const envPath = getProjectEnvironmentFile(project); 118 | const sourceFile = readIntoSourceFile(tree, appModulePath); 119 | 120 | if (isImported(sourceFile as any, IMPORT_IDENTIFIER, envPath)) { 121 | context.logger.info( 122 | '✅️ The environment is already imported in the root module' 123 | ); 124 | return tree; 125 | } 126 | 127 | const change = insertImport( 128 | sourceFile as any, 129 | appModulePath, 130 | IMPORT_IDENTIFIER, 131 | envPath.replace(/\.ts$/, '') 132 | ) as InsertChange; 133 | 134 | const recorder = tree.beginUpdate(appModulePath); 135 | recorder.insertLeft(change.pos, change.toAdd); 136 | tree.commitUpdate(recorder); 137 | 138 | context.logger.info('✅️ Import environment into root module'); 139 | return tree; 140 | }; 141 | } 142 | 143 | function readIntoSourceFile(host: Tree, fileName: string): SourceFile { 144 | const buffer = host.read(fileName); 145 | if (buffer === null) { 146 | throw new SchematicsException(`File ${fileName} does not exist.`); 147 | } 148 | 149 | return ts.createSourceFile( 150 | fileName, 151 | buffer.toString('utf-8'), 152 | ts.ScriptTarget.Latest, 153 | true 154 | ); 155 | } 156 | --------------------------------------------------------------------------------