angular-fire-schematics
Version:
AngularFire Schematics
100 lines • 5.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const terminal_1 = require("@angular-devkit/core/src/terminal");
const schematics_1 = require("@angular-devkit/schematics");
const schematics_2 = require("@angular/cdk/schematics");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const config_1 = require("@schematics/angular/utility/config");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const project_environment_file_1 = require("../util/project-environment-file");
const version_agnostic_typescript_1 = require("../util/version-agnostic-typescript");
function default_1(options) {
return (tree, _context) => {
return schematics_1.chain([
addEnvironmentConfig(options),
importEnvironemntIntoRootModule(options),
addAngularFireModule(options)
])(tree, _context);
};
}
exports.default = default_1;
function addEnvironmentConfig(options) {
return (tree, context) => {
const workspace = config_1.getWorkspace(tree);
const project = schematics_2.getProjectFromWorkspace(workspace, options.project);
const envPath = project_environment_file_1.getProjectEnvironmentFile(project);
// verify environment.ts file exists
if (!envPath) {
return context.logger.warn(`❌ Could not find environment file: "${envPath}". Skipping firebase configuration.`);
}
// firebase config to add to environment.ts file
const insertion = ',\n' +
` firebase: {\n` +
` apiKey: '${options.apiKey}',\n` +
` authDomain: '${options.authDomain}',\n` +
` databaseURL: '${options.databaseURL}',\n` +
` projectId: '${options.projectId}',\n` +
` storageBucket: '${options.storageBucket}',\n` +
` messagingSenderId: '${options.messagingSenderId}',\n` +
` }`;
const sourceFile = readIntoSourceFile(tree, envPath);
// verify firebase config does not already exist
const sourceFileText = sourceFile.getText();
if (sourceFileText.includes(insertion)) {
return;
}
// get the array of top-level Node objects in the AST from the SourceFile
const nodes = ast_utils_1.getSourceNodes(sourceFile);
const start = nodes.find(node => node.kind === version_agnostic_typescript_1.ts.SyntaxKind.OpenBraceToken);
const end = nodes.find(node => node.kind === version_agnostic_typescript_1.ts.SyntaxKind.CloseBraceToken, start.end);
const recorder = tree.beginUpdate(envPath);
recorder.insertLeft(end.pos, insertion);
tree.commitUpdate(recorder);
context.logger.info('✅️ Environment configuration');
return tree;
};
}
function addAngularFireModule(options) {
return (tree, context) => {
const MODULE_NAME = 'AngularFireModule.initializeApp(environment.firebase)';
const workspace = config_1.getWorkspace(tree);
const project = schematics_2.getProjectFromWorkspace(workspace, options.project);
const appModulePath = ng_ast_utils_1.getAppModulePath(tree, schematics_2.getProjectMainFile(project));
// verify module has not already been imported
if (schematics_2.hasNgModuleImport(tree, appModulePath, MODULE_NAME)) {
return console.warn(terminal_1.red(`Could not import "${terminal_1.bold(MODULE_NAME)}" because "${terminal_1.bold(MODULE_NAME)}" is already imported.`));
}
// add NgModule to root NgModule imports
schematics_2.addModuleImportToRootModule(tree, MODULE_NAME, '@angular/fire', project);
context.logger.info('✅️ Import AngularFireModule into root module');
return tree;
};
}
function importEnvironemntIntoRootModule(options) {
return (tree, context) => {
const IMPORT_IDENTIFIER = 'environment';
const workspace = config_1.getWorkspace(tree);
const project = schematics_2.getProjectFromWorkspace(workspace, options.project);
const appModulePath = ng_ast_utils_1.getAppModulePath(tree, schematics_2.getProjectMainFile(project));
const envPath = project_environment_file_1.getProjectEnvironmentFile(project);
const sourceFile = readIntoSourceFile(tree, appModulePath);
if (ast_utils_1.isImported(sourceFile, IMPORT_IDENTIFIER, envPath)) {
context.logger.info('✅️ The environment is already imported in the root module');
return tree;
}
const change = ast_utils_1.insertImport(sourceFile, appModulePath, IMPORT_IDENTIFIER, envPath.replace(/\.ts$/, ''));
const recorder = tree.beginUpdate(appModulePath);
recorder.insertLeft(change.pos, change.toAdd);
tree.commitUpdate(recorder);
context.logger.info('✅️ Import environment into root module');
return tree;
};
}
function readIntoSourceFile(host, fileName) {
const buffer = host.read(fileName);
if (buffer === null) {
throw new schematics_1.SchematicsException(`File ${fileName} does not exist.`);
}
return version_agnostic_typescript_1.ts.createSourceFile(fileName, buffer.toString('utf-8'), version_agnostic_typescript_1.ts.ScriptTarget.Latest, true);
}
//# sourceMappingURL=setup-project.js.map