UNPKG

ng-afelio

Version:
66 lines (65 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.appendIntoEnvironment = exports.getEnvironmentNode = void 0; const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const ts = require("typescript"); const colors = require("colors"); const ast_util_1 = require("./ast-util"); const change_1 = require("./change"); function getEnvironmentNode(source) { const keywords = (0, ast_util_1.findNodes)(source, ts.SyntaxKind.VariableStatement); for (const keyword of keywords) { if (ts.isVariableStatement(keyword)) { const [declaration] = keyword.declarationList.declarations; if (ts.isVariableDeclaration(declaration) && declaration.initializer && declaration.name.getText() === 'environment') { return declaration.initializer.getChildAt(1); } } } } exports.getEnvironmentNode = getEnvironmentNode; function appendIntoEnvironment(projectAppPath, projectName, toAppend, toCheck, prod = false) { // const projectEnvPath = join(projectAppPath as Path, prodEnv ? '../environments/environment.prod.ts' : '../environments/environment.ts'); let devProjectEnvPath = (0, core_1.join)(projectAppPath, '../environments/environment.development.ts'); let prodProjectEnvPath = (0, core_1.join)(projectAppPath, '../environments/environment.ts'); let projectEnvPath = devProjectEnvPath; return host => { const legacyEnvironment = host.exists((0, core_1.join)(projectAppPath, '../environments/environment.prod.ts')); if (legacyEnvironment) { projectEnvPath = (0, core_1.join)(projectAppPath, '../environments/environment.ts'); prodProjectEnvPath = (0, core_1.join)(projectAppPath, '../environments/environment.prod.ts'); } if (prod) { projectEnvPath = prodProjectEnvPath; } let text = host.read(projectEnvPath); if (!text) { throw new schematics_1.SchematicsException(`Environment file on ${projectName} project does not exist.`); } const sourceText = text.toString('utf8'); const source = ts.createSourceFile(projectEnvPath, sourceText, ts.ScriptTarget.Latest, true); const node = getEnvironmentNode(source); const changes = []; if (node) { if (!toCheck || !node.getText().includes(toCheck)) { const lastRouteNode = node.getLastToken(); if (lastRouteNode) { changes.push(new change_1.InsertChange(projectEnvPath, lastRouteNode.getEnd(), `,${toAppend}`)); } else { changes.push(new change_1.InsertChange(projectEnvPath, node.getEnd(), `${toAppend}\n`)); } console.log(`${colors.green(`Changes will be applied to ${prod ? 'prod' : 'dev'} environment.`)} ${colors.yellow('Please apply it to others.')}`); } } else { throw new schematics_1.SchematicsException(`No "export const environment" found`); } (0, change_1.applyChangesToHost)(host, projectEnvPath, changes); return host; }; } exports.appendIntoEnvironment = appendIntoEnvironment;